Browse Source

Release 1ef858a12d - Channel name added

master
Dirk Alders 3 years ago
parent
commit
8beacaef22
3 changed files with 18639 additions and 13404 deletions
  1. 60
    29
      __init__.py
  2. 18579
    13375
      _testresults_/unittest.json
  3. BIN
      _testresults_/unittest.pdf

+ 60
- 29
__init__.py View File

50
 """The Tested Interpreter-Versions"""
50
 """The Tested Interpreter-Versions"""
51
 
51
 
52
 
52
 
53
-class RegistrationError(BaseException):
54
-    pass
55
-
56
-
57
 class callback_storage(dict):
53
 class callback_storage(dict):
58
     def __init__(self):
54
     def __init__(self):
59
         dict.__init__(self)
55
         dict.__init__(self)
81
         return (None, None, None)
77
         return (None, None, None)
82
 
78
 
83
     def add(self, service_id, data_id, callback, *args, **kwargs):
79
     def add(self, service_id, data_id, callback, *args, **kwargs):
84
-        if self.get(service_id, data_id) != (None, None, None):
85
-            raise RegistrationError("Callback for service_id (%s) and data_id (%s) already exists" % (repr(service_id), repr(data_id)))
80
+        cb_data = self.get(service_id, data_id)
81
+        if cb_data != (None, None, None):
82
+            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__))
86
         if service_id not in self:
83
         if service_id not in self:
87
             self[service_id] = {}
84
             self[service_id] = {}
88
         self[service_id][data_id] = (callback, args, kwargs)
85
         self[service_id][data_id] = (callback, args, kwargs)
137
 
134
 
138
     .. literalinclude:: ../../socket_protocol/_examples_/socket_protocol__struct_json_protocol_client.log
135
     .. literalinclude:: ../../socket_protocol/_examples_/socket_protocol__struct_json_protocol_client.log
139
     """
136
     """
140
-    LOG_PREFIX = 'SJP:'
141
-
142
     SID_AUTH_SEED_REQUEST = 1
137
     SID_AUTH_SEED_REQUEST = 1
143
     SID_AUTH_KEY_REQUEST = 2
138
     SID_AUTH_KEY_REQUEST = 2
144
     SID_AUTH_KEY_CHECK_REQUEST = 3
139
     SID_AUTH_KEY_CHECK_REQUEST = 3
145
     SID_AUTH_KEY_CHECK_RESPONSE = 4
140
     SID_AUTH_KEY_CHECK_RESPONSE = 4
141
+    SID_CHANNEL_NAME_REQUEST = 5
142
+    SID_CHANNEL_NAME_RESPONSE = 6
146
     SID_READ_REQUEST = 10
143
     SID_READ_REQUEST = 10
147
     SID_READ_RESPONSE = 11
144
     SID_READ_RESPONSE = 11
148
     SID_WRITE_REQUEST = 20
145
     SID_WRITE_REQUEST = 20
153
     SID_RESPONSE_DICT = {SID_AUTH_SEED_REQUEST: SID_AUTH_KEY_REQUEST,
150
     SID_RESPONSE_DICT = {SID_AUTH_SEED_REQUEST: SID_AUTH_KEY_REQUEST,
154
                          SID_AUTH_KEY_REQUEST: SID_AUTH_KEY_CHECK_REQUEST,
151
                          SID_AUTH_KEY_REQUEST: SID_AUTH_KEY_CHECK_REQUEST,
155
                          SID_AUTH_KEY_CHECK_REQUEST: SID_AUTH_KEY_CHECK_RESPONSE,
152
                          SID_AUTH_KEY_CHECK_REQUEST: SID_AUTH_KEY_CHECK_RESPONSE,
153
+                         SID_CHANNEL_NAME_REQUEST: SID_CHANNEL_NAME_RESPONSE,
156
                          SID_READ_REQUEST: SID_READ_RESPONSE,
154
                          SID_READ_REQUEST: SID_READ_RESPONSE,
157
                          SID_WRITE_REQUEST: SID_WRITE_RESPONSE,
155
                          SID_WRITE_REQUEST: SID_WRITE_RESPONSE,
158
                          SID_EXECUTE_REQUEST: SID_EXECUTE_RESPONSE}
156
                          SID_EXECUTE_REQUEST: SID_EXECUTE_RESPONSE}
