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
|
/*@@
@file Startup.c
@date Wed Sep 13 21:26:56 2000
@author Tom Goodale
@desc
Scheduled routines for the HTTPD thorn.
@enddesc
@version $Header$
@@*/
#include "cctk.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef CCTK_PTHREADS
#include <pthread.h>
#endif
#include "cctk_Arguments.h"
#include "cctk_Parameters.h"
#include "httpd.h"
#include "Steer.h"
#include "Redirect.h"
static const char *rcsid = "$Header$";
CCTK_FILEVERSION(CactusConnect_HTTPD_Startup_c);
/********************************************************************
********************* Local Data Types ***********************
********************************************************************/
/********************************************************************
********************* Local Routine Prototypes *********************
********************************************************************/
#ifdef CCTK_PTHREADS
static void * HTTP_Thread(void *cctkGH);
static void HTTP_SetupPollingThread(cGH *cctkGH);
#endif
/********************************************************************
***************** Scheduled Routine Prototypes *********************
********************************************************************/
int HTTP_StartServer(void);
int HTTP_FirstServ(void);
void HTTP_Work(CCTK_ARGUMENTS);
int HTTP_Shutdown(void);
/********************************************************************
********************* Other Routine Prototypes *********************
********************************************************************/
/********************************************************************
********************* Local Data *****************************
********************************************************************/
#ifdef CCTK_PTHREADS
static pthread_t polling_thread;
static int thread_started = 0;
#endif
/********************************************************************
********************* External Routines **********************
********************************************************************/
/*@@
@routine HTTP_StartServer
@date Wed Sep 13 21:26:56 2000
@author Tom Goodale
@desc
Starts the webserver.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
int HTTP_StartServer(void)
{
int use_port;
char *environ_port;
DECLARE_CCTK_PARAMETERS
#ifndef CCTK_PTHREADS
if (use_pthreads)
{
CCTK_WARN (1, "Parameter 'HTTPD::use_pthreads' is set to \"yes\" but you "
"didn't configure with PTHREADS. Setting will be ignored.");
}
#endif
/* get HTTPD port to use from the 'port' parameter or the shell environment */
use_port = port;
environ_port = getenv ("HTTPD_PORT");
if (environ_port)
{
use_port = atoi (environ_port);
if (use_port >= 1 && use_port <= 65535)
{
CCTK_VInfo (CCTK_THORNSTRING, "Environment variable HTTPD_PORT is set to "
"%d and will override parameter setting from 'HTTPD::port'",
use_port);
}
else
{
CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
"Environment variable HTTPD_PORT is set to '%s' which cannot "
"be parsed as a valid port number. Using parameter setting "
"'HTTPD::port = %d' instead.", environ_port, (int) port);
use_port = port;
}
}
if(CCTK_MyProc(NULL) == 0)
{
/* Does the server provide any pages by default ? */
if(provide_pages)
{
HTTP_RegisterPages();
}
HTTP_SetupServer(use_port, queue_length, hunt);
}
/* Do we need to redirect to a master server ? */
HTTP_SetupRedirect(use_port,queue_length,hunt);
return 0;
}
/*@@
@routine HTTP_FirstServ
@date Tue Nov 12 20:32:36 2002
@author Tom Goodale
@desc
Listen for connection requests.
@enddesc
@calls
@calledby
@history
@hdate Tue Nov 12 20:33:07 2002
@hauthor Tom Goodale
@hdesc This was originally in HTTP_StartServer but
that made it impossible to spawn a task telling
it where the webserver was if httpd::pause was "true".
@endhistory
@@*/
int HTTP_FirstServ(void)
{
httpState state;
HTTP_UpdateState(NULL, &state);
do
{
if(HTTP_IsServer())
{
HTTP_Poll(NULL, state.timeout_seconds, state.timeout_useconds);
}
if(state.steer == 1)
{
HTTP_SteerDispatch();
HTTP_UpdateState(NULL, &state);
}
if(state.terminate)
{
HTTP_Terminate(NULL);
}
} while(state.paused);
return 0;
}
/*@@
@routine HTTP_Work
@date Wed Sep 13 21:26:56 2000
@author Tom Goodale
@desc
Main working routine for the webserver.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
void HTTP_Work(CCTK_ARGUMENTS)
{
DECLARE_CCTK_ARGUMENTS
DECLARE_CCTK_PARAMETERS
httpState state;
#ifdef CCTK_PTHREADS
if(HTTP_IsServer() && use_pthreads && ! thread_started)
{
HTTP_SetupPollingThread(cctkGH);
}
#endif
HTTP_UpdateState(cctkGH, &state);
do
{
#ifdef CCTK_PTHREADS
if(!use_pthreads)
{
#endif
if(HTTP_IsServer())
{
HTTP_Poll(cctkGH, state.timeout_seconds, state.timeout_useconds);
}
#ifdef CCTK_PTHREADS
}
#endif
if(state.steer == 1)
{
HTTP_SteerDispatch();
HTTP_UpdateState(cctkGH, &state);
}
if(state.terminate)
{
HTTP_Terminate(cctkGH);
}
} while(state.paused);
}
/*@@
@routine HTTP_Shutdown
@date Wed Sep 13 21:26:56 2000
@author Tom Goodale
@desc
Shutdown routine for the webserver.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
int HTTP_Shutdown(void)
{
DECLARE_CCTK_PARAMETERS
#ifdef CCTK_PTHREADS
/* Wait for the polling thread to exit */
if(HTTP_IsServer() && use_pthreads && thread_started)
{
CCTK_ParameterSet("terminate", CCTK_THORNSTRING, "yes");
pthread_join(polling_thread, NULL);
}
#endif
if(HTTP_IsServer())
{
HTTP_ShutdownServer();
}
return 0;
}
/********************************************************************
********************* Local Routines *************************
********************************************************************/
/*@@
@routine HTTP_Thread
@date Wed Oct 25 17:04:59 2000
@author Tom Goodale
@desc
The http thread routine.
@enddesc
@calls
@calledby
@history
@endhistory
@var cctkGH
@vdesc The cGH
@vtype cGH *
@vio inout
@vcomment
@endvar
@@*/
#ifdef CCTK_PTHREADS
static void * HTTP_Thread(void *cctkGH)
{
CCTK_INT terminate;
int type;
do
{
/* Only poll for a few seconds in case we are terminated */
HTTP_Poll((cGH *)cctkGH, 10, 0);
terminate = *(const CCTK_INT *)CCTK_ParameterGet("terminate",CCTK_THORNSTRING, &type);
} while(! terminate);
return NULL;
}
/*@@
@routine HTTP_SetupPollingThread
@date Wed Oct 25 17:05:38 2000
@author Tom Goodale
@desc
Sets up the http polling thread.
@enddesc
@calls
@calledby
@history
@endhistory
@var cctkGH
@vdesc The cGH
@vtype cGH *
@vio inout
@vcomment
@endvar
@@*/
static void HTTP_SetupPollingThread(cGH *cctkGH)
{
if(pthread_create(&polling_thread, NULL, HTTP_Thread, (void *)cctkGH))
{
perror("pthread_create: ");
CCTK_Exit(cctkGH,99);
}
else
{
thread_started = 1;
}
}
#endif /* CCTK_PTHREADS */
|