00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00030 _disconnectAll();
00031 }
00032
00033
00034 Result EventChannel::add(EventActor* actor, bool notice)
00035 {
00036
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
00045 if (isConnected(actor->getName())) return EVENT_ALREADY_CONNECTED;
00046
00047
00048 mActorDb[actor->getName()] = actor;
00049
00050
00051 if (notice) actor->_noticeConnected(this);
00052
00053
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
00065 if (!isConnected(actor->getName())) return EVENT_NOT_CONNECTED;
00066
00067
00068 mActorDb.erase(mActorDb.find(actor->getName()));
00069
00070
00071 if (notice) actor->_noticeDisconnected(this);
00072
00073
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
00083 _nrEngineProfile("EventChannel._disconnectAll");
00084
00085
00086 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, "EventChannel (%s): Disconnect all actors", getName().c_str());
00087
00088
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
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
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
00130 _nrEngineProfile("EventChannel.deliver");
00131
00132
00133
00134 while (!mEventQueue.empty()){
00135 emit(mEventQueue.top());
00136 mEventQueue.pop();
00137 }
00138
00139 }
00140
00141 };
00142