1 package lejos.pc.tools;
\r
5 import lejos.pc.comm.*;
\r
8 * Socket Proxy for NXT
\r
9 * Has two main functions. The first is to connect to an existing
\r
10 * server socket on the specified port. The second function is to
\r
11 * create a socket server. In this case the proxy assumes that the
\r
12 * NXT will then send a command to inform the proxy of the next action
\r
15 * Currently only supports TCP connections
\r
17 * @author Ranulf Green
\r
20 public class SocketProxy {
\r
22 private String host;
\r
24 private DataInputStream inFromNXT;
\r
25 private DataOutputStream outToNXT;
\r
26 private ServerSocket serverSocket;
\r
27 private Socket sock;
\r
31 * An instance of Socket proxy will allow for transparent forwarding
\r
32 * of messages between server and NXT using a socket connection
\r
33 * @param NXTName The name of the NXT to connect to
\r
34 * @param NXTaddress The physical address of the NXT
\r
36 public SocketProxy(String NXTName, String NXTaddress){
\r
38 // create a Bluetooth connection with the NXT
\r
39 NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
\r
40 NXTInfo[] nxtInfo = new NXTInfo[1];
\r
42 nxtInfo[0] = new NXTInfo(NXTName,NXTaddress);
\r
44 System.out.println("Connecting to " + nxtInfo[0].btResourceString);
\r
46 // check to see if NXT really exists, if not exit
\r
47 boolean isOpen = false;
\r
49 isOpen = nxtComm.open(nxtInfo[0]);
\r
50 } catch(NXTCommException n) {
\r
51 System.err.println(n.getMessage());
\r
55 System.out.println("Failed to open " + nxtInfo[0].name);
\r
59 inFromNXT = new DataInputStream(nxtComm.getInputStream());
\r
60 outToNXT = new DataOutputStream(nxtComm.getOutputStream());
\r
62 // check to see if socket is a server or a client
\r
63 boolean isServer = inFromNXT.readBoolean();
\r
67 newSocketConnection();
\r
70 catch (UnknownHostException e) {e.printStackTrace();}
\r
71 catch (IOException e) {e.printStackTrace();}
\r
75 * Creates a new socket server if instructed by the NXT
\r
76 * @throws IOException
\r
78 private void newSocketServer() throws IOException{
\r
79 int port = inFromNXT.readInt();
\r
80 serverSocket = new ServerSocket(port);
\r
81 boolean cmdMode = true;
\r
83 // wait for command from NXT
\r
84 byte command = inFromNXT.readByte();
\r
86 waitForConnection();
\r
89 // TODO support for other socket server functions
\r
94 * Allows negotiation of the accept() method of Socket server
\r
95 * @throws IOException
\r
97 private void waitForConnection()throws IOException{
\r
99 sock = serverSocket.accept();
\r
101 // inform the NXT of the new Connection
\r
102 outToNXT.writeBoolean(true);
\r
105 DataInputStream inFromSocket = new DataInputStream(sock.getInputStream());
\r
106 DataOutputStream outToSocket = new DataOutputStream(sock.getOutputStream());
\r
108 // listen for incoming data from socket
\r
109 new forward(sock, inFromSocket, outToNXT);
\r
111 // listen for incoming data from NXT
\r
112 new forwardNXT(sock, inFromNXT, outToSocket);
\r
117 * Allows for a connection to be made using the details supplied from the NXT
\r
118 * @throws UnknownHostException
\r
119 * @throws IOException
\r
121 private void newSocketConnection() throws UnknownHostException, IOException
\r
123 // the first byte from the NXT contains the length of the host name in chars
\r
124 int len = inFromNXT.readByte();
\r
125 char[] hostChars = new char[len];
\r
127 // following the first byte the host name is transmitted
\r
128 for(int i=0;i<len;i++){
\r
129 hostChars[i] = inFromNXT.readChar();
\r
131 // following the host name an int containing the port number of the socket to connect to
\r
133 port = inFromNXT.readInt();
\r
134 host = new String(hostChars);
\r
136 System.out.println("Host: " + host + " port: " + port);
\r
137 // create a socket connection with the specified host using the specified port
\r
139 sock = new Socket(host, port);
\r
140 outToNXT.writeBoolean(true);
\r
143 DataInputStream inFromSocket = new DataInputStream(sock.getInputStream());
\r
145 DataOutputStream outToSocket = new
\r
146 DataOutputStream(sock.getOutputStream());
\r
148 // listen for incoming data from socket
\r
149 new forward(sock, inFromSocket, outToNXT);
\r
151 // listen for incoming data from NXT
\r
152 new forwardNXT(sock, inFromNXT, outToSocket);
\r
156 * Allows for the forwarding of messages from Socket to NXT
\r
157 * @author Ranulf Green
\r
159 private class forward extends Thread{
\r
160 private DataOutputStream dout;
\r
161 private DataInputStream din;
\r
163 private Socket sock;
\r
167 * @param sock the socket with which the connection is made
\r
168 * @param dis the input stream to read
\r
169 * @param dos the output stream to forward to
\r
171 public forward(Socket sock, DataInputStream dis, DataOutputStream dos){
\r
179 * Causes a new thread to be invoked
\r
183 boolean flushed = true;
\r
185 int a = din.available();
\r
187 //System.out.println("Reading!" + a);
\r
189 int in = din.readUnsignedByte();
\r
191 //System.out.println("In Thread: Socket closed:" + in);
\r
195 //System.out.println("Sending " + in);
\r
196 dout.writeByte(in);
\r
197 }else if(!flushed){
\r
198 //System.out.println("Flushing");
\r
205 } catch (InterruptedException e) {}
\r
207 }catch(IOException ioe){ioe.printStackTrace();};
\r
212 * Class to forward messages from NXT to socket
\r
213 * @author Ranulf Green
\r
216 private class forwardNXT extends Thread{
\r
217 private DataOutputStream dout;
\r
218 private DataInputStream din;
\r
220 private Socket sock;
\r
225 * @param dis input stream from NXT
\r
226 * @param dos output stream to socket
\r
228 public forwardNXT(Socket sock, DataInputStream dis, DataOutputStream dos){
\r
237 * causes a new thread to be invoked
\r
242 int in = din.readUnsignedByte();
\r
244 System.out.println("In Thread: Socket closed:" + in);
\r
249 dout.writeByte(in);
\r
255 } catch (InterruptedException e) {}
\r
257 }catch(IOException ioe){ioe.printStackTrace();};
\r
261 public static void main(String[] args) {
\r
262 if(args.length!=2){
\r
263 System.out.println("USAGE: java SocketProxy <NXTName> <NXTAddress>");
\r
266 new SocketProxy(args[0],args[1]);
\r