/************************************************************************************ * 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.vehicular.routePlan.infrastructureRouted; import java.io.ObjectInputStream; import java.io.ByteArrayInputStream; import java.io.Serializable; import java.util.StringTokenizer; import java.util.ArrayList; import java.util.Vector; import vnsim.applications.trafficview.SimulatedCarInfo; import vnsim.applications.vitp.CarRunningVITP; import vnsim.applications.vitp.routing.RoutingPacket; import vnsim.core.Communicator; import vnsim.core.events.SendEvent; import vnsim.map.object.Cross; import vnsim.map.object.Globals; import vnsim.map.object.Road; import vnsim.vehicular.generator.Mobility; import vnsim.vehicular.routePlan.RouteComputingUtils; import vnsim.vehicular.simulator.CarInstance; import vnsim.vehicular.simulator.Location; import vnsim.vehicular.simulator.NotMovingPersonality; import vnsim.vehicular.simulator.RouteSegment; import vnsim.vehicular.simulator.RoutingUtils; import vnsim.vehicular.simulator.intersections.DirectedRoadSegment; import vnsim.vehicular.simulator.intersections.Intersection; public class InfrastructureNode extends CarRunningVITP { Intersection correspondingIntersection = null; Vector correspondingDelayRecords = new Vector(); public InfrastructureNode(int vehicleId, short roadIdx, short pointIdx, Intersection p) { super(vehicleId, (byte) 0, 0.0, roadIdx, pointIdx, (byte) 0, 0.0); this.correspondingIntersection = p; for (int i = 0; i < correspondingIntersection.segments.size(); i++) { for (int j = 0; j < Globals.routePlanConstants.delays.size(); j++) { if (Globals.routePlanConstants.delays.get(j).equals(correspondingIntersection.segments.get(i))) { this.correspondingDelayRecords.add(Globals.routePlanConstants.delays.get(j)); break; } } } if (this.correspondingIntersection.segments.size() != this.correspondingDelayRecords.size()) { System.out.println("Err: delayrecords are not properly initialized"); System.exit(0); } } public void onReceive(byte[] bytesReceived, Serializable m, Communicator sender) { // super.onReceive(bytesReceived); // System.out.println("SEMAFOR on receive!"); 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); super.onReceive(bytesReceived, m, sender); // SimulatedCarInfo sc = new SimulatedCarInfo(); // 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 < trafficDB.size(); i++) // if ((trafficDB.get(i)).getVehicleId() == sc.getVehicleId()) { // synchronized (trafficDB) { // trafficDB.set(i, sc); // // System.out.println("Insert "+dc.getVehicleId() +" at // // "+i); // added = true; // } // break; // } // if (!added) // synchronized (trafficDB) { // trafficDB.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!"); super.onReceive(bytesReceived, m, sender); // 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: System.out.println("I AM INFRASTRUCTURE RECEIVED REQUEST!"); message = new byte[bytesReceived.length - 1]; for (int i = 0; i < message.length; i++) { message[i] = bytesReceived[i + 1]; } String reply = parseRoutingRequest(message); if (reply != null) { this.broadcastRoadPacket(reply); } break; } } public String parseRoutingRequest(byte[] rec) { String replyBody = null; String body = new String(rec); System.out.println("Struct cu id=" + this.vehicleId + " primesc un mesaj:" + body+ "r="+this.roadIdx+ "p="+this.pointIdx); StringTokenizer st = null; boolean cont = false; int cr, cp2, cp1; try { st = new StringTokenizer(body); String type = null; type = st.nextToken(); if (!type.equals("DR")) { return null; } st.nextToken(); Cross c; cr = (new Integer(st.nextToken())).intValue(); cp1 = (new Integer(st.nextToken())).intValue(); cp2 = (new Integer(st.nextToken())).intValue(); // System.out.println("cere sem de pe sourceRoadIdx "+cr+"p1="+cp1+" p2="+cp2); if ((cr == this.getRealPos().getRoadIdx()) && (cp2 == this.getRealPos().getPointIdx())) { cont = true; } else { for (int i = 0; i < Globals.map.roads.get(this.getRealPos() .getRoadIdx()).crosses.size(); i++) { c = Globals.map.roads.get(this.getRealPos().getRoadIdx()).crosses .get(i); if (c.getCrossRoadIndex() == cr && c.getCrossPointIndex() == cp2 && (c.getPointIndex() == this.getRealPos() .getPointIdx())) { cont = true; break; } } } } catch (Exception e) { return null; } if (!cont) { System.out.println("Struct " + this.vehicleId + " got messg not for me but for" + cr + " " + cp2); return null; } try { // replyBody = new String(body); // replyBody = replyBody.substring(replyBody.indexOf(' ')); // replyBody="RDR "+replyBody; replyBody = "R" + body; int rd, pt; RouteComputingUtils rc = new RouteComputingUtils(); try { rd = (new Integer(st.nextToken())).intValue(); pt = (new Integer(st.nextToken())).intValue(); boolean nextToMe = false; int p1, p2; Road r = Globals.map.roads.get(this.getRealPos().getRoadIdx()); if (this.getRealPos().getRoadIdx() == rd) { // System.out.println("destination e pe str cu mine"); if (pt < this.getRealPos().getPointIdx()) { p1 = pt; p2 = this.getRealPos().getPointIdx(); } else { p2 = pt; p1 = this.getRealPos().getPointIdx(); } boolean foundCross = false; for (int i = 0; i < r.crosses.size(); i++) { if ((r.crosses.get(i).getPointIndex() > p1) && ((r.crosses.get(i).getPointIndex()) < p2)) { // System.out // .println("Gasesc un cros pana in destination in pct " // + r.crosses.get(i).getPointIndex()); foundCross = true; break; } } if (!foundCross) { nextToMe = true; replyBody = replyBody.concat(" " + rd + " " + this.getRealPos().getPointIdx() + " " + pt); // System.out.println(" Nu am gasit cross intre noi"); } } if (!nextToMe) { // System.out.println("caut in crossuri"); for (int i = 0; i < r.crosses.size(); i++) { if ((r.crosses.get(i).getCrossRoadIndex() == rd) && (r.crosses.get(i).getPointIndex() == this.pointIdx)) { // System.out.println("Am cross cu strada aia "); if (pt < r.crosses.get(i).getCrossPointIndex()) { p1 = pt; p2 = r.crosses.get(i).getCrossPointIndex(); } else { p2 = pt; p1 = r.crosses.get(i).getCrossPointIndex(); } boolean foundCross = false; Road tpR = Globals.map.roads.get(rd); for (int j = 0; j < tpR.crosses.size(); j++) { if ((tpR.crosses.get(j).getPointIndex() > p1) && ((tpR.crosses.get(j).getPointIndex()) < p2)) { // System.out // .println("Mai am un cross pana la punctul // ala pe cross road"); foundCross = true; break; } } if (!foundCross) { nextToMe = true; replyBody = replyBody.concat(" " + rd + " " + r.crosses.get(i).getCrossPointIndex() + " " + pt); // System.out // .println("Nu mai e cross pe noua strada intre // mine si destination"); break; } } } } if (!nextToMe) { // System.out.println("NU E LANGA MINE"); ArrayList points = rc .dijkstraPathWithDelay(new Location(this .getRoadIdx(), this.getPointIdx()), new Location(rd, pt), new Location(cr, cp2), new Location(cr, cp1)); if (points != null) { // System.out.println(" am points:" +points); ArrayList ret = RoutingUtils .mergeRoute(points); // System.out.println(" am merged points:" + ret); ret = RoutingUtils.splitRoute(ret); // System.out.println(" am ruta:" + ret); int ii = 0; while (ii < ret.size()) { if (ret.get(ii).pt1 == ret.get(ii).pt2) { // System.out.println(" scot din ruta:" + ret+" // poz "+ii); ret.remove(ii); } else { ii++; } } replyBody = replyBody.concat(" " + ret.get(0).roadIndex + " " + ret.get(0).pt1 + " " + ret.get(0).pt2); // System.out.println(" ret "+ret.get(0)+"repl // "+replyBody); // System.out.println("Gasesc ruta"); } else { System.out.println("Nu gasesc ruta"); } // System.out.println("Struct cu id=" + this.getVehicleId() // + " raspund cu " + replyBody); } } catch (Exception e) { System.out.println("Incorect format for the request"); e.printStackTrace(); return null; } } catch (Exception e) { e.printStackTrace(); return null; } // System.out.println("returnez: " + replyBody); return replyBody; } public Intersection getCurrespondingIntersection() { return correspondingIntersection; } public void updateDelays() { for (int i = 0; i < this.correspondingDelayRecords.size(); i++) { int p1, p2, r, nr = 0; r = this.correspondingDelayRecords.get(i).roadSegment.roadIndex; p2 = this.correspondingDelayRecords.get(i).roadSegment.pointIndex; p1 = this.correspondingDelayRecords.get(i).previosCrossPoint; CarInstance car; if (p1 < p2) { for (int j = 0; j < Globals.map.roads.get(r).carsOnThisRoad .size(); j++) { car = Globals.map.roads.get(r).carsOnThisRoad.get(j); if ((car.getPointIdx() >= p1) && (car.getPointIdx() <= p2)) { if (car.route[car.routeIndex].pt1 < car.route[car.routeIndex].pt2) { nr++; } } } } else { for (int j = 0; j < Globals.map.roads.get(r).carsOnThisRoad .size(); j++) { car = Globals.map.roads.get(r).carsOnThisRoad.get(j); if ((car.getPointIdx() <= p1) && (car.getPointIdx() >= p2)) { if (car.route[car.routeIndex].pt1 > car.route[car.routeIndex].pt2) { nr++; } } } } // System.out.println("infrastruct" + this.getVehicleId() + "nr=" + nr // + " road r=" + r + " p1=" + p1 + " p2=" + p2 + " dr=" // + this.mySegments.get(i).location); long d = 0L; d = (long) (nr * 60 * 60); d = (d / (this.correspondingDelayRecords.get(i).getCapacity())); this.correspondingDelayRecords.get(i).setDelay(d); } } public static void createInfrastructureNodes(){ for (int i = 0; i < Globals.map.allIntersections.size(); i++) { Intersection it = Globals.map.allIntersections.get(i); DirectedRoadSegment dr = it.segments.get(0); CarInstance car = null; car = Mobility.createNewInvisibleCar(dr.roadIndex, dr.pointIndex, 0, 0, 0.0, new NotMovingPersonality(), 0.0, Globals.engine.lastCarID, dr.roadIndex, dr.pointIndex + 1, new RouteSegment((short) dr.roadIndex, (short) dr.pointIndex, (short) (dr.pointIndex + 1)), 5); if (car != null) { Globals.engine.lastCarID++; System.out.println("Created invisible carReceivingMsg id=" + (Globals.engine.lastCarID - 1) + " at road " + dr.roadIndex + " point " + dr.pointIndex); addInfrastructureNode(car, it); } else { System.out.println("nu am putut creea o struct"); } } } public static void addInfrastructureNode(CarInstance car, Intersection pos) { if (car == null) return; synchronized (Globals.engine.cars) { InfrastructureNode ic; if (car.routingType == 5) { if (Globals.engine.crtTime == 0) { ic = new InfrastructureNode(car.ID, car.getRoadIdx(), car.getPointIdx(), pos); ic.setTimestamp(Globals.engine.crtTime); ic.setRealPos(car); Globals.engine.cars.add(ic); Globals.engine.infrastructure.add(ic); ic.init(); } else { int poz = Globals.engine.cars.indexOf(new SimulatedCarInfo(car.ID)); if (poz == -1) { ic = new InfrastructureNode(car.ID, car.getRoadIdx(), car.getPointIdx(), pos); ic.setTimestamp(Globals.engine.crtTime); ic.setRealPos(car); Globals.engine.schedEvent(new SendEvent(Globals.engine.crtTime + 1, ic, SimulatedCarInfo.STANDARD_MESSAGE_ID)); Globals.engine.cars.add(ic); Globals.engine.infrastructure.add(ic); ic.init(); } else { ((SimulatedCarInfo) Globals.engine.cars.get(poz)).setRealPos(car); } } } } } }