CERN -> IPT Group -> FrameMaker at CERN
Information Service User's Guide

6.2 Subscribe for a single information

"Listing 6.1" shows how to an application can subscribe for an individual information in the IS repository.

Listing 6.1   Subscribe for a single information

1: // include main IS header file
2: #include <is/info.h>
3: #include <is/inforeceiver.h>
4: #include <ipc/core.h>
5:
6: void callback(ISCallbackInfo * isc){
7: std::cout << "CALLBACK:: " << isc->name() << std::endl;
8: std::cout << "Reason code : " << isc->reason() << std::endl;
9:
10: ISInfoInt isi;
11: isc->value(isi);
12: std::cout << isi;
13: }
14:
15: int main( int ac, char ** av ){
16: // Initialise communication library
17: IPCCore::init( ac, av );
18:
19: // Create the instance of partition
20: IPCPartition partition("MyPartition");
21:
22: // Create the IS receiver instance in the specific partitition
23: ISInfoReceiver rec(partitition);
24:
25: // Subscribe
26: rec.subscribe( "MyServer.DeviceVoltage", callback );
27:
28: // call method run to block the current thread until
29: // somebody will call the stop for the rec object
30: rec.run();
31:
32: // Remove subscription
33: rec.unsubscribe( "MyServer.DeviceVoltage" );
34:
35: return 0;
36: }

First the user program has to include the is/info.h header file which declares the ISInfoReceiver class. This class provides the main interface for the information subscribers. Then the program creates an instance of the IPCPartition class. This instance represents the partition in which the Information Service will be used. The ISInfoReceiver class constructor takes this partition object as parameter.


Actually ISInfoReceiver constructor has two parameters. The second one is a boolean value which defines whether callbacks will be serialized into a single thread or they will be executed in parallel in the context of several concurrent threads. For compatibility with the early versions of the IS, the default value of this parameter is set to true, which means that callbacks will be serialized. But it's strongly recommended to set this parameter to false to allow concurrent callbacks processing, which drastically increases the callback processing rate. Note that in this case the callback code has to be thread safe, i.e. it has to guard every access to shared data by mutex.

Line 25 shows how to subscribe for the changes of the information called "MyServer.DeviceVoltage". Line 30 shows how to block the current thread to prevent it from exiting immediately. This example calls the run method of the ISInfoReceiver class to do this. This method is implemented by the IPCServer class from which the ISInfoReceiver inherits. For more information about the IPCServer class see [9]. But of course this is not mandatory, a user can use his own way of blocking the thread till it is necessary.

When the "MyServer.DeviceVoltage" information is changed the callback function is called. The isc argument (line 4) is a pointer to the object that describes the information changes. One can get name, type and value of the changed information from this object. In addition it can also be used to get the reason why the callback has been called. The valid reasons are: information has been inserted, updated or removed from the IS repository.


2 July 1998 - WebMaster
Copyright © CERN 1998