helper.py

The helper module contains the InmHelper, a convenience class for INM communication.

CAUTION: This module is NOT inherently thread-safe.  In a multi-threaded application, access to thread-shared objects from this module MUST be synchronized in application code.

Summary
helper.pyThe helper module contains the InmHelper, a convenience class for INM communication.
InmHelperConvenience class for INM communication.
Variables
DEFAULT_SRCADRDefault INM source address.
DEFAULT_IP_ADRDefault IP address of internal InetMessageChannel.
DEFAULT_UDP_PORTDefault UDP port of internal InetMessageChannel.
DEFAULT_LINK_UDP_PORTDefault target UDP port of internal InetMessageChannel.
TypShorthand alias of StandardTypes.
ResShorthand alias of StandardResults.
RegShorthand alias of StandardRegisters.
RCShorthand alias of ResultCode.
VCShorthand alias of ValueConversions.
Functions and Properties
__init__Instance initializer.
channelThe encapsulated MessageChannel of this InmHelper.
msg_factoryThe encapsulated MessageFactory of this InmHelper.
link_adrThe INM link address this InmHelper uses to send messages.
openOpens the encapsulated channel.
closeCloses the encapsulated channel.
header_to_bytesConstructs an INM message header and returns it as a bytes object.
msg_to_bytesConstructs an INM message and returns it as a bytes object.
msg_to_bytes_mvalConstructs an INM message with a multipart message value and returns it as a bytes object.
header_to_hexConstructs an INM message header and returns it as a list of hex strings.
msg_to_hexConstructs an INM message and returns it as a list of hex strings.
msg_to_hex_mvalConstructs an INM message with a multipart message value and returns it as list of hex strings.
sendConstructs and sends an INM message.
send_mvalConstructs and sends an INM message with a multipart message value.
recvAttempts to receive an INM message.
sendrecvFirst constructs and sends an INM message, then attempts to receive one.
sendrecv_mvalFirst constructs and sends an INM message with a multipart value, then attempts to receive a message.

InmHelper

class InmHelper

Convenience class for INM communication.  Encapsulates a MessageChannel and provides a simplified interface for sending and receiving messages via that channel.

An InmHelper can be used as a context manager, opening the encapsulated MessageChannel when the context is entered and then closing the channel upon exit from the context.  If the attempt to open the message channel fails, a MessageChannelError is thrown by the __enter__ method.

Code Example

import inm.helper as helper

dstadr = 1
debug0_val = 0x02

h = helper.InmHelper()  # Create InmHelper with default message channel.

res = h.open()  # Open the message channel.
if res != h.RC.SUCCESS:  # Use convenient shorthand reference to enum type.
  print(f'Failed to open message channel: {res.name}')

# Ask INM node 1 about its firmware version.
res, header, msg, link_adr = h.sendrecv(dstadr, h.Typ.REG_READ, h.Reg.FWVERSION)
if res != h.RC.SUCCESS:
  print(f'Failed to read from FWVERSION: {res.name}')

r_index, fw_ver, req_id = msg.format_mval(conv=h.VC.Int)  # Unpack INM_REG_READ_RES payload.
print(f'Node {dstadr} has firmware version {fw_ver}.')

# Write to debug register 0 at node 1.
res, header, msg, link_adr = h.sendrecv_mval(dstadr, h.Typ.REG_WRITE, (h.Reg.DEBUG0, debug0_val))
if res != h.RC.SUCCESS:
  print(f'Failed to write to DEBUG0: {res.name}')

std_res, req_id = msg.format_mval(conv=h.VC.Int)  # Unpack INM_RESULT payload.
if std_res == h.Res.OK:  # successful register write at destination
  print(f'Wrote {debug0_val:#04x} to DEBUG0.')

