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

StreamLogger.java

00001 package riverrat.testing;
00002 
00003 import java.net.*;
00004 import java.io.*;
00005 
00009 public class StreamLogger {
00011         protected static String file = "out.xml";
00012         
00014         protected static String host = "localhost";
00015         
00017         protected static int port = 8080;
00018         
00022         public static void printUsage() {
00023                 System.out.println("`XMLLogger' is a test data logger for the RiverRat project.");
00024                 System.out.println("usage: java riverrat.testing.XMLLogger [OPTIONS]");
00025                 System.out.println("");
00026                 System.out.println("Options:");
00027                 System.out.println("  -h, --help\t\t\tPrint this message and exit.");
00028                 System.out.println("  -p, --port PORT\t\tUse server port PORT.");
00029                 System.out.println("  -o, --out\t\t\tFile to write to.");
00030                 System.out.println("  -s, --server SERVER\t\tConnect to host SERVER.");
00031                 System.out.println("");
00032                 System.out.println("Author: Scott Torborg 2004");
00033                 System.out.println("RiverRat Project http://mit.edu/riverrat");
00034                 System.exit(1);
00035         }
00036         
00043         public static void main(String args[]) {
00044                 int n = args.length;
00045                 //parse args
00046                 for(int i = 0; i < n; i++) {
00047                         String s = args[i].toLowerCase();
00048                         if(s.equals("--help") || s.equals("-h")) {
00049                                 printUsage();
00050                         } else if(s.equals("--port") || s.equals("-p")) {
00051                                 if(i+1 >= n) printUsage();
00052                                 i++;
00053                                 try {
00054                                         port = Integer.parseInt(args[i]);
00055                                 } catch(NumberFormatException e) {
00056                                         System.err.println("Bad TCP port number.");
00057                                         System.exit(1);
00058                                 }
00059                         } else if(s.equals("--out") || s.equals("-o")) {
00060                                 if(i+1 >= n) printUsage();
00061                                 i++;
00062                                 file = args[i];
00063                         } else if(s.equals("--server") || s.equals("-s")) {
00064                                 if(i+1 >= n) printUsage();
00065                                 i++;
00066                                 host = args[i];
00067                         }
00068                 }
00069                 
00070                 //do the stuff
00071                 
00072                 FileOutputStream output = null;
00073                 
00074                 //open file
00075                 try {
00076                         output = new FileOutputStream(file);
00077                 } catch(FileNotFoundException e) {
00078                         System.err.println("File cannot be created.");
00079                         System.exit(1);
00080                 }
00081                 
00082                 Socket s = null;
00083                 InputStream stream = null;
00084                 int data = 0;
00085                 
00086                 //open socket to host:port
00087                 try {
00088                         s = new Socket(host, port);
00089                         try {
00090                                 System.out.println("Connection accepted: "+s);
00091                                 stream = s.getInputStream();
00092                                 //echo data from socket to file
00093                                 while((data = stream.read()) >= 0) {
00094                                         output.write(data);
00095                                 }
00096                         } finally {
00097                                 s.close();
00098                         }
00099                 } catch(SocketException e) {
00100                         System.err.println("SocketException caught: "+e.getMessage());
00101                 } catch(Exception e) {
00102                         System.err.println("Error: "+e.getMessage());
00103                 }
00104         }
00105 }
 

Brought to you by the RiverRat team.