forked from JiangXL/SocketViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketSync.py
111 lines (98 loc) · 3.13 KB
/
SocketSync.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
105
106
107
108
109
110
111
import time
import struct
import socket
import threading
"""
Use socket broadcast variables
| Version | Commit
| 0.1 | workable version
| 2020307 | fix update relay due to network traffic
Ref: https://keelii.com/2018/09/24/socket-programming-in-python/
"""
class SocketSync():
""" General SocketSync Class """
def __init__(self):
self._subscribers = []
self.PORT = 60001
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setblocking(0)
self.sock.settimeout(0.001)
def structure(self):
""" Create and maintance variable info"""
return None
def attach(self, var):
self._subscribers.append(var)
def detach(self, var):
self._subscribers.remove(var)
class Server(SocketSync):
""" Server for SocketSync """
def __init__(self):
super().__init__()
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(('0.0.0.0', self.PORT))
self.sock.listen(1)
self.accept()
def accept(self):
""" Try to accept last one connect require """
while(True):
try:
self.conn, addr = self.sock.accept()
break
except:
time.sleep(3)
self.conn.setblocking(0)
self.conn.settimeout(0.001)
print("Found ParameterViewer", end=" ")
def send(self):
self.sync_run = True
var_last = 0
while (self.sync_run):
# Send all parameters
for var in self._subscribers:
try:
# Only send updated value to avoid network traffic
if var_last != var.value:
self.conn.sendall(struct.pack('>d', var.value))
var_last = var.value
except ConnectionError:
print("Lost ParameterViewer", end=" ")
self.accept()
#time.sleep(0.001)
# Revice all parameters
try:
data = self.conn.recv(8)
var.value = float(struct.unpack('>d', data)[0])
except:
#print("*", end="")
None
def sync(self):
self.sync_thread = threading.Thread(target=self.send)
self.sync_thread.start()
def sync_stop(self):
self.sync_run = False
self.sync_thread.join()
class Client(SocketSync):
""" Client for SocketSync """
def __init__(self, HOST):
super().__init__()
self.HOST = HOST
self.sock.connect((self.HOST, self.PORT))
self.sock.setblocking(0)
self.sock.settimeout(0.001)
def recv_var(self):
try:
return struct.unpack('>d', self.sock.recv(8))[0]
except socket.timeout:
#print("*", end="")
return None
#except struct.error:
# self.connect()
def send(self, _var):
self.sock.sendall(struct.pack('>d', _var))
class Var():
""" Physical quantity Class """
def __init__(self, value):
self.name = "x"
self.value = value
self.unit = ""
self.range = ""