[Previous] [Contents] [Index] [Next]

Writing Data Server Applications

This chapter includes:

The data server is a process that maintains a shared state among other processes - it's like a global environment. It can be used for many tasks, but specifically can be accessed from the Slinger webserver in support of dynamic HTML. For more information about the data server, see the TCP/IP User's Guide.


Note: The example in this chapter uses HTML as the interface to the data server, but the data server isn't limited to HTML.

To write an external application, use the data server library functions:

  1. Register your application with the data server using ds_register().
  2. Use the data server library functions to monitor or control the device.
  3. Close your application with ds_deregister().

Data server library

The data server library consists of these functions:

ds_register()
Register your application with the data server.
ds_deregister()
Deregister your application with the data server.
ds_create()
Create a data server variable.
ds_clear()
Delete a data server variable.
ds_set()
Set a data server variable.
ds_get()
Get a data server variable.
ds_flags()
Set the flags for a data server variable.

For descriptions of these functions, see the TCP/IP Libraries chapter.

Example

Here's a simple (and nonfunctional) example of monitoring the temperature of an oven from a remote client:

Here's what the qnxvar tokens on the client's web page would look like:

<!--Show the current oven temperature-->
<!--#qnxvar format="<P>The oven temperature is %s degrees F." -->
<!--#qnxvar read="oven1 5" -->

If the temperature of the oven is currently 500 degrees F, the output looks like:

<P>The oven temperature is 500 degrees F.

Here's what the application monitoring the oven would look like:

// This program obtains the temperature of an oven, and
// then updates a data variable in the data server, to
// be read by slinger if the appropriate token is in an
// html page slinger is serving. 

#include <stdlib.h>
#include <stdio.h>
#include <ds.h>
#include <string.h>

#define MAXLEN 4

int main(void)
{
  ds_t ds_descriptor;
  char ovenID[7], oven_temp[MAXLEN], flag=0;
  int length = MAXLEN;

  ds_descriptor = ds_register();
  if(ds_descriptor==-1){
    perror("ds_register");
    exit(1);
  }
  
  strcpy(ovenID,"oven1");

  if(ds_create(ds_descriptor, ovenID, flag, 0)==-1){
    perror("ds_create");
    exit(1);
  } 
  
  // Obtain the an initial temperature for the oven
  // to initialize the data server variable. strcpy
  // that value into oven_temp 
    
  ds_set(ds_descriptor,ovenID,oven_temp,length);
 
  //Now let's update the temperature at some time interval

  while(1)
  //you might want some kind of decision to exit the program. 
  {
    
     //obtain the current temperature from the oven

     //strcpy that temp reading into the oven_temp variable

     ds_set(ds_descriptor,ovenID,oven_temp,length);

     //wait a predetermined amount of time
 
   } 

   ds_clear(ds_descriptor,ovenID);
   ds_deregister(ds_descriptor);

}

The output HTML page would reflect the current temperature stored in the data server.

When this process exits, the data server variable is no longer available because the flag argument passed to ds_create() was 0.

If this application needs some data passed from HTML text, another variable would be created by calling ds_create() and would be used to pass information to it by using the qnxvar write token in some HTML text. The application could get the data by calling ds_get() and/or react to the change in data by receiving a proxy or signal.

Here's what's happening:


Oven example


Monitoring an oven via the data server.


In summary:

For information about the qnxvar token, see the description of slinger in the TCP/IP User's Guide.


[Previous] [Contents] [Index] [Next]