-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_socket.py
104 lines (86 loc) · 2.92 KB
/
client_socket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import threading
import socket
import riddler_interface as interface
class sock(threading.Thread):
def __init__(self):
super(sock, self).__init__(None)
self.socket = None
# Allow other classes to subscribe to incoming data
self.subscriptions = {}
self.subscribers = []
# State handling
self.end = threading.Event()
self.connected = threading.Event()
self.name = "sock"
self.lock = threading.Lock()
self.start()
def subscribe(self, caller, data_type, callback):
if not data_type in self.subscriptions:
self.subscriptions[data_type] = [callback]
else:
self.subscriptions[data_type].append(callback)
if caller not in self.subscribers:
self.subscribers.append(caller)
def connect(self, host, port):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.socket.connect((host, int(port)))
self.socket.settimeout(.5)
except socket.error as e:
self.error = e
self.socket = None
return False
else:
self.connected.set()
self.publish_connect()
return True
def disconnect(self):
if self.socket:
self.connected.clear()
self.socket.close()
self.socket = None
self.publish_disconnect()
def run(self):
while not self.end.is_set():
try:
# Wait for GUI to start connection
if not self.connected.wait(.5):
continue
# Read data from controller
obj = interface.recv(self.socket)
if obj:
self.handle_obj(obj)
else:
# Connected closed by remote, clean up
self.disconnect()
except socket.timeout:
continue
except socket.error as e:
print(e)
self.disconnect()
def stop(self):
self.disconnect()
self.end.set()
def handle_obj(self, obj):
self.publish_data(obj)
def send(self, cmd, **vals):
if not self.socket:
return
self.lock.acquire()
interface.send_client(self.socket, cmd, **vals)
self.lock.release()
def publish_data(self, obj):
if not obj.cmd in self.subscriptions:
# No one is interested in the data
return
# Deliver the object to interested receivers
for subscriber in self.subscriptions[obj.cmd]:
subscriber(obj)
def publish_disconnect(self):
for subscriber in self.subscribers:
subscriber.controller_disconnected()
def publish_connect(self):
for subscriber in self.subscribers:
subscriber.controller_connected()
def get_error(self):
return self.error