1 28 29 package org.objectweb.comanche.lib; 30 31 import java.io.InputStreamReader ; 33 import java.io.PrintStream ; 34 import java.io.LineNumberReader ; 35 import java.io.File ; 36 import java.io.InputStream ; 37 import java.io.FileInputStream ; 38 import java.io.IOException ; 39 40 import java.net.InetAddress ; 41 import java.net.ServerSocket ; 42 import java.net.UnknownHostException ; 43 import java.net.Socket ; 44 45 import org.objectweb.util.cmdline.lib.ApplicationBase; 46 import org.objectweb.util.cmdline.lib.DefaultCommandLine; 47 import org.objectweb.util.cmdline.api.OptionArgument; 48 import org.objectweb.util.cmdline.lib.DefaultOptionArgument; 49 50 53 public class Application 54 extends ApplicationBase 55 { 56 62 63 private OptionArgument rootDirectoryArgument_; 64 65 66 private OptionArgument portArgument_; 67 68 69 private OptionArgument groupAddress_; 70 71 77 78 public Application() 79 { 80 super( 82 new DefaultCommandLine("comanche", "", 84 "Run the OpenCCM Comanche HTTP server.", true) 85 ); 86 87 getCommandLine().setArguments(new String [0]); 89 90 rootDirectoryArgument_ = 92 new DefaultOptionArgument("--rootdir", 93 "directory", 94 "Set the root directory, default is ./", 95 "." 96 ); 97 getCommandLine().addOption(rootDirectoryArgument_); 98 99 portArgument_ = 101 new DefaultOptionArgument("--port", "port", 102 "Set the TCP/IP port, default is 8080.", "8080" 103 ); 104 getCommandLine().addOption(portArgument_); 105 106 groupAddress_ = 108 new DefaultOptionArgument("--group", "groupAddress", 109 "Set the address group, default is 224.0.0.100.", 110 "224.0.0.100"); 111 getCommandLine().addOption(groupAddress_); 112 } 113 114 120 126 132 public int start(String [] args) 133 { 134 String rootDirectory = rootDirectoryArgument_.getArgument(); 136 137 String addgroup = groupAddress_.getArgument(); 139 MulticastServer mcastserver = new MulticastServer(addgroup,rootDirectory); 140 mcastserver.start(); 141 System.err.println("The Comanche Server is waiting Multicast requests on mcast://" + addgroup); 142 143 int port = 0; 145 try { 146 port = Integer.parseInt(portArgument_.getArgument()); 147 } catch(NumberFormatException ex) { 148 report_exception(ex); 149 return -1; 150 } 151 152 ServerSocket server = null; 154 try { 155 server = new ServerSocket (port); 156 } catch(IOException ex) { 157 report_exception(ex); 158 return -1; 159 } 160 161 String url = null; 163 try { 164 url = "http://" + InetAddress.getLocalHost().getHostName() 165 + ':' + server.getLocalPort(); 166 } catch(UnknownHostException ex) { 167 report_exception(ex); 168 return -1; 169 } 170 171 System.err.println("The OpenCCM Comanche Server is waiting HTTP requests on " + url); 172 173 while (true) { 175 try { 176 getConsole().message("Waiting requests on " + url); 177 Socket client = server.accept(); 179 180 InputStreamReader in = new InputStreamReader (client.getInputStream()); 181 PrintStream out = new PrintStream (client.getOutputStream()); 182 183 String request = new LineNumberReader (in).readLine(); 185 getConsole().message(request); 186 187 if (request.startsWith("GET ")) { 188 File f = new File (rootDirectory + request.substring(4, request.indexOf(' ', 4))); 189 if (f.exists() && !f.isDirectory()) { 190 InputStream is = new FileInputStream (f); 191 byte[] data = new byte[is.available()]; 192 is.read(data); 193 is.close(); 194 out.print("HTTP/1.0 200 OK\n\n"); 195 out.write(data); 196 } else { 197 out.print("HTTP/1.0 404 Not Found\n\n"); 198 out.print("<html>Document not found.</html>"); 199 } 200 } 201 out.close(); 202 client.close(); 203 } catch(Exception ex) { 204 report_exception(ex); 205 } 206 } 207 } 208 209 215 219 public static void 220 main(String [] args) { 221 Application application = new Application(); 222 application.runMain(args); 223 } 224 } 225 | Popular Tags |