Documentation:Tutorial:Events - MdsWiki
Navigation
Personal tools

From MdsWiki

Revision as of 08:57, 12 October 2011; Manduchi (Talk | contribs)
(diff) ←Older revision | Current revision | Newer revision→ (diff)
Jump to: navigation, search

Asynchronous events in MDSplus

In MDSplus it is possible to generate asynchronous events as well as waiting for them. Events are often used to synchronize data visualization in jScope: an update event name can be defined in the jScope Setup Data pop-up window, forcing an update of the displayed signal whenever such an event is received. Data acquisition events in this case are generated by the data acquisition routines (usually the methods of the devices involved in data acquisition).

There are two MDSplus executables which can be called from a console: wfevent <Event_name> and setevent <event_name> . The former suspends until an event with that name has been received; the latter generated such an event.

In MDSplus there exist several implementations of asynchronous events: the most recent uses UDP multicast, and therefore events can be exchanged among applications residing on different machines in the same LAN. In order to use Multicast events it is necessary to define the env variable UDP_EVENTS and users are recommended to use them. UDP events represent the most efficient implementation but not always UDP messages are transferred among subnetworks, so a different configuration is required in this case, using TCP/IP-based events. TCP/IP - based events rely on the mdsip server. In other words, if you want to receive remote events you need to have a mdsip server running on your machine. To configure TCP/IP events you need to:

  • undefine UDP_EVENT
  • define env variable mds_event_server as the mdsip address (<ip address>[::port]) of the intended event receiver. If more than one machine has to be involved in event reception, define mds_event_server as the list, separated by a semicolon, of the mdsip addesses of the event receivers

The Classes for Event Management

MDSplus defines one class, Event, for event generation and reception. In addition to notification, MDSplus events can bring data, either raw or "packed" into Data instances. The event issuer can then optionally associate data top the event, which will be received by every listener for that event. Events can be generated by the following methods:

  • setevent(name) to generate an event with no associated data
  • seteventRaw(name, buffer) to generate an event carrying a buffer of bytes
  • setevent(name, Data) to generate an event carrying a Data instance

To handle event reception it is necessary to extend Event class by associating the actions to be executed upon event reception. Class Event in fact defines the virtual function run(), which is called after the class instantiation whenever the event with the specified name has been received. The overriden run() method can then retrieve received data by calling superclass' getData() or getRaw() methods. The following code shows how to define a new Event class called MyEvent whose action is to print the content of the received buffer. It is possible to test the programs using the command

setevent <event_name> <Data String>

which generates an event with the specified name and passing the second parameter as a string buffer.

C++

#include <mdsobjects.h>
using namespace MDSplus;
class MyEvent:public Event
{
public:
    MyEvent(char *name):Event(name){}
    void run()
    {
        int bufSize;
        char *name = getName(); //Get the name of the event
        char *date = getTime()->getDate(); //Get the event reception date in string format
        char *buf =  getRaw(&bufSize); //Get raw data
        char *str = new char[bufSize+1]; //Make it a string
        memcpy(str, buf, bufSize);
        str[bufSize] = 0;
        cout << "RECEIVED EVENT " << name << " AT " << date << " WITH DATA  " << str << "\n";
    }
};

Java:

import MDSplus.*;
class MyEvent extends Event
{
     public MyEvent(java.lang.String event)throws Exception
     {
         super(event);
    }
    public void run()
    {
        System.out.println("RECEIVED EVENT "+ getName()+" AT " + getTime().getDate()+" WITH DATA  " + new java.lang.String(getRaw()));
    }
}

Python:

>>> class MyEvent(Event):
...      def run(self):
...        print("RECEIVED EVENT " + self.getName())
...        print(self.getTime())
...        print(self.getRaw())
...
...
>>>