package vnsim.vehicular.routePlan.cityRouting; import java.io.Serializable; import java.util.ArrayList; import java.util.StringTokenizer; import vnsim.core.Communicator; import vnsim.map.object.Globals; import vnsim.network.dsrc.CarRunningDSRC; import vnsim.vehicular.simulator.CarInstance; import vnsim.vehicular.simulator.Location; import vnsim.vehicular.simulator.RouteSegment; import vnsim.vehicular.simulator.RoutingUtils; public class CityCarRunningDSRC extends CarRunningDSRC { private String lastMetrics = ""; public CityCarRunningDSRC(int vehicleId, byte lane, double speed, short roadIdx, short pointIdx, byte direction, double offset) { super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); // TODO Auto-generated constructor stub } 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.ROAD_METRICS_PROT: message = new byte[bytesReceived.length - 1]; for (int i = 0; i < message.length; i++) { message[i] = bytesReceived[i + 1]; } changeRoute(new String(message)); break; default: super.onReceive(bytesReceived, m, sender); break; } } private boolean changeRoute(String message) { if (Globals.lastWeekCongestions == null) { return false; } if (!message.startsWith("RMR ")) { // got a message that is not mine and not yet answered return false; } CarInstance car = this.getRealPos(); if (car.routeIndex == car.route.length - 1) { // car is on the last route return false; } if (lastMetrics.equals(message)) { // car has already computed route for this metrics return false; } if (car.route[car.routeIndex].pt2 == car.getPointIdx()) { // car is at a crossroad return false; } lastMetrics = message; StringTokenizer st = new StringTokenizer(message, "C"); st.nextToken(); // type ArrayList updatedCongestion = new ArrayList(); while (st.hasMoreTokens()) { updatedCongestion.add(RoadSegmentCongestion .createRoadSegmentCongestion(st.nextToken())); } // recalculate route CityRouteComputingUtlis rc = new CityRouteComputingUtlis( updatedCongestion); // if not near me RouteSegment rs = car.route[car.routeIndex]; ArrayList points = rc.dijkstraPath(new Location(rs.roadIndex, rs.pt2), car.destination, new Location(rs.roadIndex, rs.pt2), new Location(rs.roadIndex, rs.pt1)); if (points != null) { ArrayList route = CityRouteComputingUtlis .mergeCityRoute(points); route = RoutingUtils.splitRoute(route); // setRoute RouteSegment[] carRoute = new RouteSegment[car.routeIndex + 1 + route.size()]; for (int j = 0; j < car.routeIndex + 1; j++) { carRoute[j] = car.route[j]; } for (int j = 0; j < route.size(); j++) { carRoute[car.routeIndex + 1 + j] = route.get(j); } car.route = carRoute; car.setIntersection(); } return true; } }