183
                          AUTH_STATE_KEY_TRANSFERRED: 'Key has been sent',
181
                          AUTH_STATE_KEY_TRANSFERRED: 'Key has been sent',
184
                          AUTH_STATE_TRUSTED_CLIENT: 'Trusted Client'}
182
                          AUTH_STATE_TRUSTED_CLIENT: 'Trusted Client'}
185
 
183
 
186
-    def __init__(self, comm_instance, secret=None, auto_auth=False):
184
+    def __init__(self, comm_instance, secret=None, auto_auth=False, channel_name=None):
185
+        self.__comm_inst__ = comm_instance
187
         self.__secret__ = secret
186
         self.__secret__ = secret
188
         self.__auto_auth__ = auto_auth
187
         self.__auto_auth__ = auto_auth
188
+        self.__channel_name__ = channel_name
189
+        #
189
         self.__clean_receive_buffer__()
190
         self.__clean_receive_buffer__()
190
         self.__callbacks__ = callback_storage()
191
         self.__callbacks__ = callback_storage()
191
         self.__callbacks__.add(self.SID_AUTH_SEED_REQUEST, 0, self.__authentificate_create_seed__)
192
         self.__callbacks__.add(self.SID_AUTH_SEED_REQUEST, 0, self.__authentificate_create_seed__)
192
         self.__callbacks__.add(self.SID_AUTH_KEY_REQUEST, 0, self.__authentificate_create_key__)
193
         self.__callbacks__.add(self.SID_AUTH_KEY_REQUEST, 0, self.__authentificate_create_key__)
193
         self.__callbacks__.add(self.SID_AUTH_KEY_CHECK_REQUEST, 0, self.__authentificate_check_key__)
194
         self.__callbacks__.add(self.SID_AUTH_KEY_CHECK_REQUEST, 0, self.__authentificate_check_key__)
194
         self.__callbacks__.add(self.SID_AUTH_KEY_CHECK_RESPONSE, 0, self.__authentificate_process_feedback__)
195
         self.__callbacks__.add(self.SID_AUTH_KEY_CHECK_RESPONSE, 0, self.__authentificate_process_feedback__)
196
+        self.__callbacks__.add(self.SID_CHANNEL_NAME_REQUEST, 0, self.__channel_name_request__)
197
+        self.__callbacks__.add(self.SID_CHANNEL_NAME_RESPONSE, 0, self.__channel_name_response__)
195
         self.__authentification_state_reset__()
198
         self.__authentification_state_reset__()
196
         self.__seed__ = None
199
         self.__seed__ = None
197
-        self.__comm_inst__ = comm_instance
198
         self.__comm_inst__.register_callback(self.__data_available_callback__)
200
         self.__comm_inst__.register_callback(self.__data_available_callback__)
199
         self.__comm_inst__.register_connect_callback(self.__connection_established__)
201
         self.__comm_inst__.register_connect_callback(self.__connection_established__)
200
         self.__comm_inst__.register_disconnect_callback(self.__authentification_state_reset__)
202
         self.__comm_inst__.register_disconnect_callback(self.__authentification_state_reset__)
201
 
203
 
204
+    def __log_prefix__(self):
205
+        postfix = ' (client)' if self.__comm_inst__.IS_CLIENT else ' (server)'
206
+        if self.__channel_name__ is None:
207
+            return __name__ + postfix + ':'
208
+        else:
209
+            return self.__channel_name__ + postfix + ':'
210
+
202
     def connected(self):
211
     def connected(self):
203
         return self.__comm_inst__.is_connected()
212
         return self.__comm_inst__.is_connected()
204
 
213
 
210
 
219
 
211
     def __connection_established__(self):
220
     def __connection_established__(self):
212
         self.__clean_receive_buffer__()
221
         self.__clean_receive_buffer__()
222
+        if not self.__comm_inst__.IS_CLIENT:
223
+            self.send(self.SID_CHANNEL_NAME_REQUEST, 0, self.__channel_name__)
213
         if self.__auto_auth__ and self.__comm_inst__.IS_CLIENT and self.__secret__ is not None:
224
         if self.__auto_auth__ and self.__comm_inst__.IS_CLIENT and self.__secret__ is not None:
214
             self.authentificate()
225
             self.authentificate()
215
 
