Browse Source

Channel logging implemented

master
Dirk Alders 4 years ago
parent
commit
7146692712
1 changed files with 38 additions and 23 deletions
  1. 38
    23
      __init__.py

+ 38
- 23
__init__.py View File

54
 
54
 
55
     .. note:: This class is not designed for direct usage.
55
     .. note:: This class is not designed for direct usage.
56
     """
56
     """
57
-    LOG_PREFIX = 'TCP_IP:'
57
+    DEFAULT_CHANNEL_NAME = 'all_others'
58
+
58
     RX_LENGTH = 0xff
59
     RX_LENGTH = 0xff
59
     COM_TIMEOUT = 0.5
60
     COM_TIMEOUT = 0.5
60
     IS_CLIENT = False
61
     IS_CLIENT = False
62
     def __init__(self, host, port, rx_log_lvl=logging.INFO):
63
     def __init__(self, host, port, rx_log_lvl=logging.INFO):
63
         self.host = host
64
         self.host = host
64
         self.port = port
65
         self.port = port
66
+        self.init_channel_name()
65
         self.__socket__ = None
67
         self.__socket__ = None
66
         self.__data_available_callback__ = None
68
         self.__data_available_callback__ = None
67
         self.__supress_data_available_callback__ = False
69
         self.__supress_data_available_callback__ = False
75
         self.__queue__.enqueue(5, self.__receive_task__, rx_log_lvl)
77
         self.__queue__.enqueue(5, self.__receive_task__, rx_log_lvl)
76
         self.__queue__.run()
78
         self.__queue__.run()
77
 
79
 
80
+    def init_channel_name(self, channel_name=None):
81
+        if channel_name is None:
82
+            self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__ + '.' + self.DEFAULT_CHANNEL_NAME)
83
+        else:
84
+            self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__ + '.' + channel_name)
85
+
78
     def is_connected(self):
86
     def is_connected(self):
79
         return self.__connection__ is not None
87
         return self.__connection__ is not None
80
 
88
 
105
                 if self.__connection__ is None:
113
                 if self.__connection__ is None:
106
                     return None
114
                     return None
107
                 if time.time() > tm + timeout:
115
                 if time.time() > tm + timeout:
108
-                    logger.warning('%s TIMEOUT (%ss): Not enough data in buffer. Requested %s and buffer size is %d.', self.LOG_PREFIX, repr(timeout), repr(num or 'all'), len(self.__receive_buffer__))
116
+                    self.logger.warning('TIMEOUT (%ss): Not enough data in buffer. Requested %s and buffer size is %d.', repr(timeout), repr(num or 'all'), len(self.__receive_buffer__))
109
                     return None
117
                     return None
110
                 time.sleep(0.05)
118
                 time.sleep(0.05)
111
             if num is None:
119
             if num is None:
129
 
137
 
130
         This method sends data via the initiated communication channel.
138
         This method sends data via the initiated communication channel.
131
         """
139
         """
