EventActor.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 "EventActor.h"
00017 #include "EventManager.h"
00018 #include "EventChannel.h"
00019 #include "Engine.h"
00020 
00021 namespace nrEngine{
00022 
00023         //------------------------------------------------------------------------
00024         EventActor::EventActor()
00025         {
00026         
00027         }
00028 
00029         //------------------------------------------------------------------------
00030         EventActor::EventActor(const std::string& name) : mName(name){
00031         }
00032                         
00033         //------------------------------------------------------------------------
00034         EventActor::~EventActor()
00035         {
00036                 // first let each channel know, that we want to disconnect now
00037                 std::list<std::string>::iterator it = mChannel.begin();
00038                 for (; it != mChannel.end(); it++){
00039                         Engine::sEventManager()->getChannel(*it)->del(this, false);
00040                 }
00041 
00042         }
00043 
00044         //------------------------------------------------------------------------
00045         void EventActor::setName(const std::string& name)
00046         {
00047                 mName = name;
00048         }
00049         
00050         
00051         //------------------------------------------------------------------------
00052         const std::string& EventActor::getName() const
00053         {
00054                 return mName;
00055         }
00056 
00057         //------------------------------------------------------------------------
00058         /*void EventActor::OnEvent(const EventChannel& channel, SharedPtr<Event> event)
00059         {
00060                 // we call the pure virtual derived function which has to be
00061                 // overloaded for different event types. 
00062                 _OnEvent(channel, event);
00063         }*/
00064         
00065         //------------------------------------------------------------------------
00066         Result EventActor::emit(SharedPtr<Event> event)
00067         {
00068                 // iterate through channels and emit messages
00069                 std::list<std::string>::iterator it = mChannel.begin();
00070                 for (; it != mChannel.end(); it++)
00071                 {
00072                         Result res = Engine::sEventManager()->emit(*it, event);
00073                         if (res != OK) 
00074                         {
00075                                 NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "EventActor: Cannot emit message to '%s'", it->c_str());
00076                                 return res;
00077                         }
00078                 }
00079                 return OK;
00080         }
00081 
00082         //------------------------------------------------------------------------
00083         Result EventActor::connect (const std::string& name)
00084         {
00085                 // get a channel
00086                 SharedPtr<EventChannel> channel = Engine::sEventManager()->getChannel(name);
00087                 if (!channel) return EVENT_NO_CHANNEL_FOUND;
00088 
00089                 // check if we are already connected
00090                 if (isConnected(name)) return EVENT_ALREADY_CONNECTED;
00091                 
00092                 // ask the channel to connect me to it
00093                 channel->add(this);
00094                                 
00095                 // ok
00096                 return OK;
00097         }
00098         
00099         //------------------------------------------------------------------------
00100         Result EventActor::disconnect (const std::string& name)
00101         {
00102                 // get a channel
00103                 SharedPtr<EventChannel> channel = Engine::sEventManager()->getChannel(name);
00104                 if (!channel) return EVENT_NO_CHANNEL_FOUND;
00105                 
00106                 // check if we are already connected
00107                 if (!isConnected(name)) return EVENT_NOT_CONNECTED;
00108                 
00109                 // ask the channel to connect me to it
00110                 channel->del(this);
00111 
00112                 // ok
00113                 return OK;
00114         }
00115 
00116         //------------------------------------------------------------------------
00117         bool EventActor::isConnected(const std::string& name)
00118         {
00119                 // search for the name in the list
00120                 return std::find(mChannel.begin(), mChannel.end(), name) != mChannel.end();
00121         }
00122 
00123         //------------------------------------------------------------------------
00124         void EventActor::_noticeConnected(EventChannel* channel)
00125         {
00126                 // add the name of this channel to the list
00127                 if (!isConnected(channel->getName()))
00128                         mChannel.push_back(channel->getName());
00129         }
00130 
00131         
00132         //------------------------------------------------------------------------
00133         void EventActor::_noticeDisconnected(EventChannel* channel)
00134         {
00135                 // delete the name of this channel in the connection list
00136                 if (isConnected(channel->getName()))
00137                         mChannel.erase(std::find(mChannel.begin(), mChannel.end(), channel->getName()));
00138         }
00139         
00140 }; // end namespace     
00141 

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