00001 package riverrat.testing;
00002
00003 import org.xml.sax.Attributes;
00004 import org.xml.sax.helpers.AttributesImpl;
00005 import java.io.*;
00006 import java.util.*;
00007 import java.net.*;
00008
00014 public class NMEAFakeServer extends FakeServer {
00016 protected static String file = "track6.raw";
00017
00019 protected BufferedReader breader;
00020
00022 protected boolean DELAY_INPUT = false;
00023
00028 protected boolean FILE_DATA;
00029
00030 protected static final String hostname = "localhost";
00031
00032
00034 protected String nmeahost = "localhost";
00035
00037 protected int nmeaport = 9999;
00038
00040 public NMEAFakeServer() {
00041 super();
00042 FILE_DATA = true;
00043
00044
00045
00046 }
00047
00049 public NMEAFakeServer(int newport) {
00050 super(newport);
00051 FILE_DATA = true;
00052
00053
00054
00055 }
00056
00058 public NMEAFakeServer(String newhost, int newport) {
00059 super();
00060 FILE_DATA = false;
00061 nmeahost = newhost;
00062 nmeaport = newport;
00063 }
00064
00068 public void setupState() {
00069
00070 startDocument();
00071
00072 AttributesImpl race_attrs = new AttributesImpl();
00073 race_attrs.addAttribute(null, null, "regatta", "string", "Schell");
00074 race_attrs.addAttribute(null, null, "race", "int", "4");
00075 race_attrs.addAttribute(null, null, "id", "int", "2492");
00076 startElement(null, null, "race", (Attributes) race_attrs);
00077
00078 startElement(null, null, "setup", null);
00079
00080 AttributesImpl starttime_attrs = new AttributesImpl();
00081 starttime_attrs.addAttribute(null, null, "date", "string", "3/12/04");
00082 starttime_attrs.addAttribute(null, null, "time", "int", "1079136231671");
00083 startElement(null, null, "starttime", starttime_attrs);
00084 endElement(null, null, "starttime");
00085
00086 AttributesImpl boat_attrs = new AttributesImpl();
00087 boat_attrs.addAttribute(null, null, "sn", "int", "24");
00088 boat_attrs.addAttribute(null, null, "id", "int", "0");
00089 startElement(null, null, "boat", boat_attrs);
00090 endElement(null, null, "boat");
00091
00092 AttributesImpl mark_attrs = new AttributesImpl();
00093 mark_attrs.addAttribute(null, null, "id", "int", "1");
00094 mark_attrs.addAttribute(null, null, "position", "string", "dynamic");
00095 startElement(null, null, "mark", mark_attrs);
00096 endElement(null, null, "mark");
00097
00098 mark_attrs = new AttributesImpl();
00099 mark_attrs.addAttribute(null, null, "id", "int", "0");
00100 mark_attrs.addAttribute(null, null, "position", "string", "dynamic");
00101 startElement(null, null, "mark", mark_attrs);
00102 endElement(null, null, "mark");
00103
00104 endElement(null, null, "setup");
00105 startElement(null, null, "live", null);
00106 }
00107
00108 public void endRace() {
00109 endElement(null, null, "live");
00110 endElement(null, null, "race");
00111 }
00112
00113 public void printBoat(float lat, float lon, long time) {
00114 AttributesImpl boat_attrs = new AttributesImpl();
00115 boat_attrs.addAttribute(null, null, "id", "int", "0");
00116 boat_attrs.addAttribute(null, null, "time", "long", ""+time);
00117 boat_attrs.addAttribute(null, null, "lat", "float", ""+lat);
00118 boat_attrs.addAttribute(null, null, "lon", "float", ""+lon);
00119 startElement(null, null, "boat", boat_attrs);
00120 endElement(null, null, "boat");
00121 }
00122
00126 public void mainServer() {
00127
00128
00129 if(FILE_DATA) {
00130 System.out.print("opening file...");
00131 openFile(file, DELAY_INPUT);
00132 System.out.println("opened file.");
00133 System.out.print("setting up BufferedReader...");
00134 breader = new BufferedReader(new InputStreamReader(fileInputStream));
00135 System.out.println("set up BufferedReader.");
00136 } else {
00137 try {
00138 System.out.println("opening NMEA stream on "+nmeahost+":"+nmeaport+"...");
00139 breader = new BufferedReader(new InputStreamReader((new Socket(nmeahost, nmeaport)).getInputStream()));
00140 System.out.println("opened NMEA stream.");
00141 } catch(UnknownHostException e) {
00142 System.err.println("NMEA Host not found.");
00143 System.exit(1);
00144 } catch(SocketException e) {
00145 System.err.println("Socket exception connecting to NMEA host.");
00146 System.exit(1);
00147 } catch(IOException e) {
00148 System.err.println("IO Exception connecting to NMEA host.");
00149 System.exit(1);
00150 }
00151 }
00152
00153 Date currentdate = new Date(System.currentTimeMillis());
00154 String logfilename = "nmealog/"+currentdate.toString();
00155
00156
00157 PrintWriter filewriter = null;
00158
00159 try {
00160 filewriter = new PrintWriter(new FileWriter(logfilename), true);
00161 } catch(IOException e) {
00162 System.err.println("Cannot write to file.");
00163 System.err.println(e.getMessage());
00164 System.exit(1);
00165 }
00166
00167 System.out.println("using log file: "+logfilename);
00168 filewriter.println("using log file.");
00169 filewriter.flush();
00170
00171 sout.print("<?xml version=\"1.0\" ?>");
00172
00173 String line = null;
00174 String[] sentence;
00175 long time = 0;
00176 float lat = 0.0f, lon = 0.0f;
00177
00178
00179 setupState();
00180
00181
00182
00183 try {
00184 while((line = breader.readLine()) != null) {
00185
00186 filewriter.println(line);
00187
00188
00189
00190
00191 if(line.startsWith("$GPGGA")) {
00192 sentence = line.split(",");
00193 if(sentence[0].equals("$GPGGA")) {
00194 System.out.println("++++++++++++++++");
00195
00196
00197 try {
00198 try {
00199 time = Long.decode(sentence[1]).longValue();
00200 } catch(NumberFormatException e) {
00201 }
00202 } catch(ArrayIndexOutOfBoundsException e) {
00203 }
00204
00205
00206 try {
00207 try {
00208 lat = (Float.parseFloat(sentence[2].substring(0,2)))
00209 + (Float.parseFloat(sentence[2].substring(2))/60.0f);
00210 } catch(IndexOutOfBoundsException e) {
00211 } catch(NumberFormatException e) {
00212 }
00213 if(sentence[3].equals("S")) lat = -lat;
00214 } catch(ArrayIndexOutOfBoundsException e) {
00215 }
00216
00217
00218 try {
00219 try {
00220 lon = (Float.parseFloat(sentence[4].substring(0,3)))
00221 + (Float.parseFloat(sentence[4].substring(3))/60.0f);
00222 } catch(IndexOutOfBoundsException e) {
00223 } catch(NumberFormatException e) {
00224 }
00225 if(sentence[5].equals("W")) lon = -lon;
00226 } catch(ArrayIndexOutOfBoundsException e) {
00227 }
00228 }
00229 System.out.println("latitude is "+lat);
00230 System.out.println("longitude is "+lon);
00231 System.out.println("time is "+time);
00232 printBoat(lat, lon, time);
00233 }
00234 }
00235 } catch(IOException e) {
00236 }
00237
00238
00239 System.out.println("done parsing stream/file.");
00240
00241
00242 endRace();
00243 }
00244
00251 public static void main(String args[]) {
00252 if(args.length != 0) {
00253
00254 filename = args[0];
00255 }
00256 NMEAFakeServer testserver = new NMEAFakeServer(hostname, 9999);
00257 Thread serverthread = new Thread(testserver);
00258 serverthread.start();
00259
00260 while(true) {
00261 System.out.println("This is the main loop.");
00262 try {
00263 Thread.sleep(10000);
00264 } catch(InterruptedException e) {
00265 System.err.println("Interrupted.");
00266 }
00267 }
00268 }
00269 }