Преглед на файлове

Release 1ef858a12d - Channel name added

master
Dirk Alders преди 3 години
родител
ревизия
8beacaef22
променени са 3 файла, в които са добавени 18639 реда и са изтрити 13404 реда
  1. 60
    29
      __init__.py
  2. 18579
    13375
      _testresults_/unittest.json
  3. Двоични данни
      _testresults_/unittest.pdf

+ 60
- 29
__init__.py Целия файл

@@ -50,10 +50,6 @@ __INTERPRETER__ = (2, 3)
50 50
 """The Tested Interpreter-Versions"""
51 51
 
52 52
 
53
-class RegistrationError(BaseException):
54
-    pass
55
-
56
-
57 53
 class callback_storage(dict):
58 54
     def __init__(self):
59 55
         dict.__init__(self)
@@ -81,8 +77,9 @@ class callback_storage(dict):
81 77
         return (None, None, None)
82 78
 
83 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 83
         if service_id not in self:
87 84
             self[service_id] = {}
88 85
         self[service_id][data_id] = (callback, args, kwargs)
@@ -137,12 +134,12 @@ class struct_json_protocol(object):
137 134
 
138 135
     .. literalinclude:: ../../socket_protocol/_examples_/socket_protocol__struct_json_protocol_client.log
139 136
     """
140
-    LOG_PREFIX = 'SJP:'
141
-
142 137
     SID_AUTH_SEED_REQUEST = 1
143 138
     SID_AUTH_KEY_REQUEST = 2
144 139
     SID_AUTH_KEY_CHECK_REQUEST = 3
145 140
     SID_AUTH_KEY_CHECK_RESPONSE = 4
141
+    SID_CHANNEL_NAME_REQUEST = 5
142
+    SID_CHANNEL_NAME_RESPONSE = 6
146 143
     SID_READ_REQUEST = 10
147 144
     SID_READ_RESPONSE = 11
148 145
     SID_WRITE_REQUEST = 20
@@ -153,6 +150,7 @@ class struct_json_protocol(object):
153 150
     SID_RESPONSE_DICT = {SID_AUTH_SEED_REQUEST: SID_AUTH_KEY_REQUEST,
154 151
                          SID_AUTH_KEY_REQUEST: SID_AUTH_KEY_CHECK_REQUEST,
155 152
                          SID_AUTH_KEY_CHECK_REQUEST: SID_AUTH_KEY_CHECK_RESPONSE,
153
+                         SID_CHANNEL_NAME_REQUEST: SID_CHANNEL_NAME_RESPONSE,
156 154
                          SID_READ_REQUEST: SID_READ_RESPONSE,
157 155
                          SID_WRITE_REQUEST: SID_WRITE_RESPONSE,
158 156
                          SID_EXECUTE_REQUEST: SID_EXECUTE_RESPONSE}
@@ -183,22 +181,33 @@ class struct_json_protocol(object):
183 181
                          AUTH_STATE_KEY_TRANSFERRED: 'Key has been sent',
184 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 186
         self.__secret__ = secret
188 187
         self.__auto_auth__ = auto_auth
188
+        self.__channel_name__ = channel_name
189
+        #
189 190
         self.__clean_receive_buffer__()
190 191
         self.__callbacks__ = callback_storage()
191 192
         self.__callbacks__.add(self.SID_AUTH_SEED_REQUEST, 0, self.__authentificate_create_seed__)
192 193
         self.__callbacks__.add(self.SID_AUTH_KEY_REQUEST, 0, self.__authentificate_create_key__)
193 194
         self.__callbacks__.add(self.SID_AUTH_KEY_CHECK_REQUEST, 0, self.__authentificate_check_key__)
194 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 198
         self.__authentification_state_reset__()
196 199
         self.__seed__ = None
197
-        self.__comm_inst__ = comm_instance
198 200
         self.__comm_inst__.register_callback(self.__data_available_callback__)
199 201
         self.__comm_inst__.register_connect_callback(self.__connection_established__)
200 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 211
     def connected(self):
203 212
         return self.__comm_inst__.is_connected()
204 213
 
@@ -210,11 +219,33 @@ class struct_json_protocol(object):
210 219
 
211 220
     def __connection_established__(self):
212 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 224
         if self.__auto_auth__ and self.__comm_inst__.IS_CLIENT and self.__secret__ is not None:
214 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 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 249
         self.__authentification_state__ = self.AUTH_STATE_UNKNOWN_CLIENT
219 250
 
220 251
     def __analyse_frame__(self, frame):
@@ -253,12 +284,12 @@ class struct_json_protocol(object):
253 284
     def __data_available_callback__(self, comm_inst):
254 285
         frame = comm_inst.receive()
255 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 288
         else:
258 289
             msg = self.__analyse_frame__(frame)
259 290
             logger.info(
260 291
                 '%s RX <- status: %s, service_id: %s, data_id: %s, data: "%s"',
261
-                self.LOG_PREFIX,
292
+                self.__log_prefix__(),
262 293
                 repr(msg.get_status()),
263 294
                 repr(msg.get_service_id()),
264 295
                 repr(msg.get_data_id()),
@@ -272,14 +303,14 @@ class struct_json_protocol(object):
272 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 304
                     status = self.STATUS_AUTH_REQUIRED
274 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 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 309
                     status = self.STATUS_BUFFERING_UNHANDLED_REQUEST
279 310
                     data = None
280 311
                 else:
281 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 314
                         status, data = callback(msg, *args, **kwargs)
284 315
                     except TypeError:
285 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,14 +320,14 @@ class struct_json_protocol(object):
289 320
                 # RESPONSE RECEIVED
290 321
                 #
291 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 324
                 if callback is None:
294 325
                     status = self.STATUS_OKAY
295 326
                     data = None
296 327
                     self.__buffer_received_data__(msg)
297 328
                 else:
298 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 331
                         status, data = callback(msg, *args, **kwargs)
301 332
                     except TypeError:
302 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,10 +338,10 @@ class struct_json_protocol(object):
307 338
         if not msg.get_data_id() in self.__msg_buffer__[msg.get_service_id()]:
308 339
             self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()] = []
309 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 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 345
         self.__msg_buffer__ = {}
315 346
 
316 347
     def receive(self, service_id, data_id, timeout=1):
@@ -324,7 +355,7 @@ class struct_json_protocol(object):
324 355
             cnt += 1
325 356
             time.sleep(0.1)
326 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 359
         return data
329 360
 
330 361
     def __mk_msg__(self, status, service_id, data_id, data):
@@ -349,7 +380,7 @@ class struct_json_protocol(object):
349 380
 
350 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 384
         return self.__comm_inst__.send(self.__build_frame__(service_id, data_id, data, status), timeout=timeout, log_lvl=logging.DEBUG)
354 385
 
355 386
     def register_callback(self, service_id, data_id, callback, *args, **kwargs):
@@ -393,7 +424,7 @@ class struct_json_protocol(object):
393 424
         """
