nrEngine::EventChannel Class Reference
[Event-Management]

Event channel used for communication between application/engine's components. More...


Public Member Functions

 EventChannel (EventManager *manager, const std::string &name)
 Create a new instance of the communication channel.
virtual ~EventChannel ()
 Releas eused memory and destroy hte instance.
Result add (EventActor *actor, bool notice=true)
Result del (EventActor *actor, bool notice=true)
NR_FORCEINLINE const std::string & getName () const
void emit (SharedPtr< Event > event)
void push (SharedPtr< Event > event)
void deliver ()

Protected Types

typedef std::map< std::string,
EventActor * > 
ActorDatabase
 Store here the mapping between actor names and their connections.
typedef std::priority_queue<
SharedPtr< Event >, std::vector<
SharedPtr< Event > >, GreatEvent<
SharedPtr< Event > > > 
EventQueue
 We store our event messages in a priority queue.

Protected Member Functions

bool isConnected (const std::string &name)
 Check whenever a given actor is already connected.
void _disconnectAll ()
 Disconnect all actors from the channel.

Protected Attributes

std::string mName
 Unique name of the communication channel.
EventManagermParentManager
 We always does store a pointer to the event manager where this channel belongs to.
ActorDatabase mActorDb
 Connected actor database.
EventQueue mEventQueue
 Store the event messages in this variable.

Friends

class EventManager
 The event manager system is a friend to this class.

Data Structures

struct  GreatEvent


Detailed Description

Event channel used for communication between application/engine's components.

EventChannel represents a message bus for the communication between system components. Events/messages could be send through this channel to all listeners connected to this channel.
In our engine we mix the concept of event messaging and state machines. So we do not have only events but also states. The states are stored in communication channels, so each listener of a channel could also be noticed if any state changes. This is also how we handle it in the nrEngine.
The communication actors are connected to the channel by its name. Also the lifetime of each actor is tracked. So if an actor is removed from the memory, so it will be automaticaly disconnected from the database. We do not use smart pointers for this purpose, because it will cause in undefined working flow. i.e. if you think the actor is removed from the memory because you have deleted it, it still get noticed about the things going on the channel. So this will ends up in undefined state.In our implementation the destructor of EventActor does simply say all channels it connected to, that the object does not exists anymore, so it can be disconnected.

Definition at line 52 of file EventChannel.h.


Member Function Documentation

Result nrEngine::EventChannel::add ( EventActor actor,
bool  notice = true 
)

Connect a new actor to the channel. The actor will be connected so it get noticed about any communication on the channel.

The actor has to implement default event reactions needed for our channel communication system. Compilation error will occur if the reactions to the default events are not implemented.

Parameters:
actor An actor to connect to the channel
notice Should channel let notice the actor that he is connected now (default YES)

Definition at line 34 of file EventChannel.cpp.

References nrEngine::EventActor::_noticeConnected(), nrEngine::EVENT_ALREADY_CONNECTED, nrEngine::EVENT_NO_VALID_ACTOR, getName(), nrEngine::EventActor::getName(), isConnected(), nrEngine::Log::LL_DEBUG, nrEngine::Log::LL_ERROR, nrEngine::Log::LOG_ENGINE, mActorDb, and nrEngine::OK.

Result nrEngine::EventChannel::del ( EventActor actor,
bool  notice = true 
)

Disconnect an actor from the channel. It is a good coding style if you call this function, when you do not need the connection anymore. However our engine does provide you the possibility to forget about disconnecting the actors from the channel, because the lifetime of an actor will be tracked. If the object does not exists anymore, so it will be automaticaly disconnected.

If you want to be performant, so remove actors from the channels, if they do not need the connection anymore.

Parameters:
actor An actor already connected to the channel
notice Should channel notice the actor, about disconnection (default YES)

Definition at line 60 of file EventChannel.cpp.

References nrEngine::EventActor::_noticeDisconnected(), nrEngine::EVENT_NOT_CONNECTED, getName(), nrEngine::EventActor::getName(), isConnected(), nrEngine::Log::LL_DEBUG, nrEngine::Log::LOG_ENGINE, mActorDb, and nrEngine::OK.

NR_FORCEINLINE const std::string& nrEngine::EventChannel::getName (  )  const

Get the name of the channel

Definition at line 93 of file EventChannel.h.

Referenced by _disconnectAll(), nrEngine::EventActor::_noticeConnected(), nrEngine::EventActor::_noticeDisconnected(), add(), and del().

void nrEngine::EventChannel::emit ( SharedPtr< Event event  ) 

Emit a certain event to a channel. This will send this event to all connected actors, so they get noticed about new event.

Parameters:
event Smart pointer to an event object

Definition at line 105 of file EventChannel.cpp.

References mActorDb.

Referenced by deliver(), and push().

void nrEngine::EventChannel::push ( SharedPtr< Event event  ) 

Instead of emit() this will not send the message directly to the channel listeners, but it store the message first in a queue based on priority numbers of the events. When you call deliver(), then all messages are getting delivered to the channel listeners

Parameters:
event Smart pointer to the event message
NOTE: If event priority is immediately so the message will be emitted immediately without be stored in the queue

Definition at line 115 of file EventChannel.cpp.

References emit(), nrEngine::CPriority::IMMEDIATE, and mEventQueue.

void nrEngine::EventChannel::deliver (  ) 

Deliver all stored event messages from the queue to the actors connected to the channel.

Definition at line 127 of file EventChannel.cpp.

References emit(), and mEventQueue.


The documentation for this class was generated from the following files:
Generated on Wed Sep 12 23:19:43 2007 for nrEngine by  doxygen 1.5.1