Exercise 7 Solution: Network code
BendySender:
1
24
25
26 package org.sunspotworld;
27
28 import com.sun.spot.io.j2me.radiostream.RadiostreamConnection;
29 import com.sun.spot.peripheral.NoRouteException;
30 import com.sun.spot.peripheral.Spot;
31 import com.sun.spot.util.IEEEAddress;
32 import com.sun.squawk.VM;
33 import java.io.DataInputStream;
34 import java.io.DataOutputStream;
35 import java.io.IOException;
36 import javax.microedition.io.Connector;
37
38
41 public class BendySender {
42
43 private String myAddress;
44 private String otherAddress;
45 private long ourAddr;
46 private final int PORT = 33;
47 private static final int XMIT_POWER = -31;
48 private String otherSPOTAddress;
49 private boolean connected;
50 private RadiostreamConnection conn = null;
51 private DataInputStream dis = null;
52 private DataOutputStream dos = null;
53 private Bendy sensor;
54
55
58 public BendySender() {
59 init();
60 }
61
62
65 public void init() {
66 ourAddr = Spot.getInstance().getRadioPolicyManager().getIEEEAddress();
67 Spot.getInstance().getRadioPolicyManager().setOutputPower(XMIT_POWER);
68 System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));
69 myAddress = IEEEAddress.toDottedHex(ourAddr);
70 otherSPOTAddress = VM.getManifestProperty("ServoSPOT");
71 System.out.println("Other radio address = " + otherSPOTAddress);
72 try {
73 conn = (RadiostreamConnection) Connector.open("radiostream://" + getOtherAddress() + ":" + PORT);
74 dis = (DataInputStream) conn.openDataInputStream();
75 dos = (DataOutputStream) conn.openDataOutputStream();
76 } catch (IOException e) {
77 e.printStackTrace();
78 }
79 }
80
81
82
83
93 public boolean connect() {
94 System.out.println("Starting connection to " + getOtherAddress() + " on port " + PORT);
95 connected = false;
96 while (!connected) {
97
98 try {
99 System.out.println("Sending min:" + sensor.getBendMin() + ";center:" + sensor.getBendCenter() + ";max:"
» + sensor.getBendMax());
100 dos.writeUTF("min:" + sensor.getBendMin() + ";center:" + sensor.getBendCenter() + ";max:" +
» sensor.getBendMax());
101 dos.flush();
102 String response = dis.readUTF();
103 if (response.equalsIgnoreCase("GO")) {
104 System.out.println("Calibration complete!");
105 connected = true;
106 }
107 } catch (IOException ex) {
108 ex.printStackTrace();
109 }
110
111 }
112 return connected;
113 }
114
115
118 public void closeConnection() {
119 if (connected == false) {
120 return;
121 }
122 connected = false;
123 try {
124 dos.close();
125 dis.close();
126 conn.close();
127 } catch (IOException e) {
128 e.printStackTrace();
129 }
130 }
131
132
138 public void send(String message) throws IOException, NoRouteException {
139 dos.writeUTF(message);
140 dos.flush();
141 }
142
143 public String getAddress() {
144 return myAddress;
145 }
146
147 public String getOtherAddress() {
148 return otherAddress;
149 }
150
151 }
Servo Receiver:
1
24
25 package org.sunspotworld;
26
27 import com.sun.spot.io.j2me.radiostream.RadiostreamConnection;
28 import com.sun.spot.peripheral.NoRouteException;
29 import com.sun.spot.peripheral.Spot;
30 import com.sun.spot.peripheral.TimeoutException;
31 import com.sun.spot.util.IEEEAddress;
32 import com.sun.spot.util.Utils;
33 import com.sun.squawk.VM;
34 import java.io.DataInputStream;
35 import java.io.DataOutputStream;
36 import java.io.IOException;
37 import javax.microedition.io.Connector;
38
39
40
45 public class ServoReceiver implements Runnable {
46
47 private String myAddress;
48 private String otherAddress;
49 private final int PORT = 33;
50
53 private static int XMIT_POWER = -31;
54
57 private boolean connected;
58 private RadiostreamConnection conn = null;
59 private DataInputStream dis = null;
60 private DataOutputStream dos = null;
61 private int max = 0;
62 private int min = 0;
63 private int center = 0;
64 private ServoMover owner;
65
66
67
68
72 public ServoReceiver(ServoMover owner) {
73 setOwner(owner);
74 init();
75
76 }
77
78
79
80
83 public void init() {
84 long ourAddr = Spot.getInstance().getRadioPolicyManager().getIEEEAddress();
85 Spot.getInstance().getRadioPolicyManager().setOutputPower(XMIT_POWER);
86 System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));
87 otherAddress = VM.getManifestProperty("BendySPOT");
88 System.out.println("Other radio address = " + otherAddress);
89 System.out.println("Starting connection to " + getOtherAddress() + " on port " + PORT);
90 try {
91 conn = (RadiostreamConnection) Connector.open("radiostream://" + getOtherAddress() + ":" + PORT);
92 dis = (DataInputStream) conn.openDataInputStream();
93 dos = (DataOutputStream) conn.openDataOutputStream();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97 }
98
99
103 public boolean connect() {
104 connected = false;
105 String tmp = null;
106
107 while (!connected) {
108 try {
109 tmp = dis.readUTF();
110 System.out.println("Received: " + tmp);
111 } catch (TimeoutException e) {
112 System.out.println("Timeout... other end is not responding");
113 } catch (IOException e1) {
114 System.out.println("Exception on readUTF");
115 }
116
117 if (tmp != null) {
118 if (tmp.startsWith("min")) {
119 parseConfig(tmp);
120 try {
121 send("GO");
122 connected = true;
123 } catch (IOException ex) {
124 ex.printStackTrace();
125 }
126 }
127 }
128 }
129 return connected;
130 }
131
132
135 public void run() {
136
137 while (true) {
138 try {
139 owner.moveServo(readValue());
140 } catch (IOException ex) {
141 ex.printStackTrace();
142 }
143 }
144 }
145
146
151 private int readValue() throws IOException {
152 String val = dis.readUTF();
153 if (val.startsWith("min")) {
154 parseConfig(val);
155 try {
156 send("GO");
157 } catch (IOException ex) {
158 ex.printStackTrace();
159 }
160 return this.getCenter();
161 } else {
162 return Integer.parseInt(val);
163 }
164 }
165
166
170 private void parseConfig(String config) {
171 String[] vals = Utils.split(config, ';');
172 this.min = Integer.parseInt(Utils.split(vals[0], ':')[1]);
173 this.center = Integer.parseInt(Utils.split(vals[1], ':')[1]);
174 this.max = Integer.parseInt(Utils.split(vals[2], ':')[1]);
175 System.out.println("Received Configuration data ... Min: " + this.min + " Center: " + this.center + " Max: " +
» this.max);
176 }
177
178
179
180
183 public void closeConnection() {
184 if (connected == false) {
185 return;
186 }
187 connected = false;
188 try {
189 dos.close();
190 dis.close();
191 conn.close();
192 } catch (IOException e) {
193 e.printStackTrace();
194 }
195 }
196
197
203 public void send(String message) throws IOException, NoRouteException {
204 dos.writeUTF(message);
205 dos.flush();
206 }
207
208
209
213 public String getMyAddress() {
214 return myAddress;
215 }
216
217
221 public void setMyAddress(String myAddress) {
222 this.myAddress = myAddress;
223 }
224
225
229 public String getOtherAddress() {
230 return otherAddress;
231 }
232
233
237 public void setOtherAddress(String otherAddress) {
238 this.otherAddress = otherAddress;
239 }
240
241
245 public int getMax() {
246 return max;
247 }
248
249
253 public int getMin() {
254 return min;
255 }
256
257
261 public int getCenter() {
262 return center;
263 }
264
265
269 public void setOwner(ServoMover owner) {
270 this.owner = owner;
271 }
272 }
Go on to summary