summaryrefslogtreecommitdiff
path: root/src/Sockets/EventHandler.cpp
blob: 4d9034259e965ba0d96716c1207c7d61cd2d802c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/** \file EventHandler.cpp
 **	\date  2005-12-07
 **	\author grymse@alhem.net
**/
/*
Copyright (C) 2005,2007  Anders Hedstrom

This library is made available under the terms of the GNU GPL.

If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about 
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include "EventHandler.h"
#include "IEventOwner.h"
#include "Event.h"
#include "Socket.h"
#include "TcpSocket.h"
#include "ListenSocket.h"


#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif


EventHandler::EventHandler(StdLog *p) : SocketHandler(p), m_quit(false), m_socket(NULL)
{
}


EventHandler::EventHandler(Mutex& m,StdLog *p) : SocketHandler(m, p), m_quit(false), m_socket(NULL)
{
}


EventHandler::~EventHandler()
{
	while (m_events.size())
	{
		std::list<Event *>::iterator it = m_events.begin();
		Event *e = *it;
		e -> GetFrom() -> SetHandlerInvalid();
		delete e;
		m_events.erase(it);
	}
}


bool EventHandler::GetTimeUntilNextEvent(struct timeval *tv)
{
	if (!m_events.size())
		return false;
	std::list<Event *>::iterator it = m_events.begin();
	if (it != m_events.end())
	{
		EventTime now;
		mytime_t diff = (*it) -> GetTime() - now;
		if (diff < 1)
		{
			diff = 1;
		}
		tv -> tv_sec = static_cast<long>(diff / 1000000);
		tv -> tv_usec = static_cast<long>(diff % 1000000);
		return true;
	}
	return false;
}


void EventHandler::CheckEvents()
{
	EventTime now;
	std::list<Event *>::iterator it = m_events.begin();
	while (it != m_events.end() && (*it) -> GetTime() < now)
	{
		Event *e = *it;
		Socket *s = dynamic_cast<Socket *>(e -> GetFrom());
		/*
		s == NULL    This is another object implementing 'IEventOwner' and not a socket.
		s != NULL    This is a Socket implementing IEventOwner, and we can check that the
			     object instance still is valid using SocketHandler::Valid.
		*/
		if (!s || (s && Valid(s)))
		{
			e -> GetFrom() -> OnEvent(e -> GetID());
		}
		for (it = m_events.begin(); it != m_events.end(); ++it)
			if (*it == e)
				break;
		delete e;
		if (it != m_events.end())
			m_events.erase(it);
		it = m_events.begin();
	}
}


long EventHandler::AddEvent(IEventOwner *from,long sec,long usec)
{
	Event *e = new Event(from, sec, usec);
	std::list<Event *>::iterator it = m_events.begin();
	while (it != m_events.end() && *(*it) < *e)
	{
		it++;
	}
	m_events.insert(it, e);
	if (m_socket)
	{
		m_socket -> Send("\n");
	}
	return e -> GetID();
}


void EventHandler::ClearEvents(IEventOwner *from)
{
	bool repeat;
	do
	{
		repeat = false;
		for (std::list<Event *>::iterator it = m_events.begin(); it != m_events.end(); it++)
		{
			Event *e = *it;
			if (e -> GetFrom() == from)
			{
				delete e;
				m_events.erase(it);
				repeat = true;
				break;
			}
		}
	} while (repeat);
}


void EventHandler::EventLoop()
{
	while (!m_quit)
	{
		struct timeval tv;
		if (GetTimeUntilNextEvent(&tv))
		{
			Select(&tv);
			CheckEvents();
		}
		else
		{
			Select();
		}
	}
}


void EventHandler::SetQuit(bool x)
{
	m_quit = x;
}


void EventHandler::RemoveEvent(IEventOwner *from, long eid)
{
	for (std::list<Event *>::iterator it = m_events.begin(); it != m_events.end(); it++)
	{
		Event *e = *it;
		if (from == e -> GetFrom() && eid == e -> GetID())
		{
			delete e;
			m_events.erase(it);
			break;
		}
	}
}


void EventHandler::Add(Socket *p)
{
	if (!m_socket)
	{
		ListenSocket<TcpSocket> *l = new ListenSocket<TcpSocket>(*this);
		l -> SetDeleteByHandler();
		l -> Bind("127.0.0.1", 0);
		m_port = l -> GetPort();
		SocketHandler::Add(l);
		m_socket = new TcpSocket( *this );
		m_socket -> SetDeleteByHandler();
		m_socket -> SetConnectTimeout(5);
		m_socket -> SetConnectionRetry(-1);
#ifdef ENABLE_RECONNECT
		m_socket -> SetReconnect(true);
#endif
		m_socket -> Open("127.0.0.1", m_port);
		SocketHandler::Add(m_socket);
	}
	SocketHandler::Add( p );
}


#ifdef SOCKETS_NAMESPACE
}
#endif