-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoloWeb.c
420 lines (342 loc) · 12.7 KB
/
BoloWeb.c
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//
// BoloWeb v0.8
// ©1996 Pavani Diwanji <pavani@CS.Stanford.EDU>
//
// BoloWeb is freeware. Providing that you acknowledge my authorship,
// you may distribute it any way you like, or do anything else with it,
// including producing derivative works from the source code.
//
// This application is the server application which receives input queries from
// the HTTP server, gets the most current bolo game list from the BoloTracker and
// and hands back to HTTP server a bolo game list in HTML format.
// Note: This is a background application that gets launched when the first
// query is received from the Mosaic client.
// As per the rules of background applications, the following care is taken:
// - Its compeltely transperent with no ui. The only way to see it when running
// is to use the process manager / taskit.
// - Between events, the sleep/nap time is very high so it does not hog the
// system resources.
// - Use of stack space is minimal - heap space is used mainly for queries.
// SIZE resource: canBackground and onlyBackground bits are set.
// the application is divided up into two main files which contain group of routines:
// 1. Stuart's utility routines that do name resolution
// 2. Debbugging routines
// 3. BoloTracker output parsing and building of intermediate rep game list.
// 4. Routiens that do TCP connectivity to Bolo tracker
// 5. Apple Event routines that receive the apple events from HTTP server
// and send a reply back.
#include <stdio.h> // for vsprintf
#include <stdarg.h> // for variable argument lists
#include <string.h> // for strlen
#include "my_types.h"
#include "BoloWeb.h"
#include "InputParser.h"
#include "html.h"
Boolean quitting = FALSE;
// We go to the tracker only every n sec where n is the update interval
// here it is set to 1 minute.
#define UPDATE_INTERVAL 60
#define MAX_HTML 65536
// Under normal circustances construct the html: 64k html buffer
local char *html;
local int html_index = 0;
unsigned long last_update=0;
#define kGetHTTPLEventClass 'WWW�'
#define kGetHTTPLEventID 'sdoc'
export void do_memcheck(void)
{
Ptr p = NewPtr(4);
if (p) DisposePtr(p);
}
// stuart's debug utility
export void debug(char *format, ...)
{
u_char buffer[256];
va_list ptr;
va_start(ptr,format);
buffer[0] = vsprintf((char *)buffer+1, format, ptr);
va_end(ptr);
DebugStr(buffer);
}
// Mustn't write more than 4K at a time
// index is index into the html buffer
local Boolean WriteHTML(int *index, const char *format, ...)
{
Boolean result = true;
va_list ptr;
va_start(ptr,format);
if (*index > MAX_HTML - PageTrailerSize - 4096) result = false; // Not enough space in the buffer
else *index += vsprintf(&html[*index], format, ptr);
va_end(ptr);
return result;
}
local Boolean WriteTrailerHTML(int *index, const char *format, Str255 lut, Str255 lud)
{
int incr;
if (((*index) + PageTrailerSize) > MAX_HTML)
return false; // Not enough space in the buffer
// Else put it in - here we use sprintf as lut and lud are pascal style strings
if ((incr = sprintf(html+(*index), format, lut, lud)) != EOF)
(*index) += incr;
else return false;
return true;
}
// Initalize the html buffer with constant header - so we do not have to
// do it on every call
local Boolean InitHTML()
{
html_index = 0;
PageTrailerSize = strlen(PageTrailer) + 1 + 2*sizeof(Str255);
if (WriteHTML(&html_index, HTTPHeader) && WriteHTML(&html_index, PageHeader)) {
return true;
}
return false;
}
// Write the trailer html
// Convert the game info list into html and put the resultant html into the buffer
local Boolean ConvertToHTML(GameInfoList *gil)
{
GameInfoList *curr = NULL;
Str255 last_update_date;
Str255 last_update_time;
short i=0;
// HTML content skeleton for every game
static const char GameHTML [] =
"<h3><img alt=\"o\" src=\"images/MapIcon.gif\" width=32 height=16> %s \r\n"
"(v 0.%ld.%ld %s) %s %s </h3> \r\n"
"<h4>\r\n"
"<DD><img alt=\"o\" src=\"images/tankgrn.gif\" width=16 height=12> Players %d\r\n"
"<DD><img alt=\"o\" src=\"images/Pillbox.gif\" width=16 height=16> PillBoxes %d\r\n"
"<DD><img alt=\"o\" src=\"images/NeutBase.gif\" width=16 height=16> Neutral Bases %d\r\n"
"<DD><img alt=\"o\" src=\"images/%s\" width=16 height=16> Hidden Mines %s\r\n"
"<DD><img alt=\"o\" src=\"images/%s\" width=18 height=16> Robots %s\r\n";
static const char SitesStart [] = "";
static const char JoinSiteHTML [] = " <DD><img alt=\"o\" src=\"images/Site.gif\" width=12 height=15> <a href=\"%s\">%s</a> (%s) \r\n";
static const char SiteHTML [] = " <DD><img alt=\"o\" src=\"images/Site.gif\" width=12 height=15> %s (%s) \r\n";
static const char SitesEnd [] = "</h4>\r\n";
static const char GameEnd[] = "<HR>";
static const char Exclaim [] = "<img alt=\"o\" src=\"images/Exclamation.gif\" width=16 height=16>";
IUTimeString(last_update, TRUE, last_update_time);
IUDateString(last_update, TRUE, last_update_date);
// Intialize the buffer
if (!InitHTML())
{ debug("cannot initialize html"); return false; }
// construct the HTML format for the Game List elements.
// Use standard 64k html buffer - truncate the html buffer if the all the games
// cannot fit in
// Put the html for all the games in the html buffer
curr = gil;
while (curr) {
char *mines = (curr->hidden_mines)?"Allowed":"Not Allowed";
char *mines_icon = (curr->hidden_mines)?"HiddenMine.gif":"NoHiddenMine.gif";
char *robots = (curr->robots)?"Allowed":"Not Allowed";
char *robots_icon = (curr->robots)?"Brain.gif":"NoBrain.gif";
const char *pwd = (curr->passwd[0]!=NULL)?Exclaim:"";
int old_html_index = html_index;
// Write in basic game information
if (!WriteHTML(&html_index, GameHTML,
curr->map_name, (curr->vers)>>8, (curr->vers)&0xff, curr->game_type, pwd, curr->passwd,
curr->num_players, curr->num_pillboxes, curr->num_bases,
mines_icon, mines, robots_icon, robots))
{ html_index = old_html_index; break; }
// if any sites listed
if (curr->num_sites>0) {
if (!WriteHTML(&html_index, SitesStart))
{ html_index = old_html_index; break; }
for (i=0; i<curr->num_sites; ++i) {
if (curr->vers>=WEBAWARE_BOLO_VERSION) {
if (!WriteHTML(&html_index, JoinSiteHTML,
curr->ip_addr[i], // curr->port[i],
curr->ip_addr[i], curr->place[i]))
{ html_index = old_html_index; break; }
} else {
if (!WriteHTML(&html_index, SiteHTML,
curr->ip_addr[i], curr->place[i]))
{ html_index = old_html_index; break; }
}
}
if (!WriteHTML(&html_index, SitesEnd))
{ html_index = old_html_index; break; }
}
// put in games end marker
if (!WriteHTML(&html_index, GameEnd))
{ html_index = old_html_index; break; }
curr = curr->next;
}
if (!WriteTrailerHTML(&html_index, PageTrailer, last_update_date, last_update_time))
{ debug("Cannot put the trailer in -error!!"); return false;}
return true;
}
// ****** Code to cope with required Apple Events ********
local OSErr MoreParameters(const AppleEvent *e)
{
DescType returnedType;
Size actualSize;
OSErr err;
//debug("in MoreParameters");
memcheck();
err = AEGetAttributePtr(e, keyMissedKeywordAttr, typeWildCard,
&returnedType, NULL, 0, &actualSize);
if (err != errAEDescNotFound) return (errAEParamMissed); else return(noErr);
}
local pascal OSErr MyHandleOApp(const AppleEvent *e, AppleEvent *r, long Refcon)
{
OSErr err;
r;
Refcon;
//debug("in MyHandleOApp");
memcheck();
err = MoreParameters(e);
return(err);
}
local pascal OSErr MyHandleODoc(const AppleEvent *e, AppleEvent *r, long Refcon)
{
//FSSpec myFSS;
AEDescList docList;
OSErr myErr;
//long index;
//long itemsInList;
//Size actualSize;
//AEKeyword keywd;
//DescType returnedType;
r;
Refcon;
e;
//debug("in MyHandleODoc");
memcheck();
myErr = AEGetParamDesc(e, keyDirectObject, typeAEList, &docList);
if (myErr) return(myErr);
myErr = MoreParameters(e);
if (myErr) { AEDisposeDesc(&docList); return(myErr); }
AEDisposeDesc(&docList);
return(noErr);
}
local pascal OSErr MyHandlePDoc(const AppleEvent *e, AppleEvent *r, long Refcon)
{
e;
r;
Refcon;
//debug("in MyHandlePDoc");
memcheck();
return(errAENoUserInteraction);
}
local pascal OSErr MyHandleQuit(const AppleEvent *e, AppleEvent *r, long Refcon)
{
OSErr err;
r;
Refcon;
//debug("in MyHandleQuit");
memcheck();
err = MoreParameters(e);
if (err) return(err);
quitting = TRUE;
return(noErr);
}
// This routine receives the Apple event URL, gets the most current Bolo List from
// the BoloTracker and presents it in HTML format.
local pascal OSErr MyHandleHTTP(const AppleEvent *event, AppleEvent *reply, long Refcon)
{
OSErr err = noErr;
unsigned long current_time;
GameInfoList *new_gamelist = NULL;
event;
Refcon;
// make sure we do not contact the bolo tracker on every request.
// Only every UPDATE_INTERVAL sec we send out for update...
//debug("Starting MyHandleHTTP");
memcheck();
GetDateTime(¤t_time);
if (current_time-last_update>=UPDATE_INTERVAL){
//debug("Before GetGameList");
memcheck();
// get a new game list
new_gamelist = GetGameList();
//debug("After building game list");
memcheck();
// If there is an error contacting the bolo tracker
// display the old html with corresponding timestamp.
// If we do manage to get a new game info list,
// then construct the new html and dispose the game list, once we have
// html output
if (new_gamelist!=NULL) {
last_update = current_time;
// Put the new games information in HTML format
ConvertToHTML(new_gamelist);
//debug("After converting new game list");
memcheck();
if (new_gamelist) {
DisposeGameList(new_gamelist); new_gamelist = NULL;
//debug("After disposing new game list");
memcheck();
}
}
}
// Give the resultant html back to HTTP server.
if (!html_index) err = AEPutParamPtr(reply, keyDirectObject, typeChar, Errorhtml, strlen(Errorhtml));
else err = AEPutParamPtr(reply, keyDirectObject, typeChar, html, html_index);
//debug("After returning new html reply");
memcheck();
if (err != noErr) return err;
return(noErr);
}
void main(void)
{
EventRecord e;
GameInfoList *new_gamelist = NULL;
//debug("Starting up");
//debug("Starting up; hs; g;");
// Initialize the Quickdraw globals - needed by Apple Events Manager
// Really?
InitGraf(&qd.thePort);
SetApplLimit(GetApplLimit() - 0x8000); // Add 32K to the stack
MaxApplZone();
// debug("After MaxApplZone");
memcheck();
MoreMasters();
// debug("After MoreMasters");
memcheck();
// Flush any events already in queue
FlushEvents(everyEvent, 0);
// printf("Starting up\n");
AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, MyHandleOApp, 0, FALSE);
AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, MyHandleODoc, 0, FALSE);
AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, MyHandlePDoc, 0, FALSE);
AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, MyHandleQuit, 0, FALSE);
AEInstallEventHandler(kGetHTTPLEventClass, kGetHTTPLEventID, MyHandleHTTP, 0, FALSE);
// debug("After AEInstallEventHandler");
memcheck();
html = NewPtr(MAX_HTML);
if (!html) { debug("failed to initialize - out of memory?"); return; }
// debug("After NewPtr");
memcheck();
// Initalize the last_update with current time - UPDATE_INTERVAL
GetDateTime(&last_update); last_update = last_update - UPDATE_INTERVAL;
// debug("After GetDateTime");
memcheck();
// Initalise the input parser
if (InitGameList() != noErr) { debug("failed to initialize - out of memory?"); return; }
// debug("Entering event loop");
memcheck();
// Loop around and process all the events that come by until the application
// received a quit event.
do
{
// Loop until there is an event
// the large parameter 0xffffffff tells the system I do not need to receive
// the null events
if (WaitNextEvent(everyEvent, &e, 1000, NULL))
{
switch (e.what)
{
case mouseDown: case mouseUp: break;
case keyDown: case keyUp: case autoKey: break;
case updateEvt: BeginUpdate((WindowPtr)e.message); EndUpdate((WindowPtr)e.message); break;
case activateEvt: break;
case osEvt: break; // Ignore; may be suspend, resume, convert clip, etc.
case kHighLevelEvent: AEProcessAppleEvent(&e); break;
default: debug("Got unexpected event %d", e.what); break;
}
}
} while (!quitting);
return;
}