Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

SerialProxy.java

Go to the documentation of this file.
00001 /*
00002  * SerialProxy.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki.util;
00024 
00025 import ikayaki.squid.*;
00026 
00027 import java.io.IOException;
00028 import java.io.PrintStream;
00029 import java.text.DateFormat;
00030 import java.text.SimpleDateFormat;
00031 import java.util.Date;
00032 
00038 public class SerialProxy {
00039 
00040     private static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
00041 
00042     public static void main(String[] args) {
00043         if (args.length != 2) {
00044             System.out.println("Forwards commands sent between two serial ports and logs them.");
00045             System.out.println("Usage: serialproxy <COM#> <COM#>");
00046             System.out.println("Example: serialproxy COM2 COM5");
00047             return;
00048         }
00049         SerialIO portOne;
00050         SerialIO portTwo;
00051 
00052         try {
00053             portOne = SerialIO.openPort(new SerialParameters(args[0]));
00054             portTwo = SerialIO.openPort(new SerialParameters(args[1]));
00055 
00056             System.out.println("Timestamp\tSender\tMessage");
00057             new Forwarder(portOne, portTwo, System.out);
00058             new Forwarder(portTwo, portOne, System.out);
00059 
00060             //wait for signal to quit 8)
00061             System.in.read();
00062 
00063         } catch (SerialIOException e) {
00064             e.printStackTrace();
00065         } catch (IOException e) {
00066             e.printStackTrace();
00067         }
00068     }
00069 
00070     private static class Forwarder implements SerialIOListener {
00071 
00072         private SerialIO in;
00073         private SerialIO out;
00074         private PrintStream log;
00075 
00076         public Forwarder(SerialIO in, SerialIO out) {
00077             this(in, out, null);
00078         }
00079 
00080         public Forwarder(SerialIO in, SerialIO out, PrintStream log) {
00081             in.addSerialIOListener(this);
00082             this.in = in;
00083             this.out = out;
00084             this.log = log;
00085         }
00086 
00087         public void serialIOEvent(SerialIOEvent event) {
00088             try {
00089                 if (log != null) {
00090                     log.println(dateFormat.format(new Date()) + "\t" + in.getPortName() + "\t" + event.getLogMessage());
00091                 }
00092                 out.writeMessage(event.getMessage());
00093             } catch (SerialIOException ex) {
00094                 ex.printStackTrace();
00095             }
00096         }
00097     }
00098 }

Generated on Fri May 6 16:00:33 2005 for Squid by  doxygen 1.4.1