394 425
         if self.__secret__ is not None:
395 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 428
             self.send(self.SID_AUTH_SEED_REQUEST, 0, None)
398 429
             cnt = 0
399 430
             while cnt < timeout * 10:
@@ -419,7 +450,7 @@ class struct_json_protocol(object):
419 450
             return hashlib.sha512(seed.encode('utf-8') + self.__secret__.encode('utf-8')).hexdigest()
420 451
 
421 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 454
         self.__authentification_state__ = self.AUTH_STATE_SEED_TRANSFERRED
424 455
         if sys.version_info >= (3, 0):
425 456
             self.__seed__ = binascii.hexlify(os.urandom(32)).decode('utf-8')
@@ -428,7 +459,7 @@ class struct_json_protocol(object):
428 459
         return self.STATUS_OKAY, self.__seed__
429 460
 
430 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 463
         self.__authentification_state__ = self.AUTH_STATE_KEY_TRANSFERRED
433 464
         seed = msg.get_data()
434 465
         key = self.__authentificate_salt_and_hash__(seed)
@@ -438,21 +469,21 @@ class struct_json_protocol(object):
438 469
         key = msg.get_data()
439 470
         if key == self.__authentificate_salt_and_hash__(self.__seed__):
440 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 473
             return self.STATUS_OKAY, True
443 474
         else:
444 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 477
             return self.STATUS_OKAY, False
447 478
 
448 479
     def __authentificate_process_feedback__(self, msg):
449 480
         feedback = msg.get_data()
450 481
         if feedback:
451 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 484
         else:
454 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 487
         return self.STATUS_OKAY, None
457 488
 
458 489
 

+ 18579
- 13375
_testresults_/unittest.json
Файловите разлики са ограничени, защото са твърде много
Целия файл


Двоични данни
_testresults_/unittest.pdf Целия файл


Loading…
Отказ
Запис