summaryrefslogtreecommitdiff
path: root/roombacomm-client/src/com/hackingroomba/roombacomm/net
diff options
context:
space:
mode:
authorIdo Hadanny <ido.hadanny@gmail.com>2011-09-22 21:30:13 +0300
committerIdo Hadanny <ido.hadanny@gmail.com>2011-09-22 21:30:13 +0300
commit53b947df484159f5934898ba6c0aa69a9c869007 (patch)
tree189d5903a67765c390de8f1e3423e6b80cde57d1 /roombacomm-client/src/com/hackingroomba/roombacomm/net
parent654ca6d59e99b5e72b05dfb917bc5888c4ba6b1b (diff)
added stuff
Diffstat (limited to 'roombacomm-client/src/com/hackingroomba/roombacomm/net')
-rw-r--r--roombacomm-client/src/com/hackingroomba/roombacomm/net/DumpMethods.java70
-rw-r--r--roombacomm-client/src/com/hackingroomba/roombacomm/net/RoombaCommTCPServer.java94
-rw-r--r--roombacomm-client/src/com/hackingroomba/roombacomm/net/SimpleTest.java131
-rw-r--r--roombacomm-client/src/com/hackingroomba/roombacomm/net/TextHttpServer.java109
4 files changed, 404 insertions, 0 deletions
diff --git a/roombacomm-client/src/com/hackingroomba/roombacomm/net/DumpMethods.java b/roombacomm-client/src/com/hackingroomba/roombacomm/net/DumpMethods.java
new file mode 100644
index 0000000..962629e
--- /dev/null
+++ b/roombacomm-client/src/com/hackingroomba/roombacomm/net/DumpMethods.java
@@ -0,0 +1,70 @@
+
+package com.hackingroomba.roombacomm.net;
+
+import java.lang.reflect.*;
+
+import com.hackingroomba.roombacomm.*;
+
+
+public class DumpMethods {
+
+ RoombaCommSerial rcs;
+
+ public static void main(String[] args) {
+ new DumpMethods(args);
+ }
+
+ public DumpMethods(String[] args) {
+ rcs = new RoombaCommSerial();
+
+ if( args.length == 0 ) {
+ dumpMethods(rcs);
+ return;
+ }
+
+ String method_name = args[0];
+
+ /*
+ for( int i=1; i<args.length; i++ ) {
+ String s = args[i];
+ try { a0 = Integer.parseInt( s );
+ } catch( Exception e ) { }
+
+ }
+ */
+ }
+
+ public void dumpMethods(Object obj) {
+ //Object result = method.invoke(obj, new Object[0]);
+ //Class c = Class.forName("roombacomm.RoombaComm");
+ try {
+ Method m[] = obj.getClass().getMethods();
+ for( int i=0; i< m.length; i++ )
+ System.out.println( m[i].getName() +" -- "+ m[i].toString() );
+ } catch( Exception e ) {
+ e.printStackTrace();
+ }
+ }
+
+ public void getMethod( String name, Object obj ) {
+ try {
+ String mname = name;
+ Class[] types = new Class[] {};
+ Method method = obj.getClass().getMethod(mname, types);
+ System.out.println("class: "+method.getDeclaringClass());
+ System.out.println("method: "+method.toString());
+ } catch( Exception e ) {
+ e.printStackTrace();
+ }
+ }
+
+}
+ /*
+ try {
+ serialEventMethod =
+ parent.getClass().getMethod("serialEvent",
+ new Class[] { Serial.class });
+ } catch (Exception e) {
+ // no such method, or an error.. which is fine, just ignore
+ }
+ */
diff --git a/roombacomm-client/src/com/hackingroomba/roombacomm/net/RoombaCommTCPServer.java b/roombacomm-client/src/com/hackingroomba/roombacomm/net/RoombaCommTCPServer.java
new file mode 100644
index 0000000..d93ab2d
--- /dev/null
+++ b/roombacomm-client/src/com/hackingroomba/roombacomm/net/RoombaCommTCPServer.java
@@ -0,0 +1,94 @@
+/*
+ * RoombaComm TCP Interface
+ *
+ *
+ * Copyright (c) 2005 Tod E. Kurt, tod@todbot.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ *
+ */
+
+
+package com.hackingroomba.roombacomm.net;
+
+import java.net.*;
+import java.io.*;
+
+public class RoombaCommTCPServer
+{
+ // default port
+ int port = 8765;
+
+ // the shutdown command received
+ private boolean shutdown = false;
+
+ public RoombaCommTCPServer() {
+ }
+
+ public void await() {
+ ServerSocket serverSocket = null;
+ try {
+ serverSocket = new ServerSocket(port, 1, null );
+ // InetAddress.getByName("127.0.0.1"));
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ /*
+ // Loop waiting for a request
+ while (!shutdown) {
+ Socket socket = null;
+ InputStream input = null;
+ OutputStream output = null;
+ try {
+ socket = serverSocket.accept();
+ input = socket.getInputStream();
+ output = socket.getOutputStream();
+
+ if( input.available() ) {
+ }
+ // create Request object and parse
+ Request request = new Request(input);
+ request.parse();
+
+ // create Response object
+ Response response = new Response(output);
+ response.setRequest(request);
+ response.sendStaticResource();
+
+ // Close the socket
+ socket.close();
+
+ //check if the previous URI is a shutdown command
+ shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ continue;
+ }
+ }
+ */
+ }
+
+ public static void main(String[] args) {
+ RoombaCommTCPServer server = new RoombaCommTCPServer();
+ server.await();
+ }
+
+}
+
+
diff --git a/roombacomm-client/src/com/hackingroomba/roombacomm/net/SimpleTest.java b/roombacomm-client/src/com/hackingroomba/roombacomm/net/SimpleTest.java
new file mode 100644
index 0000000..7f45ad1
--- /dev/null
+++ b/roombacomm-client/src/com/hackingroomba/roombacomm/net/SimpleTest.java
@@ -0,0 +1,131 @@
+/*
+ * roombacomm.net.SimpleTest
+ *
+ * Copyright (c) 2005 Tod E. Kurt, tod@todbot.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ *
+ */
+
+package com.hackingroomba.roombacomm.net;
+
+import com.hackingroomba.roombacomm.*;
+
+/**
+ * A simple test of RoombaComm and RoombaCommSerial functionality.
+ * <p>
+ * Run it with something like: <pre>
+ * java roombacomm.SimpleTest /dev/cu.KeySerial1
+ * </pre>
+ *
+ */
+public class SimpleTest {
+
+ static String usage =
+ "Usage: \n"+
+ " roombacomm.net.SimpleTest <host:port> [options]\n" +
+ "where [options] can be one or more of:\n"+
+ " -debug -- turn on debug output\n"+
+ //" -hwhandshake -- use hardware-handshaking, for Windows Bluetooth\n"+
+ //" -flush -- flush on sends(), normally not needed\n"+
+ "\n";
+ static boolean debug = false;
+ static boolean hwhandshake = false;
+ static boolean flush = false;
+
+ public static void main(String[] args) {
+ if( args.length == 0 ) {
+ System.out.println( usage );
+ System.exit(0);
+ }
+
+ String portname = args[0]; // e.g. "/dev/cu.KeySerial1"
+
+ for( int i=1; i < args.length; i++ ) {
+ if( args[i].endsWith("debug") )
+ debug = true;
+ //else if( args[i].endsWith("hwhandshake") )
+ // hwhandshake = true;
+ //else if( args[i].endsWith("flush") )
+ // flush = true;
+ }
+
+ RoombaComm roombacomm = new RoombaCommTCPClient();
+
+ roombacomm.debug = debug;
+ //roombacomm.waitForDSR = hwhandshake;
+ //roombacomm.flushOutput = flush;
+
+ String portlist[] = roombacomm.listPorts();
+ System.out.println("Available ports:");
+ for(int i=0;i<portlist.length;i++)
+ System.out.println(" "+i+": "+portlist[i]);
+
+ if( ! roombacomm.connect( portname ) ) {
+ System.out.println("Couldn't connect to "+portname);
+ System.exit(1);
+ }
+
+ System.out.println("Roomba startup on port "+portname);
+ roombacomm.startup();
+ roombacomm.control();
+ roombacomm.pause(30);
+
+ System.out.println("Checking for Roomba... ");
+ if( roombacomm.updateSensors() )
+ System.out.println("Roomba found!");
+ else
+ System.out.println("No Roomba. :( Is it turned on?");
+
+ //roombacomm.updateSensors();
+
+ System.out.println("Playing some notes");
+ roombacomm.playNote( 72, 10 ); // C
+ roombacomm.pause( 200 );
+ roombacomm.playNote( 79, 10 ); // G
+ roombacomm.pause( 200 );
+ roombacomm.playNote( 76, 10 ); // E
+ roombacomm.pause( 200 );
+
+ System.out.println("Spinning left, then right");
+ roombacomm.spinLeft();
+ roombacomm.pause(1000);
+ roombacomm.spinRight();
+ roombacomm.pause(1000);
+ roombacomm.stop();
+
+ System.out.println("Going forward, then backward");
+ roombacomm.goForward();
+ roombacomm.pause(1000);
+ roombacomm.goBackward();
+ roombacomm.pause(1000);
+ roombacomm.stop();
+
+
+ System.out.println("Moving via send()");
+ byte cmd[] = {(byte)RoombaComm.DRIVE,
+ (byte)0x00,(byte)0xfa, (byte)0x00,(byte)0x00};
+ roombacomm.send( cmd ) ;
+ roombacomm.pause(1000);
+ roombacomm.stop();
+
+ System.out.println("Disconnecting");
+ roombacomm.disconnect();
+
+ System.out.println("Done");
+ }
+}
+
diff --git a/roombacomm-client/src/com/hackingroomba/roombacomm/net/TextHttpServer.java b/roombacomm-client/src/com/hackingroomba/roombacomm/net/TextHttpServer.java
new file mode 100644
index 0000000..17450d3
--- /dev/null
+++ b/roombacomm-client/src/com/hackingroomba/roombacomm/net/TextHttpServer.java
@@ -0,0 +1,109 @@
+
+
+
+package com.hackingroomba.roombacomm.net;
+
+import java.net.*;
+import java.io.*;
+
+public class TextHttpServer {
+
+ int port = 6767;
+ /*
+ String cmds[] =
+ {
+ "reset", // zero args
+ "stop", // zero args
+ "goforward", // one optional arg
+ "gobackward", // one optional arg
+ "spinleft", // one optional arg
+ "spinright", // one optional arg
+ "beep", // two args
+ };
+ */
+
+ // the shutdown command received
+ private boolean shutdown = false;
+
+ public static void main(String[] args) {
+ TextHttpServer server = new TextHttpServer();
+ server.await();
+ }
+
+
+ public void await() {
+ System.out.println("awaiting connections on port "+port+"...");
+
+ ServerSocket serverSocket = null;
+ try {
+ serverSocket = new ServerSocket(port, 1, null);
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+
+ // Loop waiting for a request
+ while (!shutdown) {
+ Socket socket = null;
+ InputStream input = null;
+ OutputStream output = null;
+ try {
+ socket = serverSocket.accept(); // this blocks
+
+ input = socket.getInputStream();
+ output = socket.getOutputStream();
+
+ StringBuffer request = parseRequest( input );
+ String uristr = parseUri( request.toString() );
+ System.out.println("uristr: "+uristr);
+
+ URI uri = new URI( uristr );
+ System.out.println("path:"+uri.getPath()+", query:"+uri.getQuery());
+
+ // Close the socket
+ socket.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ //System.exit(1);
+ }
+ }
+
+ }
+
+
+
+ public StringBuffer parseRequest(InputStream input) {
+ // Read a set of characters from the socket
+ StringBuffer request = new StringBuffer(2048);
+ int i;
+ byte[] buffer = new byte[2048];
+ try {
+ i = input.read(buffer);
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ i = -1;
+ }
+ for (int j=0; j<i; j++) {
+ request.append((char) buffer[j]);
+ }
+ System.out.print(request.toString());
+
+ return request;
+ }
+
+ private String parseUri(String requestString) {
+ int index1, index2;
+ index1 = requestString.indexOf(' ');
+ if (index1 != -1) {
+ index2 = requestString.indexOf(' ', index1 + 1);
+ if (index2 > index1)
+ return requestString.substring(index1 + 1, index2);
+ }
+ return null;
+ }
+
+
+}