Sunday, January 02, 2005

Sample Event Brokerage

Sometime back I believe ,I said that I would post some code that demoed the process of Event Brokerage.I am putting the same in this blog.Having been so much hands on in .net throughout,I could but possibly put the code ,only in C# :) . I assume many are familiar with the language derived from Java and C++.It has this feature of delegates that so well suits our needs.Delegates are much similar to function pointers in C++ in terms of functionality and help in implementing callbacks.Note how the publisher and the subscriber have been isolated in the code,this imlementation is based on the observer pattern(for more information on Observer Pattern design click here) .Much of the code has been explained in the commenting itself.It can also be easily followed of course!!!.The code requires at least version 1 of .net framework to execute.Here we go..........

using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Event
{

//dummy events to be used by the subscribers
public enum EventsToGenerate {NewEvent}
//delegate used to pass the callback in the subscriber
//to the event broker

public delegate void Notify(object sender,EventsToGenerate evt) ;

/// Class is an end appliction that subscribes for
/// an event and also invokes the publisher for publishing an event.
///

class RunEvent
{
/// The main entry point for the application.
[STAThread] static void Main(string[] args)
{
//subscribe for a particular event
EventConsumer consumer = new EventConsumer() ;
consumer.Subscribe(EventsToGenerate.NewEvent) ;

//now publish a particular event type and
//see if the consumer is called
EventGenerator generateEvent = new EventGenerator() ; generateEvent.Publish (EventsToGenerate.NewEvent) ;

}
}

/// descibes an user defined event
class Events : EventArgs
{
EventsToGenerate evt ;
public Events(EventsToGenerate evt):base()
{
this.evt = evt ;
}
public EventsToGenerate Evt
{
get
{
return this.evt ;
}
}
}

/// It is the event publisher
class EventGenerator
{
EventBroker broker = EventBroker.GetEventBroker() ;
public void Publish( EventsToGenerate evt)
{
broker.Delegate(Event.EventsToGenerate.NewEvent) ;
}
}

/// Defines the subscriber logic
class EventConsumer
{
public void Subscribe(EventsToGenerate evt)
{
if(evt == EventsToGenerate.NewEvent)
{
EventBroker broker = EventBroker.GetEventBroker() ;
Notify notify = new Notify(Handler) ;
broker.EventDistributor(notify) ;
}
}

/// callback function which will be called by the event broker when events of a
///particular type occur
///

/// this can be used to determine the source of the event
/// event that occurred
protected void Handler(object sender,EventsToGenerate evt)
{
if(evt == EventsToGenerate.NewEvent)
MessageBox.Show("Handled the NewEvent! My God!!!") ;
else
{
MessageBox.Show("Couldnt get the NewEvent! My God!!!") ;
}
}
}

/// class provides brokerage between the publishers and the subscribers
///

class EventBroker
{
private static EventBroker broker ;

/// singleton event broker
/// single instance of event broker
public static EventBroker GetEventBroker()
{
if(broker == null)
{
broker = new EventBroker() ;
return broker ;
}
else
return broker ;
}

public event Notify notifyConsumers ;

/// notify the subscribers
/// the event to be notified to the subscribers
public void Delegate(EventsToGenerate evt)
{
this.notifyConsumers(this,evt) ;
}
/// subscribers use this to register the callbacks
/// with the event broker so that the event broker
/// can notify the callbacks whenever events occur
///

///
public void EventDistributor(Notify callback)
{
notifyConsumers += new Notify(callback);
}
}
}
Voila!!!!!

0 Comments:

Post a Comment

<< Home