00001 /******************************************************************************* 00002 00003 @file AbstractServer.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.AbstractServer; 00040 00041 private import mango.log.Logger; 00042 00043 private import mango.io.Exception, 00044 mango.io.ServerSocket, 00045 mango.io.SocketConduit; 00046 00047 private import mango.io.model.IConduit; 00048 00049 private import mango.utils.Text, 00050 mango.utils.ServerThread; 00051 00052 private import mango.utils.model.IServer; 00053 00054 00055 /****************************************************************************** 00056 00057 Exposes the foundation of a multi-threaded Socket server. This is 00058 subclassed by mango.http.server.HttpServer, which itself would 00059 likely be subclassed by a SecureHttpServer. 00060 00061 ******************************************************************************/ 00062 00063 class AbstractServer : IServer 00064 { 00065 private InternetAddress bind; 00066 private int threads; 00067 private int backlog; 00068 private ServerSocket socket; 00069 private ILogger logger; 00070 00071 /********************************************************************** 00072 00073 Setup this server with the requisite attributes. The number 00074 of threads specified dictate exactly that. You might have 00075 anything between 1 thread and several hundred, dependent 00076 upon the underlying O/S and hardware. 00077 00078 Parameter 'backlog' specifies the max number of"simultaneous" 00079 connection requests to be handled by an underlying socket 00080 implementation. 00081 00082 **********************************************************************/ 00083 00084 this (InternetAddress bind, int threads, int backlog, ILogger logger = null) 00085 in { 00086 assert (bind); 00087 assert (backlog >= 0); 00088 assert (threads > 0 && threads < 1025); 00089 } 00090 body 00091 { 00092 this.bind = bind; 00093 this.threads = threads; 00094 this.backlog = backlog; 00095 00096 // save our logger for later reference 00097 if (logger is null) 00098 logger = Logger.getLogger ("mango.utils.AbstractServer"); 00099 this.logger = logger; 00100 00101 } 00102 00103 /********************************************************************** 00104 00105 Concrete server must expose a name 00106 00107 **********************************************************************/ 00108 00109 protected abstract char[] toString(); 00110 00111 /********************************************************************** 00112 00113 Concrete server must expose a ServerSocket factory 00114 00115 **********************************************************************/ 00116 00117 protected abstract ServerSocket createSocket (InternetAddress bind, int backlog); 00118 00119 /********************************************************************** 00120 00121 Concrete server must expose a ServerThread factory 00122 00123 **********************************************************************/ 00124 00125 protected abstract ServerThread createThread (ServerSocket socket); 00126 00127 /********************************************************************** 00128 00129 Concrete server must expose a service handler 00130 00131 **********************************************************************/ 00132 00133 abstract void service (ServerThread thread, IConduit conduit); 00134 00135 /********************************************************************** 00136 00137 Provide support for figuring out the remote address 00138 00139 **********************************************************************/ 00140 00141 char[] getRemoteAddress (IConduit conduit) 00142 { 00143 SocketConduit socket = cast(SocketConduit) conduit; 00144 InternetAddress addr = cast(InternetAddress) socket.remoteAddress(); 00145 00146 if (addr) 00147 return addr.toAddrString(); 00148 return "127.0.0.1"; 00149 } 00150 00151 /********************************************************************** 00152 00153 Provide support for figuring out the remote host. Not 00154 currently implemented. 00155 00156 **********************************************************************/ 00157 00158 char[] getRemoteHost (IConduit conduit) 00159 { 00160 return null; 00161 } 00162 00163 /********************************************************************** 00164 00165 Return the local port we're attached to 00166 00167 **********************************************************************/ 00168 00169 int getPort () 00170 { 00171 InternetAddress addr = cast(InternetAddress) socket.localAddress(); 00172 return addr.port(); 00173 } 00174 00175 /********************************************************************** 00176 00177 Return the local address we're attached to 00178 00179 **********************************************************************/ 00180 00181 char[] getHost () 00182 { 00183 InternetAddress addr = cast(InternetAddress) socket.localAddress(); 00184 return addr.toAddrString(); 00185 } 00186 00187 /********************************************************************** 00188 00189 Return the logger associated with this server 00190 00191 **********************************************************************/ 00192 00193 ILogger getLogger () 00194 { 00195 return logger; 00196 } 00197 00198 /********************************************************************** 00199 00200 Start this server 00201 00202 **********************************************************************/ 00203 00204 void start () 00205 { 00206 // have the subclass create a ServerSocket for us 00207 socket = createSocket (bind, backlog); 00208 00209 // instantiate and start all threads 00210 for (int i=threads; --i >= 0;) 00211 createThread (socket).start(); 00212 00213 char[] info = "Server "~toString()~" started on "~ 00214 socket.localAddress().toString()~ 00215 " with "~Text.toString(threads)~" accept threads, "~ 00216 Text.toString(backlog)~" backlogs"; 00217 00218 // indicate what's going on 00219 logger.info (info); 00220 } 00221 }