Documentation:Tutorial:Events - MdsWiki
Navigation
Personal tools

From MdsWiki

Revision as of 15:45, 22 October 2010; 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_EVENTSand users are recommended to use them.

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 retireve 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())
...
...
>>>