Skip to main content

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).

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:

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:

TCP inbound adapter's OnTask method checks for data on TCP port, creates a stream and sends it to ProcessInput which send

In more detail:

  1. 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.

  2. 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.

  3. 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:

  1. Examine the input object (pInput) and decide how to use it.

  2. 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.

  3. For the request message, set its properties as appropriate, using values in the input.

  4. 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).

  5. 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.

  6. 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.

  7. 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:

  1. Add an instance of your business service class to the production.

  2. 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.

  3. Enable the business service.

  4. Run the production.

Related Options

EnsLib.TCP contains additional TCP inbound adapters such as EnsLib.TCP.FramedInboundAdapterOpens in a new tab. See the class reference for details.

InterSystems IRIS also provides specialized business service classes that use TCP adapters, and one of those might be suitable for your needs. If so, no programming would be needed. See Connectivity Options.

You can develop a new inbound adapter class based on the EnsLib.TCP.InboundAdapterOpens in a new tab or any of its subclasses. See Creating Custom TCP Adapter Classes.

FeedbackOpens in a new tab