2 Defines abstract classes which describe fundamental communication concepts as
7 # Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
15 class IoTCommunication(object):
16 """Aggregates Rx and Tx channel for sending and receiving IoTData."""
18 def __init__(self, tx, rx):
22 class IotComm(object):
24 Implements communication concepts of starting and stopping communication.
25 Only methods _start() and _stop() should be implemented by child classes.
33 Called by start() method, should contain implementation specific
36 raise NotImplementedError()
39 """Starts communication"""
46 Called by stop() method, should contain implementation specific
47 procedures to stop communication and release resources.
49 raise NotImplementedError()
52 """Stops communication"""
58 """Returns True if the communication is started, False otherwise"""
64 Describes protocol specific blocking sync TX channel.
65 Uses protocol specific encoder to encode IoTData and protocol specific
66 decoder to decode result.
69 def __init__(self, encoder, decoder):
70 self.encoder = encoder
71 self.decoder = decoder
72 super(IoTTx, self).__init__()
74 def send(self, iotdata):
76 Uses encoder to encode iotdata to protocol specific message, sends the
77 message and decodes result to iotdata and returns the decoded result
79 raise NotImplementedError()
84 Describes protocol specific non-blocking async RX channel.
85 Uses protocol specific decoder to decode received protocol message to
86 IoTData and protocol specific encoder is used to encode result of
90 def __init__(self, decoder, encoder):
91 self.encoder = encoder
92 self.decoder = decoder
93 self.receive_cb = None
94 super(IoTRx, self).__init__()
96 def start(self, receive_cb):
98 Starts communication and stores receive_cb() method which process iotdata
99 input and returns result as iotdata object.
100 The receive_cb() is called when protocol specific message is received and
101 decoded. Result of the receive_cb() is encoded to the protocol specific
102 message and sent as reply if needed.
104 self.receive_cb = receive_cb
105 super(IoTRx, self).start()
109 Stops the communication, releases resources and clears stored
112 super(IoTRx, self).stop()
113 self.receive_cb = None