Using the Inbound TCP Adapters
This page describes how to use the primary TCP inbound adapters (EnsLib.TCP.CountedInboundAdapterOpens in a new tab, EnsLib.TCP.CountedXMLInboundAdapterOpens in a new tab, and EnsLib.TCP.TextLineInboundAdapterOpens in a new tab).
After configuring a production that uses TCP inbound adapters, you should use the Port Authority report to check for port conflicts. Also note that port conflicts are possible in scenarios that the Port Authority cannot detect, such as when multiple instances are running on the same machine.
Overview of Inbound TCP Adapter
InterSystems IRIS® data platform provides the following inbound TCP adapters, all of which are subclasses of EnsLib.TCP.InboundAdapterOpens in a new tab:
-
EnsLib.TCP.CountedInboundAdapterOpens in a new tab supports incoming TCP connections over which a TCP client and TCP listener exchange blocks of data, with the block length specified in the first 4 bytes of the block. The adapter uses the block length to acquire the meaningful portion of the data from the client application.
-
EnsLib.TCP.CountedXMLInboundAdapterOpens in a new tab acts as a TCP listener for an XTE server. The adapter receives XML data in counted TCP format and imports the XML data into InterSystems IRIS. The result is an instantiated InterSystems IRIS object.
-
EnsLib.TCP.TextLineInboundAdapterOpens in a new tab supports incoming TCP connections over which a TCP client and TCP listener exchange data as text strings that end with a line terminator character. The default terminator is the newline character (ASCII 10).
An important example of a business service that uses the EnsLib.TCP.TextLineInboundAdapterOpens in a new tab is the built-in business service class EnsLib.TCP.StatusServiceOpens in a new tab. This class provides a command-line interface that allows a console user or command-line script to retrieve basic status information from a running production. For more information, see Adding a TCP Status Service.
Overall Behavior
Each TCP inbound adapter checks for data on a specified port, reads the input, and sends the input as a stream to the associated business service. The business service, which you create and configure, uses this stream and communicates with the rest of the production. The following figure shows the overall flow:
In more detail:
-
Each time the adapter encounters input from its configured data source, it calls the internal ProcessInput() method of the business service class, passing the stream as an input argument.
-
The internal ProcessInput() method of the business service class executes. This method performs basic production tasks such as maintaining internal information as needed by all business services. You do not customize or override this method, which your business service class inherits.
-
The ProcessInput() method then calls your custom OnProcessInput() method, passing the input object. The requirements for this method are described later in this page.
The response message follows the same path, in reverse.
Creating a Business Service to Use a TCP Inbound Adapter
To use any of the TCP adapters in your production, create a new business service class as described here. Later, add it to your production and configure it.
You must also create appropriate message classes, if none yet exist. See Defining Messages.
The following list describes the basic requirements of the business service class:
-
Your business service class should extend Ens.BusinessServiceOpens in a new tab.
-
In your class, the ADAPTER parameter should equal EnsLib.TCP.CountedInboundAdapterOpens in a new tab, EnsLib.TCP.CountedXMLInboundAdapterOpens in a new tab, or EnsLib.TCP.TextLineInboundAdapterOpens in a new tab.
-
Your class must implement the OnProcessInput() method, as described in Implementing the OnProcessInput Method.
-
Your class can implement the OnConnect() callback method. This method is called after the OnTask() method, each time the TCP inbound adapter establishes a new connection to or from a remote system.
If you implement this method, it must have the following signature:
Method OnConnect(pTimeout As %Numeric) As %Status
Implement OnConnect() to take some action each time a new connection is established, for example, to send a logon sequence or a handshake token. The timeout argument is automatically provided by the TCP inbound adapter. It takes its value from the Read Timeout adapter setting. For details about Read Timeout, see Reference for Settings.
If OnConnect() returns an error status, the new connection fails and the adapter is disconnected. If an untrapped exception occurs within OnConnect(), the adapter catches it and continues as if OnConnect() were not implemented.
For other options and general information, see Defining a Business Service Class.
-
Your class can implement the OnInit() method to perform any special setup actions. This method is called from the %OnNew() method. If the Job Per Connection setting is set to true, then OnInit() is called from %OnNew() each time the listener spawns a new job to accept a connection.
-
Your class can implement the OnTearDown() method to perform any special tear down actions. This method is called from the %OnClose() method. Specifically, %OnClose() calls the OnTearDown() method of the adapter and then calls the OnTearDown() method of the business service. If the Job Per Connection setting is set to true, then OnTearDown() is called just before each connection job is closed.
Important:If your new business service class includes custom code in the OnTearDown() method, the custom code must invoke the OnTearDown() method of the superclass to enable the proper functioning of the business service, for example:
//Sets a node of the ^%zexample global to the ID of the current process // and invokes the OnTearDown() method of the superclass Method OnTearDown() As %Status { set ^%zexample($i(^%zexample),"onteardown")=$j Return ##super() }
You must also implement Try/Catch logic as appropriate to handle errors. For information about invoking a superclass method, see ##superclass Syntax.
Implementing the OnProcessInput() Method
This section describes the method signature for OnProcessInput(), which depends upon the adapter, and describes how to implement this method.
Signature for OnProcessInput() for EnsLib.TCP.CountedInboundAdapter
If your business service class uses EnsLib.TCP.CountedInboundAdapterOpens in a new tab, your OnProcessInput() method should have the following signature:
Method OnProcessInput(pInput As %Library.GlobalCharacterStream,
Output pOutput As %Library.AbstractStream) As %Status
Where:
-
pInput contains the incoming data stream that the TCP client has directed to the adapter.
-
pOutput contains any response that the business service might provide to the TCP client.
Signature for OnProcessInput() for EnsLib.TCP.CountedXMLInboundAdapter
If your business service class uses EnsLib.TCP.CountedXMLInboundAdapterOpens in a new tab, your OnProcessInput() method should have the following signature:
Method OnProcessInput(pInput As %RegisteredObject,
Output pOutput As %RegisteredObject) As %Status
Where:
-
pInput can be any of the objects specified by the Accept Class Names adapter setting.
For details about Accept Class Names, see Reference for Settings.
-
pOutput contains any response that you might need to return to the XTE server.
Signature for OnProcessInput() for EnsLib.TCP.TextLineInboundAdapter
If your business service class uses EnsLib.TCP.TextLineInboundAdapterOpens in a new tab, your OnProcessInput() method should have the following signature:
Method OnProcessInput(pInput As Ens.StringContainer,
Output pOutput As Ens.StringContainer) As %Status
Where:
-
pInput contains the incoming line of text.
-
pOutput contains the outgoing response string (if any).
Implementing OnProcessInput()
In all cases, the OnProcessInput() method should do some or all of the following:
-
Examine the input object (pInput) and decide how to use it.
-
Create an instance of the request message, which will be the message that your business service sends.
For information on creating message classes, see Defining Messages.
-
For the request message, set its properties as appropriate, using values in the input.
-
Call a suitable method of the business service to send the request to some destination within the production. Specifically, call SendRequestSync(), SendRequestAsync(), or (less common) SendDeferredResponse(). For details, see Sending Request Messages
Each of these methods returns a status (specifically, an instance of %StatusOpens in a new tab).
-
Make sure that you set the output argument (pOutput). Typically you set this equal to the response message that you have received. This step is required.
-
If the data source expects an acknowledgment or response to its input, OnProcessInput() must create this response and relay it to the data source, via the adapter.
-
Return an appropriate status. This step is required.
Example for EnsLib.TCP.TextLineInboundAdapter
The following is an example of a business service class that uses EnsLib.TCP.TextLineInboundAdapterOpens in a new tab.
Class TestTCPTextLine.AuthorizationTCPService Extends Ens.BusinessService
{
/// Name of the adapter class
Parameter ADAPTER = "EnsLib.TCP.TextLineInboundAdapter";
Method OnProcessInput(pInput As Ens.StringContainer,
pOutput As Ens.StringContainer) As %Status
{
set $ZT = "EXCEPTION"
set st = $$$OK
do {
if ('$isobject($get(pInput))) { quit }
// Input must have the following format: 'PatientCode:ProcedureCode'
set tSubject = pInput.StringValue
$$$TRACE("received line "_tSubject)
set req = ##class(TestTCPTextLine.AuthorizationRequest).%New()
set req.PatientCode = $piece(tSubject,":",1)
set req.ProcedureCode = $piece(tSubject,":",2)
set st = ..SendRequestSync("AuthorizationProcess", req, .resp)
quit:$$$ISERR(st)
set pOutput=
##class(Ens.StringContainer).%New(resp.AuthorizationFlag_
":"_resp.AuthorizationCode)
} while (0)
EXIT
//do ..Adapter.Disconnect()
quit st
EXCEPTION
set $ZT = ""
set st = $$$EnsSystemError
goto EXIT
}
}
Adding and Configuring the Business Service
To add your business service to a production, use the Management Portal to do the following:
-
Add an instance of your business service class to the production.
-
Configure the business service. For information on the settings, see Reference for Settings.
When you configure the business service, you specify a value for the Allowed IP Addresses settings, along other settings. Note that Allowed IP Addresses provides a way to enable the adapter to initiate the connection.
-
Enable the business service.
-
Run the production.