h.close()  # Close the message channel.
Summary
Variables
DEFAULT_SRCADRDefault INM source address.
DEFAULT_IP_ADRDefault IP address of internal InetMessageChannel.
DEFAULT_UDP_PORTDefault UDP port of internal InetMessageChannel.
DEFAULT_LINK_UDP_PORTDefault target UDP port of internal InetMessageChannel.
TypShorthand alias of StandardTypes.
ResShorthand alias of StandardResults.
RegShorthand alias of StandardRegisters.
RCShorthand alias of ResultCode.
VCShorthand alias of ValueConversions.
Functions and Properties
__init__Instance initializer.
channelThe encapsulated MessageChannel of this InmHelper.
msg_factoryThe encapsulated MessageFactory of this InmHelper.
link_adrThe INM link address this InmHelper uses to send messages.
openOpens the encapsulated channel.
closeCloses the encapsulated channel.
header_to_bytesConstructs an INM message header and returns it as a bytes object.
msg_to_bytesConstructs an INM message and returns it as a bytes object.
msg_to_bytes_mvalConstructs an INM message with a multipart message value and returns it as a bytes object.
header_to_hexConstructs an INM message header and returns it as a list of hex strings.
msg_to_hexConstructs an INM message and returns it as a list of hex strings.
msg_to_hex_mvalConstructs an INM message with a multipart message value and returns it as list of hex strings.
sendConstructs and sends an INM message.
send_mvalConstructs and sends an INM message with a multipart message value.
recvAttempts to receive an INM message.
sendrecvFirst constructs and sends an INM message, then attempts to receive one.
sendrecv_mvalFirst constructs and sends an INM message with a multipart value, then attempts to receive a message.

Variables

DEFAULT_SRCADR

DEFAULT_SRCADR = 99

Default INM source address.

DEFAULT_IP_ADR

DEFAULT_IP_ADR = '127.0.0.1'

Default IP address of internal InetMessageChannel.

DEFAULT_UDP_PORT

DEFAULT_UDP_PORT = 2999

Default UDP port of internal InetMessageChannel.

DEFAULT_LINK_UDP_PORT

DEFAULT_LINK_UDP_PORT = 3000

Default target UDP port of internal InetMessageChannel.

Typ

Typ = inm.StandardTypes

Shorthand alias of StandardTypes.

Res

Res = inm.StandardResults

Shorthand alias of StandardResults.

Reg

Reg = inm.StandardRegisters

Shorthand alias of StandardRegisters.

RC

RC = inm.ResultCode

Shorthand alias of ResultCode.

VC

VC = inm.ValueConversions

Shorthand alias of ValueConversions.

Functions and Properties

__init__

def __init__(self,  
channel = None,
msg_factory = None,
srcadr = None,
ip_adr = None,
udp_port = None,
tcp_port = None,
timeout = None,
link_adr = None)

Instance initializer.  Offers convenient default behaviors.  For example, creating an InmHelper with zero arguments should result in an internal InetMessageChannel with a default debugging configuration (i.e. use UDP port 2999 to communicate with port 3000 on the loopback interface, as INM node 99).

Parameters

channelThe MessageChannel that the InmHelper should use to send and receive INM messages.  If this is None, the other parameters will be used to initialize an internally created InetMessageChannel.
msg_factoryThe MessageFactory that the InmHelper should use to create INM Message objects.  If this parameter is None, the msg_factory attribute of the encapsulated MessageChannel will be used.
srcadrINM source address of the encapsulated MessageChannel.  Used only if the channel parameter is None.  If this parameter is None, the value of the class attribute DEFAULT_SRCADR will be used.
ip_adrIP address of the internal InetMessageChannel that is created if the channel parameter is None.  Specify the address as a hostname or dotted-decimal string.  If this parameter is None, the value of the class attribute DEFAULT_IP_ADR will be used.
udp_portUDP port of the internal InetMessageChannel that is created if the channel parameter is None.  If this parameter is None, the value of the class attribute DEFAULT_UDP_PORT will be used.
tcp_portTCP port of the internal InetMessageChannel that is created if the channel parameter is None.  If this parameter is None, the port number used for the UDP port is also used for the TCP port.  NOTE: In the current implementation of InetMessageChannel, no TCP socket is actually used, so this parameter makes no difference.
timeoutReceive timeout of the internal InetMessageChannel that is created if the channel parameter is None.  MUST be an instance of the standard library class datetime.timedelta.  If this parameter is None, receive operations will never time out.
link_adrINM link address (think of it as the router address of an IP network) of the internal InetMessageChannel that is created if the channel parameter is None.  Should be a tuple of the format (link_ip_adr, link_udp_port).  If this parameter is None, the used link_ip_adr will be the same as the IP address of the encapsulated channel, while link_udp_port will have the value of the class attribute DEFAULT_LINK_UDP_PORT.

channel

self.channel

The encapsulated MessageChannel of this InmHelper.

msg_factory

self.msg_factory

The encapsulated MessageFactory of this InmHelper.

