OSDN Git Service

24e90d486885dc3d92eff6b417c3ee34a733d1cb
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / nxt / comm / NXTServerSocket.java
1 import java.io.DataInputStream;\r
2 import java.io.DataOutputStream;\r
3 import java.io.IOException;\r
4 \r
5 import lejos.nxt.comm.BTConnection;\r
6 import lejos.nxt.comm.NXTSocket;\r
7 import lejos.nxt.comm.USBConnection;\r
8 \r
9 public class NXTServerSocket {\r
10 \r
11         private int port;\r
12         private BTConnection btc;\r
13         private USBConnection usbc;\r
14         private boolean isBluetooth;\r
15         private final boolean isServer = true;\r
16         \r
17         \r
18         \r
19         /**\r
20          * Constructor. Creates a new Server Socket over an open bluetooth connection\r
21          * @param port The port to listen on\r
22          * @param btc The bluetooth connection to open\r
23          * @throws IOException \r
24          */\r
25         public NXTServerSocket(int port, BTConnection btc) throws IOException{\r
26                 this.port = port;\r
27                 this.btc = btc;\r
28                 isBluetooth = true;\r
29                 negotiateConnection();\r
30         }\r
31         \r
32         /**\r
33          * Constructor. Creates a new Server Socket over an open usb connection\r
34          * @param port The port to listen on\r
35          * @param usbc The usb connection to open\r
36          * @throws IOException \r
37          */\r
38         public NXTServerSocket(int port, USBConnection usbc) throws IOException{\r
39                 this.port = port;\r
40                 this.usbc = usbc;\r
41                 isBluetooth = false;\r
42                 negotiateConnection();\r
43         }\r
44         \r
45         private void negotiateConnection() throws IOException{\r
46                 DataOutputStream dos = openDataOutputStream();\r
47                 dos.writeBoolean(isServer);\r
48                 dos.writeInt(port);\r
49                 dos.flush();\r
50                 dos.close();\r
51                 \r
52         }\r
53         \r
54         private DataOutputStream openDataOutputStream() throws IOException{\r
55                 DataOutputStream dos;\r
56                 if(isBluetooth){dos = new DataOutputStream(btc.openOutputStream());}\r
57                 else{dos = new DataOutputStream(usbc.openOutputStream());}\r
58                 return dos;\r
59         }\r
60         \r
61         private DataInputStream openDataInputStream() throws IOException{\r
62                 DataInputStream dis;\r
63                 if(isBluetooth){dis = new DataInputStream(btc.openInputStream());}\r
64                 else{dis = new DataInputStream(usbc.openInputStream());}\r
65                 return dis;\r
66         }\r
67         \r
68         /**\r
69          * Waits untill there is a socket connection available. When this becomes true\r
70          * a new NXTSocket is returned\r
71          * @return NXTSocket\r
72          * @throws IOException \r
73          */\r
74         public NXTSocket accept() throws IOException{\r
75                 DataOutputStream dos = openDataOutputStream();\r
76                 DataInputStream dis = openDataInputStream();\r
77                 // inform the proxy of the command\r
78                 dos.writeByte(1);\r
79                 dos.flush();\r
80                 dis.readBoolean();\r
81                 dos.close();\r
82                 dis.close();\r
83                 if(isBluetooth){return new NXTSocket(btc);}\r
84                 else{return new NXTSocket(usbc);}\r
85                 \r
86         }\r
87 \r
88 }\r