Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

ServerThread.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file ServerThread.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032         
00033         @version        Initial version, April 2004      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.utils.ServerThread;
00040 
00041 private import std.thread;
00042 
00043 private import  mango.io.Conduit,
00044                 mango.io.Exception,
00045                 mango.io.ServerSocket,
00046                 mango.io.SocketConduit;
00047 
00048 private import  mango.utils.AbstractServer;
00049 
00050 /******************************************************************************
00051 
00052         Subclasses Thread to provide the basic server-thread loop. This
00053         functionality could also be implemented as a delegate, however, 
00054         we also wish to subclass in order to add thread-local data (see
00055         HttpThread).
00056 
00057 ******************************************************************************/
00058 
00059 class ServerThread : Thread
00060 {
00061         ServerSocket    socket;
00062         AbstractServer  server;
00063 
00064         /**********************************************************************
00065 
00066                 Construct a ServerThread for the given Server, upon the 
00067                 specified socket
00068                  
00069         **********************************************************************/
00070 
00071         this (AbstractServer server, ServerSocket socket)
00072         {
00073                 this.server = server;
00074                 this.socket = socket;
00075         }
00076 
00077         /**********************************************************************
00078 
00079                 Execute this thread until the Server says to halt. Each
00080                 thread waits in the socket.accept() state, waiting for
00081                 a connection request to arrive. Upon selection, a thread
00082                 dispatches the request via the request service-handler
00083                 and, upon completion, enters the socket.accept() state
00084                 once more.
00085 
00086         **********************************************************************/
00087 
00088         override int run()
00089         {
00090                 while (true)
00091                        try {
00092                            // should we bail out?
00093                            if (Socket.isCancelled)
00094                                return 0;
00095 
00096                            // wait for a socket connection
00097                            SocketConduit sc = socket.accept ();
00098 
00099                            // did we get a valid response?
00100                            if (sc)
00101                                // yep: process this request
00102                                server.service (this, sc);
00103                            else
00104                               // server may be halting ...
00105                               if (! Socket.isCancelled)
00106                                     server.getLogger.error ("Socket accept() failed");
00107 
00108                            } catch (IOException x)
00109                                    {
00110                                    server.getLogger.error ("IOException: "~x.toString);
00111                                    }
00112                             catch (Object x)
00113                                    {
00114                                    server.getLogger.fatal ("Exception: "~x.toString);
00115                                    }
00116         }
00117 }

Generated on Sat Apr 9 20:11:28 2005 for Mango by doxygen 1.3.6