[31] | 1 | /************************************************************************************ |
---|
| 2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
| 3 | * All rights reserved. |
---|
| 4 | * Refer to LICENSE for terms and conditions of use. |
---|
| 5 | ***********************************************************************************/ |
---|
| 6 | package vnsim.applications.vitp; |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | import java.io.ByteArrayInputStream; |
---|
| 10 | import java.io.ObjectInputStream; |
---|
| 11 | import java.io.Serializable; |
---|
| 12 | import java.nio.ByteBuffer; |
---|
| 13 | import java.util.TreeMap; |
---|
| 14 | import java.util.Vector; |
---|
| 15 | |
---|
| 16 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
| 17 | import vnsim.applications.vitp.peer.Peer; |
---|
| 18 | import vnsim.applications.vitp.routing.RoutingModule; |
---|
| 19 | import vnsim.applications.vitp.routing.RoutingPacket; |
---|
| 20 | import vnsim.applications.vitp.services.MessageProcessServices; |
---|
| 21 | import vnsim.applications.vitp.ui.UserInterface; |
---|
| 22 | import vnsim.applications.vitp.utils.MessageIndexModule; |
---|
| 23 | import vnsim.core.Communicator; |
---|
| 24 | import vnsim.map.object.Globals; |
---|
| 25 | import vnsim.vehicular.routePlan.infrastructureRouted.InfrastructureReply; |
---|
| 26 | import vnsim.vehicular.simulator.Location; |
---|
| 27 | import vnsim.vehicular.simulator.RouteSegment; |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | public class CarRunningVITP extends SimulatedCarInfo { |
---|
| 34 | |
---|
| 35 | public Peer p; |
---|
| 36 | |
---|
| 37 | public RoutingModule r; |
---|
| 38 | |
---|
| 39 | private MessageIndexModule mim; |
---|
| 40 | |
---|
| 41 | private UserInterface ui; |
---|
| 42 | |
---|
| 43 | private MessageProcessServices mps; |
---|
| 44 | |
---|
| 45 | public TreeMap<Integer, byte[]> messagesToSend = new TreeMap<Integer, byte[]>(); |
---|
| 46 | |
---|
| 47 | public TreeMap<Integer, RouteSegment> periodicalBroadcasts = new TreeMap<Integer, RouteSegment>(); |
---|
| 48 | |
---|
| 49 | public boolean isInformed = false; |
---|
| 50 | |
---|
| 51 | public Vector<Integer> sentTypes = new Vector<Integer>(); |
---|
| 52 | |
---|
| 53 | public CarRunningVITP(int vehicleId, byte lane, double speed, |
---|
| 54 | short roadIdx, short pointIdx, byte direction, double offset) |
---|
| 55 | { |
---|
| 56 | super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); |
---|
| 57 | p = new Peer(); |
---|
| 58 | r = new RoutingModule(); |
---|
| 59 | mim = new MessageIndexModule(); |
---|
| 60 | ui = new UserInterface(); |
---|
| 61 | mps = new MessageProcessServices(this); |
---|
| 62 | |
---|
| 63 | r.attachModules(this, mim, p); |
---|
| 64 | p.attachModules(r, mps, ui); |
---|
| 65 | ui.attachModules(p); |
---|
| 66 | |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public void broadcastRoadPacket(String toSend) |
---|
| 70 | { |
---|
| 71 | // System.out.println("car Vitp sending"+toSend); |
---|
| 72 | int t = scheduleSingleSendEvent(Globals.MESSAGE_PROCESSING_TIME, null); |
---|
| 73 | |
---|
| 74 | try { |
---|
| 75 | this.messagesToSend.put(t, toSend.getBytes()); |
---|
| 76 | this.sentTypes.add(new Integer(t)); |
---|
| 77 | } catch (Exception ex) { |
---|
| 78 | System.out.println("EXCEPTION CarRunningVITP Broadcast packet:" |
---|
| 79 | + ex.toString()); |
---|
| 80 | ex.printStackTrace(); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public void onReceive(byte[] bytesReceived, Serializable m, Communicator sender) { |
---|
| 86 | if (!isEquipped || bytesReceived == null) |
---|
| 87 | return; |
---|
| 88 | // is it a VITP packet? |
---|
| 89 | byte header = bytesReceived[0]; |
---|
| 90 | byte[] message = null; |
---|
| 91 | switch (header) { |
---|
| 92 | // case Globals.PROT_NEIGHBOR_DISCOVERY: |
---|
| 93 | // // ByteBuffer bb = ByteBuffer.wrap(bytesReceived, 1, |
---|
| 94 | // // bytesReceived.length-1); |
---|
| 95 | // SimulatedCarInfo sc = new SimulatedCarInfo(); |
---|
| 96 | // byte[] message = new byte[bytesReceived.length - 1]; |
---|
| 97 | // for (int i = 0; i < message.length; i++) { |
---|
| 98 | // message[i] = bytesReceived[i + 1]; |
---|
| 99 | // } |
---|
| 100 | // sc.wrap(message); |
---|
| 101 | // boolean added = false; |
---|
| 102 | // for (int i = 0; i < neighbors.size(); i++) |
---|
| 103 | // if ((neighbors.get(i)).getVehicleId() == sc.getVehicleId()) { |
---|
| 104 | // synchronized (neighbors) { |
---|
| 105 | // neighbors.set(i, sc); |
---|
| 106 | // // System.out.println("Insert "+dc.getVehicleId() +" at |
---|
| 107 | // // "+i); |
---|
| 108 | // added = true; |
---|
| 109 | // } |
---|
| 110 | // break; |
---|
| 111 | // } |
---|
| 112 | // if (!added) |
---|
| 113 | // synchronized (neighbors) { |
---|
| 114 | // neighbors.add(sc); |
---|
| 115 | // // System.out.println("Insert "+dc.getVehicleId() +" at |
---|
| 116 | // // "+(Globals.neighbors.size()-1)); |
---|
| 117 | // } |
---|
| 118 | // break; |
---|
| 119 | case Globals.VITP_PROT: |
---|
| 120 | // System.out.println("I AM :"+vehicleId+"; RECEIVED VITP!"); |
---|
| 121 | message = new byte[bytesReceived.length - 1]; |
---|
| 122 | for (int i = 0; i < message.length; i++) { |
---|
| 123 | message[i] = bytesReceived[i + 1]; |
---|
| 124 | } |
---|
| 125 | try { |
---|
| 126 | ObjectInputStream ois = new ObjectInputStream( |
---|
| 127 | new ByteArrayInputStream(message)); |
---|
| 128 | RoutingPacket rp = (RoutingPacket) ois.readObject(); |
---|
| 129 | |
---|
| 130 | r.postMessageFromBelow(rp); |
---|
| 131 | |
---|
| 132 | } catch (Exception ex) { |
---|
| 133 | System.out.println("EXCEPTION CarRunningVITP:" + ex.toString()); |
---|
| 134 | ex.printStackTrace(); |
---|
| 135 | return; |
---|
| 136 | } |
---|
| 137 | break; |
---|
| 138 | case Globals.ROAD_PLANNING_PROT: |
---|
| 139 | message = new byte[bytesReceived.length - 1]; |
---|
| 140 | for (int i = 0; i < message.length; i++) { |
---|
| 141 | message[i] = bytesReceived[i + 1]; |
---|
| 142 | } |
---|
| 143 | (new InfrastructureReply(new String(message),this.getRealPos())).analizeMessage(); |
---|
| 144 | |
---|
| 145 | // System.out.println("Received a packet back"); |
---|
| 146 | break; |
---|
| 147 | default: |
---|
| 148 | super.onReceive(bytesReceived, m, sender); |
---|
| 149 | break; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | public byte[] prepareMessage(int messageType) { |
---|
| 154 | |
---|
| 155 | if (!isEquipped) |
---|
| 156 | return null; |
---|
| 157 | ByteBuffer bb = ByteBuffer.allocate(28); |
---|
| 158 | if (messageType == STANDARD_MESSAGE_ID){ |
---|
| 159 | return super.prepareMessage(messageType); |
---|
| 160 | |
---|
| 161 | // bb.put((byte) Globals.PROT_NEIGHBOR_DISCOVERY); |
---|
| 162 | // bb.put(toBytes()); |
---|
| 163 | // return bb.array(); |
---|
| 164 | }else{ // VITP |
---|
| 165 | byte[] b = messagesToSend.get(messageType); |
---|
| 166 | if (b == null) { |
---|
| 167 | // System.out.println("NULL |
---|
| 168 | // TIME="+Globals.engine.crtTime+"ID="+getVehicleId()); |
---|
| 169 | return null; |
---|
| 170 | } |
---|
| 171 | bb = ByteBuffer.allocate(b.length + 1); |
---|
| 172 | if (this.sentTypes.contains(new Integer(messageType))) { |
---|
| 173 | if (b.length>1 && b[0]=='R' && b[1]=='M') { |
---|
| 174 | bb.put(Globals.ROAD_METRICS_PROT); |
---|
| 175 | } else { |
---|
| 176 | bb.put(Globals.ROAD_PLANNING_PROT); |
---|
| 177 | } |
---|
| 178 | this.sentTypes.remove(new Integer(messageType)); |
---|
| 179 | messagesToSend.remove(messageType); |
---|
| 180 | bb.put(b); |
---|
| 181 | //System.out.println("Sending a query to/from infrastruct! "); |
---|
| 182 | //if (this instanceof InfrastructureNode) { |
---|
| 183 | // System.out.println("Sunt semafor!"); |
---|
| 184 | //} |
---|
| 185 | return bb.array(); |
---|
| 186 | } else { |
---|
| 187 | bb.put(Globals.VITP_PROT); |
---|
| 188 | bb.put(b); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | // remove it if it's not a periodical broadcast, |
---|
| 192 | // or it is a periodical broadcast, but the node is not |
---|
| 193 | // within the destination region anymore |
---|
| 194 | if (!periodicalBroadcasts.containsKey(messageType)) { |
---|
| 195 | messagesToSend.remove(messageType); |
---|
| 196 | } |
---|
| 197 | if (periodicalBroadcasts.containsKey(messageType)) { |
---|
| 198 | RouteSegment r = periodicalBroadcasts.get(messageType); |
---|
| 199 | Location myLoc = new Location(getRoadIdx(), getPointIdx()); |
---|
| 200 | if (!r.contains(myLoc)) { |
---|
| 201 | messagesToSend.remove(messageType); |
---|
| 202 | return null; |
---|
| 203 | // System.out.println("ID="+getVehicleId()+"; DISCARDING |
---|
| 204 | // BROADCAST!"); |
---|
| 205 | } else { |
---|
| 206 | Statistics.addBroadcast(10); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | return bb.array(); |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|