[31] | 1 | package vnsim.vehicular.routePlan.cityRouting; |
---|
| 2 | |
---|
| 3 | import java.io.Serializable; |
---|
| 4 | import java.util.ArrayList; |
---|
| 5 | import java.util.StringTokenizer; |
---|
| 6 | |
---|
| 7 | import vnsim.core.Communicator; |
---|
| 8 | import vnsim.map.object.Globals; |
---|
| 9 | import vnsim.network.dsrc.CarRunningDSRC; |
---|
| 10 | import vnsim.vehicular.simulator.CarInstance; |
---|
| 11 | import vnsim.vehicular.simulator.Location; |
---|
| 12 | import vnsim.vehicular.simulator.RouteSegment; |
---|
| 13 | import vnsim.vehicular.simulator.RoutingUtils; |
---|
| 14 | |
---|
| 15 | public class CityCarRunningDSRC extends CarRunningDSRC { |
---|
| 16 | |
---|
| 17 | private String lastMetrics = ""; |
---|
| 18 | |
---|
| 19 | public CityCarRunningDSRC(int vehicleId, byte lane, double speed, |
---|
| 20 | short roadIdx, short pointIdx, byte direction, double offset) { |
---|
| 21 | super(vehicleId, lane, speed, roadIdx, pointIdx, direction, offset); |
---|
| 22 | // TODO Auto-generated constructor stub |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | public void onReceive(byte[] bytesReceived, Serializable m, Communicator sender) { |
---|
| 26 | if (!isEquipped || bytesReceived == null) |
---|
| 27 | return; |
---|
| 28 | // is it a VITP packet? |
---|
| 29 | byte header = bytesReceived[0]; |
---|
| 30 | byte[] message = null; |
---|
| 31 | switch (header) { |
---|
| 32 | |
---|
| 33 | case Globals.ROAD_METRICS_PROT: |
---|
| 34 | message = new byte[bytesReceived.length - 1]; |
---|
| 35 | for (int i = 0; i < message.length; i++) { |
---|
| 36 | message[i] = bytesReceived[i + 1]; |
---|
| 37 | } |
---|
| 38 | changeRoute(new String(message)); |
---|
| 39 | break; |
---|
| 40 | default: |
---|
| 41 | super.onReceive(bytesReceived, m, sender); |
---|
| 42 | break; |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | private boolean changeRoute(String message) { |
---|
| 47 | if (Globals.lastWeekCongestions == null) { |
---|
| 48 | return false; |
---|
| 49 | } |
---|
| 50 | if (!message.startsWith("RMR ")) { |
---|
| 51 | // got a message that is not mine and not yet answered |
---|
| 52 | return false; |
---|
| 53 | } |
---|
| 54 | CarInstance car = this.getRealPos(); |
---|
| 55 | if (car.routeIndex == car.route.length - 1) { |
---|
| 56 | // car is on the last route |
---|
| 57 | return false; |
---|
| 58 | } |
---|
| 59 | if (lastMetrics.equals(message)) { |
---|
| 60 | // car has already computed route for this metrics |
---|
| 61 | return false; |
---|
| 62 | } |
---|
| 63 | if (car.route[car.routeIndex].pt2 == car.getPointIdx()) { |
---|
| 64 | // car is at a crossroad |
---|
| 65 | return false; |
---|
| 66 | } |
---|
| 67 | lastMetrics = message; |
---|
| 68 | StringTokenizer st = new StringTokenizer(message, "C"); |
---|
| 69 | st.nextToken(); // type |
---|
| 70 | ArrayList<RoadSegmentCongestion> updatedCongestion = new ArrayList<RoadSegmentCongestion>(); |
---|
| 71 | while (st.hasMoreTokens()) { |
---|
| 72 | updatedCongestion.add(RoadSegmentCongestion |
---|
| 73 | .createRoadSegmentCongestion(st.nextToken())); |
---|
| 74 | } |
---|
| 75 | // recalculate route |
---|
| 76 | CityRouteComputingUtlis rc = new CityRouteComputingUtlis( |
---|
| 77 | updatedCongestion); |
---|
| 78 | // if not near me |
---|
| 79 | RouteSegment rs = car.route[car.routeIndex]; |
---|
| 80 | ArrayList<Location> points = rc.dijkstraPath(new Location(rs.roadIndex, |
---|
| 81 | rs.pt2), car.destination, new Location(rs.roadIndex, rs.pt2), |
---|
| 82 | new Location(rs.roadIndex, rs.pt1)); |
---|
| 83 | if (points != null) { |
---|
| 84 | ArrayList<RouteSegment> route = CityRouteComputingUtlis |
---|
| 85 | .mergeCityRoute(points); |
---|
| 86 | route = RoutingUtils.splitRoute(route); |
---|
| 87 | // setRoute |
---|
| 88 | RouteSegment[] carRoute = new RouteSegment[car.routeIndex + 1 |
---|
| 89 | + route.size()]; |
---|
| 90 | for (int j = 0; j < car.routeIndex + 1; j++) { |
---|
| 91 | carRoute[j] = car.route[j]; |
---|
| 92 | } |
---|
| 93 | for (int j = 0; j < route.size(); j++) { |
---|
| 94 | carRoute[car.routeIndex + 1 + j] = route.get(j); |
---|
| 95 | } |
---|
| 96 | car.route = carRoute; |
---|
| 97 | car.setIntersection(); |
---|
| 98 | } |
---|
| 99 | return true; |
---|
| 100 | } |
---|
| 101 | } |
---|