~pbms-core/pbms/5.11-beta

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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * 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
 *
 * Original author: Paul McCullagh
 * Continued development: Barry Leslie
 *
 * 2007-05-25
 *
 * H&G2JCtL
 *
 * Network interface.
 *
 */

#include "CSConfig.h"
#include "CSGlobal.h"
#include "CSLog.h"

#include "Network_ms.h"
#include "ConnectionHandler_ms.h"
#include "Util_ms.h"

MSSystemThread		*MSNetwork::gSystemThread;
time_t				MSNetwork::gCurrentTime;
time_t				MSNetwork::gLastService;
CSThreadList		*MSNetwork::gHandlerList;
CSSync				MSNetwork::gListenerLock;
CSSocket			*MSNetwork::gListenerSocket;
MSConnectionHandler	*MSNetwork::gListenerThread;
u_int				MSNetwork::gWaitingToListen;
int					MSNetwork::handlerCount;

/*
 * -------------------------------------------------------------------------
 * SYSTEM THREAD
 */

bool MSSystemThread::doWork()
{
	bool	killed = true;

	enter_();
	MSNetwork::gCurrentTime = time(NULL);
	if ((MSNetwork::gCurrentTime - MSNetwork::gLastService) >= (MS_IDLE_THREAD_TIMEOUT/2)) {
		MSNetwork::gLastService = MSNetwork::gCurrentTime;
		while (!myMustQuit && killed) {
			killed = MSNetwork::killListener();
			MSNetwork::gCurrentTime = time(NULL);
		}
	}
	return_(true);
}

/*
 * -------------------------------------------------------------------------
 * NETWORK FUNCTIONS
 */

void MSNetwork::startUp(int port)
{
	enter_();
	gCurrentTime = time(NULL);
	gLastService = gCurrentTime;
	gListenerSocket = NULL;
	handlerCount = 0;
	
	CSL.lock();
	CSL.log(self, CSLog::Protocol, "Media Stream Daemon ");
	if (port) {
	CSL.log(self, CSLog::Protocol, " listening on port ");
	CSL.log(self, CSLog::Protocol, port);
	} else
		CSL.log(self, CSLog::Protocol, " not published ");
	CSL.log(self, CSLog::Protocol, "\n");
	CSL.unlock();

	new_(gHandlerList, CSThreadList());
	if (port) {
	gListenerSocket = CSSocket::newSocket();
	gListenerSocket->publish(NULL, port);
	} else 
		gListenerSocket = NULL;

	new_(gSystemThread, MSSystemThread(1000 /* 1 sec */, NULL));
	gSystemThread->start();
	exit_();
}

void MSNetwork::shutDown()
{
	enter_();

	if (gSystemThread) {
		gSystemThread->stop();
		gSystemThread->release();
		gSystemThread = NULL;
	}

	/* This will set all threads to quiting: */
	if (gHandlerList)
		gHandlerList->quitAllThreads();

	/* Close the socket: */
	if (gListenerThread)
		gListenerThread->shuttingDown = true; // Block error messages as a result of the listener being killed
	
	lock_(&gListenerLock);
	if (gListenerSocket) {
		try_(a) {
			gListenerSocket->release();
		}
		catch_(a) {
			self->logException();
		}
		cont_(a);
	}
	gListenerSocket = NULL;
	unlock_(&gListenerLock);

	if (gHandlerList) {
		try_(b) {
			/* This will stop any threads remaining: */
			gHandlerList->release();
		}
		catch_(b) {
			self->logException();
		}
		cont_(b);
	}

	CSL.log(self, CSLog::Protocol, "PrimeBase Media Stream Daemon no longer published\n");
	exit_();
}

void MSNetwork::startConnectionHandler()
{
	char				buffer[120];
	MSConnectionHandler	*thread;

	enter_();
	handlerCount++;
	snprintf(buffer, 120, "NetworkHandler%d", handlerCount);
	lock_(gHandlerList);
	thread = MSConnectionHandler::newHandler(MSNetwork::gHandlerList);
	unlock_(gHandlerList);
	push_(thread);
	thread->threadName = CSString::newString(buffer);
	thread->start();
	release_(thread);
	exit_();
}

/*
 * Return NULL of a connection cannot be openned, and the
 * thread must quit.
 */
CSSocket *MSNetwork::openConnection(MSConnectionHandler *handler)
{
	CSSocket *sock;

	enter_();
	
	if(!MSNetwork::gListenerSocket) {
		return NULL;
	}
	
	sock = CSSocket::newSocket();
	push_(sock);

	/* Wait for a connection: */
	if (!lockListenerSocket(handler)) {
		release_(sock);
		return_(NULL);
	}

	try_(a) {
		sock->open(MSNetwork::gListenerSocket);
	}
	catch_(a) {
		unlockListenerSocket();
		throw_();
	}
	cont_(a);

	handler->lastUse = gCurrentTime;

	unlockListenerSocket();

	pop_(sock);
	return_(sock);
}

void MSNetwork::startNetwork()
{
	enter_();
	startConnectionHandler();
	exit_();
}

bool MSNetwork::lockListenerSocket(MSConnectionHandler *handler)
{
	bool socket_locked = false;

	enter_();
	if (handler->myMustQuit)
		return false;
	lock_(&gListenerLock);
	if (gListenerSocket) {
		/* Wait for the listen socket to be freed: */
		if (gListenerThread) {
			gWaitingToListen++;
			handler->amWaitingToListen = true;
			while (gListenerThread) {
				if (handler->myMustQuit)
					break;
				try_(a) {
					gListenerLock.wait(2000);
				}
				catch_(a) {
					/* Catch any error */;
				}
				cont_(a);
			}
			gWaitingToListen--;
			handler->amWaitingToListen = false;
		}
		if (!handler->myMustQuit) {
			gListenerThread = handler;
			socket_locked = true;
		}
	}
	unlock_(&gListenerLock);
	return_(socket_locked);
}

void MSNetwork::unlockListenerSocket()
{
	enter_();
	lock_(&gListenerLock);
	gListenerThread = NULL;
	gListenerLock.wakeup();
	unlock_(&gListenerLock);
	exit_();
}

/* Kill a listener if possible!
 * Return true if a thread was killed.
 */
bool MSNetwork::killListener()
{
	MSConnectionHandler	*ptr = NULL;

	enter_();
	lock_(&gListenerLock);
	if (gListenerThread && gWaitingToListen > 0) {
		/* Kill one: */
		lock_(gHandlerList);
		ptr = (MSConnectionHandler *) gHandlerList->getBack();
		while (ptr) {
			if (ptr->amWaitingToListen) {
				if (gCurrentTime > ptr->lastUse && (gCurrentTime - ptr->lastUse) > MS_IDLE_THREAD_TIMEOUT) {
					ptr->myMustQuit = true;
					ptr->wakeup();
					break;
				}
			}
			ptr = (MSConnectionHandler *) ptr->getNextLink();
		}
		unlock_(gHandlerList);
	}
	unlock_(&gListenerLock);
	if (ptr) {
		ptr->join();
		return_(true);
	}
	return_(false);
}