Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | 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         version (Ares) 
00089                  alias void ThreadReturn;
00090               else
00091                  alias int ThreadReturn;
00092 
00093         override ThreadReturn run ()
00094         {
00095                 while (true)
00096                        try {
00097                            // should we bail out?
00098                            if (Socket.isCancelled)
00099                                return 0;
00100 
00101                            // wait for a socket connection
00102                            SocketConduit sc = socket.accept ();
00103 
00104                            // did we get a valid response?
00105                            if (sc)
00106                                // yep: process this request
00107                                server.service (this, sc);
00108                            else
00109                               // server may be halting ...
00110                               if (! Socket.isCancelled)
00111                                     server.getLogger.error ("Socket accept() failed");
00112 
00113                            } catch (IOException x)
00114                                    {
00115                                    server.getLogger.error ("IOException: "~x.toString);
00116                                    }
00117                             catch (Object x)
00118                                    {
00119                                    server.getLogger.fatal ("Exception: "~x.toString);
00120                                    }
00121         }
00122 }

Generated on Mon Nov 14 10:59:40 2005 for Mango by  doxygen 1.4.0