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

RiverRatListener.java

00001 package riverrat;
00002 
00003 import riverrat.testing.DelayedInputStream;
00004 import java.io.InputStream;
00005 import java.io.PrintWriter;
00006 import java.net.Socket;
00007 import java.net.SocketException;
00008 import java.util.Vector;
00009 import org.xml.sax.XMLReader;
00010 import org.xml.sax.InputSource;
00011 import org.xml.sax.ContentHandler;
00012 import org.xml.sax.Attributes;
00013 import org.xml.sax.SAXException;
00014 import org.xml.sax.SAXParseException;
00015 import org.xml.sax.helpers.DefaultHandler;
00016 import org.xml.sax.helpers.XMLReaderFactory;
00017 
00022 public class RiverRatListener extends DefaultHandler implements Runnable {
00024         static int port = 8080;
00025         
00027         static String host = "localhost";
00028         
00033         static final boolean DELAY_INPUT = false;
00034         
00036         static final boolean DEBUG = false;
00037                 
00038         static DefaultHandler defaulthandler;
00039         static ContentHandler contenthandler;
00040         static PrintWriter out;
00041         static XMLReader parser;
00042         static Socket socket;
00043         static InputStream stream;
00044         static InputSource source;
00045         
00051         public RiverRatListener() {
00052         }
00053                 
00064         public RiverRatListener(ContentHandler somehandler) {
00065                 contenthandler = somehandler;
00066         }
00067         
00078         public RiverRatListener(ContentHandler somehandler, String connect_host, int connect_port) {
00079                 this(somehandler);
00080                 host = connect_host; port = connect_port;
00081         }
00082         
00089         public void startElement(String uri, String localpart, String rawname, Attributes attrs) throws SAXException {
00090                 System.out.println("< --- "+rawname);
00091                 int length = attrs != null ? attrs.getLength() : 0;
00092                 for (int i = 0; i < length; i++) {
00093                         System.out.println("Attribute: "+attrs.getQName(i)+' '+attrs.getValue(i));
00094                 }
00095         }
00096 
00103         public void endElement(String uri, String localpart, String rawname) throws SAXException {
00104                 System.out.println(" --- >"+rawname);
00105         }
00106 
00113         public void startDocument() {
00114                 System.out.println("Document started.");
00115         }
00116 
00123         public void endDocument() {
00124                 System.out.println("Document ended.");
00125         }
00126                 
00132         public void warning(SAXParseException ex) throws SAXException {
00133                 System.err.println("Warning: "+ex);
00134         }
00135 
00141         public void error(SAXParseException ex) throws SAXException {
00142                 System.err.println("Error: "+ex);
00143         }
00144 
00150         public void fatalError(SAXParseException ex) throws SAXException {
00151                 System.err.println("Fatal Error: "+ex);
00152         }
00153         
00162         public void run() {
00163                 defaulthandler = new RiverRatListener();
00164                 if(contenthandler == null) {
00165                         contenthandler = defaulthandler;
00166                 }
00167                 if(DEBUG) {
00168                         if (contenthandler instanceof Race) System.out.println("contenthandler is the Race class");
00169                         if (contenthandler instanceof RiverRatListener) System.out.println("contenthandler is the RiverRatListener class");
00170                 }
00171                 out = new PrintWriter(System.out);
00172                 try { //instantiate parser
00173                         parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
00174                 } catch (Exception e) {
00175                         System.err.println("error: Unable to instantiate parser");
00176                 }
00177                 //set parser features
00178                 try {
00179                         parser.setFeature("http://xml.org/sax/features/namespaces",true);
00180                         parser.setFeature("http://xml.org/sax/features/validation",false);
00181                         parser.setFeature("http://apache.org/xml/features/validation/schema",false);
00182                         parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",false);
00183                 } catch (SAXException e) {
00184                         System.err.println("warning: Parser doesn't support features... "+e);
00185                 }
00186                 parser.setContentHandler(contenthandler);
00187                 //tells the XML parser to use the contenthandler class as it's Content handler.
00188                 parser.setErrorHandler(defaulthandler);
00189                 //tells the XML parser to use the defaulthandler class as it's error handler.
00190                 System.out.println("Attempting connection to "+host+" on port "+port);
00191                 try { //try to open socket
00192                         socket = new Socket(host, port);        //try to open TCP connection to host on port
00193                         try { //try to parse the stream
00194                                 System.out.println("Connection accepted: "+socket);
00195                                 if(DELAY_INPUT) {
00196                                         stream = new DelayedInputStream(socket.getInputStream()); //grabs TCP socket input stream
00197                                 } else {
00198                                         stream = socket.getInputStream();
00199                                 }
00200                                 source = new InputSource(stream);
00201                                 source.setSystemId(""+port);
00202                                 if(source == null) System.err.println("InputSource is null!!!!!");
00203                                 try {
00204                                         parser.parse(source); //do the magic shit
00205                                 } catch(NullPointerException e) {
00206                                         System.err.println("NullPointerException trying to parse. This probably means that the ContentHandler class is null.");
00207                                 }
00208                         } finally {
00209                                 socket.close();
00210                         }
00211                 } catch (SocketException se) {
00212                         System.err.println("SocketException caught: "+se.getMessage());
00213                 } catch (SAXParseException se) {
00214                         System.err.println("SAXParseException caught: "+se.getMessage());
00215                 } catch (Exception e) {
00216                         System.err.println("Error: "+e.getMessage());
00217                         e.printStackTrace(System.err);
00218                 } //try
00219         } //run()
00220         
00225         public static void main(String args[]) {
00226                 RiverRatListener rrlistener = new RiverRatListener();
00227                 Thread listenthread = new Thread(rrlistener);
00228                 listenthread.start();
00229                 while(true) {
00230                         System.out.println("This is the main loop.");
00231                         try {
00232                                 Thread.sleep(10000);
00233                         } catch (InterruptedException e) {
00234                                 System.err.println("Interrupted!");
00235                         }
00236                 }
00237         } //main()
00238 } //class RiverRatListener
 

Brought to you by the RiverRat team.