iotcs.transport module

class iotcs.transport.AccessToken(expires, tokenType, token)

Bases: object

static fromJson(data)
getExpires()
getToken()
getTokenType()
hasExpired()
toString()
class iotcs.transport.RequestBuffer(size)

Bases: object

In order to utilize the same memory used for the bytaarray of requests, the requests will be indexed as a tuple (<begin>,<end>) where <begin> is the index of a requests opening brace ‘{‘ and <end> the index of the closing brace ‘}’. Another tuple will be formed to contain an array of “(<begin>,<end>)” tuples and the byte array of request messages. This tuple will be placed in a queue.

The requests will be tracked using “currentrequest” which is a tuple of the index of the “(<begin>,<end>)” tuple and the unqueued tuple.

A running count of bytes is maintained for requests queued and processed.

availablebytes
currentjsonrequest
currentrequest
currentrequestlock
getAvailableBytes()

Buffer space available for messages :return:

getLogger()
getSize()
getUsedBytes()
jsonrequestsqueue
read()

Return the next dispatchable request as a bytearray :return: json formatted bytearray for a request message or None

requestsqueue
size
write(requests)
Parameters:requests – byte array of request messages
Returns:
class iotcs.transport.SecureConnection(trustedAssetsManager, useOnlySharedSecret=False)

Bases: iotcs.common.Closeable

Provide secure request methods to an IoT server.

Init the SecureConnection.

Parameters:
  • trustedAssetsManager – a iotcs.device.impl.UnifiedTrustedAssetsManager instance
  • useOnlySharedSecre – if True use only the shared secret for requests on this SecureConnection otherwise use signed credentials.
close()

Close the connection.

closed
delete(url, queryparams, data, headers=None, timeout=None)

Send a DELETE message.

Parameters:
  • url – the REST api resource URN
  • queryparams
  • data
  • headers
  • timeout
disconnect()

Disconnect this SecureConnection.

get(url, queryparams, headers=None, timeout=None)

Return the effective IoT server REST api URN.

Parameters:
  • url
  • queryparams
  • headers
  • timeout
getEndpointId()

Return the device client identifier using this SecureConnection.

Returns:the device client identifier
Raises:raises exceptions raised by trustetAssetsMananger.getEndpointId() if iotcs.device.impl.UnifiedTrustedAssetsManager.getClientId() returns none None.
getTrustedAssetsManager()

Return the trustedAssetsManager for this `SecureConnection.

Returns:trustedAssetsManager
isClosed()

Return True if this SecureConnection is closed, else False.

patch(url, queryparams, data, headers=None, timeout=None)

Send a PATCH message.

Parameters:
  • url – the REST api resource URN
  • queryparams
  • data – the message payload in json format
  • headers
  • timeout
post(url, queryparams, data, headers=None, timeout=None)

Send a POST message.

Parameters:
  • url – the REST api resource URN
  • queryParams
  • data – the message payload in json format
  • headers
  • timeout – socket read timeout in milliseconds. timeout of zero is interpreted as an infinite timeout.
put(url, queryparams, data, headers=None, timeout=None)

Send a PUT message.

Parameters:
  • url – the REST api resource URN
  • queryparams
  • data – the message payload in json format
  • headers
  • timeout
trustedAssetsManager
useOnlySharedSecret
usesOnlySharedSecret()

Return useOnlySharedSecret.

Returns:useOnlySharedSecret
class iotcs.transport.SendReceiveImpl

Bases: abc.ABC

SendReceiveImpl is an implementation of the DirectlyConnectedDevice.send() and DirectlyConnectedDevice.receive() methods. The send is synchronous. Receive should be considered as a synchronous call. The implementation has a buffer for receiving request messages. If there are no messages in the buffer, the receive implementation will send a message to the server to receive any pending requests the server may have.

The size of the buffer can be configured by setting the property “request_buffer_size” in the “messaging” section of iotcs.config.config.ini.

Messages received from server are handled using a circular buffer of bytes. The first two bytes at the head of the buffer always contains the number of bytes in the message that follows. The tail points to the next available chunk of buffer. When bytes are written to the buffer, the bytes are written from (tail+2) to the end of the buffer, and then from buffer[0] if necessary. In this way, the buffer wraps from tail to head. Head keeps moving as messages are read. Tail keeps moving as requests are received. Bytes in the buffer never have to be moved.

The number of bytes available are sent to the server in a query parameter to the messages REST API:

POST /iot/api/v2/messages?acceptBytes=<number-of-bytes>
DISABLE_LONG_POLLING_PROPERTY = 'disable_long_polling'
LONG_POLLING_TIMEOUT_OFFSET_PROPERTY = 'long_polling_timeout_offset'
MIN_ACCEPT_BYTES_HEADER = 'X-min-acceptBytes'
REQUEST_BUFFER_SIZE_PROPERTY = 'request_buffer_size'
SEND_RECEIVE_TIMEOUT_PROPERTY = 'send_receive_timeout'
USE_DEFAULT_TIMEOUT_VALUE = -1
getLogger()
head
post(payload, timeout=-1)

Messages are received when posting, an application when using HTTP long polling can set the read timeout (in milliseconds) when receiving, the so it is only need for the HttpSendReceiveImpl, which passes it to HttpSecureConnection, which passes it to HttpClient, which passes it to HttpsUrlConnection.setReadTimeout, in which timeout of zero is interpreted as an infinite timeout.

However, if a negative value is given, it will be converted to be the default response timeout.

Parameters:
  • payload
  • timeout
Returns:

receive(timeout)

Originates from DirectlyConnectedDevice.receive().

timeout (in milliseconds) is used when using HTTP long polling, so it is only needed for the HttpSendReceiveImpl requests, which passes it to HttpSecureConnection, which passes it to HttpClient, which passes it to <WHAT IS THE URLLIB3 API FOR SETTING TIMEOUT> A timeout of zero is interpreted as an infinite timeout.

However, if a negative value is given, it will be converted to be the default response timeout.

Get request messages from the IOT Cloud Service :param timeout: :return: RequestMessage

receiveLock
recvQueue
requestBuffer
requestBufferSize
send(messages)
Parameters:messages – A list of Message
Returns:
Raises:StateException if message cannot be buffered
sendCallTime
sendCallTimeLock
sendReceiveTimeLimit
tail
useLongPolling