link_adr

self.link_adr

The INM link address this InmHelper uses to send messages.

open

def open(self)

Opens the encapsulated channel.  Calls MessageChannel.open.

Returns

ResultCode.SUCCESS if the channel was successfully opened, otherwise another ResultCode indicating what went wrong.

close

def close(self)

Closes the encapsulated channel.  Calls MessageChannel.close.

header_to_bytes

def header_to_bytes(self,  
dstadr,  
msg_id = None,
srcadr = None)

Constructs an INM message header and returns it as a bytes object.  Useful for finding out the exact on-wire representation of a header.

Parameters

dstadrDestination INM node address in the header to construct.
msg_idINM message ID.  If this is None, the next message ID to be generated by the channel will be used.
srcadrSource INM node address in the header to construct.  If this is None, the configured source address of the channel will be used.

Returns

A bytes object containing the standard binary on-wire representation of the specified INM message header.

msg_to_bytes

def msg_to_bytes(self,  
dstadr,  
typ,  
val,  
int_size = None,
msg_id = None,
srcadr = None,
with_header = True)

Constructs an INM message and returns it as a bytes object.  Useful for finding out the exact on-wire representation of a message.

Parameters

dstadrDestination INM node address of the message to construct.
typINM message type.
valINM message value.
int_sizeField size in bytes of an integer message value.  If this is None, the default int_size of the msg_factory will be used.
msg_idINM message ID.  If this is None, the next message ID to be generated by the channel will be used.
srcadrSource INM node address of the message to construct.  If this is None, the configured source address of the channel will be used.
with_headerBoolean flag specifying whether an INM header should be prepended to the constructed message.

Returns

A bytes object containing the standard binary on-wire representation of the specified INM message.  (Or None, if the message couldn’t be constructed.)

msg_to_bytes_mval

def msg_to_bytes_mval(self,  
dstadr,  
typ,  
mval,  
int_size = None,
msg_id = None,
srcadr = None,
with_header = True)

Constructs an INM message with a multipart message value and returns it as a bytes object.  Useful for finding out the exact on-wire representation of a message.

Parameters

dstadrDestination INM node address of the message to construct.
typINM message type.
mvalMultipart INM message value.  MUST be a sequence.
int_sizeField size in bytes of an integer message value.  This may be a list or tuple containing a separate integer size for each element of the val argument.  If this is None, the default int_size of the msg_factory will be used.
msg_idINM message ID.  If this is None, the next message ID to be generated by the channel will be used.
srcadrSource INM node address of the message to construct.  If this is None, the configured source address of the channel will be used.
with_headerBoolean flag specifying whether an INM header should be prepended to the constructed message.

Returns

A bytes object containing the standard binary on-wire representation of the specified INM message.  (Or None, if the message couldn’t be constructed.)

header_to_hex

def header_to_hex(self,  
dstadr,  
msg_id = None,
srcadr = None)

Constructs an INM message header and returns it as a list of hex strings.  Useful for finding out the exact on-wire representation of a header.

Parameters

dstadrDestination INM node address in the header to construct.
msg_idINM message ID.  If this is None, the next message ID to be generated by the channel will be used.
srcadrSource INM node address in the header to construct.  If this is None, the configured source address of the channel will be used.

Returns

A list of two-character hexadecimal strings, corresponding to bytes in the standard binary on-wire representation of the specified INM message header.

msg_to_hex

def msg_to_hex(self,  
dstadr,  
typ,  
val,  
int_size = None,
msg_id = None,
srcadr = None,
with_header = True)

Constructs an INM message and returns it as a list of hex strings.  Useful for finding out the exact on-wire representation of a message.

Parameters

dstadrDestination INM node address of the message to construct.
typINM message type.
valINM message value.
int_sizeField size in bytes of an integer message value.  If this is None, the default int_size of the msg_factory will be used.
msg_idINM message ID.  If this is None, the next message ID to be generated by the channel will be used.
srcadrSource INM node address of the message to construct.  If this is None, the configured source address of the channel will be used.
with_headerBoolean flag specifying whether an INM header should be prepended to the constructed message.

Returns

A list of two-character hexadecimal strings, corresponding to bytes in the standard binary on-wire representation of the specified INM message.  (Or None, if the message couldn’t be constructed.)

msg_to_hex_mval