226
 
227
+    def __channel_name_request__(self, msg):
228
+        data = msg.get_data()
229
+        if data is None:
230
+            return self.STATUS_OKAY, self.__channel_name__
231
+        else:
232
+            prev_channel_name = self.__channel_name__
233
+            self.__channel_name__ = data
234
+            if prev_channel_name is not None and prev_channel_name != data:
235
+                logger.warning('%s overwriting user defined channel name from %s to %s', self.__log_prefix__(), repr(prev_channel_name), repr(data))
236
+            elif prev_channel_name is None:
237
+                logger.info('%s channel name is now %s', self.__log_prefix__(), repr(self.__channel_name__))
238
+            return self.STATUS_OKAY, None
239
+
240
+    def __channel_name_response__(self, msg):
241
+        data = msg.get_data()
242
+        if self.__channel_name__ is None and data is not None:
243
+            self.__channel_name__ = data
244
+            logger.info('%s channel name is now %s', self.__log_prefix__(), repr(self.__channel_name__))
245
+        return self.STATUS_OKAY, None
246
+
216
     def __authentification_state_reset__(self):
247
     def __authentification_state_reset__(self):
217
-        logger.info("%s Resetting authentification state to AUTH_STATE_UNKNOWN_CLIENT", self.LOG_PREFIX)
248
+        logger.info("%s Resetting authentification state to AUTH_STATE_UNKNOWN_CLIENT", self.__log_prefix__())
218
         self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
249
         self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
219
 
250
 
220
     def __analyse_frame__(self, frame):
251
     def __analyse_frame__(self, frame):
253
     def __data_available_callback__(self, comm_inst):
284
     def __data_available_callback__(self, comm_inst):
254
         frame = comm_inst.receive()
285
         frame = comm_inst.receive()
255
         if not self.__check_frame_checksum__(frame):
286
         if not self.__check_frame_checksum__(frame):
256
-            logger.warning("%s Received message has a wrong checksum and will be ignored: %s.", self.LOG_PREFIX, stringtools.hexlify(frame))
287
+            logger.warning("%s Received message has a wrong checksum and will be ignored: %s.", self.__log_prefix__(), stringtools.hexlify(frame))
257
         else:
288
         else:
258
             msg = self.__analyse_frame__(frame)
289
             msg = self.__analyse_frame__(frame)
259
             logger.info(
290
             logger.info(
260
                 '%s RX <- status: %s, service_id: %s, data_id: %s, data: "%s"',
291
                 '%s RX <- status: %s, service_id: %s, data_id: %s, data: "%s"',
261
-                self.LOG_PREFIX,
292
+                self.__log_prefix__(),
262
                 repr(msg.get_status()),
293
                 repr(msg.get_status()),
263
                 repr(msg.get_service_id()),
294
                 repr(msg.get_service_id()),
264
                 repr(msg.get_data_id()),
295
                 repr(msg.get_data_id()),
272
                 if self.__secret__ is not None and not self.check_authentification_state() and msg.get_service_id() not in self.SID_AUTH_LIST:
303
                 if self.__secret__ is not None and not self.check_authentification_state() and msg.get_service_id() not in self.SID_AUTH_LIST:
273
                     status = self.STATUS_AUTH_REQUIRED
304
                     status = self.STATUS_AUTH_REQUIRED
274
                     data = None
305
                     data = None
275
-                    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!'))
306
+                    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!'))
276
                 elif callback is None:
307
                 elif callback is None:
277
-                    logger.warning("%s Received message with no registered callback. Sending negative response.", self.LOG_PREFIX)
308
+                    logger.warning("%s Received message with no registered callback. Sending negative response.", self.__log_prefix__())
278
                     status = self.STATUS_BUFFERING_UNHANDLED_REQUEST
309
                     status = self.STATUS_BUFFERING_UNHANDLED_REQUEST
279
                     data = None
310
                     data = None
280
                 else:
311
                 else:
281
                     try:
312
                     try:
282
-                        logger.debug("%s Executing callback %s to process received data", self.LOG_PREFIX, callback.__name__)
313
+                        logger.debug("%s Executing callback %s to process received data", self.__log_prefix__(), callback.__name__)
283
                         status, data = callback(msg, *args, **kwargs)
314
                         status, data = callback(msg, *args, **kwargs)
284
                     except TypeError:
315
                     except TypeError:
285
                         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())))
