1 | package vnsim.applications.emissions; |
---|
2 | |
---|
3 | import java.io.ByteArrayOutputStream; |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.Serializable; |
---|
6 | import java.nio.ByteBuffer; |
---|
7 | |
---|
8 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
9 | import vnsim.core.CarInfo; |
---|
10 | import vnsim.core.Communicator; |
---|
11 | import vnsim.map.object.Globals; |
---|
12 | import vnsim.map.object.PeanoKey; |
---|
13 | import vnsim.map.object.Point; |
---|
14 | import vnsim.map.object.Road; |
---|
15 | import vnsim.network.RadioDev; |
---|
16 | |
---|
17 | /* |
---|
18 | * Class that simulates the car: |
---|
19 | * the car is equipped with a radio device and is capable |
---|
20 | * of transmitting and receiving messages sent on the network |
---|
21 | * |
---|
22 | * @author: Adriana Szekeres |
---|
23 | */ |
---|
24 | |
---|
25 | public class EmissionsCar extends SimulatedCarInfo{ |
---|
26 | |
---|
27 | /* Constructors */ |
---|
28 | public EmissionsCar() { |
---|
29 | super(); |
---|
30 | } |
---|
31 | |
---|
32 | public EmissionsCar(int vehicleId) { |
---|
33 | super(vehicleId); |
---|
34 | } |
---|
35 | |
---|
36 | public EmissionsCar(int vehicleId, byte lane, double speed, short roadIdx, short pointIdx, byte direction, |
---|
37 | double offset) { |
---|
38 | super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); |
---|
39 | } |
---|
40 | |
---|
41 | public EmissionsCar(SimulatedCarInfo sci) { |
---|
42 | super(sci); |
---|
43 | } |
---|
44 | |
---|
45 | /* Function called when this car received a packet - the packet was previously |
---|
46 | * encapsulated into a scheduled event that is now "played" in the Engine class, |
---|
47 | * in method playEvent() |
---|
48 | */ |
---|
49 | @Override |
---|
50 | public void onReceive(byte[] bytesReceived, Serializable message, Communicator sender){ |
---|
51 | if (!isEquipped || bytesReceived == null) |
---|
52 | return; |
---|
53 | |
---|
54 | /* PROT_SETOFCARS: Diffusion protocol - read TrafficView paper |
---|
55 | * |
---|
56 | * personal note: I'm not very sure about how much info |
---|
57 | * gets to the TrafficLights |
---|
58 | */ |
---|
59 | if (bytesReceived[0] == Globals.PROT_SETOFCARS){ |
---|
60 | parsePacket(bytesReceived); |
---|
61 | } |
---|
62 | |
---|
63 | /* PROT_NEIGHBOR_DISCOVERY: Is this flooding? Or just one hop propagation? |
---|
64 | * As far as I can see, the car doesn't propagate this message. |
---|
65 | * |
---|
66 | */ |
---|
67 | if (bytesReceived[0] == Globals.PROT_NEIGHBOR_DISCOVERY){ |
---|
68 | // first record in message - the sender |
---|
69 | CarInfo sc = new CarInfo(); |
---|
70 | sc.wrap(bytesReceived, 2); |
---|
71 | |
---|
72 | boolean added = false; |
---|
73 | for (int i=0;i<trafficDB.size();i++) |
---|
74 | if ((trafficDB.get(i)).getVehicleId() == sc.getVehicleId()){ |
---|
75 | synchronized(trafficDB){ |
---|
76 | trafficDB.set(i,sc); |
---|
77 | added = true; |
---|
78 | } |
---|
79 | break; |
---|
80 | } |
---|
81 | if (!added) |
---|
82 | synchronized(trafficDB){ |
---|
83 | trafficDB.add(sc); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /* |
---|
88 | * PROT_TL_FEEDBACK: the message received from the Traffic Lights |
---|
89 | * |
---|
90 | */ |
---|
91 | if (bytesReceived[0] == Globals.PROT_TL_FEEDBACK) { |
---|
92 | |
---|
93 | /* |
---|
94 | * The packet contains: |
---|
95 | * - the protocolType = first byte = Globals.PROT_TL_FEEDBACK |
---|
96 | * - the index of the traffic lights intersection |
---|
97 | * - the cycleLength = the next short in the packet |
---|
98 | * - the number of segments the intersection controls |
---|
99 | * - for each segment controlled (the segment is a DirectedRoadSegment class): |
---|
100 | * - the road index - | |
---|
101 | * - the point index - | |
---|
102 | * - the segment's direction |- see DirectedRoadSegment class |
---|
103 | * - the current color |
---|
104 | * - the remaining time |
---|
105 | * - the remaining no of cycles (?) |
---|
106 | * - the distance between what (?) - don't know yet |
---|
107 | * |
---|
108 | * - the following commented fragment of code checks whether if the first segment |
---|
109 | * received in the packet is really the first segment |
---|
110 | * |
---|
111 | * personal note: I don't get it why this check is necessary |
---|
112 | */ |
---|
113 | |
---|
114 | /*PeanoKey pkx = null; |
---|
115 | if (getRealPos().intersection != null) { |
---|
116 | Road rx = Globals.map.roads.get(getRealPos().intersection.segments.get(0).roadIndex); |
---|
117 | Point px = rx.points.get(getRealPos().intersection.segments.get(0).pointIndex); |
---|
118 | pkx = Globals.map.peanoKeys.get(px.getPeanoKeyIdx()); |
---|
119 | } |
---|
120 | |
---|
121 | ByteBuffer bb = ByteBuffer.wrap(bytesReceived); |
---|
122 | bb.get(); |
---|
123 | |
---|
124 | int cycleLength = bb.getShort(); |
---|
125 | int segno = bb.get(); |
---|
126 | int ridx, pidx; |
---|
127 | Road r = null; |
---|
128 | Point p = null; |
---|
129 | try { |
---|
130 | ridx = bb.getShort(); |
---|
131 | r = Globals.map.roads.get(ridx); |
---|
132 | pidx = bb.getShort(); |
---|
133 | p = r.points.get(pidx); |
---|
134 | PeanoKey pk = Globals.map.peanoKeys.get(p.getPeanoKeyIdx()); |
---|
135 | if (pkx != null && !pk.equals(pkx)) |
---|
136 | return; |
---|
137 | } catch (Exception e) { |
---|
138 | System.out.println("Bad TL feedback message: \n"); |
---|
139 | e.printStackTrace(); |
---|
140 | return; |
---|
141 | }*/ |
---|
142 | |
---|
143 | System.out.println("Car received message from TRAFFIC LIGHT"); |
---|
144 | ByteBuffer bb = ByteBuffer.wrap(bytesReceived); |
---|
145 | bb.get(); //get rid of the first byte = the protocol type (Globals.PROT_TL_FEEDBACK) |
---|
146 | |
---|
147 | int intersectionIndex = bb.getInt(); |
---|
148 | |
---|
149 | /* check if the packet was send by the next intersection traffic light */ |
---|
150 | if (intersection != null) |
---|
151 | if (intersectionIndex != Globals.map.allIntersections.indexOf(intersection)) |
---|
152 | /* if packet not from the next intersection drop? or forward to other |
---|
153 | * possible interested cars? |
---|
154 | */ |
---|
155 | return; |
---|
156 | |
---|
157 | int cycleLength = bb.getShort(); |
---|
158 | int segno = bb.get(); |
---|
159 | |
---|
160 | phaseRemainingTime = -1; |
---|
161 | currentPhase = -1; |
---|
162 | queueSize = -1; |
---|
163 | infoTime = Globals.engine.crtTime; |
---|
164 | interGreenPhaseTime = -1; |
---|
165 | this.cycleLength = cycleLength; |
---|
166 | |
---|
167 | for (int i = 0; i < segno; i++) { |
---|
168 | |
---|
169 | int ridx = bb.getShort(); |
---|
170 | Road r = Globals.map.roads.get(ridx); |
---|
171 | int pidx = bb.getShort(); |
---|
172 | //Point p = r.points.get(pidx); |
---|
173 | int dir = bb.get(); |
---|
174 | int color = bb.get(); |
---|
175 | int remaining = bb.getShort(); |
---|
176 | int interPhase = bb.getShort(); |
---|
177 | |
---|
178 | double qsize = -1; |
---|
179 | for (int j = 1; j <= r.laneNo; j++) { |
---|
180 | double qs = bb.getDouble(); |
---|
181 | if (j == this.getLane()) |
---|
182 | qsize = qs; |
---|
183 | } |
---|
184 | if (ridx == this.roadIdx |
---|
185 | && ((this.getPointIdx() > pidx && this.direction == 0 && dir == 1) || (this.getPointIdx() < pidx |
---|
186 | && this.direction == 1 && dir == 0))) { |
---|
187 | phaseRemainingTime = remaining; |
---|
188 | currentPhase = color; |
---|
189 | queueSize = qsize; |
---|
190 | interGreenPhaseTime = interPhase; |
---|
191 | if (Globals.engine.startedWithGUI && Globals.demo.mv.currentCar != null |
---|
192 | && this.equals(Globals.demo.mv.currentCar)) { |
---|
193 | Globals.demo.st.setSemTime(color, remaining); |
---|
194 | Globals.demo.st.setDemand("sw", (int) (queueSize * 1000), 0, false); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | /*if (i < segno - 1) |
---|
199 | try { |
---|
200 | ridx = bb.getShort(); |
---|
201 | r = Globals.map.roads.get(ridx); |
---|
202 | pidx = bb.getShort(); |
---|
203 | p = r.points.get(pidx); |
---|
204 | } catch (Exception e) { |
---|
205 | System.out.println("Bad TL feedback message: \n"); |
---|
206 | e.printStackTrace(); |
---|
207 | return; |
---|
208 | } |
---|
209 | */ |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | public byte[] prepareMessage(int messageType){ |
---|
216 | byte[] bytesToSend = null; |
---|
217 | //if (realPos.finished) |
---|
218 | // return null; |
---|
219 | if (!isEquipped) |
---|
220 | return null; |
---|
221 | |
---|
222 | //for messageType = 0, standard neighborDiscovery message |
---|
223 | if (messageType == STANDARD_MESSAGE_ID){ |
---|
224 | //neighbor discovery |
---|
225 | if (messageType == Globals.PROT_NEIGHBOR_DISCOVERY){ |
---|
226 | byte[] localinfo = toBytes(); |
---|
227 | ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); |
---|
228 | outBytes.write(Globals.PROT_NEIGHBOR_DISCOVERY); |
---|
229 | outBytes.write(CRTDIR); |
---|
230 | try{ |
---|
231 | outBytes.write(localinfo); |
---|
232 | }catch(IOException e){ |
---|
233 | } |
---|
234 | bytesToSend = outBytes.toByteArray(); |
---|
235 | } |
---|
236 | if (messageType == Globals.PROT_SETOFCARS){ |
---|
237 | |
---|
238 | if (Globals.probabilisticModel){ |
---|
239 | decideTransmissionMode(); |
---|
240 | |
---|
241 | if (pulseMessage){ |
---|
242 | if (isPromiscuousMode() && speed > 1){ |
---|
243 | super.state = 0; |
---|
244 | setPromiscuousMode(false); |
---|
245 | } |
---|
246 | if (super.state == 3){ |
---|
247 | setPromiscuousMode(true); |
---|
248 | return null; |
---|
249 | } |
---|
250 | } |
---|
251 | else |
---|
252 | { |
---|
253 | setPromiscuousMode(false); |
---|
254 | super.state = 0; |
---|
255 | } |
---|
256 | } |
---|
257 | // if (li.getToSend().getRoadIdx() != li.getPrevSent().getRoadIdx()){ |
---|
258 | // state = CRTDIR; |
---|
259 | // } |
---|
260 | |
---|
261 | /* int carsno; |
---|
262 | ByteArrayOutputStream outBytes = null; |
---|
263 | |
---|
264 | while(true){ |
---|
265 | int roadIndex = -1, roadIndex1 = -1; |
---|
266 | int dir = -1, direction1 = -1; |
---|
267 | carsno = 1; |
---|
268 | int off = 0; |
---|
269 | outBytes = new ByteArrayOutputStream(); |
---|
270 | |
---|
271 | if (comState == CRTDIR){ |
---|
272 | roadIndex = getRoadIdx(); |
---|
273 | dir = getDirection(); |
---|
274 | }else |
---|
275 | if (comState == OPPDIR){ |
---|
276 | roadIndex = getRoadIdx(); |
---|
277 | dir = 1 - getDirection(); |
---|
278 | } |
---|
279 | |
---|
280 | outBytes.write(Globals.PROT_SETOFCARS); // leave room for number of records |
---|
281 | outBytes.write(0); // leave room for number of records |
---|
282 | outBytes.write(comState); |
---|
283 | off++; |
---|
284 | |
---|
285 | try{ |
---|
286 | //local information |
---|
287 | if (this.speed < 1 ){ |
---|
288 | super.state = 3; //promiscuous mode |
---|
289 | }else{ |
---|
290 | super.state = 0; |
---|
291 | } |
---|
292 | /* if (realPos.turningLeft) |
---|
293 | setSignal((byte)1); |
---|
294 | else |
---|
295 | setSignal((byte)0); |
---|
296 | byte[] car = toBytes(); |
---|
297 | outBytes.write(car); |
---|
298 | off += car.length; |
---|
299 | |
---|
300 | |
---|
301 | if (!pulseMessage){ |
---|
302 | ListIterator<CarInfo> it = trafficDB.listIterator(); |
---|
303 | for (; it.hasNext(); ){ |
---|
304 | CarInfo sc = it.next(); |
---|
305 | if (sc.getRoadIdx() == roadIndex && sc.getDirection() == dir){ |
---|
306 | car = sc.toBytes(); |
---|
307 | if (off + car.length > Globals.MTU_SIZE - 100){ |
---|
308 | // no more room in the packet for this car |
---|
309 | //TODO |
---|
310 | break; |
---|
311 | } |
---|
312 | outBytes.write(car); |
---|
313 | off += car.length; |
---|
314 | carsno ++; |
---|
315 | } |
---|
316 | |
---|
317 | } |
---|
318 | } |
---|
319 | }catch(IOException e){ |
---|
320 | System.out.println("Error sending cars"); |
---|
321 | e.printStackTrace(); |
---|
322 | } |
---|
323 | |
---|
324 | /* if (comState == OPPDIR && carsno == 1){ |
---|
325 | // no cars on the opposed movement |
---|
326 | // comState = REST; |
---|
327 | comState = CRTDIR; |
---|
328 | continue; |
---|
329 | }*/ |
---|
330 | // if (comState == REST && carsno == 1){ |
---|
331 | // // no more cars |
---|
332 | // comState = CRTDIR; |
---|
333 | // } |
---|
334 | /* break; |
---|
335 | } |
---|
336 | |
---|
337 | bytesToSend = outBytes.toByteArray(); |
---|
338 | bytesToSend[1] = (byte)carsno; //set the first byte to be the number of records |
---|
339 | |
---|
340 | /* if (comState == CRTDIR){ |
---|
341 | comState = OPPDIR; |
---|
342 | reportedNeighbors = 0; |
---|
343 | }else |
---|
344 | if (comState == OPPDIR) |
---|
345 | comState = CRTDIR; |
---|
346 | // comState = REST;*/ |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | return bytesToSend; |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|