File: c-ch2.htm

package info (click to toggle)
aolserver 3.4.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 22,692 kB
  • ctags: 33,612
  • sloc: ansic: 171,340; tcl: 10,218; sh: 3,821; cpp: 2,779; makefile: 2,041; yacc: 1,648; perl: 456; php: 13
file content (456 lines) | stat: -rw-r--r-- 22,605 bytes parent folder | download | duplicates (2)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<HTML><HEAD>
<TITLE>C API Reference  -- C Interface Examples</TITLE>
<LINK rel=Previous href="c-ch1.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="c-ch3.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
  <TR>
    <TD ALIGN=LEFT>
      <A NAME="topofpage"></A> <IMG  SRC="as-c-sm.gif">
    </TD>
    <TD ALIGN=RIGHT>
      <A href="c-ch1.htm"><IMG  BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
      <A href=toc.htm> <IMG  BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
      <A href=master.htm> <IMG  BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
      <A href="c-ch3.htm"> <IMG  BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
      <A name="7983"> </A>
    </TD>
  </TR>
</TABLE>

<a name="4422">
</a><h3>C Interface Examples</h3>
<p><a name="4424">
</a>The following examples provide an introduction to the AOLserver C API. More examples are provided in the "<a href="c-app.htm#3527">C Examples</a>" appendix on <a href="c-app.htm#3527">page 485</a>.</p>
<a name="4426">
</a><h4>"Hello World" in AOLserver C</h4>
<p><a name="4428">
</a>The first example is analogous to the Hello World Tcl script example.</p>
<a name="157391">
</a><h5>Hello World in C:</h5>
<ol>
<li>In your favorite editor, create the <code>hello.c</code> file with the following content. (You can also find this example in the <code>examples/c/hello</code> directory.)
<a name="157392">
</a><p>
</ol>
<pre>    <a name="157393"></a>
    <a name="22250"></a>#include "ns.h"
    <a name="166411"></a>
    <a name="22251"></a>static Ns_OpProc Hello;
    <a name="166406"></a>
    <a name="166415"></a>int Ns_ModuleVersion = 1;
    <a name="166416"></a>
    <a name="166419"></a>int
    <a name="22253"></a>Ns_ModuleInit(char *hServer, char *hModule)
    <a name="22254"></a>{
    <a name="22255"></a>   Ns_RegisterRequest(hServer, "GET", "/HelloWorld", Hello,
    <a name="157385"></a>		NULL, NULL, 0);
    <a name="166401"></a>	return NS_OK;
    <a name="22256"></a>}
    <a name="22257"></a>
    <a name="166420"></a>static int
    <a name="22258"></a>Hello(Ns_OpContext context, Ns_Conn *conn)
    <a name="22259"></a>{
    <a name="22260"></a>	char html[] = "&lt;HTML&gt;&lt;BODY&gt;Hello World!&lt;/BODY&gt;&lt;/HTML&gt;";
    <a name="22261"></a>	return Ns_ConnReturnHtml(conn, 200, html, strlen(html));
    <a name="22262"></a>}
