00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00034 createChannel(NR_DEFAULT_EVENT_CHANNEL);
00035 }
00036
00037
00038 EventManager::~EventManager()
00039 {
00040
00041 mChannelDb.clear();
00042
00043
00044 mFactoryDb.clear();
00045 }
00046
00047
00048 Result EventManager::createChannel(const std::string& name)
00049 {
00050
00051 if (getChannel(name)) return EVENT_CHANNEL_EXISTS;
00052
00053
00054 SharedPtr<EventChannel> channel(new EventChannel(this, name));
00055
00056
00057 mChannelDb[name] = channel;
00058 NR_Log(Log::LOG_ENGINE, "EventManager: New channel \"%s\" created", name.c_str());
00059
00060
00061 return OK;
00062 }
00063
00064
00065 Result EventManager::removeChannel(const std::string& name)
00066 {
00067
00068 SharedPtr<EventChannel> channel = getChannel(name);
00069 if (!channel) return EVENT_CHANNEL_NOT_EXISTS;
00070
00071
00072 channel->_disconnectAll();
00073 mChannelDb.erase(mChannelDb.find(name));
00074
00075
00076 NR_Log(Log::LOG_ENGINE, "EventManager: Remove channel \"%s\"", name.c_str());
00077
00078
00079 return OK;
00080 }
00081
00082
00083 SharedPtr<EventChannel> EventManager::getChannel(const std::string& name)
00084 {
00085
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
00098 ChannelDatabase::iterator it = mChannelDb.begin();
00099 for (; it != mChannelDb.end(); it++)
00100 it->second->deliver();
00101
00102
00103 return OK;
00104 }
00105
00106
00107 Result EventManager::emit(const std::string& name, SharedPtr<Event> event)
00108 {
00109
00110 _nrEngineProfile("EventManager.emit");
00111
00112
00113
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
00123 SharedPtr<EventChannel> channel = getChannel(name);
00124 if (channel == NULL)
00125 return EVENT_CHANNEL_NOT_EXISTS;
00126
00127 channel->push(event);
00128 }
00129
00130
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
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
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
00173 if (mFactoryDb.size() == 0) return OK;
00174
00175
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 };
00187