132
-        cnt = 0
133
-        while self.__connection__ is None and cnt < int(10 * timeout):
134
-            time.sleep(.1)  # give some time to establish the connection
135
-            cnt += 1
136
-        if self.__connection__ is not None:
137
-            self.__connection__.sendall(data)
138
-            logger.log(log_lvl, '%s TX -> "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
139
-            return True
140
-        else:
141
-            logger.warning('%s Cound NOT send -> "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
142
-            return False
140
+        tm = time.time()
141
+        while time.time() - tm < timeout:
142
+            if self.__connection__ is not None:
143
+                try:
144
+                    self.__connection__.sendall(data)
145
+                except BlockingIOError:
146
+                    time.sleep(.1)  # try again till timeout exceeds
147
+                except BrokenPipeError:
148
+                    self.logger.exception('Exception while sending data')
149
+                    self.__connection_lost__()
150
+                    return False
151
+                else:
152
+                    self.logger.log(log_lvl, 'TX -> "%s"', stringtools.hexlify(data))
153
+                    return True
154
+            else:
155
+                time.sleep(.1)  # give some time to establish the connection
156
+        self.logger.warning('Cound NOT send -> "%s"', stringtools.hexlify(data))
157
+        return False
143
 
158
 
144
     def register_callback(self, callback):
159
     def register_callback(self, callback):
145
         """
160
         """
184
                     time.sleep(.05)
199
                     time.sleep(.05)
185
             else:
200
             else:
186
                 if len(data) > 0:
201
                 if len(data) > 0:
187
-                    logger.log(rx_log_lvl, '%s RX <- "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
202
+                    self.logger.log(rx_log_lvl, 'RX <- "%s"', stringtools.hexlify(data))
188
                     self.__receive_buffer__ += data
203
                     self.__receive_buffer__ += data
189
                 else:
204
                 else:
190
                     self.__connection_lost__()
205
                     self.__connection_lost__()
204
         self.__connection__.close()
219
         self.__connection__.close()
205
         self.__connection__ = None
220
         self.__connection__ = None
206
         self.__client_address__ = None
221
         self.__client_address__ = None
207
-        logger.info('%s Connection lost...', self.LOG_PREFIX)
222
+        self.logger.info('Connection lost...')
208
         if self.__disconnect_callback__ is not None:
223
         if self.__disconnect_callback__ is not None:
209
             self.__disconnect_callback__()
224
             self.__disconnect_callback__()
210
 
225
 
211
     def __clean_receive_buffer__(self):
226
     def __clean_receive_buffer__(self):
212
-        logger.debug("%s Cleaning up receive-buffer", self.LOG_PREFIX)
227
+        self.logger.debug("Cleaning up receive-buffer")
213
         self.__receive_buffer__ = ""
228
         self.__receive_buffer__ = ""
214
 
229
 
215
     def close(self):
230
     def close(self):
258
             self.__socket__.settimeout(self.COM_TIMEOUT)
273
             self.__socket__.settimeout(self.COM_TIMEOUT)
259
             self.__socket__.setblocking(False)
274
             self.__socket__.setblocking(False)
260
         if not self.__listening_message_displayed__:
275
         if not self.__listening_message_displayed__:
261
-            logger.info('%s Server listening to %s:%d', self.LOG_PREFIX, self.host, self.port)
276
+            self.logger.info('Server listening to %s:%d', self.host, self.port)
262
             self.__listening_message_displayed__ = True
277
             self.__listening_message_displayed__ = True
263
         try:
278
         try:
264
             self.__connection__, self.__client_address__ = self.__socket__.accept()
279
             self.__connection__, self.__client_address__ = self.__socket__.accept()
268
             else:
283
             else:
269
                 time.sleep(.05)
284
                 time.sleep(.05)
270
         else:
285
         else:
271
-            logger.info('%s Connection established... (from %s)', self.LOG_PREFIX, self.client_address())
286
+            self.logger.info('Connection established... (from %s)', self.client_address())
272
             self.__clean_receive_buffer__()
287
             self.__clean_receive_buffer__()
273
             self.__connection__.setblocking(False)
288
             self.__connection__.setblocking(False)
274
             if self.__connect_callback__ is not None:
289
             if self.__connect_callback__ is not None:
313
                 self.__connection__ = None
328
                 self.__connection__ = None
314
                 time.sleep(.05)
329
                 time.sleep(.05)
315
         else:
330
         else:
316
-            logger.info('%s Connection to %s:%s established', self.LOG_PREFIX, self.host, self.port)
331
+            self.logger.info('Connection to %s:%s established', self.host, self.port)
317
             self.__clean_receive_buffer__()
332
             self.__clean_receive_buffer__()
318
             self.__connection__ = self.__socket__
333
             self.__connection__ = self.__socket__
319
             if self.__connect_callback__ is not None:
334
             if self.__connect_callback__ is not None:
346
         self.__stp__ = stringtools.stp.stp()
361
         self.__stp__ = stringtools.stp.stp()
347
 
362
 
348
     def __clean_receive_buffer__(self):
363
     def __clean_receive_buffer__(self):
349
-        logger.debug("%s Cleaning up receive-buffer", self.LOG_PREFIX)
364
+        self.logger.debug("Cleaning up receive-buffer")
350
         self.__receive_buffer__ = []
365
         self.__receive_buffer__ = []
351
 
366
 
352
     def receive(self, timeout=1):
367
     def receive(self, timeout=1):
377
         This method sends one message via the initiated communication channel.
392
         This method sends one message via the initiated communication channel.
378
         """
393
         """
379
         if tcp_base.send(self, stringtools.stp.build_frame(data), timeout=timeout, log_lvl=logging.DEBUG):
394
         if tcp_base.send(self, stringtools.stp.build_frame(data), timeout=timeout, log_lvl=logging.DEBUG):
380
-            logger.log(log_lvl, '%s TX -> "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
395
+            self.logger.log(log_lvl, 'TX -> "%s"', stringtools.hexlify(data))
381
             return True
396
             return True
382
         else:
397
         else:
383
             return False
398
             return False
395
                     time.sleep(.05)
410
                     time.sleep(.05)
396
             else:
411
             else:
397
                 if len(data) > 0:
412
                 if len(data) > 0:
398
-                    logger.debug('%s -- <- "%s"', self.LOG_PREFIX, stringtools.hexlify(data))
413
+                    self.logger.debug('-- <- "%s"', stringtools.hexlify(data))
399
                     content = self.__stp__.process(data)
414
                     content = self.__stp__.process(data)
400
                     for msg in content:
415
                     for msg in content:
401
-                        logger.log(rx_log_lvl, '%s RX  <- "%s"', self.LOG_PREFIX, stringtools.hexlify(msg))
416
+                        self.logger.log(rx_log_lvl, 'RX  <- "%s"', stringtools.hexlify(msg))
402
                         self.__receive_buffer__.append(msg)
417
                         self.__receive_buffer__.append(msg)
403
                 else:
418
                 else:
404
                     self.__connection_lost__()
419
                     self.__connection_lost__()

Loading…
Cancel
Save