EventManager.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 "EventManager.h"
00017 #include "Log.h"
00018 #include "Profiler.h"
00019 #include "ITask.h"
00020 #include "EventChannel.h"
00021 #include "Event.h"
00022 #include "EventFactory.h"
00023 
00024 
00025 namespace nrEngine{
00026 
00027         //------------------------------------------------------------------------
00028         EventManager::EventManager(){
00029                 setTaskName("EventSystem");
00030 
00031                 NR_Log(Log::LOG_ENGINE, "EventManager: Initialize the event management system");
00032 
00033                 // create default system wide channel
00034                 createChannel(NR_DEFAULT_EVENT_CHANNEL);
00035         }
00036 
00037         //------------------------------------------------------------------------
00038         EventManager::~EventManager()
00039         {
00040                 // clear the database, so all channels are deleted
00041                 mChannelDb.clear();
00042 
00043                 // clear the factory list
00044                 mFactoryDb.clear();
00045         }
00046 
00047         //------------------------------------------------------------------------
00048         Result EventManager::createChannel(const std::string& name)
00049         {
00050                 // check first whenever such a channel is already in the database
00051                 if (getChannel(name)) return EVENT_CHANNEL_EXISTS;
00052 
00053                 // channel is not in the database, so create it and fill it's data
00054                 SharedPtr<EventChannel> channel(new EventChannel(this, name));
00055 
00056                 // push the channel into the database
00057                 mChannelDb[name] = channel;
00058                 NR_Log(Log::LOG_ENGINE, "EventManager: New channel \"%s\" created", name.c_str());
00059 
00060                 // OK
00061                 return OK;
00062         }
00063 
00064         //------------------------------------------------------------------------
00065         Result EventManager::removeChannel(const std::string& name)
00066         {
00067                 // check first whenever such a channel is already in the database
00068                 SharedPtr<EventChannel> channel = getChannel(name);
00069                 if (!channel) return EVENT_CHANNEL_NOT_EXISTS;
00070 
00071                 // disconnect all the actor from the channel
00072                 channel->_disconnectAll();
00073                 mChannelDb.erase(mChannelDb.find(name));
00074 
00075                 // log info
00076                 NR_Log(Log::LOG_ENGINE, "EventManager: Remove channel \"%s\"", name.c_str());
00077 
00078                 // OK
00079                 return OK;
00080         }
00081 
00082         //------------------------------------------------------------------------
00083         SharedPtr<EventChannel> EventManager::getChannel(const std::string& name)
00084         {
00085                 // search for such an entry in the db
00086                 ChannelDatabase::iterator it = mChannelDb.find(name);
00087 
00088                 if (it == mChannelDb.end()) return SharedPtr<EventChannel>();
00089 
00090                 return it->second;
00091         }
00092 
00093         //------------------------------------------------------------------------
00094         Result EventManager::updateTask()
00095         {
00096 
00097                 // go through each channel and deliver the messages
00098                 ChannelDatabase::iterator it = mChannelDb.begin();
00099                 for (; it != mChannelDb.end(); it++)
00100                         it->second->deliver();
00101 
00102                 // ok
00103                 return OK;
00104         }
00105 
00106         //------------------------------------------------------------------------
00107         Result EventManager::emit(const std::string& name, SharedPtr<Event> event)
00108         {
00109                 // Profiling of the engine
00110                 _nrEngineProfile("EventManager.emit");
00111 
00112 
00113                 // if user want to send the message to all channels
00114                 if (name.length() == 0){
00115                         NR_Log(Log::LOG_ENGINE, Log::LL_CHATTY, "EventManager: Emit event '%s' to all channels", event->getEventType());
00116                         ChannelDatabase::iterator it = mChannelDb.begin();
00117                         for (; it != mChannelDb.end(); it++)
00118                                 it->second->push(event);
00119 
00120                 }else{
00121                         NR_Log(Log::LOG_ENGINE, Log::LL_CHATTY, "EventManager: Emit event '%s' to '%s'", event->getEventType(), name.c_str());
00122                         // get the channel according to the name and emit the message
00123                         SharedPtr<EventChannel> channel = getChannel(name);
00124                         if (channel == NULL)
00125                                 return EVENT_CHANNEL_NOT_EXISTS;
00126 
00127                         channel->push(event);
00128                 }
00129 
00130                 // ok
00131                 return OK;
00132         }
00133 
00134         //------------------------------------------------------------------------
00135         Result EventManager::emitSystem(SharedPtr<Event> event)
00136         {
00137                 return emit(NR_DEFAULT_EVENT_CHANNEL, event);
00138         }
00139 
00140 
00141         //------------------------------------------------------------------------
00142         SharedPtr<Event> EventManager::createEvent(const std::string& eventType)
00143         {
00144                 // find the factory, able to create this kind of events
00145                 FactoryDatabase::iterator it = mFactoryDb.begin();
00146                 for (; it != mFactoryDb.end(); it++){
00147                         if (it->second->isSupported(eventType))
00148                                 return it->second->create(eventType);
00149                 }
00150 
00151                 return SharedPtr<Event>();
00152         }
00153 
00154         //------------------------------------------------------------------------
00155         Result EventManager::registerFactory(const std::string& name, SharedPtr<EventFactory> factory)
00156         {
00157                 // get a factory by name
00158                 FactoryDatabase::iterator it = mFactoryDb.find(name);
00159                 if (it != mFactoryDb.end()){
00160                         NR_Log(Log::LOG_ENGINE, Log::LL_WARNING, "EventManager: The event factory %s is already registered", name.c_str());
00161                         return EVENT_FACTORY_FOUND;
00162                 }
00163 
00164                 NR_Log(Log::LOG_ENGINE, Log::LL_NORMAL, "EventManager: Register event factory %s", name.c_str());
00165                 mFactoryDb[name] = factory;
00166                 return OK;
00167         }
00168 
00169         //------------------------------------------------------------------------
00170         Result EventManager::removeFactory(const std::string& name)
00171         {
00172                 // check if the the factory list is not empty
00173                 if (mFactoryDb.size() == 0) return OK;
00174 
00175                 // get a factory by name
00176                 FactoryDatabase::iterator it = mFactoryDb.find(name);
00177                 if (it == mFactoryDb.end()){
00178                         NR_Log(Log::LOG_ENGINE, Log::LL_WARNING, "EventManager: The event factory %s was not found", name.c_str());
00179                         return EVENT_FACTORY_NOT_FOUND;
00180                 }
00181                 NR_Log(Log::LOG_ENGINE, Log::LL_NORMAL, "EventManager: Remove event factory %s", name.c_str());
00182                 mFactoryDb.erase(it);
00183                 return OK;
00184         }
00185 
00186 }; // end namespace
00187 

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