RiverRat  - An MIT iCampus project http://web.mit.edu/riverrat/

Main Page | Software Documentation | Hardware Documentation | People | Contact | Wiki

Main Page | Class Hierarchy | Class List | File List | Class Members

FakeServer.java

00001 package riverrat.testing;
00002 
00003 import org.xml.sax.*;
00004 import org.xml.sax.helpers.*;
00005 
00006 import riverrat.BasicServer;
00007 import java.net.*;
00008 import java.io.*;
00009 import java.util.*;
00010 import javax.comm.*;
00011 
00016 public class FakeServer extends BasicServer {
00018         protected static String filename = "race.xml";
00019         
00021         protected InputStream fileInputStream = null;
00022         
00024         public FakeServer() {
00025                 super();
00026         }
00027         
00032         public FakeServer(int newport) {
00033                 super(newport);
00034         }
00035         
00039         public void openFile(String name, boolean delay) {
00040                 try {
00041                         if(delay) {
00042                                 fileInputStream = new DelayedInputStream(new FileInputStream(name));
00043                         } else {
00044                                 fileInputStream = new FileInputStream(name);
00045                         }
00046                 } catch(FileNotFoundException e) {
00047                         System.err.println("File not found: "+name);
00048                         System.exit(1);
00049                 }
00050         }
00051         
00057         public void mainServer() {
00058                 sout.println("Yarrr, matey! The TCP clients, they've breached the outer walls!");
00059                 sout.println("Connected.");
00060         }
00061         
00065         public static void printUsage() {
00066                 System.out.println("`FakeServer' is a test server for the RiverRat project.");
00067                 System.out.println("usage: java riverrat.testing.FakeServer [OPTIONS]");
00068                 System.out.println("");
00069                 System.out.println("Options:");
00070                 System.out.println("  -h, --help\t\t\tPrint this message and exit.");
00071                 System.out.println("  -p, --port PORT\t\tUse server port PORT.");
00072                 System.out.println("  -f, --format {xml|nmea}\tUse input file format xml or nmea.");
00073                 System.out.println("  -i, --input FILENAME\t\tUse FILENAME for input.");
00074                 System.out.println("");
00075                 System.out.println("Author: Scott Torborg 2004");
00076                 System.out.println("RiverRat Project http://mit.edu/riverrat");
00077                 System.exit(1);
00078         }
00079         
00086         public static void main(String args[]) {
00087                 int n = args.length;
00088                 String serverType = null;
00089                 int serverPort = 8080;
00090                 for(int i = 0; i < n; i++) {
00091                         String s = args[i].toLowerCase();
00092                         if (s.equals("--help") || s.equals("-h")) {
00093                                 printUsage();
00094                         } else if(s.equals("--port") || s.equals("-p")) {
00095                                 if(i+1 >= n) printUsage();
00096                                 i++;
00097                                 try {
00098                                         serverPort = Integer.parseInt(args[i]);
00099                                 } catch(NumberFormatException e) {
00100                                         System.err.println("Bad TCP port number.");
00101                                         System.exit(1);
00102                                 }
00103                         } else if(s.equals("--format") || s.equals("-f")) {
00104                                 if(i+1 >= n) printUsage();
00105                                 i++;
00106                                 if(args[i].equals("xml")) {
00107                                         serverType = "xml";
00108                                 } else if(args[i].equals("nmea")) {
00109                                         serverType = "nmea";
00110                                 } else {
00111                                         System.err.println("Bad server type.");
00112                                         System.exit(1);
00113                                 }
00114                         } else if(s.equals("--input") || s.equals("-i")) {
00115                                 if(i+1 >= n) printUsage();
00116                                 i++;
00117                                 filename = args[i];
00118                         }
00119                 }
00120                 
00121                 FakeServer testserver = null;
00122                 if(serverType == "xml") {
00123                         testserver = new XMLFakeServer(serverPort);
00124                 } else if(serverType == "nmea") {
00125                         testserver = new NMEAFakeServer(serverPort);
00126                 } else {
00127                         testserver = new FakeServer(serverPort);
00128                 }
00129                 Thread serverthread = new Thread(testserver);
00130                 serverthread.start();
00131                 
00132                 while(true) {
00133                         System.out.println("This is the main loop.");
00134                         try {
00135                                 Thread.sleep(10000);
00136                         } catch(InterruptedException e) {
00137                                 System.err.println("Interrupted.");
00138                         }
00139                 }
00140         }
00141 }
 

Brought to you by the RiverRat team.