Simple Messaging Client
The Goal
Illustrate the basic messaging concepts
Python script
This script was demonstrated at the SeisComP3 workshop in Erice. It should be relatively self-explaining, but full understanding does require certain knowlege of Python.
The script does nothing but
- connect to a SeisComP3 messaging server
- subscribe to messages sent to messaging group "EVENT"
- listen to these messages
- dump the event IDs to the screen
No actual real-world use case but a truly minimum example for a SeisComP3 application.
import sys, traceback, seiscomp3.Client class EventListener(seiscomp3.Client.Application): def __init__(self): seiscomp3.Client.Application.__init__(self, len(sys.argv), sys.argv) self.setMessagingEnabled(True) self.setDatabaseEnabled(True, True) self.setPrimaryMessagingGroup(seiscomp3.Communication.Protocol.LISTENER_GROUP) self.addMessagingSubscription("EVENT") def doSomethingWithEvent(self, event): try: ####################################### #### Include your custom code here #### print "event.publicID = %s" % event.publicID() ####################################### except: info = traceback.format_exception(*sys.exc_info()) for i in info: sys.stderr.write(i) def updateObject(self, parentID, object): # called if an updated object is received event = seiscomp3.DataModel.Event.Cast(object) if event: print "received update for event %s" % event.publicID() self.doSomethingWithEvent(event) def addObject(self, parentID, object): # called if a new object is received event = seiscomp3.DataModel.Event.Cast(object) if event: print "received new event %s" % event.publicID() self.doSomethingWithEvent(event) def run(self): # needs not be re-implemented print "Hi! The EventListener is now running." return seiscomp3.Client.Application.run(self) app = EventListener() sys.exit(app())
Note that the EventListener class is derived from the application class seiscomp3.Client.Application from which it inherits most of the functionality. For instance the ability to connect to the messaging and to the database are both provided by seiscomp3.Client.Application; the EventListener only has to enable messaging and database usage in the __init__ routine. The real action takes place in the doSomethingWithEvent routine, which is called by both updateObject and addObject, depending on whether the event object received is a newly added or just and updated event.
Attachments
-
scclient.py
(1.6 KB) - added by saul
-
run.sh
(75 bytes) - added by saul