Documentation:Tutorial:Events - MdsWiki
Personal tools

From MdsWiki

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_EVENTS
  • 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.


#include <mdsobjects.h>
using namespace MDSplus;
class MyEvent:public Event
    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";


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


import sys
from MDSplus import *

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

def main(arg):

if __name__=="__main__":
      arg = str(sys.argv[1])

Using the Python example, try the following. From the event receiver's terminal, you can execute the above Python code that will be listening for events (assuming the above code was saved in a file with the name ""):

python hi!

The receiver will be listening. Now, open a new terminal and execute:

setevent hi! my_data_string

The receiver's terminal will print on the screen something like the following:

where the Byte_Unsigned(...) in this example is the data string received.