|
@@ -88,11 +88,7 @@ class callback_storage(dict):
|
88
|
88
|
def add(self, service_id, data_id, callback, *args, **kwargs):
|
89
|
89
|
cb_data = self.get(service_id, data_id)
|
90
|
90
|
if cb_data != (None, None, None):
|
91
|
|
-<<<<<<< HEAD
|
92
|
91
|
self.logger.warning("Overwriting existing callback %s for service_id (%s) and data_id (%s) to %s!", repr(cb_data[0].__name__), repr(service_id), repr(data_id), repr(callback.__name__))
|
93
|
|
-=======
|
94
|
|
- logger.warning("Overwriting existing callback %s for service_id (%s) and data_id (%s) to %s!" , repr(cb_data[0].__name__), repr(service_id), repr(data_id), repr(callback.__name__))
|
95
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
96
|
92
|
if service_id not in self:
|
97
|
93
|
self[service_id] = {}
|
98
|
94
|
self[service_id][data_id] = (callback, args, kwargs)
|
|
@@ -147,11 +143,8 @@ class struct_json_protocol(object):
|
147
|
143
|
|
148
|
144
|
.. literalinclude:: ../../socket_protocol/_examples_/socket_protocol__struct_json_protocol_client.log
|
149
|
145
|
"""
|
150
|
|
-<<<<<<< HEAD
|
151
|
146
|
DEFAULT_CHANNEL_NAME = 'all_others'
|
152
|
147
|
|
153
|
|
-=======
|
154
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
155
|
148
|
SID_AUTH_SEED_REQUEST = 1
|
156
|
149
|
SID_AUTH_KEY_REQUEST = 2
|
157
|
150
|
SID_AUTH_KEY_CHECK_REQUEST = 3
|
|
@@ -203,13 +196,9 @@ class struct_json_protocol(object):
|
203
|
196
|
self.__comm_inst__ = comm_instance
|
204
|
197
|
self.__secret__ = secret
|
205
|
198
|
self.__auto_auth__ = auto_auth
|
206
|
|
-<<<<<<< HEAD
|
207
|
199
|
#
|
208
|
200
|
self.__callbacks__ = callback_storage(channel_name)
|
209
|
201
|
self.__init_channel_name__(channel_name)
|
210
|
|
-=======
|
211
|
|
- self.__channel_name__ = channel_name
|
212
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
213
|
202
|
#
|
214
|
203
|
self.__clean_receive_buffer__()
|
215
|
204
|
self.__callbacks__.add(self.SID_AUTH_SEED_REQUEST, 0, self.__authentificate_create_seed__)
|
|
@@ -224,7 +213,6 @@ class struct_json_protocol(object):
|
224
|
213
|
self.__comm_inst__.register_connect_callback(self.__connection_established__)
|
225
|
214
|
self.__comm_inst__.register_disconnect_callback(self.__authentification_state_reset__)
|
226
|
215
|
|
227
|
|
-<<<<<<< HEAD
|
228
|
216
|
def __init_channel_name__(self, channel_name):
|
229
|
217
|
self.__comm_inst__.init_channel_name(channel_name)
|
230
|
218
|
self.__callbacks__.init_channel_name(channel_name)
|
|
@@ -241,14 +229,6 @@ class struct_json_protocol(object):
|
241
|
229
|
|
242
|
230
|
def __log_prefix__(self):
|
243
|
231
|
return ' SP client:' if self.__comm_inst__.IS_CLIENT else ' SP server:'
|
244
|
|
-=======
|
245
|
|
- def __log_prefix__(self):
|
246
|
|
- postfix = ' (client)' if self.__comm_inst__.IS_CLIENT else ' (server)'
|
247
|
|
- if self.__channel_name__ is None:
|
248
|
|
- return __name__ + postfix + ':'
|
249
|
|
- else:
|
250
|
|
- return self.__channel_name__ + postfix + ':'
|
251
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
252
|
232
|
|
253
|
233
|
def connected(self):
|
254
|
234
|
return self.__comm_inst__.is_connected()
|
|
@@ -272,39 +252,22 @@ class struct_json_protocol(object):
|
272
|
252
|
return self.STATUS_OKAY, self.__channel_name__
|
273
|
253
|
else:
|
274
|
254
|
prev_channel_name = self.__channel_name__
|
275
|
|
-<<<<<<< HEAD
|
276
|
255
|
self.__init_channel_name__(data)
|
277
|
256
|
if prev_channel_name is not None and prev_channel_name != data:
|
278
|
257
|
self.logger.warning('%s overwriting user defined channel name from %s to %s', self.__log_prefix__(), repr(prev_channel_name), repr(data))
|
279
|
258
|
elif prev_channel_name is None:
|
280
|
259
|
self.logger.info('%s channel name is now %s', self.__log_prefix__(), repr(self.__channel_name__))
|
281
|
|
-=======
|
282
|
|
- self.__channel_name__ = data
|
283
|
|
- if prev_channel_name is not None and prev_channel_name != data:
|
284
|
|
- logger.warning('%s overwriting user defined channel name from %s to %s', self.__log_prefix__(), repr(prev_channel_name), repr(data))
|
285
|
|
- elif prev_channel_name is None:
|
286
|
|
- logger.info('%s channel name is now %s', self.__log_prefix__(), repr(self.__channel_name__))
|
287
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
288
|
260
|
return self.STATUS_OKAY, None
|
289
|
261
|
|
290
|
262
|
def __channel_name_response__(self, msg):
|
291
|
263
|
data = msg.get_data()
|
292
|
264
|
if self.__channel_name__ is None and data is not None:
|
293
|
|
-<<<<<<< HEAD
|
294
|
265
|
self.__init_channel_name__(data)
|
295
|
266
|
self.logger.info('%s channel name is now %s', self.__log_prefix__(), repr(self.__channel_name__))
|
296
|
267
|
return self.STATUS_OKAY, None
|
297
|
268
|
|
298
|
269
|
def __authentification_state_reset__(self):
|
299
|
270
|
self.logger.info("%s Resetting authentification state to AUTH_STATE_UNKNOWN_CLIENT", self.__log_prefix__())
|
300
|
|
-=======
|
301
|
|
- self.__channel_name__ = data
|
302
|
|
- logger.info('%s channel name is now %s', self.__log_prefix__(), repr(self.__channel_name__))
|
303
|
|
- return self.STATUS_OKAY, None
|
304
|
|
-
|
305
|
|
- def __authentification_state_reset__(self):
|
306
|
|
- logger.info("%s Resetting authentification state to AUTH_STATE_UNKNOWN_CLIENT", self.__log_prefix__())
|
307
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
308
|
271
|
self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
|
309
|
272
|
|
310
|
273
|
def __analyse_frame__(self, frame):
|
|
@@ -343,11 +306,7 @@ class struct_json_protocol(object):
|
343
|
306
|
def __data_available_callback__(self, comm_inst):
|
344
|
307
|
frame = comm_inst.receive()
|
345
|
308
|
if not self.__check_frame_checksum__(frame):
|
346
|
|
-<<<<<<< HEAD
|
347
|
309
|
self.logger.warning("%s Received message has a wrong checksum and will be ignored: %s.", self.__log_prefix__(), stringtools.hexlify(frame))
|
348
|
|
-=======
|
349
|
|
- logger.warning("%s Received message has a wrong checksum and will be ignored: %s.", self.__log_prefix__(), stringtools.hexlify(frame))
|
350
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
351
|
310
|
else:
|
352
|
311
|
msg = self.__analyse_frame__(frame)
|
353
|
312
|
self.logger.info(
|
|
@@ -366,24 +325,14 @@ class struct_json_protocol(object):
|
366
|
325
|
if self.__secret__ is not None and not self.check_authentification_state() and msg.get_service_id() not in self.SID_AUTH_LIST:
|
367
|
326
|
status = self.STATUS_AUTH_REQUIRED
|
368
|
327
|
data = None
|
369
|
|
-<<<<<<< HEAD
|
370
|
328
|
self.logger.warning("%s Received message needs authentification: %s. Sending negative response.", self.__log_prefix__(), self.AUTH_STATUS_NAMES.get(self.__authentification_state__, 'Unknown authentification status!'))
|
371
|
329
|
elif callback is None:
|
372
|
330
|
self.logger.warning("%s Received message with no registered callback. Sending negative response.", self.__log_prefix__())
|
373
|
|
-=======
|
374
|
|
- logger.warning("%s Received message needs authentification: %s. Sending negative response.", self.__log_prefix__(), self.AUTH_STATUS_NAMES.get(self.__authentification_state__, 'Unknown authentification status!'))
|
375
|
|
- elif callback is None:
|
376
|
|
- logger.warning("%s Received message with no registered callback. Sending negative response.", self.__log_prefix__())
|
377
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
378
|
331
|
status = self.STATUS_BUFFERING_UNHANDLED_REQUEST
|
379
|
332
|
data = None
|
380
|
333
|
else:
|
381
|
334
|
try:
|
382
|
|
-<<<<<<< HEAD
|
383
|
335
|
self.logger.debug("%s Executing callback %s to process received data", self.__log_prefix__(), callback.__name__)
|
384
|
|
-=======
|
385
|
|
- logger.debug("%s Executing callback %s to process received data", self.__log_prefix__(), callback.__name__)
|
386
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
387
|
336
|
status, data = callback(msg, *args, **kwargs)
|
388
|
337
|
except TypeError:
|
389
|
338
|
raise TypeError('Check return value of callback function {callback_name} for service_id {service_id} and data_id {data_id}'.format(callback_name=callback.__name__, service_id=repr(msg.get_service_id()), data_id=repr(msg.get_data_id())))
|
|
@@ -393,22 +342,14 @@ class struct_json_protocol(object):
|
393
|
342
|
# RESPONSE RECEIVED
|
394
|
343
|
#
|
395
|
344
|
if msg.get_status() not in [self.STATUS_OKAY]:
|
396
|
|
-<<<<<<< HEAD
|
397
|
345
|
self.logger.warning("%s Received message has a peculiar status: %s", self.__log_prefix__(), self.STATUS_NAMES.get(msg.get_status(), 'Unknown status response!'))
|
398
|
|
-=======
|
399
|
|
- logger.warning("%s Received message has a peculiar status: %s", self.__log_prefix__(), self.STATUS_NAMES.get(msg.get_status(), 'Unknown status response!'))
|
400
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
401
|
346
|
if callback is None:
|
402
|
347
|
status = self.STATUS_OKAY
|
403
|
348
|
data = None
|
404
|
349
|
self.__buffer_received_data__(msg)
|
405
|
350
|
else:
|
406
|
351
|
try:
|
407
|
|
-<<<<<<< HEAD
|
408
|
352
|
self.logger.debug("%s Executing callback %s to process received data", self.__log_prefix__(), callback.__name__)
|
409
|
|
-=======
|
410
|
|
- logger.debug("%s Executing callback %s to process received data", self.__log_prefix__(), callback.__name__)
|
411
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
412
|
353
|
status, data = callback(msg, *args, **kwargs)
|
413
|
354
|
except TypeError:
|
414
|
355
|
raise TypeError('Check return value of callback function {callback_name} for service_id {service_id} and data_id {data_id}'.format(callback_name=callback.__name__, service_id=repr(msg.get_service_id()), data_id=repr(msg.get_data_id())))
|
|
@@ -419,17 +360,10 @@ class struct_json_protocol(object):
|
419
|
360
|
if not msg.get_data_id() in self.__msg_buffer__[msg.get_service_id()]:
|
420
|
361
|
self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()] = []
|
421
|
362
|
self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()].append(msg)
|
422
|
|
-<<<<<<< HEAD
|
423
|
363
|
self.logger.debug("%s Message data is stored in buffer and is now ready to be retrieved by receive method", self.__log_prefix__())
|
424
|
364
|
|
425
|
365
|
def __clean_receive_buffer__(self):
|
426
|
366
|
self.logger.debug("%s Cleaning up receive-buffer", self.__log_prefix__())
|
427
|
|
-=======
|
428
|
|
- logger.debug("%s Message data is stored in buffer and is now ready to be retrieved by receive method", self.__log_prefix__())
|
429
|
|
-
|
430
|
|
- def __clean_receive_buffer__(self):
|
431
|
|
- logger.debug("%s Cleaning up receive-buffer", self.__log_prefix__())
|
432
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
433
|
367
|
self.__msg_buffer__ = {}
|
434
|
368
|
|
435
|
369
|
def receive(self, service_id, data_id, timeout=1):
|
|
@@ -443,11 +377,7 @@ class struct_json_protocol(object):
|
443
|
377
|
cnt += 1
|
444
|
378
|
time.sleep(0.1)
|
445
|
379
|
if data is None and cnt >= timeout * 10:
|
446
|
|
-<<<<<<< HEAD
|
447
|
380
|
self.logger.warning('%s TIMEOUT (%ss): Requested data (service_id: %s; data_id: %s) not in buffer.', self.__log_prefix__(), repr(timeout), repr(service_id), repr(data_id))
|
448
|
|
-=======
|
449
|
|
- logger.warning('%s TIMEOUT (%ss): Requested data (service_id: %s; data_id: %s) not in buffer.', self.__log_prefix__(), repr(timeout), repr(service_id), repr(data_id))
|
450
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
451
|
381
|
return data
|
452
|
382
|
|
453
|
383
|
def __mk_msg__(self, status, service_id, data_id, data):
|
|
@@ -472,11 +402,7 @@ class struct_json_protocol(object):
|
472
|
402
|
|
473
|
403
|
This methods sends out a message with the given content.
|
474
|
404
|
"""
|
475
|
|
-<<<<<<< HEAD
|
476
|
405
|
self.logger.log(log_lvl, '%s TX -> status: %d, service_id: %d, data_id: %d, data: "%s"', self.__log_prefix__(), status, service_id, data_id, repr(data))
|
477
|
|
-=======
|
478
|
|
- logger.log(log_lvl, '%s TX -> status: %d, service_id: %d, data_id: %d, data: "%s"', self.__log_prefix__(), status, service_id, data_id, repr(data))
|
479
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
480
|
406
|
return self.__comm_inst__.send(self.__build_frame__(service_id, data_id, data, status), timeout=timeout, log_lvl=logging.DEBUG)
|
481
|
407
|
|
482
|
408
|
def register_callback(self, service_id, data_id, callback, *args, **kwargs):
|
|
@@ -520,11 +446,7 @@ class struct_json_protocol(object):
|
520
|
446
|
"""
|
521
|
447
|
if self.__secret__ is not None:
|
522
|
448
|
self.__authentification_state__ = self.AUTH_STATE_SEED_REQUESTED
|
523
|
|
-<<<<<<< HEAD
|
524
|
449
|
self.logger.info("%s Requesting seed for authentification", self.__log_prefix__())
|
525
|
|
-=======
|
526
|
|
- logger.info("%s Requesting seed for authentification", self.__log_prefix__())
|
527
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
528
|
450
|
self.send(self.SID_AUTH_SEED_REQUEST, 0, None)
|
529
|
451
|
cnt = 0
|
530
|
452
|
while cnt < timeout * 10:
|
|
@@ -550,11 +472,7 @@ class struct_json_protocol(object):
|
550
|
472
|
return hashlib.sha512(seed.encode('utf-8') + self.__secret__.encode('utf-8')).hexdigest()
|
551
|
473
|
|
552
|
474
|
def __authentificate_create_seed__(self, msg):
|
553
|
|
-<<<<<<< HEAD
|
554
|
475
|
self.logger.info("%s Got seed request, sending seed for authentification", self.__log_prefix__())
|
555
|
|
-=======
|
556
|
|
- logger.info("%s Got seed request, sending seed for authentification", self.__log_prefix__())
|
557
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
558
|
476
|
self.__authentification_state__ = self.AUTH_STATE_SEED_TRANSFERRED
|
559
|
477
|
if sys.version_info >= (3, 0):
|
560
|
478
|
self.__seed__ = binascii.hexlify(os.urandom(32)).decode('utf-8')
|
|
@@ -563,11 +481,7 @@ class struct_json_protocol(object):
|
563
|
481
|
return self.STATUS_OKAY, self.__seed__
|
564
|
482
|
|
565
|
483
|
def __authentificate_create_key__(self, msg):
|
566
|
|
-<<<<<<< HEAD
|
567
|
484
|
self.logger.info("%s Got seed, sending key for authentification", self.__log_prefix__())
|
568
|
|
-=======
|
569
|
|
- logger.info("%s Got seed, sending key for authentification", self.__log_prefix__())
|
570
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
571
|
485
|
self.__authentification_state__ = self.AUTH_STATE_KEY_TRANSFERRED
|
572
|
486
|
seed = msg.get_data()
|
573
|
487
|
key = self.__authentificate_salt_and_hash__(seed)
|
|
@@ -577,36 +491,21 @@ class struct_json_protocol(object):
|
577
|
491
|
key = msg.get_data()
|
578
|
492
|
if key == self.__authentificate_salt_and_hash__(self.__seed__):
|
579
|
493
|
self.__authentification_state__ = self.AUTH_STATE_TRUSTED_CLIENT
|
580
|
|
-<<<<<<< HEAD
|
581
|
494
|
self.logger.info("%s Got correct key, sending positive authentification feedback", self.__log_prefix__())
|
582
|
495
|
return self.STATUS_OKAY, True
|
583
|
496
|
else:
|
584
|
497
|
self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
|
585
|
498
|
self.logger.info("%s Got incorrect key, sending negative authentification feedback", self.__log_prefix__())
|
586
|
|
-=======
|
587
|
|
- logger.info("%s Got correct key, sending positive authentification feedback", self.__log_prefix__())
|
588
|
|
- return self.STATUS_OKAY, True
|
589
|
|
- else:
|
590
|
|
- self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
|
591
|
|
- logger.info("%s Got incorrect key, sending negative authentification feedback", self.__log_prefix__())
|
592
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
593
|
499
|
return self.STATUS_OKAY, False
|
594
|
500
|
|
595
|
501
|
def __authentificate_process_feedback__(self, msg):
|
596
|
502
|
feedback = msg.get_data()
|
597
|
503
|
if feedback:
|
598
|
504
|
self.__authentification_state__ = self.AUTH_STATE_TRUSTED_CLIENT
|
599
|
|
-<<<<<<< HEAD
|
600
|
505
|
self.logger.info("%s Got positive authentification feedback", self.__log_prefix__())
|
601
|
506
|
else:
|
602
|
507
|
self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
|
603
|
508
|
self.logger.warning("%s Got negative authentification feedback", self.__log_prefix__())
|
604
|
|
-=======
|
605
|
|
- logger.info("%s Got positive authentification feedback", self.__log_prefix__())
|
606
|
|
- else:
|
607
|
|
- self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
|
608
|
|
- logger.warning("%s Got negative authentification feedback", self.__log_prefix__())
|
609
|
|
->>>>>>> 8beacaef2257e82852e1c0b5442ec4d09b891b7c
|
610
|
509
|
return self.STATUS_OKAY, None
|
611
|
510
|
|
612
|
511
|
|