MasterScan
Lab_Matlab_control Master Branch
|
Singleton to handle socket communications.
dotsTheMessenger uses an instance of dotsAllSocketObjects to do communications between Matlab instances (or looped-back communictaions to the same Matlab instance). Both Matlab instances are expected to be using dotsTheMessenger.
dotsTheMessenger improves on basic socket messaging in three ways:
Properties and Events | |
Property | socketsInfo |
topsGroupedList of info about opened sockets | |
Property | clockFunction |
any function that returns the current time as a number | |
Property | receiveTimeout |
time in seconds to wait to receive a message More... | |
Property | ackTimeout |
time in seconds to wait for acknowledgement of a sent message More... | |
Property | sendRetries |
number of times to resend unacknowledged messages More... | |
Property | socketClassName |
class name of a dotsAllSocketObjects subclass More... | |
Property | defaultClientIP |
string IP address that could be used for a client-side socket More... | |
Property | defaultServerIP |
string IP address that could be used for a server-side socket More... | |
Property | defaultClientPort |
port number that could be used for a client-side socket More... | |
Property | defaultServerPort |
port number that could be used for a server-side socket More... | |
Property | notAcknowledgedStatus = -111 |
status code indicating a send message was never acknowqledged | |
Property | notReceivedStatus = -222 |
status code indicating no message was received | |
![]() | |
null | obj = theObject(varargin) |
Return the current class instance from the private constructor. | |
null | g = gui() |
Launch a grapical interface for the current instance. | |
Protected Properties | |
Property | socketObject |
an instance of socketClassName More... | |
Property | sentPrefix |
1-byte prefix for the previous sent message | |
Property | receivedPrefix |
1-byte prefixs for the previous received message at each socket | |
Property | prefixModulus = 255 |
roll-over value for 1-byte message prefixes More... | |
Methods | |
initialize (self) | |
Restore the current instance to a fresh state. | |
status ackTime | sendMessageFromSocket (self, msg, sock, ackTimeout, sendRetries) |
Send a message from the given socket. More... | |
msg status | receiveMessageAtSocket (self, sock, receiveTimeout) |
Receive any message that arrived at the given socket. More... | |
sock | openSocket (self, localIP, localPort, remoteIP, remotePort) |
Open a socket for communicating via Ethernet and UDP. More... | |
status | closeSocket (self, sock) |
Close a socket. More... | |
status | flushSocket (self, sock) |
Clear any data messages and inernal accounting for a socket. More... | |
sock | openDefaultServerSocket (self) |
Open a socket with some default "server" properties. | |
sock | openDefaultClientSocket (self) |
Open a socket with some default "client" properties. | |
![]() | |
null | reset (varargin) |
Restore a fresh state without deleting the current instance. | |
null | initialize (self) |
Restore a fresh state without deleting the current object, used by constructor and reset(). More... | |
initializeLists (self, listNames) | |
Create or refresh topsGroupedList instances. | |
set (self, varargin) | |
Set multiple properties. | |
Protected Methods | |
setSocketReceivedPrefix (self, sock, prefix) | |
Keep track of the last message prefix received at a socket. | |
prefix | getSocketReceivedPrefix (self, sock) |
Get the last message prefix received at a socket. | |
Static Methods | |
static obj | theObject (varargin) |
Access the current instance. | |
static | reset (varargin) |
Restore the current instance to a fresh state. | |
static g | gui () |
Launch a graphical interface for messenger properties. | |
static description | describeAddress (IP, port) |
Describe an IP address and socket with one string. More... | |
Private Methods | |
self | dotsTheMessenger (varargin) |
Constructor is private. More... | |
|
private |
Constructor is private.
dotsTheMessenger is a singleton object, so its constructor is not accessible. Use dotsTheMessenger.theObject() to access the current instance.
|
static |
Describe an IP address and socket with one string.
IP | string IP address like '127.0.0.1' |
numeric | port number |
Concatnates IP and port into a single descriptive string, with a colon delimiter between IP and port.
status ackTime sendMessageFromSocket | ( | self | , |
msg | , | ||
sock | , | ||
ackTimeout | , | ||
sendRetries | |||
) |
Send a message from the given socket.
msg | variable to send as a message |
sock | numeric identifier for a socket, as returned by openSocket() |
ackTimeout | seconds to wait for message acknowledgement |
sendRetries | number of times to resend if unacknowledged |
Converts msg to an array of bytes and sends the bytes from sock, to whatever address and port were specified in the call to openSocket(). msg must be one of the following variable types:
If msg is a cell or struct, it must compose elements of these types. Nested cells and structs are supported. Any msg or element that is an array, including cell arrays, should be at most two-dimensional (mxn). struct arrays should be one-dimenstional (1xn) but may have any number of fields.
If ackTimeout is non-negative, waits for the receiver to send back an acknowledgement of msg. If no acknowledgement arrives within ackTimeout seconds, re-sends msg. Waits and re-sends up to sendRetries times. If ackTimeout is negative, does not wait for any ancknowledgement. ackTimeout and sendRetries may be omitted, in which case the ackTimeout and sendRetries properties are used.
Returns a status code. May return notAcknowledgedStatus if the message was sent but never acknowledged. Other negative status indicates an error and that msg may not have been sent.
Also returns as a second output the amount of time spent waiting for message acknowledgement, whether or not it was ever acknowledged, in units of clockFunction.
msg status receiveMessageAtSocket | ( | self | , |
sock | , | ||
receiveTimeout | |||
) |
Receive any message that arrived at the given socket.
sock | a numeric identifier for a socket, as returned by openSocket() |
receiveTimeout | seconds to wait for a message to arrive |
Waits up to receiveTimeout seconds for a message to arrive at the given sock. If a message arrives (or had already arrived), returns the message variable. Otherwise, returns []. receiveTimeout may be omitted, in which case the receiveTimeout property is used.
Also returns a status code as a second output. May return notReceivedStatus if no message was available. May return other negative status codes if there was an error reading from sock or decoding message bytes into a variable.
sock openSocket | ( | self | , |
localIP | , | ||
localPort | , | ||
remoteIP | , | ||
remotePort | |||
) |
Open a socket for communicating via Ethernet and UDP.
localIP | string IP address for this machine |
localPort | integer port number to go with the local IP address |
remoteIP | string IP address for this or another machine |
remotePort | integer port number to go with the remote IP address |
Opens a network socket connection between ports at two IP addresses. The IP addresses may be the same or different. localIP should be the address of this machine, and may be the "loopback" address, '127.0.0.1'. The port numbers must be different.
Returns a numeric identifier for the new UDP connection, suitable for use with sendMessageFromSocket(), receiveMessageAtSocket(), and waitForMessageAtSocket();
Stores information about each opened socket in socketsInfo
status closeSocket | ( | self | , |
sock | |||
) |
Close a socket.
sock | a numeric identifier for a socket, as returned by openSocket() |
Frees the resources associeted with sock.
Returns a status code. A negative status code probably indicates that sock is not a valid socket identifier.
Searches socketsInfo for sock and removes all instances it finds.
status flushSocket | ( | self | , |
sock | |||
) |
Clear any data messages and inernal accounting for a socket.
sock | a numeric identifier for a socket, as returned by openSocket() |
Reads from sock until it has no queued messages and resets dotsTheMessenger's internal accounting for sock, but does not close the sock.
Returns a status code. A negative status code probably indicates that sock is not a valid socket identifier.
Property receiveTimeout |
time in seconds to wait to receive a message
Automatically gets the machine-specific default from dotsTheMachineConfiguration.
Property ackTimeout |
time in seconds to wait for acknowledgement of a sent message
Automatically gets the machine-specific default from dotsTheMachineConfiguration.
Property sendRetries |
number of times to resend unacknowledged messages
Automatically gets the machine-specific default from dotsTheMachineConfiguration.
Property socketClassName |
class name of a dotsAllSocketObjects subclass
Instantiates an instance of socketClassName to use for opening and closing sockets, and sending and receiving messages. The named dotsAllSocketObjects subclass should be appropriate for the local hardware, operating system, etc.
Automatically gets the machine-specific default from dotsTheMachineConfiguration.
Property defaultClientIP |
string IP address that could be used for a client-side socket
Automatically gets the machine-specific default from dotsTheMachineConfiguration.
Property defaultServerIP |
string IP address that could be used for a server-side socket
Automatically gets the machine-specific default from dotsTheMachineConfiguration.
Property defaultClientPort |
port number that could be used for a client-side socket
Automatically gets the machine-specific default from dotsTheMachineConfiguration.
Property defaultServerPort |
port number that could be used for a server-side socket
Automatically gets the machine-specific default from dotsTheMachineConfiguration.
|
protected |
an instance of socketClassName
The instance of socketClassName that dotsTheMessenger is using for opening and closing sockets, and sending and receiving messages.
|
protected |
roll-over value for 1-byte message prefixes
prefixModulus wants to be 256, but because of Matlab clips integers rather than rolling them over, 255 is easier to work with. Consider that 255+1 equals 255, not 0.