|COGENT Version 2.4 Help|
The Socket/Server box (only available in the registered version of COGENT) is the server half of a TCP/IP interface for COGENT. (See Socket/Client for the client half of the TCP/IP interface.) The interface allows multiple models to be run simultaneously (but asynchronously) on the same or different machines, communicating with each other via Socket boxes, and it allows COGENT models to interact with networked resources (such as web servers and Telnet clients).
A Socket/Server box listens on a port (whose number is specified by a property of the box) for client connection requests. It starts listening when the box is initialised, and checks for new connections every cycle; it accepts all incoming connection requests, setting up an open connection for each. This continues until the box is stopped or reinitialised, when it is shutdown and all current connections are closed. Reinitialisation, of course, implies that the box starts listening again immediately afterwards.
Other than special triggers, all messages sent to a Socket/Server box along a send arrow will be transmitted along all open connections from that box. Similarly, any messages incoming through open connections will be transmitted along all send arrows leading from the Socket/Server box.
A model containing one or more Socket/Server boxes will continue running if any connections from or to a box in the model are still open, or if a server box is listening for connections.
Sending a Socket/Server box a close message will close all open connections from or to that box, but it will not shut down the server, if one is running. Sending the box a stop message will close all open connections to or from the box, like the close message, and moreover, it will shut down server in that box. This allows the usual default conditions to govern whether the whole model halts naturally or continues running.
It should be noted that unlike COGENT models in general, model runs using Socket boxes (servers or client) cannot be halted and restarted in a later OOS session. This is because ending the OOS session will close any open connections, so clients will not still be connected when the model is restarted. Initialisation is usually required.
Socket boxes support two data formats to transmit and receive data: raw mode sends and receives lists of ASCII values, corresponding to lines of textual output and input, whereas the default mode sends and receives prolog terms. Raw mode is useful to allow COGENT models to interact with arbitrary networked systems, such as web servers, or with Telnet clients for user interaction. The default mode, by contrast, simply sends and receives prolog terms. This is more appropriate for direct communication between COGENT models.
Raw mode: If the box is configured to use raw mode, messages corresponding to lines of output must be specified as raw(LineString) where LineString is a prolog string, or a list of ASCII values, for example raw("hello") or equivalently raw([104, 101, 108, 108, 111]). These strings are each formatted on the output stream with a following newline (ASCII 10).
Similarly, lines of input from connections, terminated with newlines, are converted to raw(LineString) format before being passed along send arrows to other boxes. The newline character is not included in the parsed string, and any carriage returns (ASCII 13) in the input are ignored.
Default mode: In the default mode, any prolog term sent to a Socket box (except special triggers) will be sent on along any open connections to the peers, which must also be in default mode to ensure success.
So far, we have assumed that all outgoing messages will be sent to all open connections, and incoming messages could come from any connection, without identification. But a single Socket/Server may be connected to any number of clients, and similarly a single client may be connected to any number of Socket/Server boxes. Obviously there may be cases when we wish to send different messages to different connections, and when we want to identify the source of an incoming message. Both require a method for identifying individual open connections uniquely.
The problem of unique reference can be addressed by creating a system of identifiers. We have chosen to implement this as the state of the box, by analogy with the state of a buffer. In the present case, the state of the box comprises a list of unique identifiers for each currently open connection. It is maintained by the system, so it is not possible to directly add or delete elements to or from the state. Each element has the form socket(Host, UniqueID) where Host gives the hostname and port number, or a numeric IP address, and UniqueID is an integer generated by the system. It is possible to use the standard match procedure to read elements from the state.
Sending messages to individual connections can be achieved by simply attaching this identifier to the message to be sent. For example, if Socket=socket('localhost':15000, 8), then we can send a raw-mode message to this connection alone by using the form raw(Socket, LineString). For default mode, we have introduced a special term message/2, which is used in an analogous way: message(Socket, PrologTerm).
Attaching Socket identifiers to incoming messages happens analogously. It is necessary to specify that the box should tag incoming messages using a special property Tag Messages. If this is checked, all incoming messages will be in the form raw(Socket, LineString) or message(Socket, PrologTerm) instead of the default forms.
A final refinement of this tagging mechanism allows the setting of aliases for socket identifiers. If a connection has an alias, it can be specified instead of a socket/2 term on outgoing messages, and it will be used instead of a socket/2 term on tagged incoming messages, and when matching against the contents of the state. Aliases can be set in a variety of ways: first, an alternative form of the open message has a second argument that specifies the alias to be used for the connection, for example open(localhost:15000, myalias). Secondly, for an already-existing connection, it can be given an alias using a special message alias(Socket, Alias). Finally, a remote peer can specify its own alias, using a special message call_me(Alias). Setting an alias for a connection overrides any previous aliases for that connection.
We would like to thank David Glasspool and John Fox for their generous support, which allowed Peter Yule (email@example.com) to develop this box.
|COGENT Version 2.4 Help|