The Event System in POX

POX is a publish/subscribe paradigm controller. In a publish/subscribe pattern, there are 3 parts -- publisher, subscriber and event. Publisher(or sender) can publish(or raise,source,dispatch) an an event without any knowledge of subscriber(or receiver). A subscriber can subscribe(or listen to ) a publisher. A publisher can be subscribed by multiple subscribers, and a subscriber can subscribe multiple publishers as well. POX works in publish/subscribe pattern and is an event-driven system.

In order to write a publish/subscribe program, we need to create 3 classes -- an event class, a publisher class, and a subscriber class. The relation between these 3 classes is shown as following figure.
alt pub/sub

Figure 1 pub/sub pattern

Event Class

Event Class can convey message from publisher to subscriber. In POX, events are subclasses of revent.Event. So to create an event, simply create a subclass of Event. You can add any extra attributes or methods you want.

First, create a python file named publisher.py

vi ./pox/ext/publisher.py  

In publisher.py, we first create an event for publisher raise.
This event class is called TestEvent, it inherits from Event class. In its constructor(init), there are two arguments arg1 and arg2. When publisher raises this event, publisher can pass two arguments into this event.

from pox.core import core  
from pox.lib.revent import *  
import pox  
import pox.openflow.libopenflow_01 as of

class TestEvent(Event):  
  def __init__(self,arg1,arg2):
    Event.__init__(self)
    print "TestEvent is initialed"
    self.par = arg1
    self.par2 = arg2
Publisher Class

Then we create a publisher in the same python file publisher.py

A publisher inherits from revent.EventMixin and declares which events it raises in a class-level variable called _eventMixin_events = set([TestEvent,]). Here publisher adds TestEvent into the eventMixin sets. Then this publisher can raise TestEvent which we created above.

Then we define a function called _handle_GoingUpEvent. This function listens to core(as the code self.listenTo(core) in __init__), and handle the GoingUpEvent event raised by core. When publisher receive GoingUpEvent, publisher will listen to openflow (self.listenTo(core.openflow)).

Next we define a function publishEvent which can raise the TestEvent.

In publishEvent function, self.raiseEvent(Eventclass, **args) can raise an Event. The first argument of raiseEvent is the event's class name, and follow the event's class name are arguments for events class.

Next we define a function _handle_PacketIn . This function can handle PacketIn event raised by openflow. When publisher receive PacketIn event, publisher call foo and foo raises the TestEvent event.

Last we define a function called launch which can register publisher into POX's core.

class Publisher(EventMixin):  
  """
  This is a Class for Publisher which can raise an event
  """
  _eventMixin_events = set([TestEvent,])
  def __init__(self):
    self.listenTo(core)
    print "Publisher is intialed\n"

  def _handle_GoingUpEvent(self, event):
    self.listenTo(core.openflow)
    print "this is in publisher, what is this for?\n"

  def publishEvent(self):
    print "publishEvent is called, will raise the test Event\n"
    self.raiseEvent(TestEvent,"firstPar","secondPar")
    print "foo raised event\n"

  def _handle_PacketIn(self,event):
    print "PacketIn event is raised,packetIn\n"
    self.publishEvent()
    return

def launch():  
  core.registerNew(Publisher)
Subsrciber Class

Next, we are going to create a Subscriber Class which can listen to Publisher and handle the TestEvent passed by Publisher.

We created a new python file called subscriber.py

In POX, a subscriber inherits from revent.EventMixin.

The Subscriber first listens to POX's core, and once GoingUpEvent is raised by core, the Subscriber listen to Publisher which has been registered into POX's core.

Then we define a function _handle_TestEvent which can handle TestEvent raised by Publisher and process event's arguments passed from Publisher.

Last function launch registers Subscriber into POX's core

vi ./pox/ext/subscriber.py  
import pox  
import pox.openflow.libopenflow_01 as of  
from pox.core import core  
from pox.lib.revent import *

class Subscriber(EventMixin):  
  def __init__(self):
    self.listenTo(core)

  def _handle_GoingUpEvent (self, event):
    self.listenTo(core.Publisher)
    print "This is in subsriber, what is this for?\n"

  def _handle_TestEvent(self,event):
    print "Test Event is raised,I heard TestEvent\n"
    print "EventPar:", event.par
    print "EventPar:", event.par2
#    event.eventMethod()
    return

def launch():  
  core.registerNew(Subscriber)

So far, We have created a event class called TestEvent, a publisher class called Publisher and a subscribe class called Subscriber. And we added TestEvent into the _eventMxin_events set and created a function called publishEvent which can raise TestEvent and pass arguments into event.

In POX the function whose form is starting _handle_eventName(self), is a function that handle the eventName. Such as def _handle_GoingUpEvent(self, event) can handle the event GoingUpEvent event raised by POX core, def _handle_PacketIn(self, event) can handle the event PacketIn raised by openflow, and def _handle_TestEvent(self, event) can handle the event TestEvent raised by Publisher.

The whole process of publish/subscribe system of above codes in POX is shown as following step:

  1. Publisher and Subscriber are registered into POX's core
  2. Publisher and Subscriber listen to POX's core
  3. Publisher and Subscriber receive an event called GoingUpEvent raised by POX's core
  4. Publisher listens to core.openflow when it received GoingUpEvent, whereas Subscriber listens to Publisher when it received GoingUpEvent
  5. Publisher receives a PacketIn event and then raise the TestEvent and pass two arguments
  6. Subscriber receives the TestEvent event and parses the two arguments passed with the event.

References:
[1] POX Wiki The Event System
[2] Wiki Publish–subscribe pattern
[3] POX中创建新的Event