1 | |
---|
2 | /** |
---|
3 | * Mazilu Sinziana |
---|
4 | * |
---|
5 | * EZCab Car Entity |
---|
6 | */ |
---|
7 | |
---|
8 | package vnsim.applications.ezcab; |
---|
9 | |
---|
10 | import java.io.IOException; |
---|
11 | import java.io.Serializable; |
---|
12 | import java.nio.ByteBuffer; |
---|
13 | import java.util.ArrayList; |
---|
14 | |
---|
15 | import vnsim.network.dsrc.*; |
---|
16 | import vnsim.core.*; |
---|
17 | import vnsim.core.events.SendEvent; |
---|
18 | import vnsim.map.object.Globals; |
---|
19 | |
---|
20 | public class EzcabCar extends CarRunningDSRC{ |
---|
21 | |
---|
22 | private int entityType = EzcabGlobals.CAB; /* type of entity: client or cab */ |
---|
23 | private int state; /* state of cab */ |
---|
24 | private int newState = EzcabGlobals.FREE_STATE; /* future state of cab */ |
---|
25 | private EzcabMessage newMessage; /* next message to be prepared */ |
---|
26 | private ArrayList<EzcabRequest> waitingMessages = new ArrayList<EzcabRequest>(); |
---|
27 | |
---|
28 | private long changeInWaitingState = 0; |
---|
29 | private int clientID; |
---|
30 | private int timeOfsending = 0; |
---|
31 | |
---|
32 | public EzcabCar(int state, int vehicleId, byte lane, double speed, short roadIdx, |
---|
33 | short pointIdx, byte direction,double offset) { |
---|
34 | super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); |
---|
35 | |
---|
36 | this.entityType = EzcabGlobals.CAB; |
---|
37 | this.state = state; |
---|
38 | } |
---|
39 | |
---|
40 | /* this should be used */ |
---|
41 | public EzcabCar(int vehicleId, byte lane, double speed, short roadIdx, |
---|
42 | short pointIdx, byte direction,double offset) { |
---|
43 | super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); |
---|
44 | |
---|
45 | this.entityType = EzcabGlobals.CAB; |
---|
46 | this.state = EzcabGlobals.FREE_STATE; |
---|
47 | } |
---|
48 | |
---|
49 | public void onReceive(byte[] bytesReceived, Serializable message, Communicator sender){ |
---|
50 | |
---|
51 | if (!isEquipped) |
---|
52 | return; |
---|
53 | |
---|
54 | if(bytesReceived == null){ |
---|
55 | processMessage(message); |
---|
56 | return; |
---|
57 | } |
---|
58 | byte header = bytesReceived[0]; |
---|
59 | |
---|
60 | switch(header) |
---|
61 | { |
---|
62 | case Globals.PROT_EZCAB: |
---|
63 | processMessage(message); |
---|
64 | break; |
---|
65 | |
---|
66 | default: |
---|
67 | super.onReceive(bytesReceived, message, sender); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | public void processMessage(Serializable message) { |
---|
72 | int idClient; |
---|
73 | int noHops; |
---|
74 | int ts; |
---|
75 | |
---|
76 | if(message == null){ |
---|
77 | return; |
---|
78 | } |
---|
79 | |
---|
80 | if(message instanceof EzcabRequest){ |
---|
81 | EzcabRequest req = (EzcabRequest)message; |
---|
82 | noHops = req.getNoHops(); |
---|
83 | idClient = req.getIdClient(); |
---|
84 | ts = req.getTimeOfSending(); |
---|
85 | |
---|
86 | /* verify if cab doesn't wait too long */ |
---|
87 | //if( ((int)(currentTime - this.changeInWaitingState)/Globals.SECOND) > EzcabGlobals.TIMEOUT){ |
---|
88 | if( ((long)(Globals.engine.getCrtTime() - this.timeOfsending)/Globals.SECOND * 1000) > EzcabGlobals.TIMEOUT ){ |
---|
89 | this.changeInWaitingState = 0; |
---|
90 | this.timeOfsending = 0; |
---|
91 | this.clientID = -1; |
---|
92 | this.state = EzcabGlobals.FREE_STATE; |
---|
93 | |
---|
94 | this.newMessage = null; |
---|
95 | this.newState = EzcabGlobals.FREE_STATE; |
---|
96 | } |
---|
97 | |
---|
98 | if(this.state == EzcabGlobals.WAITING_STATE){ |
---|
99 | /* if message is not expired */ |
---|
100 | if((noHops - 1) > 0){ |
---|
101 | /* add message to my waiting queue */ |
---|
102 | updateWaitingQueue(); |
---|
103 | if(((long)(Globals.engine.getCrtTime() - ts)/Globals.SECOND * 1000) < EzcabGlobals.TIMEOUT ){ |
---|
104 | this.newMessage = new EzcabRequest(Globals.PROT_EZCAB, EzcabGlobals.REQUEST, idClient, (noHops -1), ts); |
---|
105 | this.waitingMessages.add(req); |
---|
106 | } |
---|
107 | else{ |
---|
108 | this.newMessage = null; |
---|
109 | } |
---|
110 | /* prepare to forward message in next event handler */ |
---|
111 | } |
---|
112 | else{ |
---|
113 | this.newMessage = null; |
---|
114 | } |
---|
115 | /* in this case, state is the same */ |
---|
116 | this.newState = EzcabGlobals.WAITING_STATE; |
---|
117 | } |
---|
118 | if(this.state == EzcabGlobals.FREE_STATE){ |
---|
119 | EzcabRequest requestFromQueue = this.getMessageMaxSendTime(); |
---|
120 | if(requestFromQueue != null){ |
---|
121 | if(requestFromQueue.getTimeOfSending() > ts){ |
---|
122 | this.newMessage = requestFromQueue; |
---|
123 | this.timeOfsending = requestFromQueue.getTimeOfSending(); |
---|
124 | this.clientID = requestFromQueue.getIdClient(); |
---|
125 | |
---|
126 | this.waitingMessages.add(req); |
---|
127 | } |
---|
128 | else{ |
---|
129 | /* make a next message with the response */ |
---|
130 | this.newMessage = new EzcabResponse(Globals.PROT_EZCAB, EzcabGlobals.RESPONSE, this.vehicleId, |
---|
131 | idClient, EzcabGlobals.MAX_HOPS); |
---|
132 | //this.changeInWaitingState = System.currentTimeMillis(); |
---|
133 | this.timeOfsending = ts; |
---|
134 | this.clientID = idClient; |
---|
135 | } |
---|
136 | } |
---|
137 | else{ |
---|
138 | /* make a next message with the response */ |
---|
139 | this.newMessage = new EzcabResponse(Globals.PROT_EZCAB, EzcabGlobals.RESPONSE, this.vehicleId, |
---|
140 | idClient, EzcabGlobals.MAX_HOPS); |
---|
141 | //this.changeInWaitingState = System.currentTimeMillis(); |
---|
142 | this.timeOfsending = ts; |
---|
143 | this.clientID = idClient; |
---|
144 | } |
---|
145 | /* change next state */ |
---|
146 | this.newState = EzcabGlobals.WAITING_STATE; |
---|
147 | } |
---|
148 | |
---|
149 | if(this.state == EzcabGlobals.BUSY_STATE){ |
---|
150 | if((noHops -1) > 0){ |
---|
151 | this.newMessage = new EzcabRequest(Globals.PROT_EZCAB, EzcabGlobals.REQUEST, idClient, (noHops -1), ts); |
---|
152 | } |
---|
153 | else{ |
---|
154 | this.newMessage = null; |
---|
155 | } |
---|
156 | this.newState = EzcabGlobals.BUSY_STATE; |
---|
157 | } |
---|
158 | }//end ezcabRequest |
---|
159 | |
---|
160 | if(message instanceof EzcabAck){ |
---|
161 | EzcabAck ack = (EzcabAck)message; |
---|
162 | noHops = ack.getNoHops(); |
---|
163 | idClient = ack.getIdClient(); |
---|
164 | int idCab = ack.getIdCab(); |
---|
165 | |
---|
166 | if(this.state == EzcabGlobals.WAITING_STATE){ |
---|
167 | |
---|
168 | //if( ((currentTime - this.changeInWaitingState)%Globals.SECOND) > EzcabGlobals.TIMEOUT){ |
---|
169 | if( ((long)(Globals.engine.getCrtTime() - this.timeOfsending)/Globals.SECOND * 1000) > EzcabGlobals.TIMEOUT ){ |
---|
170 | this.changeInWaitingState = 0; |
---|
171 | this.timeOfsending = 0; |
---|
172 | this.clientID = -1; |
---|
173 | this.state = EzcabGlobals.FREE_STATE; |
---|
174 | |
---|
175 | //don't know if is necessary |
---|
176 | this.newMessage = null; |
---|
177 | this.newState = EzcabGlobals.FREE_STATE; |
---|
178 | } |
---|
179 | else{ |
---|
180 | if(this.vehicleId == idCab){//that means is for me yeeeeeyyyy! |
---|
181 | try { |
---|
182 | Engine.bwCab.write(idClient + " " + this.vehicleId + " " + (long)((float)Globals.engine.crtTime)/Globals.SECOND * 1000L + " " + (long)((float)(Globals.engine.crtTime - this.timeOfsending)/Globals.SECOND * 1000L) + "\n"); |
---|
183 | Engine.bwCab.flush(); |
---|
184 | |
---|
185 | } catch (IOException e1) { |
---|
186 | e1.printStackTrace(); |
---|
187 | } |
---|
188 | |
---|
189 | this.newMessage = null; |
---|
190 | this.newState = EzcabGlobals.BUSY_STATE; |
---|
191 | } |
---|
192 | else{ |
---|
193 | if((noHops -1) > 0){ |
---|
194 | this.newMessage = new EzcabAck(Globals.PROT_EZCAB, EzcabGlobals.ACK, idClient, idCab, (noHops - 1)); |
---|
195 | } |
---|
196 | else |
---|
197 | this.newMessage = null; |
---|
198 | this.newState = EzcabGlobals.WAITING_STATE; |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | if( (this.state == EzcabGlobals.FREE_STATE) |
---|
204 | || (this.state == EzcabGlobals.BUSY_STATE) ){ |
---|
205 | if((noHops -1) > 0){ |
---|
206 | this.newMessage = new EzcabAck(Globals.PROT_EZCAB, EzcabGlobals.ACK, idClient, idCab, (noHops - 1)); |
---|
207 | } |
---|
208 | else |
---|
209 | this.newMessage = null; |
---|
210 | this.newState = this.state; |
---|
211 | } |
---|
212 | }// end ezcabAck |
---|
213 | |
---|
214 | if(message instanceof EzcabResponse){ |
---|
215 | EzcabResponse res = (EzcabResponse)message; |
---|
216 | int idFreeCab = res.getIdFreeCab(); |
---|
217 | idClient = res.getIdClient(); |
---|
218 | noHops = res.getNoHops(); |
---|
219 | |
---|
220 | /* dosn't matter the state of the cab - he only forwards the message */ |
---|
221 | if((noHops -1) > 0){ |
---|
222 | this.newMessage = new EzcabResponse(Globals.PROT_EZCAB, EzcabGlobals.RESPONSE, idFreeCab, idClient, (noHops - 1)); |
---|
223 | } |
---|
224 | else{ |
---|
225 | this.newMessage = null; |
---|
226 | } |
---|
227 | this.newState = this.state; |
---|
228 | }// end ezcabResponse |
---|
229 | |
---|
230 | SendEvent e = new SendEvent(Globals.engine.crtTime, this, Globals.TYPE_EZCAB); |
---|
231 | e.setMessageObject(this.newMessage); |
---|
232 | Globals.engine.schedEvent(e); |
---|
233 | this.state = this.newState; |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | public byte[] prepareMessage(int messageType){ |
---|
238 | |
---|
239 | byte[] bytesToSend=null; |
---|
240 | |
---|
241 | /* update state */ |
---|
242 | this.state = this.newState; |
---|
243 | |
---|
244 | if ( (!isEquipped) || (this.newMessage == null) ) |
---|
245 | return null; |
---|
246 | |
---|
247 | if(messageType == Globals.TYPE_EZCAB){ |
---|
248 | |
---|
249 | /* if is a request message */ |
---|
250 | if(this.newMessage instanceof EzcabRequest){ |
---|
251 | EzcabRequest request = (EzcabRequest)this.newMessage; |
---|
252 | ByteBuffer bb = ByteBuffer.allocate(17); |
---|
253 | |
---|
254 | bb.put(request.getProtType()); //bb.put(Globals.PROT_EZCAB); |
---|
255 | bb.putInt(request.getMessageType()); //bb.putInt(EzcabGlobals.REQUEST); |
---|
256 | bb.putInt(request.getIdClient()); |
---|
257 | bb.putInt(request.getNoHops()); |
---|
258 | bb.putInt(request.getTimeOfSending()); |
---|
259 | |
---|
260 | bytesToSend = bb.array(); |
---|
261 | } |
---|
262 | |
---|
263 | /* if is a response message */ |
---|
264 | if(this.newMessage instanceof EzcabResponse){ |
---|
265 | EzcabResponse response = (EzcabResponse)this.newMessage; |
---|
266 | ByteBuffer bb = ByteBuffer.allocate(17); |
---|
267 | |
---|
268 | bb.put(response.getProtType()); //bb.put(Globals.PROT_EZCAB); |
---|
269 | bb.putInt(response.getMessageType()); //bb.putInt(EzcabGlobals.RESPONSE); |
---|
270 | bb.putInt(response.getIdFreeCab()); |
---|
271 | bb.putInt(response.getIdClient()); |
---|
272 | bb.putInt(response.getNoHops()); |
---|
273 | |
---|
274 | bytesToSend = bb.array(); |
---|
275 | } |
---|
276 | |
---|
277 | /* if is an ack message */ |
---|
278 | if(this.newMessage instanceof EzcabAck){ |
---|
279 | EzcabAck ack = (EzcabAck)this.newMessage; |
---|
280 | ByteBuffer bb = ByteBuffer.allocate(17); |
---|
281 | |
---|
282 | bb.put(ack.getProtType()); //bb.put(Globals.PROT_EZCAB); |
---|
283 | bb.putInt(ack.getMessageType()); //bb.putInt(EzcabGlobals.ACK); |
---|
284 | bb.putInt(ack.getIdClient()); |
---|
285 | bb.putInt(ack.getIdCab()); |
---|
286 | bb.putInt(ack.getNoHops()); |
---|
287 | |
---|
288 | bytesToSend = bb.array(); |
---|
289 | } |
---|
290 | |
---|
291 | return bytesToSend; |
---|
292 | } |
---|
293 | else{ |
---|
294 | return super.prepareMessage(messageType); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | /* remove from waiting queue expired messages */ |
---|
299 | public void updateWaitingQueue(){ |
---|
300 | int messageTime; |
---|
301 | |
---|
302 | for(int i = 0; i < this.waitingMessages.size(); i++){ |
---|
303 | messageTime = (int)(this.waitingMessages.get(i)).getTimeOfSending(); |
---|
304 | if( ((long)(Globals.engine.getCrtTime() - messageTime) / Globals.SECOND * 1000) > EzcabGlobals.TIMEOUT) |
---|
305 | this.waitingMessages.remove(i); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | public EzcabRequest getMessageMaxSendTime(){ |
---|
310 | int messageTime, maxTime = 0; |
---|
311 | EzcabRequest message = null; |
---|
312 | |
---|
313 | for(int i = 0; i < this.waitingMessages.size(); i++){ |
---|
314 | messageTime = (int)(this.waitingMessages.get(i)).getTimeOfSending(); |
---|
315 | if(messageTime > maxTime){ |
---|
316 | maxTime = messageTime; |
---|
317 | message = this.waitingMessages.get(i); |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | return message; |
---|
322 | } |
---|
323 | |
---|
324 | } |
---|