@@ -13,7 +13,7 @@ def __init__(self, observer, conn, addr):
13
13
self .name = 'no_name'
14
14
self .observer = observer
15
15
self .commands_queue = deque () # each client has independent commands queue
16
- self .__local_sync = threading .Lock () # synchronization between main and client thread
16
+ self .__local_sync = threading .RLock () # synchronization between main and client thread
17
17
self .conn = conn
18
18
self .addr = addr
19
19
self .thread = threading .Thread (target = ServingThreadWrapper .__serve , args = (self ,))
@@ -24,20 +24,25 @@ def __init__(self, observer, conn, addr):
24
24
def send (self , data ):
25
25
with self .__local_sync :
26
26
self .commands_queue .appendleft (data )
27
+ #self.__process_server_messages_queue()
27
28
28
29
# Thread safe first idle message
29
30
def post (self , data ):
30
31
with self .__local_sync :
31
32
self .commands_queue .append (data )
33
+ #self.__process_server_messages_queue()
32
34
33
35
# Thread safe pop command operation
34
36
def pop_command (self ):
35
37
with self .__local_sync :
36
38
if len (self .commands_queue ) > 0 :
37
39
return self .commands_queue .pop ()
40
+ else :
41
+ return None
38
42
39
43
# Thread-safe sever notifier
40
44
def notify (self , message ):
45
+ print ('try to notify observer' )
41
46
with ServingThreadWrapper .__global_sync :
42
47
self .observer .notify (self , message )
43
48
@@ -53,30 +58,35 @@ def close(self):
53
58
@staticmethod
54
59
def __serve (stw ):
55
60
print ('Connection attempt' , stw .addr )
61
+ stw .conn .settimeout (1 )
56
62
while not stw .closing :
57
63
ServingThreadWrapper .__process_server_messages_queue (stw ) # It might be good idea to move this to new thread
58
64
command = ServingThreadWrapper .__receive_and_parse_client_command (stw )
59
65
ServingThreadWrapper .__process_command (stw , command )
60
66
61
67
@staticmethod
62
68
def __process_server_messages_queue (stw ):
63
- package = stw .pop_command ()
64
- if package is None :
65
- return
66
69
67
- data = json .dumps (package )
68
-
69
- b = bytes (data , 'utf-8' )
70
- stw .conn .sendall (struct .pack ('i' , len (data )))
70
+ package = stw .pop_command ()
71
71
72
- stw .conn .sendall (b )
73
- print ('sent data...' , b .decode ('utf-8' ))
72
+ while not package is None :
73
+ data = json .dumps (package )
74
+ b = bytes (data , 'utf-8' )
75
+ stw .conn .sendall (struct .pack ('i' , len (data )))
76
+ stw .conn .sendall (b )
77
+ print ('sent data...' , b .decode ('utf-8' ))
78
+ package = stw .pop_command ()
74
79
75
80
@staticmethod
76
81
def __receive_and_parse_client_command (stw ):
77
82
78
- header = stw .conn .recv (4 )
79
- if not header : return
83
+ header = None
84
+ try :
85
+ header = stw .conn .recv (4 )
86
+ if not header : return
87
+ except :
88
+ # I know that it is bad idea to swallow exceptions, this is temporary trick
89
+ return
80
90
81
91
size = int (struct .unpack ('i' , header )[0 ])
82
92
print ('receiving bytes:' , size )
@@ -99,6 +109,14 @@ def __receive_and_parse_client_command(stw):
99
109
100
110
@staticmethod
101
111
def __process_command (stw , command ):
112
+ if command is None : return
102
113
print ('new command from client received' , command ['cmd' ])
103
114
if command ['cmd' ] == 'CMD_PING' :
104
115
stw .send ({'cmd' : 'CMD_PONG' , 'msg' : 'pong from server' })
116
+
117
+ if command ['cmd' ] == 'CMD_BROADCAST' :
118
+ stw .notify (command )
119
+
120
+ if command ['cmd' ] == 'CMD_MESSAGE' :
121
+ stw .notify (command )
122
+
0 commit comments