Skip to content
ebiiii edited this page Oct 21, 2014 · 1 revision

Feedback channel to a Virtual Sensor's Processing Class Through The Web Interface

   /*
    * This method is going to be called by the container when one of the input
    * streams has a data to be delivered to this virtual sensor. After receiving
    * the data, the processing class can do the processing on it and this
    * processing could possibly result in producing a new stream element in this
    * virtual sensor in which case the virtual sensor will notify the container
    * by simply adding itself to the list of the virtual sensors which have
    * produced data. (calling <code>container.publishData(this)</code>. For
    * more information please check the <code>AbstractVirtalSensor</code>
    */
public boolean dataFromWeb ( String action,String[] paramNames, Serializable[] paramValues )

Virtual Sensor Configuration

Webinput upload to virtual sensor

For simplicity and modularity, the upload inputs are regrouped into subgroups which can be selected with the upper select box. Upload inputs are optional and specified in the virtual sensor file, thus the tab only appears when it is specified. The different types are specified but are not enforced on the client side as this could be easily bypassed. The validation is done on the server side as client input should never be trusted. The following types have special input fields :

  • binary: file upload ;
  • radio: radio button with limited choice ;
  • checkbox: checkbox with limited choice ;
  • select: drop down box with limited choice ;

The radio, checkbox and select types use the syntax like radio:ele1|ele2 to specify the multiple choice. The binary field is limited by a maximum size specified in the type part with a syntax like binary:2mb. When the type begins with a star, it is compulsory. Here is a sample xml which shows all the capabilities:

<virtual-sensor name="vs">
    <processing-class>
        <class-name>gsn.vsensor.WebInteractiveVirtualSensor</class-name>
        <init-params />
        <web-input password="test">
            <command name="ploppy">
                <field name="plop1" type="radio:apple|orange|banana">one</field>
                <field name="plop2" type="*checkbox:apple|orange|banana">two</field>
                <field name="plop3" type="select:apple|orange|banana">three</field>
            </command>
            <command name="test">
                <field name="reset1" type="byte" />
                <field name="reset2" type="*binary:2mb">Files Size of 2MB max, The * means optional.</field>
            </command>
        </web-input>
        <output-structure>
            <field name="temp" type="long" description="none">-5</field>
        </output-structure>
    </processing-class>
...
</virtual-sensor>

An AJAX like behavior was required to give feedback to the user after the web-input upload. However, the XMLHTTPrequest object used to send ajax calls doesn't support file upload for security reasons. To bypass this limitation, a normal FORM element is used but uses a hidden in-line frame as a target. When the user clicks the submit button, the upload starts in the hidden IFrame to the destination /upload. When the upload ends or breaks, the servlet replies back with html code like the following code line. This JavaScript code calls back the parent window GSN core object which allows the webapp to give feedback to the user.

window.parent.GSN.msgcallback("msg",code)

On the servlet side, the Jakarta fileupload library was used to speed up development. A servlet translates the POST upload into XML-RPC format before sending it to the gsn system. The xml produced is similar to the following block lines. Normal fields are given in text format and binary fields are encoded in Base 64.

<input>
  <vsname>gpsvs</vsname>
  <command>test</command>
  <fields>
    <field>
      <name>reset1</name>
      <value>test comment</value>
    </field>
    <field>
      <name>reset2</name>
      <value>ABQOBJREFUevQ18FNW93+Cq7tza+tXB ... AARK5CYII=</value>
    </field>
    <field>
      <name>test1</name>
      <value>banana</value>
    </field>
    <field>
      <name>test2</name>
      <value>orange</value>
    </field>
    <field>
      <name>test2</name>
      <value>apple</value>
    </field>
    <field>
      <name>test3</name>
      <value>orange</value>
    </field>
  </fields>
</input>

The servlet takes care of keeping only the input fields of the command activated by the drop down menu. The xml output can be read in the sb.toString() variable and has to be validated by the gsn system. The feedback is sent to the user as explained before and corresponds, in the servlet, to the msg and code variables. The message could be any text string but should escape single quote and will be displayed at the top of the webinterface. A code higher than 200 is considered as an error and is shown in red to the user, otherwise it is in black. Nevertheless the code is not shown to the user in the current interface.

FAQ

Question: When is the sendToWrapper method called ?

The sendToWrapper method can be only called from a virtual sensor which uses this wrapper. The code in the virtual sensor's class will be something like below (sending some item to the underlying wrapper:):

vsensor.getInputStream( INPUT_STREAM_NAME ).getSource( streamSourceAliasName ).getWrapper().sendToWrapper( command , paramNames , paramValues );

So a virtual sensor can send data to the wrapper and the wrapper will forward it (if the sendToWrapper method is implemented) to the actual data source.

Clone this wiki locally