| 1 |
/* |
|---|
| 2 |
Copyright (c) 2005 Eric Anderton |
|---|
| 3 |
Original (non-DSP version) - Copyright (c) 2004 Kris Bell, Scott Sanders |
|---|
| 4 |
|
|---|
| 5 |
Permission is hereby granted, free of charge, to any person |
|---|
| 6 |
obtaining a copy of this software and associated documentation |
|---|
| 7 |
files (the "Software"), to deal in the Software without |
|---|
| 8 |
restriction, including without limitation the rights to use, |
|---|
| 9 |
copy, modify, merge, publish, distribute, sublicense, and/or |
|---|
| 10 |
sell copies of the Software, and to permit persons to whom the |
|---|
| 11 |
Software is furnished to do so, subject to the following |
|---|
| 12 |
conditions: |
|---|
| 13 |
|
|---|
| 14 |
The above copyright notice and this permission notice shall be |
|---|
| 15 |
included in all copies or substantial portions of the Software. |
|---|
| 16 |
|
|---|
| 17 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 18 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|---|
| 19 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 20 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|---|
| 21 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 22 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|---|
| 23 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|---|
| 24 |
OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 25 |
*/ |
|---|
| 26 |
|
|---|
| 27 |
pragma(lib,"ws2_32.lib"); |
|---|
| 28 |
|
|---|
| 29 |
// for a variety of servlet IO |
|---|
| 30 |
import mango.io.Uri, |
|---|
| 31 |
mango.io.Socket, |
|---|
| 32 |
mango.io.FileBucket, |
|---|
| 33 |
mango.io.DisplayWriter, |
|---|
| 34 |
mango.io.PickleRegistry; |
|---|
| 35 |
|
|---|
| 36 |
// for numeric conversion |
|---|
| 37 |
import mango.format.Int; |
|---|
| 38 |
|
|---|
| 39 |
// for threads |
|---|
| 40 |
import mango.sys.System; |
|---|
| 41 |
|
|---|
| 42 |
//for logging |
|---|
| 43 |
import mango.log.Admin, |
|---|
| 44 |
mango.log.Logger, |
|---|
| 45 |
mango.log.Configurator; |
|---|
| 46 |
|
|---|
| 47 |
// for testing the http server |
|---|
| 48 |
import mango.http.server.HttpServer; |
|---|
| 49 |
|
|---|
| 50 |
// for testing the http client |
|---|
| 51 |
import mango.http.client.HttpClient; |
|---|
| 52 |
|
|---|
| 53 |
// for testing the servlet-engine |
|---|
| 54 |
import mango.servlet.Servlet, |
|---|
| 55 |
mango.servlet.ServletContext, |
|---|
| 56 |
mango.servlet.ServletProvider; |
|---|
| 57 |
|
|---|
| 58 |
// for working with cache entries |
|---|
| 59 |
import mango.cache.Payload, |
|---|
| 60 |
mango.cache.VirtualCache; |
|---|
| 61 |
|
|---|
| 62 |
private import dsp.servlet.DSPServletProvider; |
|---|
| 63 |
|
|---|
| 64 |
import mango.format.Int; |
|---|
| 65 |
|
|---|
| 66 |
// setup a logger for module scope |
|---|
| 67 |
private Logger mainLogger; |
|---|
| 68 |
|
|---|
| 69 |
void testServletEngine () |
|---|
| 70 |
{ |
|---|
| 71 |
mainLogger.info ("registering servlets"); |
|---|
| 72 |
|
|---|
| 73 |
// construct a servlet-provider |
|---|
| 74 |
DSPServletProvider sp = new DSPServletProvider(); |
|---|
| 75 |
|
|---|
| 76 |
// create a context for admin servlets |
|---|
| 77 |
sp.addContext (new AdminContext (sp, "/admin")); |
|---|
| 78 |
|
|---|
| 79 |
// fire up a server |
|---|
| 80 |
mainLogger.info ("starting server"); |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
/*** fetch port, address and backlog from the config ***/ |
|---|
| 84 |
|
|---|
| 85 |
uint backlog = 0; |
|---|
| 86 |
char[] serverBacklog = sp.getConfiguration.get("server-backlog"); |
|---|
| 87 |
if(serverBacklog) backlog = Int.parse(serverBacklog); |
|---|
| 88 |
if(backlog == 0) backlog = 10; |
|---|
| 89 |
|
|---|
| 90 |
uint port = 0; |
|---|
| 91 |
char[] serverPort = sp.getConfiguration.get("server-port"); |
|---|
| 92 |
if(serverPort) port = Int.parse(serverPort); |
|---|
| 93 |
if(port == 0) port = 80; |
|---|
| 94 |
|
|---|
| 95 |
char[] serverAddress = sp.getConfiguration.get("server-address"); |
|---|
| 96 |
if(!serverAddress) serverAddress = "127.0.0.1"; |
|---|
| 97 |
|
|---|
| 98 |
InternetAddress addr = new InternetAddress (serverAddress,port); |
|---|
| 99 |
|
|---|
| 100 |
// create a (1 thread) server using the IProvider to service requests |
|---|
| 101 |
uint threads = 0; |
|---|
| 102 |
char[] serverThreads = sp.getConfiguration.get("server-threads"); |
|---|
| 103 |
if(serverThreads) threads = Int.parse(serverThreads); |
|---|
| 104 |
if(threads == 0) threads = 1; |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
/*** bind to port and an address ***/ |
|---|
| 108 |
|
|---|
| 109 |
HttpServer server = new HttpServer (sp, addr, threads, backlog, mainLogger); |
|---|
| 110 |
|
|---|
| 111 |
// start listening for requests (but this thread does not listen) |
|---|
| 112 |
server.start (); |
|---|
| 113 |
|
|---|
| 114 |
// send this thread to sleep for ever ... |
|---|
| 115 |
System.sleep (); |
|---|
| 116 |
|
|---|
| 117 |
// should never get here |
|---|
| 118 |
mainLogger.info ("halting server"); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
int main () |
|---|
| 122 |
{ |
|---|
| 123 |
BasicConfigurator.configure (); |
|---|
| 124 |
mainLogger = Logger.getLogger ("dsp.servlets"); |
|---|
| 125 |
mainLogger.setLevel (mainLogger.Level.Info); |
|---|
| 126 |
|
|---|
| 127 |
try { |
|---|
| 128 |
testServletEngine(); |
|---|
| 129 |
|
|---|
| 130 |
mainLogger.info ("Done"); |
|---|
| 131 |
} catch (Exception x) |
|---|
| 132 |
{ |
|---|
| 133 |
mainLogger.error (x.msg); |
|---|
| 134 |
} |
|---|
| 135 |
return 0; |
|---|
| 136 |
} |
|---|