summaryrefslogtreecommitdiff
path: root/src/manager/lib/dispatcher.c
blob: df669ceb6f86c83bbefca7dd4407d4b5a8c2df37 (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
 * @file dispatcher.c
 *
 * @brief Implementation of dispatcher_t.
 *
 */

/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include "dispatcher.h"

#include "request.h"
#include "session.h"

#include <fcgiapp.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>

#include <utils/linked_list.h>

typedef struct private_dispatcher_t private_dispatcher_t;

/**
 * private data of the task manager
 */
struct private_dispatcher_t {

	/**
	 * public functions
	 */
	dispatcher_t public;
	
	/**
	 * fcgi socket fd
	 */
	int fd;
	
	/**
	 * thread list
	 */
	pthread_t *threads;
	
	/**
	 * number of threads in "threads"
	 */
	int thread_count;
	
	/**
	 * session locking mutex
	 */
	pthread_mutex_t mutex;
	
	/**
	 * List of sessions
	 */
	linked_list_t *sessions;
	
	/**
	 * session timeout
	 */
	time_t timeout;
	
	/**
	 * List of controllers controller_constructor_t
	 */
	linked_list_t *controllers;
	
	/** 
	 * constructor function to create session context (in constructor_entry_t)
	 */
	context_constructor_t context_constructor;
	
	/**
	 * user param to context constructor
	 */
	void *param;
	
	/**
	 * thread specific initialization handler
	 */
	void (*init)(void *param);
	
	/**
	 * argument to pass to thread intiializer
	 */
	void *init_param;
	
	/**
	 * thread specific deinitialization handler
	 */
	void (*deinit)(void *param);
	
	/**
	 * param tho thread specific deinitialization handler
	 */
	void *deinit_param;
};

typedef struct {
	/** constructor function */
	controller_constructor_t constructor;
	/** parameter to constructor */
	void *param;
} constructor_entry_t;

typedef struct {
	/** session instance */
	session_t *session;
	/** condvar to wait for session */
	pthread_cond_t cond;
	/** number of threads waiting for session */
	int waiting;
	/** last use of the session */
	time_t used;
} session_entry_t;

/**
 * create a session and instanciate controllers
 */
static session_t* load_session(private_dispatcher_t *this)
{
	iterator_t *iterator;
	constructor_entry_t *entry;
	session_t *session;
	context_t *context = NULL;
	controller_t *controller;
	
	if (this->context_constructor)
	{
		context = this->context_constructor(this->param);
	}
	session = session_create(context);
	
	iterator = this->controllers->create_iterator(this->controllers, TRUE);
	while (iterator->iterate(iterator, (void**)&entry))
	{
		controller = entry->constructor(context, entry->param);
		session->add_controller(session, controller);
	}
	iterator->destroy(iterator);
	
	return session;
}

/**
 * create a new session entry
 */
static session_entry_t *session_entry_create(private_dispatcher_t *this)
{
	session_entry_t *entry;
	
	entry = malloc_thing(session_entry_t);
	entry->waiting = 1;
	pthread_cond_init(&entry->cond, NULL);
	entry->session = load_session(this);
	entry->used = time(NULL);
	
	return entry;
}

static void session_entry_destroy(session_entry_t *entry)
{
	entry->session->destroy(entry->session);
	free(entry);
}

/**
 * Implementation of dispatcher_t.add_controller.
 */
static void add_controller(private_dispatcher_t *this,
						   controller_constructor_t constructor, void *param)
{
	constructor_entry_t *entry = malloc_thing(constructor_entry_t);
	
	entry->constructor = constructor;
	entry->param = param;
	this->controllers->insert_last(this->controllers, entry);
}

/**
 * Actual dispatching code 
 */
static void dispatch(private_dispatcher_t *this)
{
	FCGX_Request fcgi_req;
	
	if (FCGX_InitRequest(&fcgi_req, this->fd, 0) == 0)
	{
		while (TRUE)
		{
			request_t *request;
			session_entry_t *current, *found = NULL;
			iterator_t *iterator;
			time_t now;
			char *sid;
			int accepted;
			
			pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
			accepted = FCGX_Accept_r(&fcgi_req);
			pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
			
			if (accepted != 0)
			{
				break;
			}
			
			/* prepare */
			request = request_create(&fcgi_req, TRUE);
			if (request == NULL)
			{
				continue;
			}
			sid = request->get_cookie(request, "SID");
			now = time(NULL);
			
			/* find session */
			iterator = this->sessions->create_iterator_locked(this->sessions, &this->mutex);
			while (iterator->iterate(iterator, (void**)&current))
			{
				/* check all sessions for timeout */
				if (current->waiting == 0 &&
					current->used < now - this->timeout)
				{
					iterator->remove(iterator);
					session_entry_destroy(current);
					continue;
				}
				if (!found && sid &&
					streq(current->session->get_sid(current->session), sid))
				{
					found = current;
					found->waiting++;
				}
			}
			iterator->destroy(iterator);
			
			if (found)
			{	/* wait until session is unused */
				pthread_mutex_lock(&this->mutex);
				while (found->waiting > 1)
				{
					pthread_cond_wait(&found->cond, &this->mutex);
				}
				pthread_mutex_unlock(&this->mutex);
			}
			else
			{	/* create a new session if not found */
				found = session_entry_create(this);
				pthread_mutex_lock(&this->mutex);
				this->sessions->insert_first(this->sessions, found);
				pthread_mutex_unlock(&this->mutex);
			}
		
			/* start processing */
			found->session->process(found->session, request);
			found->used = time(NULL);
			
			/* release session */
			pthread_mutex_lock(&this->mutex);
			found->waiting--;
			pthread_cond_signal(&found->cond);
			pthread_mutex_unlock(&this->mutex);
			
			/* cleanup */
			request->destroy(request);
			
			/*
		    FCGX_FPrintF(fcgi_req.out, "<ul>");
		    char **env = fcgi_req.envp;
		    while (*env)
		    {
		        FCGX_FPrintF(fcgi_req.out, "<li>%s</li>", *env);
		        env++;
		    }
		    FCGX_FPrintF(fcgi_req.out, "</ul>");
		    */
		}
	}
}

/**
 * Setup thread and start dispatching 
 */
static void start_dispatching(private_dispatcher_t *this)
{
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
	if (this->init)
	{
		this->init(this->init_param);
	}
	if (this->deinit)
	{
		pthread_cleanup_push(this->deinit, this->deinit_param);
		dispatch(this);
		pthread_cleanup_pop(1);
	}
	else
	{
		dispatch(this);
	}
}

/**
 * Implementation of dispatcher_t.run.
 */
static void run(private_dispatcher_t *this, int threads,
				void(*init)(void *param), void *init_param,
				void(*deinit)(void *param), void *deinit_param)
{
	this->init = init;
	this->init_param = init_param;
	this->deinit = deinit;
	this->deinit_param = deinit_param;
	this->thread_count = threads;
	this->threads = malloc(sizeof(pthread_t) * threads);
	while (threads)
	{
		if (pthread_create(&this->threads[threads - 1],
						   NULL, (void*)start_dispatching, this) == 0)
		{
			threads--;
		}
	}
}

/**
 * Implementation of dispatcher_t.waitsignal.
 */
static void waitsignal(private_dispatcher_t *this)
{
	sigset_t set;
	int sig;
	
	sigemptyset(&set);
	sigaddset(&set, SIGINT);
	sigaddset(&set, SIGTERM);
	sigaddset(&set, SIGHUP);
	sigprocmask(SIG_BLOCK, &set, NULL);
	sigwait(&set, &sig);
}

/**
 * Implementation of dispatcher_t.destroy
 */
static void destroy(private_dispatcher_t *this)
{
	FCGX_ShutdownPending();
	while (this->thread_count--)
	{
		pthread_cancel(this->threads[this->thread_count]);
		pthread_join(this->threads[this->thread_count], NULL);
	}
	this->sessions->destroy_function(this->sessions, (void*)session_entry_destroy);
	this->controllers->destroy_function(this->controllers, free);
	free(this);
}

/*
 * see header file
 */
dispatcher_t *dispatcher_create(char *socket, int timeout,
								context_constructor_t constructor, void *param)
{
	private_dispatcher_t *this = malloc_thing(private_dispatcher_t);

	this->public.add_controller = (void(*)(dispatcher_t*, controller_constructor_t, void*))add_controller;
	this->public.run = (void(*)(dispatcher_t*, int threads,void(*)(void *),void *,void(*)(void *),void *))run;
	this->public.waitsignal = (void(*)(dispatcher_t*))waitsignal;
	this->public.destroy = (void(*)(dispatcher_t*))destroy;
	
	this->sessions = linked_list_create();
	this->controllers = linked_list_create();
	this->context_constructor = constructor;
	pthread_mutex_init(&this->mutex, NULL);
	this->param = param;
    this->fd = 0;
    this->timeout = timeout;
	
    FCGX_Init();
    
    if (socket)
    {
		unlink(socket);
		this->fd = FCGX_OpenSocket(socket, 10);
	}
	return &this->public;
}