Implementation of HTTP IoTDM client python lib
[integration/test.git] / csit / libraries / IoTDM / client_libs / iot_communication_concepts.py
1 """
2  Defines abstract classes which describe fundamental communication concepts as
3  abstract classes.
4 """
5
6 #
7 # Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
8 #
9 # This program and the accompanying materials are made available under the
10 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
11 # and is available at http://www.eclipse.org/legal/epl-v10.html
12 #
13
14
15 class IoTCommunication(object):
16     """Aggregates Rx and Tx channel for sending and receiving IoTData."""
17     def __init__(self, tx, rx):
18         pass
19
20
21 class IotComm(object):
22     """
23     Implements communication concepts of starting and stopping communication.
24     Only methods _start() and _stop() should be implemented by child classes.
25     """
26     def __init__(self):
27         self._started = False
28
29     def _start(self):
30         """
31         Called by start() method, should contain implementation specific
32         procedures
33         """
34         raise NotImplementedError()
35
36     def start(self):
37         """Starts communication"""
38         if not self._started:
39             self._start()
40             self._started = True
41
42     def _stop(self):
43         """
44         Called by stop() method, should contain implementation specific
45         procedures to stop communication and release resources.
46         """
47         raise NotImplementedError()
48
49     def stop(self):
50         """Stops communication"""
51         if self._started:
52             self._stop()
53             self._started = False
54
55     def is_started(self):
56         """Returns True if the communication is started, False otherwise"""
57         return self._started
58
59
60 class IoTTx(IotComm):
61     """
62     Describes protocol specific blocking sync TX channel.
63     Uses protocol specific encoder to encode IoTData and protocol specific
64     decoder to decode result.
65     """
66     def __init__(self, encoder, decoder):
67         self.encoder = encoder
68         self.decoder = decoder
69         super(IoTTx, self).__init__()
70
71     def send(self, iotdata):
72         """
73         Uses encoder to encode iotdata to protocol specific message, sends the
74         message and decodes result to iotdata and returns the decoded result
75         """
76         raise NotImplementedError()
77
78
79 class IoTRx(IotComm):
80     """
81     Describes protocol specific non-blocking async RX channel.
82     Uses protocol specific decoder to decode received protocol message to
83     IoTData and protocol specific encoder is used to encode result of
84     handling.
85     """
86     def __init__(self, decoder, encoder):
87         self.encoder = encoder
88         self.decoder = decoder
89         self.receive_cb = None
90         super(IoTRx, self).__init__()
91
92     def start(self, receive_cb):
93         """
94         Starts communication and stores receive_cb() method which process iotdata
95         input and returns result as iotdata object.
96         The receive_cb() is called when protocol specific message is received and
97         decoded. Result of the receive_cb() is encoded to the protocol specific
98         message and sent as reply if needed.
99         """
100         self.receive_cb = receive_cb
101         super(IoTRx, self).start()
102
103     def stop(self):
104         """
105         Stops the communication, releases resources and clears stored
106         receive_cb() method
107         """
108         super(IoTRx, self).stop()
109         self.receive_cb = None