316
                         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())))
289
                 # RESPONSE RECEIVED
320
                 # RESPONSE RECEIVED
290
                 #
321
                 #
291
                 if msg.get_status() not in [self.STATUS_OKAY]:
322
                 if msg.get_status() not in [self.STATUS_OKAY]:
292
-                    logger.warning("%s Received message has a peculiar status: %s", self.LOG_PREFIX, self.STATUS_NAMES.get(msg.get_status(), 'Unknown status response!'))
323
+                    logger.warning("%s Received message has a peculiar status: %s", self.__log_prefix__(), self.STATUS_NAMES.get(msg.get_status(), 'Unknown status response!'))
293
                 if callback is None:
324
                 if callback is None:
294
                     status = self.STATUS_OKAY
325
                     status = self.STATUS_OKAY
295
                     data = None
326
                     data = None
296
                     self.__buffer_received_data__(msg)
327
                     self.__buffer_received_data__(msg)
297
                 else:
328
                 else:
298
                     try:
329
                     try:
299
-                        logger.debug("%s Executing callback %s to process received data", self.LOG_PREFIX, callback.__name__)
330
+                        logger.debug("%s Executing callback %s to process received data", self.__log_prefix__(), callback.__name__)
300
                         status, data = callback(msg, *args, **kwargs)
331
                         status, data = callback(msg, *args, **kwargs)
301
                     except TypeError:
332
                     except TypeError:
302
                         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())))
333
                         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())))
307
         if not msg.get_data_id() in self.__msg_buffer__[msg.get_service_id()]:
338
         if not msg.get_data_id() in self.__msg_buffer__[msg.get_service_id()]:
308
             self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()] = []
339
             self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()] = []
309
         self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()].append(msg)
340
         self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()].append(msg)
310
-        logger.debug("%s Message data is stored in buffer and is now ready to be retrieved by receive method", self.LOG_PREFIX)
341
+        logger.debug("%s Message data is stored in buffer and is now ready to be retrieved by receive method", self.__log_prefix__())
311
 
342
 
312
     def __clean_receive_buffer__(self):
343
     def __clean_receive_buffer__(self):
313
-        logger.debug("%s Cleaning up receive-buffer", self.LOG_PREFIX)
344
+        logger.debug("%s Cleaning up receive-buffer", self.__log_prefix__())
314
         self.__msg_buffer__ = {}
345
         self.__msg_buffer__ = {}
315
 
346
 
316
     def receive(self, service_id, data_id, timeout=1):
347
     def receive(self, service_id, data_id, timeout=1):
324
             cnt += 1
355
             cnt += 1
325
             time.sleep(0.1)
356
             time.sleep(0.1)
326
         if data is None and cnt >= timeout * 10:
357
         if data is None and cnt >= timeout * 10:
327
-            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))
358
+            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))
328
         return data
359
         return data
329
 
360
 
330
     def __mk_msg__(self, status, service_id, data_id, data):
361
     def __mk_msg__(self, status, service_id, data_id, data):
349
 
380
 
350
         This methods sends out a message with the given content.
381
         This methods sends out a message with the given content.
351
         """
382
         """
352
-        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))
383
+        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))
353
         return self.__comm_inst__.send(self.__build_frame__(service_id, data_id, data, status), timeout=timeout, log_lvl=logging.DEBUG)
384
         return self.__comm_inst__.send(self.__build_frame__(service_id, data_id, data, status), timeout=timeout, log_lvl=logging.DEBUG)
354
 
385
 
355
     def register_callback(self, service_id, data_id, callback, *args, **kwargs):
386
     def register_callback(self, service_id, data_id, callback, *args, **kwargs):
393
         """
424
         """
394
         if self.__secret__ is not None:
425
         if self.__secret__ is not None:
395
             self.__authentification_state__ = self.AUTH_STATE_SEED_REQUESTED
426
             self.__authentification_state__ = self.AUTH_STATE_SEED_REQUESTED
396
-            logger.info("%s Requesting seed for authentification", self.LOG_PREFIX)
427
+            logger.info("%s Requesting seed for authentification", self.__log_prefix__())
397
             self.send(self.SID_AUTH_SEED_REQUEST, 0, None)
