[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.vehicular.routePlan.infrastructureRouted; |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | import java.util.StringTokenizer; |
---|
| 11 | |
---|
| 12 | import vnsim.vehicular.simulator.CarInstance; |
---|
| 13 | import vnsim.vehicular.simulator.RouteSegment; |
---|
| 14 | public class InfrastructureReply { |
---|
| 15 | String message; |
---|
| 16 | |
---|
| 17 | CarInstance carReference; |
---|
| 18 | |
---|
| 19 | public InfrastructureReply(String msg, CarInstance car) { |
---|
| 20 | this.message = msg; |
---|
| 21 | this.carReference = car; |
---|
| 22 | |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | public boolean analizeMessage() { |
---|
| 26 | System.out.println("CAR "+this.carReference.ID+" got routing reply "+this.message); |
---|
| 27 | int i, j, k; |
---|
| 28 | StringTokenizer st = null; |
---|
| 29 | st = new StringTokenizer(this.message, " \n"); |
---|
| 30 | String type = st.nextToken(); |
---|
| 31 | if (type.equals("DR")) { |
---|
| 32 | // System.out.println("Car "+carReceivingMsg.ID+" got a message that is not mine and not yet answered"+this.msg); |
---|
| 33 | return false; |
---|
| 34 | |
---|
| 35 | } |
---|
| 36 | if (carReference.routeIndex < carReference.route.length - 1) { |
---|
| 37 | // System.out.println(" carReceivingMsg "+carReceivingMsg.ID+" got something old"); |
---|
| 38 | return false; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | int rdNext, p1Next, p2Next; |
---|
| 42 | int semR, semP1, semP2, destR, destP; |
---|
| 43 | |
---|
| 44 | try { |
---|
| 45 | if ((new Integer(st.nextToken())).intValue() != carReference.ID) { |
---|
| 46 | //System.out.println("Car "+carReceivingMsg.ID+" got a message that is not mine"); |
---|
| 47 | return false; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | semR = new Integer(st.nextToken()).intValue(); |
---|
| 51 | semP1 = new Integer(st.nextToken()).intValue(); |
---|
| 52 | semP2 = new Integer(st.nextToken()).intValue(); |
---|
| 53 | |
---|
| 54 | destR = new Integer(st.nextToken()).intValue(); |
---|
| 55 | destP = new Integer(st.nextToken()).intValue(); |
---|
| 56 | |
---|
| 57 | } catch (Exception e) { |
---|
| 58 | System.out.println("Car " + carReference.ID |
---|
| 59 | + " got a message that has a wrong format"); |
---|
| 60 | return false; |
---|
| 61 | } |
---|
| 62 | try { |
---|
| 63 | //System.out.println("***********************************************************************"); |
---|
| 64 | |
---|
| 65 | rdNext = (new Integer(st.nextToken())).intValue(); |
---|
| 66 | p1Next = (new Integer(st.nextToken())).intValue(); |
---|
| 67 | p2Next = (new Integer(st.nextToken())).intValue(); |
---|
| 68 | |
---|
| 69 | if ((carReference.route[carReference.routeIndex].roadIndex != semR) |
---|
| 70 | || (carReference.route[carReference.routeIndex].pt1 != semP1) |
---|
| 71 | || (carReference.route[carReference.routeIndex].pt2 != semP2)) { |
---|
| 72 | //System.out.println("carReceivingMsg "+ carReceivingMsg.ID+"un raspuns de la alt semafor "); |
---|
| 73 | return false; |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // boolean found=false; |
---|
| 78 | // for(j=0;j<carReceivingMsg.route.length;j++){ |
---|
| 79 | // if((carReceivingMsg.route[j].roadIndex==rdNext)&&(carReceivingMsg.route[j].pt1==p1Next)&&(carReceivingMsg.route[j].pt2==p2Next)){ |
---|
| 80 | // found=true; |
---|
| 81 | // break; |
---|
| 82 | // |
---|
| 83 | // } |
---|
| 84 | // |
---|
| 85 | // } |
---|
| 86 | // if(found){ |
---|
| 87 | // // System.out.println("Already received this message---------------------------------------------------"); |
---|
| 88 | // return false; |
---|
| 89 | // |
---|
| 90 | // } |
---|
| 91 | // |
---|
| 92 | |
---|
| 93 | // System.out.println("\ncar "+carReceivingMsg.ID+" got route before modif "); |
---|
| 94 | // for(i=0;i<carReceivingMsg.route.length;i++){ |
---|
| 95 | // System.out.print(" "+carReceivingMsg.route[i]); |
---|
| 96 | // } |
---|
| 97 | |
---|
| 98 | if (carReference.route[carReference.routeIndex].roadIndex == rdNext) { |
---|
| 99 | if (Math.abs(carReference.route[carReference.routeIndex].pt2 - p1Next) > 2.0) { |
---|
| 100 | System.out.println("l-am prins"); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | } |
---|
| 104 | // carReceivingMsg.pw.println("reply "+this.msg); |
---|
| 105 | // carReceivingMsg.pw.flush(); |
---|
| 106 | |
---|
| 107 | RouteSegment[] tp = new RouteSegment[this.carReference.route.length + 1]; |
---|
| 108 | for (j = 0; j < this.carReference.route.length; j++) { |
---|
| 109 | tp[j] = this.carReference.route[j]; |
---|
| 110 | } |
---|
| 111 | tp[j] = new RouteSegment((short) rdNext, (short) p1Next, |
---|
| 112 | (short) p2Next); |
---|
| 113 | carReference.route = tp; |
---|
| 114 | |
---|
| 115 | // System.out.println("\ncar "+carReceivingMsg.ID+" got route for reply "+this.msg); |
---|
| 116 | // for(i=0;i<carReceivingMsg.route.length;i++){ |
---|
| 117 | // System.out.print(" "+carReceivingMsg.route[i]); |
---|
| 118 | // } |
---|
| 119 | |
---|
| 120 | carReference.setIntersection(); |
---|
| 121 | /// carReceivingMsg.startOnRoad(); |
---|
| 122 | // System.out.println("CAR "+this.car.ID+" got next segment: "+rdNext+" p1="+p1Next+" p2="+p2Next); |
---|
| 123 | |
---|
| 124 | } catch (Exception e) { |
---|
| 125 | |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return true; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | } |
---|