def msg_to_hex_mval(self,  
dstadr,  
typ,  
mval,  
int_size = None,
msg_id = None,
srcadr = None,
with_header = True)

Constructs an INM message with a multipart message value and returns it as list of hex strings.  Useful for finding out the exact on-wire representation of a message.

Parameters

dstadrDestination INM node address of the message to construct.
typINM message type.
mvalMultipart INM message value.  MUST be a sequence.
int_sizeField size in bytes of an integer message value.  This may be a list or tuple containing a separate integer size for each element of the val argument.  If this is None, the default int_size of the msg_factory will be used.
msg_idINM message ID.  If this is None, the next message ID to be generated by the channel will be used.
srcadrSource INM node address of the message to construct.  If this is None, the configured source address of the channel will be used.
with_headerBoolean flag specifying whether an INM header should be prepended to the constructed message.

Returns

A list of two-character hexadecimal strings, corresponding to bytes in the standard binary on-wire representation of the specified INM message.  (Or None, if the message couldn’t be constructed.)

send

def send(self,  
dstadr,  
typ,  
val,  
int_size = None,
msg_id = None,
srcadr = None,
link_adr = None)

Constructs and sends an INM message.

Parameters

dstadrDestination INM node address of the message to send.
typINM message type.
valINM message value.
int_sizeField size in bytes of an integer message value.  If this is None, the default int_size of the msg_factory will be used.
msg_idINM message ID.  If this is None, a message ID will be generated by the channel.
srcadrSource INM node address of the message to send.  If this is None, the configured source address of the channel will be used.
link_adrINM link address to use when sending the message.  If this is None, the value of the link_adr attribute will be used.

Returns

A ResultCode indicating the outcome of the attempted send operation.

send_mval

def send_mval(self,  
dstadr,  
typ,  
mval,  
int_size = None,
msg_id = None,
srcadr = None,
link_adr = None)

Constructs and sends an INM message with a multipart message value.

Parameters

dstadrDestination INM node address of the message to send.
typINM message type.
mvalMultipart INM message value.  MUST be a sequence.
int_sizeField size in bytes of an integer message value.  This may be a list or tuple containing a separate integer size for each element of the val argument.  If this is None, the default int_size of the msg_factory will be used.
msg_idINM message ID.  If this is None, a message ID will be generated by the channel.
srcadrSource INM node address of the message to send.  If this is None, the configured source address of the channel will be used.
link_adrINM link address to use when sending the message.  If this is None, the value of the link_adr attribute will be used.

Returns

A ResultCode indicating the outcome of the attempted send operation.

recv

def recv(self)

Attempts to receive an INM message.

Returns

resA ResultCode indicating the outcome of the attempted receive operation.
headerThe MessageHeader of the received message, or None in case of failure.
msgThe received Message, or None in case of failure.
link_adrThe link address of the received message, or None in case of failure.

sendrecv

def sendrecv(self,  
dstadr,  
typ,  
val,  
int_size = None,
msg_id = None,
srcadr = None,
link_adr = None)

First constructs and sends an INM message, then attempts to receive one.

Parameters

dstadrDestination INM node address of the message to send.
typINM message type.
valINM message value.
int_sizeField size in bytes of an integer message value.  If this is None, the default int_size of the msg_factory will be used.
msg_idINM message ID.  If this is None, a message ID will be generated by the channel.
srcadrSource INM node address of the message to send.  If this is None, the configured source address of the channel will be used.
link_adrINM link address to use when sending the message.  If this is None, the value of the link_adr attribute will be used.

Returns

resA ResultCode indicating the outcome of the attempted send/receive operation.
headerThe MessageHeader of the received message, or None in case of failure.
msgThe received Message, or None in case of failure.
link_adrThe link address of the received message, or None in case of failure.

sendrecv_mval

def sendrecv_mval(self,  
dstadr,  
typ,  
mval,  
int_size = None,
msg_id = None,
srcadr = None,
link_adr = None)

First constructs and sends an INM message with a multipart value, then attempts to receive a message.

Parameters

dstadrDestination INM node address of the message to send.
typINM message type.
mvalMultipart INM message value.  MUST be a sequence.
int_sizeField size in bytes of an integer message value.  This may be a list or tuple containing a separate integer size for each element of the val argument.  If this is None, the default int_size of the msg_factory will be used.
msg_idINM message ID.  If this is None, a message ID will be generated by the channel.
srcadrSource INM node address of the message to send.  If this is None, the configured source address of the channel will be used.
link_adrINM link address to use when sending the message.  If this is None, the value of the link_adr attribute will be used.