428
             self.send(self.SID_AUTH_SEED_REQUEST, 0, None)
398
             cnt = 0
429
             cnt = 0
399
             while cnt < timeout * 10:
430
             while cnt < timeout * 10:
419
             return hashlib.sha512(seed.encode('utf-8') + self.__secret__.encode('utf-8')).hexdigest()
450
             return hashlib.sha512(seed.encode('utf-8') + self.__secret__.encode('utf-8')).hexdigest()
420
 
451
 
421
     def __authentificate_create_seed__(self, msg):
452
     def __authentificate_create_seed__(self, msg):
422
-        logger.info("%s Got seed request, sending seed for authentification", self.LOG_PREFIX)
453
+        logger.info("%s Got seed request, sending seed for authentification", self.__log_prefix__())
423
         self.__authentification_state__ = self.AUTH_STATE_SEED_TRANSFERRED
454
         self.__authentification_state__ = self.AUTH_STATE_SEED_TRANSFERRED
424
         if sys.version_info >= (3, 0):
455
         if sys.version_info >= (3, 0):
425
             self.__seed__ = binascii.hexlify(os.urandom(32)).decode('utf-8')
456
             self.__seed__ = binascii.hexlify(os.urandom(32)).decode('utf-8')
428
         return self.STATUS_OKAY, self.__seed__
459
         return self.STATUS_OKAY, self.__seed__
429
 
460
 
430
     def __authentificate_create_key__(self, msg):
461
     def __authentificate_create_key__(self, msg):
431
-        logger.info("%s Got seed, sending key for authentification", self.LOG_PREFIX)
462
+        logger.info("%s Got seed, sending key for authentification", self.__log_prefix__())
432
         self.__authentification_state__ = self.AUTH_STATE_KEY_TRANSFERRED
463
         self.__authentification_state__ = self.AUTH_STATE_KEY_TRANSFERRED
433
         seed = msg.get_data()
464
         seed = msg.get_data()
434
         key = self.__authentificate_salt_and_hash__(seed)
465
         key = self.__authentificate_salt_and_hash__(seed)
438
         key = msg.get_data()
469
         key = msg.get_data()
439
         if key == self.__authentificate_salt_and_hash__(self.__seed__):
470
         if key == self.__authentificate_salt_and_hash__(self.__seed__):
440
             self.__authentification_state__ = self.AUTH_STATE_TRUSTED_CLIENT
471
             self.__authentification_state__ = self.AUTH_STATE_TRUSTED_CLIENT
441
-            logger.info("%s Got correct key, sending positive authentification feedback", self.LOG_PREFIX)
472
+            logger.info("%s Got correct key, sending positive authentification feedback", self.__log_prefix__())
442
             return self.STATUS_OKAY, True
473
             return self.STATUS_OKAY, True
443
         else:
474
         else:
444
             self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
475
             self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
445
-            logger.info("%s Got incorrect key, sending negative authentification feedback", self.LOG_PREFIX)
476
+            logger.info("%s Got incorrect key, sending negative authentification feedback", self.__log_prefix__())
446
             return self.STATUS_OKAY, False
477
             return self.STATUS_OKAY, False
447
 
478
 
448
     def __authentificate_process_feedback__(self, msg):
479
     def __authentificate_process_feedback__(self, msg):
449
         feedback = msg.get_data()
480
         feedback = msg.get_data()
450
         if feedback:
481
         if feedback:
451
             self.__authentification_state__ = self.AUTH_STATE_TRUSTED_CLIENT
482
             self.__authentification_state__ = self.AUTH_STATE_TRUSTED_CLIENT
452
-            logger.info("%s Got positive authentification feedback", self.LOG_PREFIX)
483
+            logger.info("%s Got positive authentification feedback", self.__log_prefix__())
453
         else:
484
         else:
454
             self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
485
             self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
455
-            logger.warning("%s Got negative authentification feedback", self.LOG_PREFIX)
486
+            logger.warning("%s Got negative authentification feedback", self.__log_prefix__())
456
         return self.STATUS_OKAY, None
487
         return self.STATUS_OKAY, None
457
 
488
 
458
 
489
 

+ 18579
- 13375
_testresults_/unittest.json
File diff suppressed because it is too large
View File


BIN
_testresults_/unittest.pdf View File


Loading…
Cancel
Save