Logo
~Sockets~
~Examples~
~Contact~


EventHandler.cpp

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2005,2007  Anders Hedstrom
00007 
00008 This library is made available under the terms of the GNU GPL.
00009 
00010 If you would like to use this library in a closed-source application,
00011 a separate license agreement is available. For information about 
00012 the closed-source license agreement for the C++ sockets library,
00013 please visit http://www.alhem.net/Sockets/license.html and/or
00014 email license@alhem.net.
00015 
00016 This program is free software; you can redistribute it and/or
00017 modify it under the terms of the GNU General Public License
00018 as published by the Free Software Foundation; either version 2
00019 of the License, or (at your option) any later version.
00020 
00021 This program is distributed in the hope that it will be useful,
00022 but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 GNU General Public License for more details.
00025 
00026 You should have received a copy of the GNU General Public License
00027 along with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00029 */
00030 #ifdef _WIN32
00031 #pragma warning(disable:4786)
00032 #endif
00033 #include "EventHandler.h"
00034 #include "IEventOwner.h"
00035 #include "Event.h"
00036 #include "Socket.h"
00037 #include "TcpSocket.h"
00038 #include "ListenSocket.h"
00039 
00040 
00041 #ifdef SOCKETS_NAMESPACE
00042 namespace SOCKETS_NAMESPACE {
00043 #endif
00044 
00045 
00046 EventHandler::EventHandler(StdLog *p) : SocketHandler(p), m_quit(false), m_socket(NULL)
00047 {
00048 }
00049 
00050 
00051 EventHandler::EventHandler(Mutex& m,StdLog *p) : SocketHandler(m, p), m_quit(false), m_socket(NULL)
00052 {
00053 }
00054 
00055 
00056 EventHandler::~EventHandler()
00057 {
00058         while (m_events.size())
00059         {
00060                 std::list<Event *>::iterator it = m_events.begin();
00061                 Event *e = *it;
00062                 e -> GetFrom() -> SetHandlerInvalid();
00063                 delete e;
00064                 m_events.erase(it);
00065         }
00066 }
00067 
00068 
00069 bool EventHandler::GetTimeUntilNextEvent(struct timeval *tv)
00070 {
00071         if (!m_events.size())
00072                 return false;
00073         std::list<Event *>::iterator it = m_events.begin();
00074         if (it != m_events.end())
00075         {
00076                 EventTime now;
00077                 mytime_t diff = (*it) -> GetTime() - now;
00078                 if (diff < 1)
00079                 {
00080                         diff = 1;
00081                 }
00082                 tv -> tv_sec = static_cast<long>(diff / 1000000);
00083                 tv -> tv_usec = static_cast<long>(diff % 1000000);
00084                 return true;
00085         }
00086         return false;
00087 }
00088 
00089 
00090 void EventHandler::CheckEvents()
00091 {
00092         EventTime now;
00093         std::list<Event *>::iterator it = m_events.begin();
00094         while (it != m_events.end() && (*it) -> GetTime() < now)
00095         {
00096                 Event *e = *it;
00097                 Socket *s = dynamic_cast<Socket *>(e -> GetFrom());
00098                 /*
00099                 s == NULL    This is another object implementing 'IEventOwner' and not a socket.
00100                 s != NULL    This is a Socket implementing IEventOwner, and we can check that the
00101                              object instance still is valid using SocketHandler::Valid.
00102                 */
00103                 if (!s || (s && Valid(s)))
00104                 {
00105                         e -> GetFrom() -> OnEvent(e -> GetID());
00106                         e -> GetFrom() -> Decrease();
00107                 }
00108                 delete e;
00109                 m_events.erase(it);
00110                 it = m_events.begin();
00111         }
00112 }
00113 
00114 
00115 long EventHandler::AddEvent(IEventOwner *from,long sec,long usec)
00116 {
00117         Event *e = new Event(from, sec, usec);
00118         std::list<Event *>::iterator it = m_events.begin();
00119         while (it != m_events.end() && *(*it) < *e)
00120         {
00121                 it++;
00122         }
00123         m_events.insert(it, e);
00124         if (m_socket)
00125         {
00126                 m_socket -> Send("\n");
00127         }
00128         from -> Increase();
00129         return e -> GetID();
00130 }
00131 
00132 
00133 void EventHandler::ClearEvents(IEventOwner *from)
00134 {
00135         bool repeat;
00136         do
00137         {
00138                 repeat = false;
00139                 for (std::list<Event *>::iterator it = m_events.begin(); it != m_events.end(); it++)
00140                 {
00141                         Event *e = *it;
00142                         if (e -> GetFrom() == from)
00143                         {
00144                                 delete e;
00145                                 m_events.erase(it);
00146                                 repeat = true;
00147                                 from -> Decrease();
00148                                 break;
00149                         }
00150                 }
00151         } while (repeat);
00152 }
00153 
00154 
00155 void EventHandler::EventLoop()
00156 {
00157         while (!m_quit)
00158         {
00159                 struct timeval tv;
00160                 if (GetTimeUntilNextEvent(&tv))
00161                 {
00162                         Select(&tv);
00163                         CheckEvents();
00164                 }
00165                 else
00166                 {
00167                         Select();
00168                 }
00169         }
00170 }
00171 
00172 
00173 void EventHandler::SetQuit(bool x)
00174 {
00175         m_quit = x;
00176 }
00177 
00178 
00179 void EventHandler::RemoveEvent(IEventOwner *from, long eid)
00180 {
00181         for (std::list<Event *>::iterator it = m_events.begin(); it != m_events.end(); it++)
00182         {
00183                 Event *e = *it;
00184                 if (from == e -> GetFrom() && eid == e -> GetID())
00185                 {
00186                         delete e;
00187                         m_events.erase(it);
00188                         break;
00189                 }
00190         }
00191 }
00192 
00193 
00194 void EventHandler::Add(Socket *p)
00195 {
00196         if (!m_socket)
00197         {
00198                 ListenSocket<TcpSocket> *l = new ListenSocket<TcpSocket>(*this);
00199                 l -> SetDeleteByHandler();
00200                 l -> Bind("127.0.0.1", 0);
00201                 m_port = l -> GetPort();
00202                 SocketHandler::Add(l);
00203                 m_socket = new TcpSocket( *this );
00204                 m_socket -> SetDeleteByHandler();
00205                 m_socket -> SetConnectTimeout(5);
00206                 m_socket -> SetConnectionRetry(-1);
00207 #ifdef ENABLE_RECONNECT
00208                 m_socket -> SetReconnect(true);
00209 #endif
00210                 m_socket -> Open("127.0.0.1", m_port);
00211                 SocketHandler::Add(m_socket);
00212         }
00213         SocketHandler::Add( p );
00214 }
00215 
00216 
00217 #ifdef SOCKETS_NAMESPACE
00218 }
00219 #endif
00220 
00221 
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4