Returns

resA ResultCode indicating the outcome of the attempted send/receive operation.
headerThe MessageHeader of the received message, or None in case of failure.
msgThe received Message, or None in case of failure.
link_adrThe link address of the received message, or None in case of failure.
class InmHelper
Convenience class for INM communication.
DEFAULT_SRCADR = 99
Default INM source address.
DEFAULT_IP_ADR = '127.0.0.1'
Default IP address of internal InetMessageChannel.
class InetMessageChannel(BinaryMessageChannel)
A BinaryMessageChannel that sends and receives messages via a UDP socket.
DEFAULT_UDP_PORT = 2999
Default UDP port of internal InetMessageChannel.
DEFAULT_LINK_UDP_PORT = 3000
Default target UDP port of internal InetMessageChannel.
Typ = inm.StandardTypes
Shorthand alias of StandardTypes.
Standard INM message types.
Res = inm.StandardResults
Shorthand alias of StandardResults.
Standard INM result codes.
Reg = inm.StandardRegisters
Shorthand alias of StandardRegisters.
Standard INM register identifiers.
RC = inm.ResultCode
Shorthand alias of ResultCode.
Result codes for the INM module.
VC = inm.ValueConversions
Shorthand alias of ValueConversions.
Type conversion specifiers for INM message values.
def __init__(self,  
channel = None,
msg_factory = None,
srcadr = None,
ip_adr = None,
udp_port = None,
tcp_port = None,
timeout = None,
link_adr = None)
Instance initializer.
self.channel
The encapsulated MessageChannel of this InmHelper.
class MessageChannel
Abstract parent class for objects that send and receive INM messages.
self.msg_factory
The encapsulated MessageFactory of this InmHelper.
class MessageFactory
A configurable factory class for INM messages.
self.link_adr
The INM link address this InmHelper uses to send messages.
def open(self)
Opens the encapsulated channel.
def close(self)
Closes the encapsulated channel.
def header_to_bytes(self,  
dstadr,  
msg_id = None,
srcadr = None)
Constructs an INM message header and returns it as a bytes object.
def msg_to_bytes(self,  
dstadr,  
typ,  
val,  
int_size = None,
msg_id = None,
srcadr = None,
with_header = True)
Constructs an INM message and returns it as a bytes object.
def msg_to_bytes_mval(self,  
dstadr,  
typ,  
mval,  
int_size = None,
msg_id = None,
srcadr = None,
with_header = True)
Constructs an INM message with a multipart message value and returns it as a bytes object.
def header_to_hex(self,  
dstadr,  
msg_id = None,
srcadr = None)
Constructs an INM message header and returns it as a list of hex strings.
def msg_to_hex(self,  
dstadr,  
typ,  
val,  
int_size = None,
msg_id = None,
srcadr = None,
with_header = True)
Constructs an INM message and returns it as a list of hex strings.
def msg_to_hex_mval(self,  
dstadr,  
typ,  
mval,  
int_size = None,
msg_id = None,
srcadr = None,
with_header = True)
Constructs an INM message with a multipart message value and returns it as list of hex strings.
def send(self,  
dstadr,  
typ,  
val,  
int_size = None,
msg_id = None,
srcadr = None,
link_adr = None)
Constructs and sends an INM message.
def send_mval(self,  
dstadr,  
typ,  
mval,  
int_size = None,
msg_id = None,
srcadr = None,
link_adr = None)
Constructs and sends an INM message with a multipart message value.
def recv(self)
Attempts to receive an INM message.
def sendrecv(self,  
dstadr,  
typ,  
val,  
int_size = None,
msg_id = None,
srcadr = None,
link_adr = None)
First constructs and sends an INM message, then attempts to receive one.
def sendrecv_mval(self,  
dstadr,  
typ,  
mval,  
int_size = None,
msg_id = None,
srcadr = None,
link_adr = None)
First constructs and sends an INM message with a multipart value, then attempts to receive a message.
class MessageChannelError(Exception)
Exception class for MessageChannels.
class Message
Instances of subclasses of this abstract class represent INM messages.
def open(self)
Opens the MessageChannel.
Operation successfully completed.
def close(self)
Closes the MessageChannel.
class MessageHeader
Represents the header of an INM message.
Close