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."""
17 def __init__(self, tx, rx):
21 class IotComm(object):
23 Implements communication concepts of starting and stopping communication.
24 Only methods _start() and _stop() should be implemented by child classes.
31 Called by start() method, should contain implementation specific
34 raise NotImplementedError()
37 """Starts communication"""
44 Called by stop() method, should contain implementation specific
45 procedures to stop communication and release resources.
47 raise NotImplementedError()
50 """Stops communication"""
56 """Returns True if the communication is started, False otherwise"""
62 Describes protocol specific blocking sync TX channel.
63 Uses protocol specific encoder to encode IoTData and protocol specific
64 decoder to decode result.
66 def __init__(self, encoder, decoder):
67 self.encoder = encoder
68 self.decoder = decoder
69 super(IoTTx, self).__init__()
71 def send(self, iotdata):
73 Uses encoder to encode iotdata to protocol specific message, sends the
74 message and decodes result to iotdata and returns the decoded result
76 raise NotImplementedError()
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
86 def __init__(self, decoder, encoder):
87 self.encoder = encoder
88 self.decoder = decoder
89 self.receive_cb = None
90 super(IoTRx, self).__init__()
92 def start(self, receive_cb):
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.
100 self.receive_cb = receive_cb
101 super(IoTRx, self).start()
105 Stops the communication, releases resources and clears stored
108 super(IoTRx, self).stop()
109 self.receive_cb = None