</pre><p><ol>
<li>Compile the module as described in the platform-specific instructions given above.
<a name="4466">
</a><p>
<li>Copy the compiled module to the bin subdirectory of the AOLserver directory.
<a name="4468">
</a><p>
<li>Enter the following entry to the Modules section of the AOLserver configuration file (normally the nsd.ini file in the AOLserver home directory):
<a name="4470">
</a><p>
<pre>    <a name="4472"></a>hello=hello.so
</pre><p><li>Stop and restart the server. The AOLserver loads the hello.so module on startup and invokes the HelloWorldInit function. The HelloWorldInit procedure registers the Hello function to handle the /HelloWorld URL.
<a name="4474">
</a><p>
</ol>
<p><a name="4476">
</a>After restarting the AOLserver with the hello.so module, you can open the /HelloWorld module in any browser and receive a short "Hello World" message.</p>
<a name="27555">
</a><h4>An AOLserver Statistics Module</h4>
<p><a name="27565">
</a>The following example is a an example C module that maintains statistics on the number of connections and bytes sent by a server.  The example exists in the <code>examples/c/stats</code> directory under the AOLserver home directory.</p>
<pre>    <a name="27562"></a>
    <a name="27574"></a>/*
    <a name="27580"></a> * stat.c - Example C module.
    <a name="27581"></a> * 
    <a name="27582"></a> * This example incorporates many of the features of the 
    <a name="28037"></a> * AOLserver C API in a single simple, but useful, statistics
    <a name="28040"></a> * gathering module.
    <a name="27584"></a> * The module maitains statistics on the number of connections and
    <a name="27585"></a> * bytes sent by a server.  The data is stored in a simple
    <a name="27586"></a> * 'StatContext' structure which is allocated when the module is
    <a name="27587"></a> * loaded.  The StatContext stores the current, last, and total
    <a name="28041"></a> * numbers and is updated after each connection by the 'StatTrace'
    <a name="28044"></a> * connection trace procedure.  At a regular interval (the 
    <a name="28047"></a> * interval in seconds is configurable), the 'StatUpdate' 
    <a name="28050"></a> * funcation is called to log the current and total number to the
    <a name="28053"></a> * AOLserver log file and then makes the current data the last 
    <a name="28211"></a> * data.  Access to these data is provided in two forms:
    <a name="27594"></a> *
    <a name="27595"></a> * 1.  A simple C request function, 'StatRequest', will return the
    <a name="27596"></a> * current data as a simple notice page.  The StatRequest function
    <a name="27597"></a> * is registered at the GET /NS/Stat URL of the server.
    <a name="27598"></a> *
    <a name="27599"></a> * 2.  The 'ns_stat' Tcl command is added to each Tcl interpreter
    <a name="27600"></a> * of the server interpreter pool.  This allows a Tcl
    <a name="28212"></a> * script to access the current, last, or total data at any time.
    <a name="27602"></a> *
    <a name="27603"></a> * Because the AOLserver is completely multithreaded, the
    <a name="28219"></a> * statistics data can be updated and queried simultaneously.  An 
    <a name="28222"></a> * Ns_Mutex is included in the ServerContext structure to keep
    <a name="28229"></a> * multiple threads from accessing the data at the same time.
    <a name="27607"></a> *
    <a name="27608"></a> * Finally, on shutdown, the 'StatShutdown' function is called to
    <a name="27609"></a> * print out a final tally and clean up the ServerContext 
    <a name="28251"></a> * structure.
    <a name="27610"></a> *
    <a name="27611"></a> * Note that all the features of this module are specific to the
    <a name="27612"></a> * server it is loaded into.  The server handle, 
    <a name="28252"></a> * 'hServer', is used to makes sure the trace, request, Tcl
    <a name="28255"></a> * command, and shutdown functions are all run in the context of 
    <a name="28258"></a> * the single server.  
    <a name="28261"></a> * Other virtaul servers may ignore or load the
    <a name="27616"></a> * stat module as well and the AOLserver guarantees the data
    <a name="27617"></a> * will be keep separate for each server.
    <a name="27618"></a> &nbsp;*
    <a name="27621"></a> */
    <a name="27622"></a>
    <a name="27623"></a>
    <a name="27624"></a>
    <a name="27625"></a>#include "ns.h"
    <a name="27626"></a>#include "nstcl.h"
    <a name="27627"></a>
    <a name="27628"></a>/*
    <a name="27629"></a> * The default statistics update interval is 1 minute or 60
    <a name="28262"></a> * seconds.
    <a name="27630"></a> */
    <a name="27631"></a>#define DEFAULT_INTERVAL	 60
    <a name="27632"></a>
    <a name="27633"></a>/*
    <a name="27634"></a> * Definitions of the StatContext structure which is allocated
    <a name="27635"></a> * on a per-server basis.
    <a name="27636"></a> */
    <a name="27637"></a>typedef struct {
    <a name="27638"></a>	int	bytes;
    <a name="27639"></a>	int conns;
    <a name="27640"></a>} StatData;
    <a name="27641"></a>
    <a name="27642"></a>typedef struct {
    <a name="27643"></a>	Ns_Mutex lock;
    <a name="27644"></a>	int interval;
    <a name="27645"></a>	char *hServer;
    <a name="27646"></a>	StatData current;
    <a name="27647"></a>	StatData last;
    <a name="27648"></a>	StatData total;
    <a name="27649"></a>} StatContext;
    <a name="27650"></a>
    <a name="27651"></a>
    <a name="27652"></a>/*
    <a name="27653"></a> * Forward declarations of functions which will be referenced
    <a name="27654"></a> * in the stat module initialization function.
    <a name="27655"></a> */
    <a name="27656"></a>static Tcl_CmdProc StatCmd;
    <a name="27657"></a>static Ns_TclInterpInitProc StatTclInit;
    <a name="27658"></a>static Ns_TraceProc StatTrace;
    <a name="27659"></a>static Ns_Callback StatUpdate;
    <a name="27660"></a>static Ns_Callback StatShutdown;
    <a name="27661"></a>static Ns_OpProc StatRequest;
    <a name="27662"></a>
    <a name="27663"></a>/*
    <a name="27664"></a> * The Ns_ModuleVersion exported integer is used to verify
    <a name="27665"></a> * this module version when loaded.  For AOLserver 3.0,
    <a name="27666"></a> * 1 (one) is the only valid value for this variable.
    <a name="27667"></a> */
    <a name="27668"></a>int Ns_ModuleVersion = 1;
    <a name="27669"></a>
    <a name="27670"></a>/*
    <a name="27671"></a> * The Ns_ModuleInit function is the function the AOLserver
    <a name="27672"></a> * will call each time the module is loaded into a 
    <a name="27673"></a> * server.  The function is passed two parameters:
    <a name="27674"></a> *
    <a name="27675"></a> * hServer:   The server 'handle' as a string. This is the
    <a name="27676"></a> *            short name given to the virutal server such
    <a name="27677"></a> *            as 'server1'.
    <a name="27678"></a> *
    <a name="27679"></a> * hModule:   The module 'handle' as a string. This is the
    <a name="27680"></a> *            short name given to the module such as 'stat'
    <a name="27681"></a> *
    <a name="27682"></a> * For example, if this module is known as 'stat' and loaded
    <a name="27683"></a> * into the 'server1' server with entries similar to the following
    <a name="27684"></a> * in the nsd.ini file:
    <a name="27685"></a> *
    <a name="27686"></a> * [ns/servers]
    <a name="27687"></a> * server1=My First Server
    <a name="27688"></a> *
    <a name="27689"></a> * [ns/server1/modules]
    <a name="27690"></a> * stat=stat.dll
    <a name="27691"></a> *
    <a name="27692"></a> * This function would be called with "server1" and "stat" as
    <a name="27693"></a> * its arguments.
    <a name="27694"></a> *
    <a name="27695"></a> */
    <a name="27696"></a>int
    <a name="27697"></a>Ns_ModuleInit(char *hServer, char *hModule)
    <a name="27698"></a>{
    <a name="27699"></a>	char *configPath;
    <a name="27700"></a>	StatContext *ctx;
    <a name="27701"></a>
    <a name="27702"></a>
    <a name="27703"></a>	/*
    <a name="27704"></a>	 * Create and initalize the statistics context.
    <a name="27705"></a>	 */
    <a name="27706"></a>	ctx = Ns_Malloc(sizeof(StatContext));
    <a name="27707"></a>	Ns_InitializeMutex(&amp;ctx-&gt;lock);
    <a name="27708"></a>	ctx-&gt;hServer = hServer;
    <a name="27709"></a>	ctx-&gt;current.bytes = 0;
    <a name="27710"></a>	ctx-&gt;current.conns = 0;	
    <a name="27711"></a>	ctx-&gt;last.bytes = 0;
    <a name="27712"></a>	ctx-&gt;last.conns = 0;	
    <a name="27713"></a>	ctx-&gt;total.bytes = 0;
    <a name="27714"></a>	ctx-&gt;total.conns = 0;	
    <a name="27715"></a>
    <a name="27716"></a>	/*
    <a name="27717"></a>	 * Determine the statistics interval from the config file.
    <a name="27718"></a>	 * The Ns_ConfigGetPath function will expand to the
    <a name="27719"></a>	 * 'server module specific' configuration section, e.g.,
    <a name="27720"></a>	 * [ns/server1/module/stat].
    <a name="27721"></a>	 */
    <a name="27722"></a>	configPath = Ns_ConfigGetPath(hServer, hModule, NULL);
    <a name="27723"></a>	if (!Ns_ConfigGetInt(configPath, "Interval", 
    <a name="28263"></a>          &amp;ctx-&gt;interval)) 
    <a name="28264"></a>   {
    <a name="27724"></a>		ctx-&gt;interval = DEFAULT_INTERVAL;
    <a name="27725"></a>	}
    <a name="27726"></a>
    <a name="27727"></a>	/*
    <a name="27728"></a>	 * Register the trace to accumulate the statistics.
    <a name="27729"></a>	 */
    <a name="27730"></a>	Ns_RegisterServerTrace(hServer, StatTrace, ctx);
    <a name="27731"></a>
    <a name="27732"></a>	/*
    <a name="27733"></a>	 * Register the statistics update function to run at
    <a name="27734"></a>	 * regular intervals.
    <a name="27735"></a>	 */
    <a name="27736"></a>	Ns_ScheduleProc(StatUpdate, ctx, 0, ctx-&gt;interval);
    <a name="27737"></a>
    <a name="27738"></a>	/*
    <a name="27739"></a>	 * Add the GET /NS/Stat request function.
    <a name="27740"></a>	 */
    <a name="27741"></a>	Ns_RegisterRequest(hServer, "GET", "/NS/Stat", StatRequest,
    <a name="28269"></a>      NULL, ctx, 0);
    <a name="27742"></a>
    <a name="27743"></a>	/*
    <a name="27744"></a>	 * Add the Tcl "ns_stat" command to the interpreter pool.
    <a name="27745"></a>	 * Note how the context is passed to StatTclInit which
    <a name="27746"></a>	 * then passes it to the Tcl_CreateCommand function.
    <a name="27747"></a>	 */
    <a name="27748"></a>	Ns_TclInitInterps(hServer, StatTclInit, ctx);
    <a name="27749"></a>
    <a name="27750"></a>	/*
    <a name="27751"></a>	 * Register the statistics shutdown procedure which
    <a name="27752"></a>	 * cleans up the context on server shutdown.
    <a name="27753"></a>	 */
    <a name="27754"></a>	Ns_RegisterServerShutdown(hServer, StatShutdown, ctx);
    <a name="27755"></a>
    <a name="27756"></a>	return NS_OK;
    <a name="27757"></a>}
    <a name="27758"></a>
    <a name="27759"></a>
    <a name="27760"></a>/*
    <a name="27761"></a> * StatTrace is called after each connection and accumulates the
    <a name="27762"></a> * statistics.
    <a name="27763"></a> */
    <a name="27764"></a>static void
    <a name="27765"></a>StatTrace(void *ctx, Ns_Conn *conn)
    <a name="27766"></a>{
    <a name="27767"></a>	StatContext *sc;
    <a name="27768"></a>	int bytes;
    <a name="27769"></a>
    <a name="27770"></a>	sc = (StatContext *) ctx;
    <a name="27771"></a>	Ns_LockMutex(&amp;sc-&gt;lock);
    <a name="27772"></a>	bytes = Ns_ConnResponseLength(conn);
    <a name="27773"></a>	sc-&gt;current.bytes += bytes;
    <a name="27774"></a>	sc-&gt;total.bytes += bytes;
    <a name="27775"></a>	++sc-&gt;current.conns;
    <a name="27776"></a>	++sc-&gt;total.conns;
    <a name="27777"></a>	Ns_UnlockMutex(&amp;sc-&gt;lock);
    <a name="27778"></a>}
    <a name="27779"></a>
    <a name="27780"></a>
    <a name="27781"></a>/*
    <a name="27782"></a> * StatUpdate aggregates the statistics and logs the data to 
    <a name="27783"></a> * the AOLserver server log.
    <a name="27784"></a> */
    <a name="27785"></a>static void
    <a name="27786"></a>StatUpdate(void *ctx)
    <a name="27787"></a>{
    <a name="27788"></a>	StatContext *sc;
    <a name="27789"></a>
    <a name="27790"></a>	sc = (StatContext *) ctx;
    <a name="27791"></a>
    <a name="27792"></a>	Ns_LockMutex(&amp;sc-&gt;lock);
    <a name="27793"></a>	Ns_Log(Notice, "StatUpdate(%s):  Last: conns %d, bytes %d 
    <a name="28270"></a>     Total: conns %d, bytes %d",
    <a name="27794"></a>		sc-&gt;hServer, sc-&gt;current.conns, sc-&gt;current.bytes,
    <a name="27795"></a>		sc-&gt;total.conns, sc-&gt;total.bytes);
    <a name="27796"></a>	sc-&gt;last.conns = sc-&gt;current.conns;
    <a name="27797"></a>	sc-&gt;last.bytes = sc-&gt;current.bytes;
    <a name="27798"></a>	sc-&gt;current.conns = 0;
    <a name="27799"></a>	sc-&gt;current.bytes = 0;
    <a name="27800"></a>	Ns_UnlockMutex(&amp;sc-&gt;lock);
    <a name="27801"></a>}
    <a name="27802"></a>
    <a name="27803"></a>
    <a name="27804"></a>/*
    <a name="27805"></a> * StatTclInit is called once for each Tcl interpreter
    <a name="27806"></a> * in the virutal server's Tcl interpreter pool.
    <a name="27807"></a> */
    <a name="27808"></a>static int
    <a name="27809"></a>StatTclInit(Tcl_Interp *interp, void *ctx)
    <a name="27810"></a>{
    <a name="27811"></a>	Tcl_CreateCommand(interp, "ns_stat", StatCmd, ctx, NULL);
    <a name="27812"></a>	return NS_OK;
    <a name="27813"></a>}
    <a name="27814"></a>
    <a name="27815"></a>
    <a name="27816"></a>/*
    <a name="27817"></a> * StatCmd implements the Tcl "ns_stat" command.
    <a name="27818"></a> */
    <a name="27819"></a>static int
    <a name="27820"></a>StatCmd(ClientData ctx, Tcl_Interp *interp, int argc, 
    <a name="28271"></a>    char  **argv)
    <a name="27821"></a>{
    <a name="27822"></a>	StatContext *sc;
    <a name="27823"></a>	StatData *sd;
    <a name="27824"></a>
    <a name="27825"></a>	sc = (StatContext *) ctx;
    <a name="27826"></a>	if (argc != 2) {
    <a name="27827"></a>		Tcl_AppendResult(interp, "wrong # args: should be \"",
    <a name="27828"></a>			argv[0], " command\"", NULL);
    <a name="27829"></a>		return TCL_ERROR;
    <a name="27830"></a>	}
    <a name="27831"></a>	if (strcmp(argv[1], "current") == 0) {
    <a name="27832"></a>		sd = &amp;sc-&gt;current;
    <a name="27833"></a>	} else if (strcmp(argv[1], "last") == 0) {
    <a name="27834"></a>		sd = &amp;sc-&gt;last;
    <a name="27835"></a>	} else if (strcmp(argv[1], "total") == 0) {
    <a name="27836"></a>		sd = &amp;sc-&gt;total;
    <a name="27837"></a>	} else {
    <a name="27838"></a>		Tcl_AppendResult(interp, "unknown command \"",
    <a name="27839"></a>			argv[1], "\":  should be current, last, or total",
    <a name="28272"></a>          NULL);
    <a name="27840"></a>		return TCL_ERROR;
    <a name="27841"></a>	}
    <a name="27842"></a>
    <a name="27843"></a>	Ns_LockMutex(&amp;sc-&gt;lock);
    <a name="27844"></a>	sprintf(interp-&gt;result, "%d %d", sd-&gt;conns, sd-&gt;bytes);
    <a name="27845"></a>	Ns_UnlockMutex(&amp;sc-&gt;lock);
    <a name="27846"></a>	return TCL_OK;
    <a name="27847"></a>}
    <a name="27848"></a>
    <a name="27849"></a>
    <a name="27850"></a>/*
    <a name="27851"></a> * StatRequest is a simple AOLserver request function which 
    <a name="28273"></a> * returns the current data in a simple HTML page.  The page is
    <a name="28276"></a> * generated with the Ns_ConnReturnNotice() function which is
    <a name="28287"></a> * used throughout the AOLserver to generate simple HTML page
    <a name="28290"></a> * responses with the AOLserver banner logo.
    <a name="28301"></a> * Ns_ConnReturnNotice() takes a string as
    <a name="27856"></a> * the HTML page content.  We use an Ns_DString to quickly build
    <a name="27857"></a> * up the string - Ns_DString's grow as needed so we don't have
    <a name="27858"></a> * to worry about buffer overflow.  The HTML page is a simple
    <a name="27859"></a> * HTML3 &lt;TABLE&gt; which formats the current, last, and total
    <a name="27860"></a> * statistics for the  server.
    <a name="27861"></a> */
    <a name="27862"></a>static int
    <a name="27863"></a>StatRequest(void *ctx, Ns_Conn *conn)
    <a name="27864"></a>{
    <a name="27865"></a>	StatContext *sc;
    <a name="27866"></a>	Ns_DString ds;
    <a name="27867"></a>	int retcode;
    <a name="27868"></a>
    <a name="27869"></a>	sc = (StatContext *) ctx;
    <a name="27870"></a>	Ns_DStringInit(&amp;ds);
    <a name="27871"></a>
    <a name="27872"></a>	/* 
    <a name="27873"></a>	 * Build up the HTML3 &lt;TABLE&gt; with the latest data.
    <a name="27874"></a>	 */
    <a name="27875"></a>	Ns_DStringAppend(&amp;ds, "&lt;table border cellpadding=\"10\"&gt;");
    <a name="27876"></a>	Ns_DStringVarAppend(&amp;ds, "&lt;tr&gt;&lt;th&gt;", sc-&gt;hServer, "&lt;/th&gt;",
    <a name="28302"></a>       NULL);
    <a name="27877"></a>	Ns_DStringAppend(&amp;ds, 
    <a name="28303"></a>       "&lt;th&gt;Current&lt;/th&gt;&lt;th&gt;Last&lt;/th&gt;&lt;th&gt;Total&lt;/th&gt;&lt;/tr&gt;");
    <a name="27878"></a>	Ns_LockMutex(&amp;sc-&gt;lock);
    <a name="27879"></a>	Ns_DStringPrintf(&amp;ds,
    <a name="175245"></a>		"&lt;tr&gt;&lt;th&gt;# connections&lt;/th&gt;&lt;td&gt;%d&lt;/td&gt;"
    <a name="175254"></a>		"&lt;td&gt;%d&lt;/td&gt;&lt;td&gt;%d&lt;/td&gt;&lt;/tr&gt;",
    <a name="27880"></a>		sc-&gt;current.conns, sc-&gt;last.conns, sc-&gt;total.conns);
    <a name="175282"></a>	Ns_DStringPrintf(&amp;ds,
    <a name="175283"></a>		"&lt;tr&gt;&lt;th&gt;# bytes&lt;/th&gt;&lt;td&gt;%d&lt;/td&gt;"
    <a name="175295"></a>		"&lt;td&gt;%d&lt;/td&gt;&lt;td&gt;%d&lt;/td&gt;&lt;/tr&gt;",
    <a name="175284"></a>		sc-&gt;current.bytes, sc-&gt;last.bytes, sc-&gt;total.bytes);
    <a name="27883"></a>	Ns_UnlockMutex(&amp;sc-&gt;lock);
    <a name="27884"></a>	Ns_DStringAppend(&amp;ds, "&lt;/table&gt;");
    <a name="27885"></a>
    <a name="27886"></a>	/*
    <a name="27887"></a>	 * Return the HTML page using Ns_ConnReturnNotice().
    <a name="27888"></a>	 */
    <a name="27889"></a>	retcode = Ns_ConnReturnNotice(conn, 200, "Server Statistics",
    <a name="28304"></a>      ds.string);
    <a name="27890"></a>
    <a name="27891"></a>	/*
    <a name="27892"></a>	 * Don't forget to free the dstring!
    <a name="27893"></a>	 */	
    <a name="27894"></a>	Ns_DStringFree(&amp;ds);
    <a name="27895"></a>
    <a name="27896"></a>	return retcode;
    <a name="27897"></a>}
    <a name="27898"></a>		
    <a name="27899"></a>
    <a name="27900"></a>/* 
    <a name="27901"></a> * StatShutdown simple prints a final statistics entry to the
    <a name="27902"></a> * server log and then cleans up the StatContext structure.
    <a name="27903"></a> */	
    <a name="27904"></a>static void
    <a name="27905"></a>StatShutdown(void *ctx)
    <a name="27906"></a>{
    <a name="27907"></a>	StatContext *sc;
    <a name="27908"></a>
    <a name="27909"></a>	sc = (StatContext *) ctx;
    <a name="27910"></a>	Ns_Log(Notice, "StatShutdown(%s):  Total:  conns %d, 
    <a name="28305"></a>      bytes %d.",
    <a name="27911"></a>		sc-&gt;hServer, sc-&gt;total.conns, sc-&gt;total.bytes);
    <a name="27912"></a>	Ns_DestroyMutex(&amp;sc-&gt;lock);
    <a name="27913"></a>	Ns_Free(ctx);
    <a name="27914"></a>}
    <a name="27915"></a>
</pre><p>

<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="c-ch1.htm">
<IMG  BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG  BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG  BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="c-ch3.htm">
<IMG  BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright &copy; 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>