EventChannel.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 //----------------------------------------------------------------------------------
00014 // Includes
00015 //----------------------------------------------------------------------------------
00016 #include "EventChannel.h"
00017 #include "Log.h"
00018 #include "Profiler.h"
00019 
00020 namespace nrEngine{
00021 
00022         //------------------------------------------------------------------------
00023         EventChannel::EventChannel(EventManager* manager, const std::string& name) : mName(name){
00024                 mParentManager = manager;
00025         }
00026 
00027         //------------------------------------------------------------------------
00028         EventChannel::~EventChannel(){
00029                 // disconnect all actors
00030                 _disconnectAll();
00031         }
00032 
00033         //------------------------------------------------------------------------
00034         Result EventChannel::add(EventActor* actor, bool notice)
00035         {
00036                 // check if actor has got a name 
00037                 if (actor == NULL) return OK;
00038                 if (actor->getName().length() == 0)
00039                 {
00040                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "EventChannel (%s): Want to connect nameless actor", getName().c_str());
00041                         return EVENT_NO_VALID_ACTOR;
00042                 }
00043                 
00044                 // we first check whenever the actor is already connected
00045                 if (isConnected(actor->getName())) return EVENT_ALREADY_CONNECTED;
00046 
00047                 // connect the actor to the OnEvent - Signal slot
00048                 mActorDb[actor->getName()] = actor;
00049 
00050                 // notice an actor that he is got a connection now
00051                 if (notice) actor->_noticeConnected(this);
00052 
00053                 // Log debug stuff
00054                 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, "EventChannel (%s): New actor connected \"%s\"", getName().c_str(), actor->getName().c_str());
00055 
00056                 return OK;
00057         }
00058 
00059         //------------------------------------------------------------------------
00060         Result EventChannel::del(EventActor* actor, bool notice)
00061         {
00062                 if (actor == NULL) return OK;
00063                 
00064                 // we first check whenever the actor is already connected
00065                 if (!isConnected(actor->getName())) return EVENT_NOT_CONNECTED;
00066 
00067                 // disconnect the actor to the OnEvent - Signal slot
00068                 mActorDb.erase(mActorDb.find(actor->getName()));
00069 
00070                 // notice an actor that it is disconnected now
00071                 if (notice) actor->_noticeDisconnected(this);
00072 
00073                 // Log debug stuff
00074                 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, "EventChannel (%s): Actor \"%s\" disconnected ", getName().c_str(), actor->getName().c_str());
00075 
00076                 return OK;
00077         }
00078 
00079         //------------------------------------------------------------------------
00080         void EventChannel::_disconnectAll()
00081         {
00082                 // Profiling of the engine
00083                 _nrEngineProfile("EventChannel._disconnectAll");
00084 
00085                 // some logging
00086                 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, "EventChannel (%s): Disconnect all actors", getName().c_str());
00087 
00088                 // iterate through all connections and close them
00089                 ActorDatabase::iterator it = mActorDb.begin();
00090                 for (; it != mActorDb.end(); it++){
00091                         mActorDb.erase(it);
00092                         it->second->_noticeDisconnected(this);
00093                 }
00094 
00095         }
00096 
00097         //------------------------------------------------------------------------
00098         bool EventChannel::isConnected(const std::string& name)
00099         {
00100                 ActorDatabase::iterator it = mActorDb.find(name);
00101                 return it != mActorDb.end();
00102         }
00103 
00104         //------------------------------------------------------------------------
00105         void EventChannel::emit (SharedPtr<Event> event)
00106         {
00107                 // iterate through all connected actors and emit the signal
00108                 ActorDatabase::iterator it = mActorDb.begin();
00109                 for (; it != mActorDb.end(); it++){
00110                         it->second->OnEvent(*this, event);
00111                 }
00112         }
00113 
00114         //------------------------------------------------------------------------
00115         void EventChannel::push (SharedPtr<Event> event)
00116         {
00117                 // check if the event priority is immediat
00118                 if (event->getPriority() == Priority::IMMEDIATE){
00119                         emit(event);
00120                 }else{
00121                         mEventQueue.push(event);
00122                 }
00123         }
00124 
00125 
00126         //------------------------------------------------------------------------
00127         void EventChannel::deliver()
00128         {
00129                 // Profiling of the engine
00130                 _nrEngineProfile("EventChannel.deliver");
00131 
00132                 // we go through all elements in our event queue and deliver
00133                 // the messages from the queue to connected actors
00134                 while (!mEventQueue.empty()){
00135                         emit(mEventQueue.top());
00136                         mEventQueue.pop();
00137                 }
00138 
00139         }
00140 
00141 }; // end namespace
00142 

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1