-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionHandler.py
More file actions
1185 lines (969 loc) · 44.4 KB
/
ConnectionHandler.py
File metadata and controls
1185 lines (969 loc) · 44.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Begin ControlScript Import --------------------------------------------------
from extronlib.interface import (EthernetClientInterface,
EthernetServerInterface, EthernetServerInterfaceEx, SerialInterface)
from extronlib.system import Wait, Timer
## End ControlScript Import ----------------------------------------------------
from collections import deque
from time import monotonic
__version__ = '2.0.0'
def ModuleVersion():
"""The ConnectionHandler Module version.
:rtype: string
"""
return __version__
DEBUG = False
def _trace(*args, **kwargs):
if DEBUG:
print(*args, **kwargs)
def GetConnectionHandler(Interface, keepAliveQuery=None,
keepAliveQueryQualifier=None, DisconnectLimit=15,
pollFrequency=1, connectRetryTime=5,
serverTimeout=5*60):
"""
Creates a new connection handler instance tailored to the object instance
passed in the Interface argument.
.. code-block:: python
Server = GetConnectionHandler(EthernetServerInterfaceEx(9000),
serverTimeout=30)
# The connection handler will use the Global Scripter Module's Video
# Mute query function to keep the connection alive.
Dvs605 = GetConnectionHandler(DvsModule.EthernetClass('10.113.133.51',
9000, Model='DVS 605'), 'VideoMute',
DisconnectLimit=5)
# MlcKeepAlive in this example is a function that sends a programmer-
# specified query string.
Mlc206 = GetConnectionHandler(SerialInterface(MainProcessor, 'COM2'),
MlcKeepAlive)
:param Interface: The extronlib.interface for which to create a connection
handler.
:type Interface: EthernetClientInterface, EthernetServerInterfaceEx,
SerialInterface or a derivative of one of these types
:param keepAliveQuery: A function name as a string or reference to a
callable used to execute the keep alive query.
:type keepAliveQuery: string, callable
:param keepAliveQueryQualifier: When Interface is a Global Scripter Module,
any qualifiers needed by the update
function.
:type keepAliveQueryQualifier: dict
:param DisconnectLimit: Maximum number of missed responses that indicates
a disconnected device. Defaults to 15.
:type DisconnectLimit: int
:param pollFrequency: How often to send the keep alive query in seconds.
Defaults to 1.
:type pollFrequency: float
:param connectRetryTime: Number of seconds to wait before attempting to
reconnect after disconnect.
:type connectRetryTime: float
:param serverTimeout: For server Interfaces, maximum time in seconds to
allow before disconnecing idle clients. Defaults to 5
minutes.
:type serverTimeout: float.
:returns: An object instance with an API similar to an extronlib.interface
object.
:raises TypeError: if Interface is an `EthernetServerInferface` (non-Ex) or
is a UDP `EthernetServerInferfaceEx`.
:raises ValueError: if Interface is an `EthernetClientInterface` and its
Protocol type is not TCP, UDP, or SSH.
The returned object instance depends on the instance type passed in
Interface.
.. list-table::
:widths: auto
* - **Interface**
- **Returned Instance**
* - TCP EthernetClientInterface
- :py:class:`RawTcpHandler`
* - SSH EthernetClientInterface
- :py:class:`RawTcpHandler`
* - UDP EthernetClientInterface
- :py:class:`RawSimplePipeHandler`
* - TCP Global Scripter Module
- :py:class:`ModuleTcpHandler`
* - SSH Global Scripter Module
- :py:class:`ModuleTcpHandler`
* - UDP Global Scripter Module
- :py:class:`ModuleSimplePipeHandler`
* - Serial Interface
- :py:class:`RawSimplePipeHandler`
* - Serial Global Scripter Module
- :py:class:`RawSimplePipeHandler`
* - Serial-Over-Ethernet Global Scripter Module
- :py:class:`ModuleTcpHandler`
* - EthernetServerInterfaceEx
- :py:class:`ServerExHandler`
.. note:: Only TCP EthernetServerInterfaceEx instances are supported.
"""
if isinstance(Interface, EthernetServerInterface):
raise TypeError('EthernetServerInterface is not a supported interface '
'type. Use EthernetServerInterfaceEx instead.')
if isinstance(Interface, EthernetServerInterfaceEx):
if Interface.Protocol == 'UDP':
raise TypeError('UDP is not a supported protocol type. Use '
'EthernetServerInterfaceEx without the Universal '
'Connection Handler.')
return ServerExHandler(Interface, serverTimeout, connectRetryTime)
# Look for signs Interface is a Global Scripter Module. We will assume
# Interface is a Global Scripter Module if it has a callable (i.e. acts
# like a Python function) attribute with a name matching 'Update' +
# keepAliveQuery and a callable SubscribeStatus attribute.
# Connection handler classes for Global Scripter Modules use the module's
# built-in Update facility whereas the others treat keepAliveQuery as a
# user function that sends a query string the controlled device.
if isinstance(keepAliveQuery, str):
UpdateAttr = getattr(Interface, 'Update' + keepAliveQuery, None)
SubscribeAttr = getattr(Interface, 'SubscribeStatus', None)
if callable(UpdateAttr) and callable(SubscribeAttr):
IsGSModule = True
else:
IsGSModule = False
elif callable(keepAliveQuery):
IsGSModule = False
else:
raise ValueError('keepAliveQuery must be the name of an Update '
'function in a Global Scripter Module or a reference '
'to a callable function.')
if IsGSModule:
if isinstance(Interface, SerialInterface):
return ModuleSimplePipeHandler(Interface, DisconnectLimit,
pollFrequency, keepAliveQuery,
keepAliveQueryQualifier)
if isinstance(Interface, EthernetClientInterface):
if Interface.Protocol in ['TCP', 'SSH']:
return ModuleTcpHandler(Interface, DisconnectLimit,
pollFrequency, keepAliveQuery,
keepAliveQueryQualifier,
connectRetryTime)
elif Interface.Protocol == 'UDP':
return ModuleSimplePipeHandler(Interface, DisconnectLimit,
pollFrequency, keepAliveQuery,
keepAliveQueryQualifier)
else:
raise ValueError('Unsupported Ethernet protocol '
'type: {}.'.format(Interface.Protocol))
else:
if isinstance(Interface, SerialInterface):
return RawSimplePipeHandler(Interface, DisconnectLimit,
pollFrequency, keepAliveQuery)
if isinstance(Interface, EthernetClientInterface):
if Interface.Protocol in ['TCP', 'SSH']:
return RawTcpHandler(Interface, DisconnectLimit, pollFrequency,
keepAliveQuery, connectRetryTime)
elif Interface.Protocol == 'UDP':
return RawSimplePipeHandler(Interface, DisconnectLimit,
pollFrequency, keepAliveQuery)
else:
raise ValueError('Unsupported Ethernet protocol '
'type: {}.'.format(Interface.Protocol))
def _UnassignedEvent(*args, **kwargs):
pass
class ScripterModuleMixin:
"""
The ScripterModuleMixin adds methods to a ConnectionHandler subclass to
facilitate intercepting features built into Global Scriptor Modules such as
status subscription.
"""
def _AddStatusSubscriber(self):
# Check for a 'SubscribeStatus' method on the wrapped interface. If one
# exists, add the interceptor for it.
if hasattr(self._WrappedInterface, 'SubscribeStatus'):
self.SubscribeStatus = self._SubscribeStatus
# Add a subscription for ConnectionStatus routed to our handler.
self._WrappedInterface.SubscribeStatus('ConnectionStatus', None,
self._NewConnectionStatus)
def _NewSubscribedStatus(self, command, value, qualifier):
# Propogate subscription callbacks up to the client code.
callback = self._Subscriptions[command]
callback(command, value, qualifier)
def _SubscribeStatus(self, command, qualifier, callback):
# Passes status subscriptions down to the wrapped interface except for
# ConnectionStatus. ConnectionStatus subscription is instead handled in
# this class.
self._Subscriptions[command] = callback
if not command == 'ConnectionStatus':
self._WrappedInterface.SubscribeStatus(command, qualifier,
self._NewSubscribedStatus)
class ConnectionHandler:
"""
Base class for all client-type connection handlers.
This class is not intended to be used directly as it is the base class for
all connection handlers. Rather, use :py:meth:`GetConnectionHandler` to
instantiate the correct handler for your interface type.
"""
def __init__(self, Interface, pollFrequency):
self._WrappedInterface = Interface
self._PollTimer = Timer(pollFrequency, self._PollTriggered)
self._PollTimer.Pause()
# Common Event Handlers
self._Connected = _UnassignedEvent
self._Disconnected = _UnassignedEvent
self._ConnectionStatus = 'Unknown'
@property
def Connected(self):
"""
``Event:`` Triggers when the underlying interface instance connects.
The callback function must accept two parameters. The first is the
ConnectionHandler instance triggering the event and the second is the
string 'Connected'.
"""
return self._Connected
@Connected.setter
def Connected(self, handler):
_trace('set Connected handler.', handler)
if callable(handler):
self._Connected = handler
else:
raise TypeError("'handler' is not callable")
@property
def ConnectionStatus(self):
"""
Returns the current connection state: ``Connected``, ``Disconnected``,
or ``Unknown``.
"""
return self._ConnectionStatus
@property
def Disconnected(self):
"""
``Event:`` Triggers when the underlying interface instance disconnects.
The callback function must accept two parameters. The first is the
ConnectionHandlerinstance triggering the event and the second is the
string 'Disconnected'.
"""
return self._Disconnected
@Disconnected.setter
def Disconnected(self, handler):
_trace('set Disconnected handler.', handler)
if callable(handler):
self._Disconnected = handler
else:
raise TypeError("'handler' is not callable")
@property
def DisconnectLimit(self):
"""
:returns: the value set as the maximum number of missed responses
that indicates a disconnected device.
:rtype: int
"""
return self._DisconnectLimit
@property
def Interface(self):
"""
:returns: the interface instance for which this instance is handling
connection.
:rtype: an extronlib interface or a Global Scriptor Module
"""
return self._WrappedInterface
@property
def PollTimer(self):
"""
:returns: the timer instance used to schedule keep alive polling.
:rtype: extronlib.system.Timer
"""
return self._PollTimer
def __getattr__(self, name):
# This function overrides the Python-supplied version of __getattr__.
# Under the covers, accessing obj.name causes Python to first call
# __getattribute__ on obj to lookup 'name'. If that fails Python then
# calls __getattr__, which we can hook to implement fall-through to the
# underlying interface instance for methods not implemented by this
# class.
try:
return self._WrappedInterface.__getattribute__(name)
except AttributeError:
SelfName = self.__class__.__name__
WrappedName = self._WrappedInterface.__class__.__name__
raise AttributeError("'{}' object has no attribute '{}' nor was "
"one found in the underlying '{}' "
"object.".format(SelfName, name, WrappedName))
class RawSimplePipeHandler(ConnectionHandler):
def __init__(self, Interface, DisconnectLimit, pollFrequency,
keepAliveQuery):
"""
Wraps an instance of extronlib's SerialInterface or UDP
EthernetClientInterface (or derivative of these types) to provide
connect and disconnect events and periodic keep alive polling.
.. note:: Use :py:meth:`GetConnectionHandler` to instantiate the
correct connection handler rather than instantiating
:py:class:`RawSimplePipeHandler` directly.
:param Interface: The interface instance to wrap.
:type Interface: SerialInterface, EthernetClientInterface, or derivative
:param DisconnectLimit: Maximum number of missed responses that
indicates a disconnected device.
:type DisconnectLimit: int
:param pollFrequency: How often in seconds to send the keep alive
query.
:type pollFrequency: float
:param keepAliveQuery: The callback that will send the appropriate
device query. The callback must accept the
calling ConnectionHandler instance as an
argument.
:type keepAliveQuery: callable
"""
super().__init__(Interface, pollFrequency)
self._keepAliveQuery = keepAliveQuery
# Event Handlers
self._ReceiveData = _UnassignedEvent
self._WrappedInterface.ReceiveData = self._IfaceRxData
# Bookkeeping
self._SendCounter = 0
self._DisconnectLimit = DisconnectLimit
@property
def ReceiveData(self):
"""
``Event:`` Triggers when data is received by the underlying interface
instance.
The callback function must accept two parameters. The first is the
ConnectionHandler instance triggering the event and the second is the
received data as a bytes object.
"""
return self._ReceiveData
@ReceiveData.setter
def ReceiveData(self, handler):
if callable(handler):
self._ReceiveData = handler
else:
raise TypeError("'handler' is not callable")
def Connect(self):
'''
Attempts to connect and starts the polling loop. The keepAliveQuery
function will be called at the rate set by the PollTimer Interval.
'''
if self._PollTimer.State != 'Running':
self._PollTimer.Restart()
def ResponseAccepted(self):
"""
Resets the send counter. Call this function when a valid response has
been received from the controlled device.
.. note:: This function must be called whenever your code determines
that a good keep alive response has been received.
.. code-block:: python
@event(SomeDevice, 'ReceiveData')
def HandleReceiveData(interface, data):
DataBuffer += data.decode()
# Parse DataBuffer content.
if IsGoodResponse:
interface.ResponseAccepted()
"""
self._SendCounter = 0
self._NewConnectionStatus('Connected')
def Send(self, data):
'''
Send data to the controlled device without waiting for response.
:param data: string to send out
:type data: bytes, string
:raise: TypeError, IOError
.. code-block:: python
MainProjector.Send('GET POWER\\r')
'''
self._SendCounter += 1
_trace('Send: data=', data, 'count=', self._SendCounter)
if self._DisconnectLimitExceeded():
self._NewConnectionStatus('Disconnected')
self._WrappedInterface.Send(data)
def SendAndWait(self, data, timeout, **delimiter):
'''
Send data to the controlled device and wait (blocking) for response. It
returns after *timeout* seconds expires, or returns immediately if the
optional condition is satisfied.
.. note:: In addition to *data* and *timeout*, the method accepts an
optional delimiter, which is used to compare against the received
response. It supports any one of the following conditions:
* *deliLen* (int) - length of the response
* *deliTag* (byte) - suffix of the response
* *deliRex* (regular expression object) - regular expression
.. note:: The function will return an empty byte array if *timeout*
expires and nothing is received, or the condition (if provided) is
not met.
:param data: data to send.
:type data: bytes, string
:param timeout: amount of time to wait for response.
:type timeout: float
:param delimiter: optional conditions to look for in response.
:type delimiter: see above
:return: Response received data (may be empty)
:rtype: bytes
'''
self._SendCounter += 1
_trace('SendAndWait: data=', data, 'count=', self._SendCounter)
if self._DisconnectLimitExceeded():
self._NewConnectionStatus('Disconnected')
return self._WrappedInterface.SendAndWait(data, timeout, **delimiter)
def _DisconnectLimitExceeded(self):
return self._SendCounter > self._DisconnectLimit
def _IfaceRxData(self, interface, data):
"""
Intercepts the ReceiveData event emitted by the wrapped interface and
passes the event up to client code.
"""
self._ReceiveData(self, data)
def _NewConnectionStatus(self, value):
"""
Triggers the Connected or Disconnect event on connection status change.
"""
if not value == self._ConnectionStatus:
self._ConnectionStatus = value
if value == 'Connected':
self._Connected(self, value)
elif value == 'Disconnected':
self._Disconnected(self, value)
def _PollTriggered(self, timer, count):
self._keepAliveQuery(self)
class ModuleSimplePipeHandler(ScripterModuleMixin, ConnectionHandler):
def __init__(self, Interface, DisconnectLimit, pollFrequency,
keepAliveQuery, keepAliveQualifiers=None):
"""
Wraps a GlobalScripter Module instance derived from
extronlib's SerialInterface or UDP EthernetClientInterface to provide
connect/disconnect events and periodic keep alive polling.
.. note:: Use :py:meth:`GetConnectionHandler` to instantiate the
correct connection handler rather than instantiating
:py:class:`ModuleSimplePipeHandler` directly.
:param Interface: The interface instance who's connection state will be
managed.
:type Interface: SerialInterface, EthernetClientInterface, or derivative
:param DisconnectLimit: Maximum number of missed responses that
indicates a disconnected device.
:type DisconnectLimit: int
:param pollFrequency: How often to send the keep alive query in
seconds.
:type pollFrequency: float
:param keepAliveQuery: The name of an Update function that will be
queried to keep the connection alive.
:type keepAliveQuery: string
:param keepAliveQualifiers: Dictionary of parameter and value pairs to
be passed to the keep-alive function.
:type keepAliveQualifiers: dict
"""
super().__init__(Interface, pollFrequency)
self._keepAliveQuery = keepAliveQuery
self._keepAliveParams = keepAliveQualifiers
# Override the module's maximum missed response count.
self._WrappedInterface.connectionCounter = DisconnectLimit
# Bookkeeping
self._DisconnectLimit = DisconnectLimit
self._AddStatusSubscriber()
# Maps command names for which the client has status subscriptions to
# the client's callback function.
self._Subscriptions = {}
def Connect(self):
'''
Attempts to connect and starts the polling loop. The keepAliveQuery
function will be called at the rate set by the PollTimer Interval.
'''
if self._PollTimer.State != 'Running':
self._PollTimer.Restart()
def _NewConnectionStatus(self, command, value, qualifier):
if not value == self._ConnectionStatus:
self._ConnectionStatus = value
if value == 'Connected':
self._Connected(self, value)
elif value == 'Disconnected':
self._Disconnected(self, value)
# Update the client with new connection status if subscribed.
client_callback = self._Subscriptions.get('ConnectionStatus', None)
if callable(client_callback):
client_callback('ConnectionStatus', value, None)
def _PollTriggered(self, timer, count):
self._WrappedInterface.Update(self._keepAliveQuery,
self._keepAliveParams)
class RawTcpHandler(ConnectionHandler):
def __init__(self, Interface, DisconnectLimit, pollFrequency,
keepAliveQuery, connectRetryTime):
"""
Wraps an extronlib EthernetClientInterface instance using TCP
or SSH protocol to provide connect/disconnect events and periodic keep
alive polling.
.. note:: Use :py:meth:`GetConnectionHandler` to instantiate the
correct connection handler rather than instantiating
:py:class:`RawTcpHandler` directly.
:param Interface: The interface to wrap.
:type Interface: extronlib.EthernetClientInterface
:param DisconnectLimit: Maximum number of missed responses that
indicates a disconnected device.
:type DisconnectLimit: int
:param pollFrequency: How often to send the keep alive query in
seconds.
:type pollFrequency: float
:param keepAliveQuery: The callback that will send the appropriate
device query. The callback must accept the
calling ConnectionHandler instance as an
argument.
:type keepAliveQuery: callable
:param connectRetryTime: Time in seconds to wait before attempting to
reconnect to the remote host.
:type connectRetryTime: float
"""
super().__init__(Interface, pollFrequency)
self._keepAliveQuery = keepAliveQuery
# Event Handlers
self._ConnectFailed = _UnassignedEvent
self._ReceiveData = _UnassignedEvent
# Capture interface events
self._WrappedInterface.ReceiveData = self._IfaceRxData
self._WrappedInterface.Connected = self._IfaceConnected
self._WrappedInterface.Disconnected = self._IfaceDisconnected
# Bookkeeping
self._SendCounter = 0
self._DisconnectLimit = DisconnectLimit
self._ReconnectTime = connectRetryTime
self._ReconnectTimer = Timer(self._ReconnectTime,
self._AttemptReconnect)
self._ReconnectTimer.Stop()
self._AttemptingConnect = False
self._ConnectTimeout = 3
self._AutoReconnect = True
@property
def AutoReconnect(self):
"""
Controls whether or not the connection handler attempts to reconnect
after disconnect.
:return: The auto reconnect state.
:rtype: bool
"""
return self._AutoReconnect
@AutoReconnect.setter
def AutoReconnect(self, value):
if value:
self._AutoReconnect = True
else:
self._AutoReconnect = False
@property
def ConnectFailed(self):
"""
``Event:`` Triggers when a TCP connect attempt fails.
The callback function must accept two parameters. The first is the
ConnectionHandler instance triggering the event and the second is the
failure reason as a string.
.. code-block:: python
@event(SomeInterface, 'ConnectFailed')
def HandleConnectFailed(interface, reason):
print('Connect failure:', reason)
"""
return self._ConnectFailed
@ConnectFailed.setter
def ConnectFailed(self, handler):
if callable(handler):
self._ConnectFailed = handler
else:
raise TypeError("'handler' is not callable")
@property
def ReceiveData(self):
"""
``Event:`` Triggers when data is received by the underlying interface
instance.
The callback function must accept two parameters. The first is the
ConnectionHandler instance triggering the event and the second is the
received data as a bytes object.
"""
return self._ReceiveData
@ReceiveData.setter
def ReceiveData(self, handler):
if callable(handler):
self._ReceiveData = handler
else:
raise TypeError("'handler' is not callable")
def Connect(self):
'''
Attempt to connect to the server and starts the polling loop. The
keepAliveQuery function will be called at the rate set by PollTimer
Interval.
The connection will be attempted only once unless AutoReconnect is
True.
'''
self._AttemptReconnect()
if self._PollTimer.State != 'Running':
self._PollTimer.Restart()
def ResponseAccepted(self):
"""
Resets the send counter. Call this function when a valid response has
been received from the controlled device.
.. note:: This function must be called whenever your code determines
that a good keep alive response has been received.
.. code-block:: python
@event(SomeDevice, 'ReceiveData')
def HandleReceiveData(interface, data):
global DataBuffer
DataBuffer += data.decode()
# Parse DataBuffer content.
if IsGoodResponse:
interface.ResponseAccepted()
"""
self._SendCounter = 0
self._NewConnectionStatus('Connected')
def Send(self, data):
'''
Send data to the controlled device without waiting for response.
:param data: string to send out
:type data: bytes, string
:raise: TypeError, IOError
.. code-block:: python
MainProjector.Send('GET POWER\\r')
'''
self._SendCounter += 1
_trace('Send: data=', data, 'count=', self._SendCounter)
if self._DisconnectLimitExceeded():
self._WrappedInterface.Disconnect()
else:
self._WrappedInterface.Send(data)
def SendAndWait(self, data, timeout, **delimiter):
'''
Send data to the controlled device and wait (blocking) for response. It
returns after *timeout* seconds expires, or returns immediately if the
optional condition is satisfied.
.. note:: In addition to *data* and *timeout*, the method accepts an
optional delimiter, which is used to compare against the received
response. It supports any one of the following conditions:
* *deliLen* (int) - length of the response
* *deliTag* (byte) - suffix of the response
* *deliRex* (regular expression object) - regular expression
.. note:: The function will return an empty byte array if *timeout*
expires and nothing is received, or the condition (if provided) is
not met.
:param data: data to send.
:type data: bytes, string
:param timeout: amount of time to wait for response.
:type timeout: float
:param delimiter: optional conditions to look for in response.
:type delimiter: see above
:return: Response received data (may be empty)
:rtype: bytes
'''
self._SendCounter += 1
_trace('SendAndWait: data=', data, 'count=', self._SendCounter)
if not self._DisconnectLimitExceeded():
res = self._WrappedInterface.SendAndWait(data, timeout,
**delimiter)
return res
self._WrappedInterface.Disconnect()
def _AttemptReconnect(self, timer=None, count=0):
self._AttemptingConnect = True
connect_res = self._WrappedInterface.Connect(self._ConnectTimeout)
if connect_res not in ['Connected', 'AlreadyConnected']:
self._ConnectFailed(self, connect_res)
self._SendCounter = self._DisconnectLimit + 1
if self._AutoReconnect and self._ReconnectTimer.State != 'Running':
self._ReconnectTimer.Resume()
def _DisconnectLimitExceeded(self):
return self._SendCounter > self._DisconnectLimit
def _IfaceConnected(self, interface, state):
self._SendCounter = 0
self._AttemptingConnect = False
if self._ReconnectTimer.State != 'Stopped':
self._ReconnectTimer.Stop()
def _IfaceDisconnected(self, interface, state):
self._AttemptingConnect = True
if self._AutoReconnect:
if self._ReconnectTimer.State != 'Running':
self._ReconnectTimer.Resume()
self._NewConnectionStatus('Disconnected')
def _IfaceRxData(self, interface, data):
self._ReceiveData(self, data)
def _NewConnectionStatus(self, value):
if not value == self._ConnectionStatus:
self._ConnectionStatus = value
if value == 'Connected':
self._Connected(self, value)
elif value == 'Disconnected':
self._Disconnected(self, value)
def _PollTriggered(self, timer, count):
if not self._AttemptingConnect:
self._keepAliveQuery(self)
class ModuleTcpHandler(ScripterModuleMixin, ConnectionHandler):
def __init__(self, Interface, DisconnectLimit, pollFrequency,
keepAliveQuery, keepAliveQualifiers, reconnectTime):
"""
Wraps a Global Scripter Module instance derived from
extronlib's EthernetClientInterface to provide connect/disconnect events
and periodic keep alive polling.
.. note:: Use :py:meth:`GetConnectionHandler` to instantiate the
correct connection handler rather than instantiating
:py:class:`ModuleTcpHandler` directly.
:param Interface: The interface instance who's connection state will be
managed.
:type Interface: A Global Scripter Module instance derived from
EthernetClientInterface
:param DisconnectLimit: Maximum number of missed responses that
indicates a disconnected device.
:type DisconnectLimit: int
:param PollTimer: A Timer instance used to schedule keep alive queries.
:type PollTimer: extronlib.system.Timer
:param keepAliveQuery: The name of an Update function that will be
queried to keep the connection alive.
:type keepAliveQuery: string
:param keepAliveQualifiers: parameter and value pairs to be passed to
the keep-alive function.
:type keepAliveQualifiers: dict
"""
super().__init__(Interface, pollFrequency)
self._keepAliveQuery = keepAliveQuery
self._keepAliveParams = keepAliveQualifiers
# Override the module's maximum missed response count.
self._DisconnectLimit = DisconnectLimit
self._WrappedInterface.connectionCounter = DisconnectLimit
# Event Handlers
self._ConnectFailed = _UnassignedEvent
# Capture interface events
self._WrappedInterface.Connected = self._IfaceConnected
self._WrappedInterface.Disconnected = self._IfaceDisconnected
# Bookkeeping
self._MaxHistory = DisconnectLimit
self._reconnectTime = reconnectTime
self._ReconnectTimer = Timer(self._reconnectTime,
self._AttemptReconnect)
self._ReconnectTimer.Stop()
self._AttemptingConnect = False
self._ConnectHistory = deque(maxlen=self._MaxHistory)
self._AddStatusSubscriber()
# Maps command names for which the client has status subscriptions to
# the client's callback function.
self._Subscriptions = {}
self._ConnectTimeout = 3
self._AutoReconnect = True
@property
def AutoReconnect(self):
"""
Enable or disable auto reconnect.
:type value: boolean
"""
return self._AutoReconnect
@AutoReconnect.setter
def AutoReconnect(self, value):
if value:
self._AutoReconnect = True
else:
self._AutoReconnect = False
@property
def ConnectFailed(self):
"""
``Event:`` Triggers when a TCP connect attempt fails.
The callback function must accept two parameters. The first is the
ConnectionHandler instance triggering the event and the second is the
failure reason as a string.
.. code-block:: python
@event(SomeInterface, 'ConnectFailed')
def HandleConnectFailed(interface, reason):
print('Connect failure:', reason)
"""
return self._ConnectFailed
@ConnectFailed.setter
def ConnectFailed(self, handler):
if callable(handler):
self._ConnectFailed = handler
else:
raise TypeError("'handler' is not callable")
def Connect(self):
"""
Attempt to connect to the server and starts the polling loop. The
keepAliveQuery function will be called at the rate set by PollTimer
Interval.
The connection will be attempted only once unless AutoReconnect is
True.
"""
if not self._AttemptingConnect and \
self._ConnectionStatus != 'Connected':
self._AttemptReconnect()
if self._PollTimer.State != 'Running':
self._PollTimer.Restart()
def _AttemptReconnect(self, timer=None, count=0):
self._AttemptingConnect = True
connect_res = self._WrappedInterface.Connect(self._ConnectTimeout)
if connect_res not in ['Connected', 'AlreadyConnected']:
self._ConnectFailed(self, connect_res)
# Force disconnected state
self._WrappedInterface.counter = self._DisconnectLimit + 1
if self._AutoReconnect and self._ReconnectTimer.State != 'Running':
self._ReconnectTimer.Resume()
def _HasBeenConnected(self):
if len(self._ConnectHistory) >= self._ConnectHistory.maxlen:
return self._ConnectHistory.count('Connected') > 0
else:
# Return True so that we don't trigger a reconnect attempt without
# first collecting enough history.
return True
def _IfaceConnected(self, interface, state):
self._ConnectHistory.append('Connected')
self._AttemptingConnect = False
if self._ReconnectTimer.State != 'Stopped':
self._ReconnectTimer.Stop()
def _IfaceDisconnected(self, interface, state):
self._AttemptingConnect = True
if self._AutoReconnect:
if self._ReconnectTimer.State != 'Running':
self._ReconnectTimer.Resume()
def _PollTriggered(self, timer, count):
if not self._AttemptingConnect:
self._WrappedInterface.Update(self._keepAliveQuery,
self._keepAliveParams)
self._ConnectHistory.append(self._ConnectionStatus)
if not self._HasBeenConnected():
self._WrappedInterface.connectionFlag = True
self._WrappedInterface.Disconnect()
def _NewConnectionStatus(self, command, value, qualifier):
if not value == self._ConnectionStatus:
self._ConnectionStatus = value
if value == 'Connected':
self._Connected(self, value)
elif value == 'Disconnected':
self._Disconnected(self, value)
# Update the client with new connection status if subscribed.
client_callback = self._Subscriptions.get('ConnectionStatus', None)
if callable(client_callback):
client_callback('ConnectionStatus', value, None)
class ServerExHandler:
def __init__(self, Interface, clientIdleTimeout, listenRetryTime):
"""
Wraps an EthernetServerInterfaceEx instance to provide automatic
disconnection of idle clients.
.. note:: Use :py:meth:`GetConnectionHandler` to instantiate the
correct connection handler rather than instantiating
:py:class:`ServerExHandler` directly.