00001 package riverrat;
00002
00003 import java.net.*;
00004 import java.io.*;
00005 import java.util.*;
00006
00011 public class Server {
00013 protected static int port = 8080;
00014
00016 protected static int numthreads = 5;
00017
00019 protected static Vector threads;
00020
00022 protected static ServerSocket serverSocket;
00023
00025 public static void printAvailableServerTypes() {
00026 String[] servers = new String[5];
00027 servers[0] = "riverrat.LiveServer";
00028 servers[1] = "riverrat.PlaybackServer";
00029 servers[2] = "riverrat.testing.XMLFakeServer";
00030 servers[3] = "riverrat.testing.NMEAFakeServer";
00031
00032 for (int i = 0; i < servers.length; i++) {
00033 if(servers[i] != null) {
00034 try {
00035 Class testclass = Class.forName(servers[i]);
00036 System.out.println(servers[i]+" is available.");
00037 } catch(ClassNotFoundException e) {
00038 }
00039 }
00040 }
00041 }
00042
00046 public static void printUsage() {
00047 System.out.println("`Server' is a server for the RiverRat project.");
00048 System.out.println("usage: java riverrat.Server [OPTIONS] SERVERTYPE [OPTIONS]");
00049 System.out.println("");
00050 System.out.println("Options:");
00051 System.out.println(" -h, --help\t\t\tPrint this message and exit.");
00052 System.out.println(" -p, --port PORT\t\tUse server port PORT.");
00053 System.out.println("");
00054 System.out.println("Available server types are: ");
00055 printAvailableServerTypes();
00056 System.out.println("");
00057 System.out.println("Author: Scott Torborg 2004");
00058 System.out.println("RiverRat Project http://mit.edu/riverrat");
00059 System.exit(1);
00060 }
00061
00066 public static void main(String args[]) {
00067 int n = args.length;
00068 String serverType = null;
00069 Class serverclass = null;
00070
00071 if(n == 0) printUsage();
00072
00073 int serverPort = 8080;
00074 for(int i = 0; i < n; i++) {
00075 String s = args[i].toLowerCase();
00076 if (s.equals("--help") || s.equals("-h")) {
00077 printUsage();
00078 } else if(s.equals("--port") || s.equals("-p")) {
00079 if(i+1 >= n) printUsage();
00080 i++;
00081 try {
00082 port = Integer.parseInt(args[i]);
00083 } catch(NumberFormatException e) {
00084 System.err.println("Bad TCP port number.");
00085 System.exit(1);
00086 }
00087 if(i+1 >= n) printUsage();
00088 i++;
00089 }
00090 try {
00091 serverclass = Class.forName(args[i]);
00092 } catch(ClassNotFoundException e) {
00093 System.err.println("Bad server class.");
00094 System.exit(1);
00095 }
00096 }
00097
00098 try {
00099 BasicServer server = (BasicServer) serverclass.newInstance();
00100 } catch(IllegalAccessException e) {
00101 System.err.println("Server class could not be accessed.");
00102 System.exit(1);
00103 } catch(InstantiationException e) {
00104 System.err.println("Server class could not be instantiated.");
00105 System.exit(1);
00106 } catch(ClassCastException e) {
00107 System.err.println("Server class must be a BasicServer subclass.");
00108 System.exit(1);
00109 }
00110
00111 threads = new Vector();
00112 try {
00113 serverSocket = new ServerSocket(port);
00114 System.out.println("Listening on port "+port);
00115 } catch(IOException e) {
00116 System.err.println("Could not listen on: "+port);
00117 System.exit(1);
00118 }
00119
00120 while(true) {
00121 try {
00122 Socket s = serverSocket.accept();
00123 BasicServer bs = null;
00124 try {
00125 bs = (BasicServer) serverclass.newInstance();
00126 } catch(IllegalAccessException e) {
00127 System.err.println("Could not access server class.");
00128 System.exit(1);
00129 } catch(InstantiationException e) {
00130 System.err.println("Could not instantiate server class.");
00131 System.exit(1);
00132 }
00133 bs.setSocket(s);
00134 (new Thread(bs)).start();
00135 threads.addElement(bs);
00136 } catch(IOException e) {
00137 System.err.println("Error accepting socket.");
00138 }
00139 if (threads.size() > 5) {
00140 System.err.println("Too many threads, not opening any more.");
00141 break;
00142 }
00143 }
00144 }
00145 }