132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: April 2004
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module tango.net.cluster.tina.util.ServerThread;
|
|
14
|
|
15 private import tango.core.Thread,
|
|
16 tango.core.Runtime,
|
|
17 tango.core.Exception;
|
|
18
|
|
19 private import tango.net.ServerSocket;
|
|
20
|
|
21 private import tango.net.cluster.tina.util.AbstractServer;
|
|
22
|
|
23 /******************************************************************************
|
|
24
|
|
25 Subclasses Thread to provide the basic server-thread loop. This
|
|
26 functionality could also be implemented as a delegate, however,
|
|
27 we also wish to subclass in order to add thread-local data (see
|
|
28 HttpThread).
|
|
29
|
|
30 ******************************************************************************/
|
|
31
|
|
32 class ServerThread
|
|
33 {
|
|
34 private AbstractServer server;
|
|
35 private ServerSocket socket;
|
|
36
|
|
37 /**********************************************************************
|
|
38
|
|
39 Construct a ServerThread for the given Server, upon the
|
|
40 specified socket
|
|
41
|
|
42 **********************************************************************/
|
|
43
|
|
44 this (AbstractServer server, ServerSocket socket)
|
|
45 {
|
|
46 this.server = server;
|
|
47 this.socket = socket;
|
|
48 (new Thread (&run)).start;
|
|
49 }
|
|
50
|
|
51 /**********************************************************************
|
|
52
|
|
53 Execute this thread until the Server says to halt. Each
|
|
54 thread waits in the socket.accept() state, waiting for
|
|
55 a connection request to arrive. Upon selection, a thread
|
|
56 dispatches the request via the request service-handler
|
|
57 and, upon completion, enters the socket.accept() state
|
|
58 once more.
|
|
59
|
|
60 **********************************************************************/
|
|
61
|
|
62 private void run ()
|
|
63 {
|
|
64 while (Runtime.isHalting is false)
|
|
65 try {
|
|
66 // wait for a socket connection
|
|
67 auto sc = socket.accept;
|
|
68
|
|
69 // did we get a valid response?
|
|
70 if (sc)
|
|
71 // yep - process this request
|
|
72 server.service (sc);
|
|
73 else
|
|
74 // server may be halting ...
|
|
75 if (socket.isAlive)
|
|
76 server.getLogger.error ("Socket.accept failed");
|
|
77
|
|
78 } catch (IOException x)
|
|
79 server.getLogger.error ("IOException: "~x.toString);
|
|
80
|
|
81 catch (Object x)
|
|
82 server.getLogger.fatal ("Exception: "~x.toString);
|
|
83 }
|
|
84 }
|