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