/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.applications.vitp; import java.io.ByteArrayInputStream; import java.io.ObjectInputStream; import java.io.Serializable; import java.nio.ByteBuffer; import java.util.TreeMap; import java.util.Vector; import vnsim.applications.trafficview.SimulatedCarInfo; import vnsim.applications.vitp.peer.Peer; import vnsim.applications.vitp.routing.RoutingModule; import vnsim.applications.vitp.routing.RoutingPacket; import vnsim.applications.vitp.services.MessageProcessServices; import vnsim.applications.vitp.ui.UserInterface; import vnsim.applications.vitp.utils.MessageIndexModule; import vnsim.core.Communicator; import vnsim.map.object.Globals; import vnsim.vehicular.routePlan.infrastructureRouted.InfrastructureReply; import vnsim.vehicular.simulator.Location; import vnsim.vehicular.simulator.RouteSegment; public class CarRunningVITP extends SimulatedCarInfo { public Peer p; public RoutingModule r; private MessageIndexModule mim; private UserInterface ui; private MessageProcessServices mps; public TreeMap messagesToSend = new TreeMap(); public TreeMap periodicalBroadcasts = new TreeMap(); public boolean isInformed = false; public Vector sentTypes = new Vector(); public CarRunningVITP(int vehicleId, byte lane, double speed, short roadIdx, short pointIdx, byte direction, double offset) { super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); p = new Peer(); r = new RoutingModule(); mim = new MessageIndexModule(); ui = new UserInterface(); mps = new MessageProcessServices(this); r.attachModules(this, mim, p); p.attachModules(r, mps, ui); ui.attachModules(p); } public void broadcastRoadPacket(String toSend) { // System.out.println("car Vitp sending"+toSend); int t = scheduleSingleSendEvent(Globals.MESSAGE_PROCESSING_TIME, null); try { this.messagesToSend.put(t, toSend.getBytes()); this.sentTypes.add(new Integer(t)); } catch (Exception ex) { System.out.println("EXCEPTION CarRunningVITP Broadcast packet:" + ex.toString()); ex.printStackTrace(); } } public void onReceive(byte[] bytesReceived, Serializable m, Communicator sender) { if (!isEquipped || bytesReceived == null) return; // is it a VITP packet? byte header = bytesReceived[0]; byte[] message = null; switch (header) { // case Globals.PROT_NEIGHBOR_DISCOVERY: // // ByteBuffer bb = ByteBuffer.wrap(bytesReceived, 1, // // bytesReceived.length-1); // SimulatedCarInfo sc = new SimulatedCarInfo(); // byte[] message = new byte[bytesReceived.length - 1]; // for (int i = 0; i < message.length; i++) { // message[i] = bytesReceived[i + 1]; // } // sc.wrap(message); // boolean added = false; // for (int i = 0; i < neighbors.size(); i++) // if ((neighbors.get(i)).getVehicleId() == sc.getVehicleId()) { // synchronized (neighbors) { // neighbors.set(i, sc); // // System.out.println("Insert "+dc.getVehicleId() +" at // // "+i); // added = true; // } // break; // } // if (!added) // synchronized (neighbors) { // neighbors.add(sc); // // System.out.println("Insert "+dc.getVehicleId() +" at // // "+(Globals.neighbors.size()-1)); // } // break; case Globals.VITP_PROT: // System.out.println("I AM :"+vehicleId+"; RECEIVED VITP!"); message = new byte[bytesReceived.length - 1]; for (int i = 0; i < message.length; i++) { message[i] = bytesReceived[i + 1]; } try { ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(message)); RoutingPacket rp = (RoutingPacket) ois.readObject(); r.postMessageFromBelow(rp); } catch (Exception ex) { System.out.println("EXCEPTION CarRunningVITP:" + ex.toString()); ex.printStackTrace(); return; } break; case Globals.ROAD_PLANNING_PROT: message = new byte[bytesReceived.length - 1]; for (int i = 0; i < message.length; i++) { message[i] = bytesReceived[i + 1]; } (new InfrastructureReply(new String(message),this.getRealPos())).analizeMessage(); // System.out.println("Received a packet back"); break; default: super.onReceive(bytesReceived, m, sender); break; } } public byte[] prepareMessage(int messageType) { if (!isEquipped) return null; ByteBuffer bb = ByteBuffer.allocate(28); if (messageType == STANDARD_MESSAGE_ID){ return super.prepareMessage(messageType); // bb.put((byte) Globals.PROT_NEIGHBOR_DISCOVERY); // bb.put(toBytes()); // return bb.array(); }else{ // VITP byte[] b = messagesToSend.get(messageType); if (b == null) { // System.out.println("NULL // TIME="+Globals.engine.crtTime+"ID="+getVehicleId()); return null; } bb = ByteBuffer.allocate(b.length + 1); if (this.sentTypes.contains(new Integer(messageType))) { if (b.length>1 && b[0]=='R' && b[1]=='M') { bb.put(Globals.ROAD_METRICS_PROT); } else { bb.put(Globals.ROAD_PLANNING_PROT); } this.sentTypes.remove(new Integer(messageType)); messagesToSend.remove(messageType); bb.put(b); //System.out.println("Sending a query to/from infrastruct! "); //if (this instanceof InfrastructureNode) { // System.out.println("Sunt semafor!"); //} return bb.array(); } else { bb.put(Globals.VITP_PROT); bb.put(b); } // remove it if it's not a periodical broadcast, // or it is a periodical broadcast, but the node is not // within the destination region anymore if (!periodicalBroadcasts.containsKey(messageType)) { messagesToSend.remove(messageType); } if (periodicalBroadcasts.containsKey(messageType)) { RouteSegment r = periodicalBroadcasts.get(messageType); Location myLoc = new Location(getRoadIdx(), getPointIdx()); if (!r.contains(myLoc)) { messagesToSend.remove(messageType); return null; // System.out.println("ID="+getVehicleId()+"; DISCARDING // BROADCAST!"); } else { Statistics.addBroadcast(10); } } return bb.array(); } } }