[31] | 1 | |
---|
| 2 | /** |
---|
| 3 | * Mazilu Sinziana |
---|
| 4 | * |
---|
| 5 | * EZCab Client Entity |
---|
| 6 | */ |
---|
| 7 | package vnsim.applications.ezcab; |
---|
| 8 | |
---|
| 9 | import java.io.IOException; |
---|
| 10 | import java.io.Serializable; |
---|
| 11 | import java.nio.ByteBuffer; |
---|
| 12 | |
---|
| 13 | import vnsim.core.Communicator; |
---|
| 14 | import vnsim.core.Engine; |
---|
| 15 | import vnsim.core.events.SendEvent; |
---|
| 16 | import vnsim.map.object.Globals; |
---|
| 17 | import vnsim.network.dsrc.CarRunningDSRC; |
---|
| 18 | |
---|
| 19 | public class EzcabClient extends CarRunningDSRC{ |
---|
| 20 | |
---|
| 21 | private int entityType; /*type of entity: client or taxi car*/ |
---|
| 22 | private int state; /* state of client */ |
---|
| 23 | |
---|
| 24 | private int sendingTime; /* time of first request */ |
---|
| 25 | private int periodicalTime; /* periodical sending time */ |
---|
| 26 | private EzcabMessage newMessage; |
---|
| 27 | |
---|
| 28 | public EzcabClient(int state, int vehicleId, byte lane, double speed, short roadIdx, |
---|
| 29 | short pointIdx, byte direction,double offset) { |
---|
| 30 | super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); |
---|
| 31 | |
---|
| 32 | this.entityType = EzcabGlobals.CLIENT; |
---|
| 33 | this.state = state; |
---|
| 34 | |
---|
| 35 | /* we create the message */ |
---|
| 36 | sendingTime = Globals.engine.crtTime; |
---|
| 37 | periodicalTime = Globals.engine.crtTime; |
---|
| 38 | |
---|
| 39 | this.newMessage = new EzcabRequest(Globals.PROT_EZCAB, EzcabGlobals.REQUEST, this.vehicleId, |
---|
| 40 | EzcabGlobals.MAX_HOPS, Globals.engine.crtTime); |
---|
| 41 | SendEvent e = new SendEvent(Globals.engine.crtTime, this, Globals.TYPE_EZCAB); |
---|
| 42 | e.setMessageObject(newMessage); |
---|
| 43 | Globals.engine.schedEvent(e); |
---|
| 44 | |
---|
| 45 | System.out.println("Scheduled event with id "+Globals.TYPE_EZCAB); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | public EzcabClient(int vehicleId, byte lane, double speed, short roadIdx, |
---|
| 49 | short pointIdx, byte direction,double offset) { |
---|
| 50 | super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); |
---|
| 51 | |
---|
| 52 | this.entityType = EzcabGlobals.CLIENT; |
---|
| 53 | this.state = EzcabGlobals.WAITING_STATE; |
---|
| 54 | |
---|
| 55 | /* we create the message */ |
---|
| 56 | sendingTime = Globals.engine.crtTime; |
---|
| 57 | periodicalTime = Globals.engine.crtTime; |
---|
| 58 | |
---|
| 59 | this.newMessage = new EzcabRequest(Globals.PROT_EZCAB, EzcabGlobals.REQUEST, this.vehicleId, |
---|
| 60 | EzcabGlobals.MAX_HOPS, Globals.engine.crtTime); |
---|
| 61 | SendEvent e = new SendEvent(Globals.engine.crtTime, this, Globals.TYPE_EZCAB); |
---|
| 62 | e.setMessageObject(this.newMessage); |
---|
| 63 | Globals.engine.schedEvent(e); |
---|
| 64 | try { |
---|
| 65 | Engine.bwClient.write("client: " + this.vehicleId + " sending: "+ |
---|
| 66 | (float)Globals.engine.crtTime / Globals.SECOND *1000 + "\n"); |
---|
| 67 | Engine.bwClient.flush(); |
---|
| 68 | |
---|
| 69 | } catch (IOException e1) { |
---|
| 70 | e1.printStackTrace(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | System.out.println("Scheduled event with id " + Globals.TYPE_EZCAB); |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | public void onReceive(byte[] bytesReceived, Serializable m, Communicator sender){ |
---|
| 78 | |
---|
| 79 | if (!isEquipped) |
---|
| 80 | return; |
---|
| 81 | |
---|
| 82 | if(bytesReceived == null){ |
---|
| 83 | processMessage(m); |
---|
| 84 | return; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | byte header = bytesReceived[0]; |
---|
| 88 | |
---|
| 89 | switch(header) |
---|
| 90 | { |
---|
| 91 | case Globals.PROT_EZCAB: |
---|
| 92 | processMessage(m); |
---|
| 93 | break; |
---|
| 94 | |
---|
| 95 | default: |
---|
| 96 | super.onReceive(bytesReceived, m, sender); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | public void processMessage(Serializable m) { |
---|
| 101 | |
---|
| 102 | if( (((long)(Globals.engine.getCrtTime() - this.periodicalTime)/Globals.SECOND * 1000) > EzcabGlobals.TIMEOUT) |
---|
| 103 | && (this.state != EzcabGlobals.BUSY_STATE)){ |
---|
| 104 | /* make a new request */ |
---|
| 105 | this.newMessage = new EzcabRequest(Globals.PROT_EZCAB, EzcabGlobals.REQUEST, this.vehicleId, |
---|
| 106 | EzcabGlobals.MAX_HOPS, this.sendingTime); |
---|
| 107 | SendEvent e = new SendEvent(Globals.engine.crtTime, this, Globals.TYPE_EZCAB); |
---|
| 108 | e.setMessageObject(this.newMessage); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | if(m == null) |
---|
| 113 | return; |
---|
| 114 | |
---|
| 115 | if( (m instanceof EzcabRequest) || (m instanceof EzcabAck) ){ |
---|
| 116 | return; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | if(m instanceof EzcabResponse){ |
---|
| 120 | EzcabResponse res = (EzcabResponse)m; |
---|
| 121 | int idCab = res.getIdFreeCab(); |
---|
| 122 | int idClient = res.getIdClient(); |
---|
| 123 | |
---|
| 124 | if(idClient == this.vehicleId && this.state != EzcabGlobals.BUSY_STATE){ |
---|
| 125 | System.out.println("client: " + idClient + ", masina: " + idCab); |
---|
| 126 | this.state = EzcabGlobals.BUSY_STATE; |
---|
| 127 | /* randomize the answer of the client */ |
---|
| 128 | //if(System.currentTimeMillis() % 2 == 0){ |
---|
| 129 | this.newMessage = new EzcabAck(Globals.PROT_EZCAB, EzcabGlobals.ACK, this.vehicleId, idCab, EzcabGlobals.MAX_HOPS); |
---|
| 130 | //} |
---|
| 131 | //else |
---|
| 132 | // this.newMessage = null; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | SendEvent e = new SendEvent(Globals.engine.crtTime, this, Globals.TYPE_EZCAB); |
---|
| 137 | e.setMessageObject(this.newMessage); |
---|
| 138 | Globals.engine.schedEvent(e); |
---|
| 139 | |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | public byte[] prepareMessage(int messageType){ |
---|
| 144 | |
---|
| 145 | byte[] bytesToSend=null; |
---|
| 146 | |
---|
| 147 | if ( (!isEquipped) || (this.newMessage == null) ) |
---|
| 148 | return null; |
---|
| 149 | |
---|
| 150 | if(messageType == Globals.TYPE_EZCAB){ |
---|
| 151 | |
---|
| 152 | if(this.newMessage instanceof EzcabRequest){ |
---|
| 153 | EzcabRequest request = (EzcabRequest)this.newMessage; |
---|
| 154 | ByteBuffer bb = ByteBuffer.allocate(17); |
---|
| 155 | |
---|
| 156 | bb.put(request.getProtType()); //bb.put(Globals.PROT_EZCAB); |
---|
| 157 | bb.putInt(request.getMessageType()); //bb.putInt(EzcabGlobals.REQUEST); |
---|
| 158 | bb.putInt(request.getIdClient()); |
---|
| 159 | bb.putInt(request.getNoHops()); |
---|
| 160 | bb.putInt(request.getTimeOfSending()); |
---|
| 161 | |
---|
| 162 | bytesToSend = bb.array(); |
---|
| 163 | } |
---|
| 164 | if(this.newMessage instanceof EzcabAck){ |
---|
| 165 | EzcabAck ack = (EzcabAck)this.newMessage; |
---|
| 166 | ByteBuffer bb = ByteBuffer.allocate(17); |
---|
| 167 | |
---|
| 168 | bb.put(ack.getProtType()); //bb.put(Globals.PROT_EZCAB); |
---|
| 169 | bb.putInt(ack.getMessageType()); //bb.putInt(EzcabGlobals.ACK); |
---|
| 170 | bb.putInt(ack.getIdClient()); |
---|
| 171 | bb.putInt(ack.getIdCab()); |
---|
| 172 | bb.putInt(ack.getNoHops()); |
---|
| 173 | |
---|
| 174 | bytesToSend = bb.array(); |
---|
| 175 | } |
---|
| 176 | return bytesToSend; |
---|
| 177 | } |
---|
| 178 | else{ |
---|
| 179 | return super.prepareMessage(messageType); |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | } |
---|