Facility Control

 View Only
  • 1.  TCP Connection

    Posted 20 days ago

    Hi All 

    A couple of quick questions about the TCP listener.

    This post: RE: Initiate message via listener as server says that a listener object should have access to the following methods:

    public abstract boolean writeAsBytes(String bytes) 
    public abstract boolean writeBytes(byte[] bytes) 
    public abstract boolean writeString(String bytes, boolean utf8) 

     
    However, this doesn't seem to work with ogscript.getListenerById().

    What methods are available to a getListenerById where the listener is a TCP client? Or for that matter a TCP server.

    I have previously used sendUDPAsBytes with a UDP listener.

    Also, is it possible to specify the local TCP port when using TCP client - there are cases when it will be handy to be able to re-open the same connection used previously, ie reusing the local port.

    If it is possible to do a writeBytes from a TCP server, then presumably I wouldn't get the benefit of message processing, via delimiters on the incoming messages.

    Thanks.

    Simon



    ------------------------------
    Simon Liddicott
    Systems Engineer
    ------------------------------


  • 2.  RE: TCP Connection

    Posted 19 days ago

    Hi Simon, 

    The listener object returned by ogscript.getListenerById() has the following methods exposed:
    start()
    stop()
    isStarted()

    To help answer your other questions and provide examples, I've attached a panel below.

    If you're looking for a non-persistent connection, you can use a RossTalk listener with rosstalk.sendMessageWithResponse(). This will hold a connection long enough to read a single response from the server.

    If you're looking for a persistent TCP client connection, you can use the <listener> tag with connectport specified. Inside of the listener task itself, you can use this.writeString() (or any of the other write methods) to send a message. Alternatively, you can expose the listener itself as an OGScript variable, via ogscript.putObject(). You can then retrieve the variable via ogscript.getObject() outside of the listener task, allowing you to then use writeString(). This would be similar to how you intended to use ogscript.getListenerById().

    You can also do the same sort of idea with a TCP server. Here, you can use the <listener> tag with listenport specified. Inside of the listener task, you can use this.writeString() to define how the server may reply, and also expose the server connection as an OGScript variable for usage outside of that listener task.

    Again, I've attached panel code below that provides an example of how these listeners can be used. Please let me know if you have any questions.

    <abs contexttype="opengear" gridsize="20" style="">
       <meta>
          <listener autostart="true" delimitertype="newline" id="server" listenport="8765">
             <task tasktype="ogscript">if (event.isMessageEvent())
    {
       var myMessage = event.getBytesAsString().trim() + "";
       ogscript.debug("SERVER RECEIVED: " + myMessage);
       this.writeString("Here's my server reply.\n", false);
    }
    
    ogscript.putObject("server-connection", this);</task>
          </listener>
          <listener autostart="true" connecthost="localhost" connectport="8765" delimitertype="newline" id="client">
             <task tasktype="ogscript">if (event.isConnectEvent())
    {
       ogscript.putObject("listener-connection", this);
       this.writeString("Initial client message.\n", false);
    }
    else if (event.isMessageEvent())
    {
       ogscript.debug("CLIENT RECEIVED: " + event.getBytesAsString());
    }
    else if (event.isDisconnectEvent())
    {
       var lObj = ogscript.getObject("listener-connection");
       if (lObj == this)
       {
          ogscript.putObject("listener-connection", null);      
       }
    }</task>
          </listener>
       </meta>
       <button buttontype="push" height="120" left="40" name="Send RossTalk Message" top="40" width="240">
          <task tasktype="ogscript">function callback(success, sentData, resultString, exception)
    {
       if (success) {
       ogscript.debug("ROSSTALK RECEIVED: " + resultString);
       } else{
       ogscript.debug("exception: " + exception);
       }    
    }
    
    rosstalk.sendMessageWithResponse('localhost', 8765, "This is a RossTalk message.\n", '\n',callback);</task>
       </button>
       <button buttontype="push" height="120" left="40" name="Send Message From Listener" top="200" width="240">
          <task tasktype="ogscript">var listener = ogscript.getObject("listener-connection");
    if (listener != null)
    {
       listener.writeString("This is a client message.\n", false);
    }</task>
       </button>
       <button buttontype="push" height="120" left="40" name="Send Message From Server" top="360" width="240">
          <task tasktype="ogscript">var myServer = ogscript.getObject("server-connection");
    if (myServer != null)
    {
       myServer.writeString("This is a server message.\n", false);
    }</task>
       </button>
    </abs>
    


    ------------------------------
    Michael Quach
    ------------------------------