Facility Control

 View Only
  • 1.  Listener delimiter

    Posted 01-15-2019 19:54
    Hello all

    I have trouble receiving AMP messages from my GV K2 videoserver. I explain:
    K2 'answers' to AMP command with next formula:

    CMD type (2 bytes) - total length in hex(2 bytes) - rest of response. Here is message example:

    820f0009000764656661756c74 = while first 2 bytes '820f' is cmd type, 0009 its an total bytes length in hex, and then the rest of variable messages. Its always same formula.

    I want to configure the listener to read length bytes (bytes 5-8) and receive whole message.
    In this point, my only success to capture something was with 'fixed length' delimiter. So i can receive data byte by byte, as a different messages. I guess right way of delimiter type should be 'variable length' but i dont know how to set first 2 bytes offset... So:

    1. Is it possible to configure listener to 'understand' the length of that kind of messages and get it one piece?
    2. If it is impossible in native way, is there an trick to collect those 1 bytes message to 1 message string (maybe separate by timeout, longest messages is reach maximum 3 sec to recieve)?

    Thank you,

    Alex.


  • 2.  RE: Listener delimiter

    Posted 01-16-2019 22:08

    The command type being before the length count means that you cannot use any of the built-in listener delimiters to read the AMP messages. That said, there are more efficient ways to the subsequent bytes over the wire than collecting the individual bytes. The listener tasks have access to the listener's connection via a "this" object and can get the listener's input stream this way. Once you have the input stream, you can pass it through to a message parser object to read shorts, strings, byte arrays, or anything else.

    One note: if you do want to read directly from the listener's input stream, you will want to turn "blocking pause" on to avoid having the listener's receive thread read more bytes from the listener while your task is executing.

    Here is a fairly simple example that has a 2-byte header, message length, and message body format that you can run as a custom panel to see what I am talking about.

    <abs contexttype="opengear" gridsize="20" id="_top">
       <meta>
          <params>
             <param access="1" maxlength="0" name="Message Type 1" oid="Message_Type_1" type="STRING" value="Test 1" widget="text"/>
             <param access="1" maxlength="0" name="Message Type 2" oid="Message_Type_2" type="STRING" value="Test 2" widget="text"/>
          </params>
          <api>function sendMessageToListener(type, message)
    {
       var mb = ogscript.createMessageBuilder();
       mb.writeShort(type);
       mb.writeShort(message.length);
       mb.writeString(message);
       rosstalk.sendBytes('localhost', 2244, mb.toByteArray(), null);
    }
    </api>
          <listener autostart="true" blockingpause="true" delimiter="2" delimitertype="fixedlen" listenport="2244">
             <task tasktype="ogscript">if (event.isMessageEvent())
    {
       var cmdTypeBytes = event.getBytes();
       var commandType = cmdTypeBytes[0] &lt;&lt; 8 | cmdTypeBytes[1];
       var messageParser = ogscript.createMessageParser(this.getInputStream());
       var length = messageParser.readShort();
       var string = messageParser.readString(length);
    
       ogscript.rename('output', 'TYPE: ' + commandType + ' MESSAGE: ' + string);
    }</task>
          </listener>
       </meta>
       <param expand="true" height="40" left="80" oid="Message_Type_1" top="40" width="320"/>
       <param expand="true" height="40" left="80" oid="Message_Type_2" top="100" width="320"/>
       <button buttontype="push" height="40" left="400" name="Go" top="40" width="60">
          <task tasktype="ogscript">sendMessageToListener(1, params.getValue('Message_Type_1', 0));
    </task>
       </button>
       <button buttontype="push" height="40" left="400" name="Go" top="100" width="60">
          <task tasktype="ogscript">sendMessageToListener(2, params.getValue('Message_Type_2', 0));
    </task>
       </button>
       <label height="60" id="output" left="0" style="txt-align:west" top="140" width="1340"/>
    </abs>

    Please let me know if you have any follow-up questions.

    James


    #DashBoard