Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages | Examples

Device Server Example

The example server runs 10 simulated power supply devices (names: PowerSupply-1 ... PowerSupply-10). Each device has two properties:

Both properties have double values within the range 0 ... 100. The voltage values are calculated as (requestedVoltage + random jitter) and are periodically updated by the measurement simulation thread. The server supports subscription on both properties. The data objects returned as results of subscription or get calls contain two entries:

This example is aimed at explaining the basic features of the RDA server API. I've tried to keep the code short and to avoid distracting from the main topic with lots of error handling irrelevant to the RDA calls. In particular:

The code is for Unix only. Although it is fairly possible to make a portable to Windows version of the example, it would result in too many ifdef's hiding the main message.

DeviceServer Class

The DeviceServer class extends rdaDeviceServerBase and provides implementations of all abstract methods defined in the base class. It creates Device objects and delegates incoming device access calls to a proper device. It creates, runs and terminates the updater thread which animates the measurement values. This class also provides an example implementation of the server administaration calls.

device-server.gif

Here is the code implementing this class.

Device Class

The Device class implements the device access and update methods used in the DeviceServer class. It holds a mutex that prevents simultaneous access to the device data from the RDA internal threads, which execute device access methods on behalf of remote clients, and from the updater thread.

device.gif

Here is the code implementing this class.

Property Classes

The Property class implements features that are common to the Measurement and Setting properties. It provides read-only access to the property value, manages a list of subscriptions and notifies subscribers on the value updates. Subclasses of this class provide methods that allow to change the property value (Measurement::update and Setting::set ).

property.gif

Here is the code implementing the property classes.

Server main Function

The server's main function initializes the RDA, creates a DeviceServer object, and calls runServer which starts the event loop so that the server can accept requests. It also initializes the message logging system taking the initial log level from the command line, and installs a signal handler on SIGINT so that to achieve clean termination of the server on receit of this signal.

Test Client

This little client application can be used to test the example server. It accepts from the standard input the text commands that allow to invoke all basic access methods on a selected device in the server:
  • device name
    Selects device by name
  • device
    Prints the name of the selected device
  • get property
    Invokes the get method on the property and prints the property value and timestamp
  • set property value
    Invokes the set method on the property. The value must be parsable to double.
  • mon property
    Invokes the monitorOn method on the property and prints incoming subscription reports. Hitting the "Enter" key stops the subscription (invokes monitorOff on the property).
  • quit
    Terminates the application

Compiling and Linking

Compiling and linking is largely compiler and platform dependent. To build the server, you must at least link with the RDA and CORBA libraries. Additional libraries (socket, pthread) may be required on some platforms. As an example, here is the makefile which you can use to build the server and the test application on Linux systems in the PS controls environment.

Running the Example

Before running the server you need to choose the server name so that it will not conflict with the names of other servers in the system. A simple, although not 100% safe, solution is to use your login name as the server name or as a prefix to the server name. For example, the command:

deviceServer trofimov.example INFO

will start the server named trofimov.example with the initial log level INFO, and produce the following trace messages on stdout:
INFO - initializing ORB
INFO - creating device server
INFO - starting updater thread
INFO - updater thread running
INFO - new server running
    name: trofimov.example
    user: trofimov
    host: pspc8179
    OS  : Linux 2.4.18-27.7.x.cern
Now you can start the test client (in a separate window on the same or another host):

client trofimov.example

and proceed, for example, as follows:
=> device PowerSupply-1
=> get voltage
 0.0 Mon May 12 15:36:18 2003
=> set requestedVoltage 30
=> get voltage
27.7 Mon May 12 15:36:36 2003
=> mon voltage
PowerSupply-1/voltage = 31.3 Mon May 12 15:36:47 2003
PowerSupply-1/voltage = 29.6 Mon May 12 15:36:48 2003
PowerSupply-1/voltage = 25.9 Mon May 12 15:36:49 2003
PowerSupply-1/voltage = 34.9 Mon May 12 15:36:50 2003
PowerSupply-1/voltage = 30.5 Mon May 12 15:36:51 2003
PowerSupply-1/voltage = 27.3 Mon May 12 15:36:52 2003

PowerSupply-1/voltage - subscription cancelled
=> quit
To stop the server type Ctrl-C in the server window. This should result in the server process termination with the following trace messages:
INFO - terminating on signal (Interrupt)
INFO - terminating updater thread
INFO - updater thread terminates
INFO - deleting device server

RDA-2.3 documentation - 27 Jun 2007 - N.Trofimov