[31] | 1 | package vnsim.core; |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | import java.io.BufferedWriter; |
---|
| 5 | import java.io.DataInputStream; |
---|
| 6 | import java.io.DataOutputStream; |
---|
| 7 | import java.io.File; |
---|
| 8 | import java.io.FileInputStream; |
---|
| 9 | import java.io.FileOutputStream; |
---|
| 10 | import java.io.FileWriter; |
---|
| 11 | import java.io.IOException; |
---|
| 12 | import java.io.PrintWriter; |
---|
| 13 | import java.util.ArrayList; |
---|
| 14 | import java.util.Collections; |
---|
| 15 | import java.util.Iterator; |
---|
| 16 | import java.util.ListIterator; |
---|
| 17 | import java.util.Random; |
---|
| 18 | |
---|
| 19 | import vnsim.applications.adaptiveTL.IntersectionCarRecord; |
---|
| 20 | import vnsim.applications.adaptiveTL.WirelessTrafficLight; |
---|
| 21 | import vnsim.applications.emissions.EmissionsTrafficLight; |
---|
| 22 | import vnsim.applications.ezcab.EzcabCar; |
---|
| 23 | import vnsim.applications.ezcab.EzcabClient; |
---|
| 24 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
| 25 | import vnsim.applications.vitp.CarRunningVITP; |
---|
| 26 | import vnsim.applications.vitp.predefinedqueries.PredefinedQueries; |
---|
| 27 | import vnsim.applications.vitp.predefinedqueries.QueryByLocation; |
---|
| 28 | import vnsim.applications.vitp.predefinedqueries.QueryByVehicleId; |
---|
| 29 | import vnsim.core.events.CleanupEvent; |
---|
| 30 | import vnsim.core.events.Event; |
---|
| 31 | import vnsim.core.events.GPSEvent; |
---|
| 32 | import vnsim.core.events.ReceiveEvent; |
---|
| 33 | import vnsim.core.events.SendEvent; |
---|
| 34 | import vnsim.core.events.UnicastSendEvent; |
---|
| 35 | import vnsim.map.object.Globals; |
---|
| 36 | import vnsim.map.object.Map; |
---|
| 37 | import vnsim.map.object.PeanoKey; |
---|
| 38 | import vnsim.map.object.Point; |
---|
| 39 | import vnsim.map.object.Road; |
---|
| 40 | import vnsim.map.utils.GPSutil; |
---|
| 41 | import vnsim.network.RadioDev; |
---|
| 42 | import vnsim.network.dsrc.Application; |
---|
| 43 | import vnsim.network.dsrc.CarRunningDSRC; |
---|
| 44 | import vnsim.network.dsrc.DSRCStatisticComponent; |
---|
| 45 | import vnsim.network.dsrc.DSRCStatistics; |
---|
| 46 | import vnsim.network.dsrc.EmergencyRequest; |
---|
| 47 | import vnsim.network.dsrc.MAC80211; |
---|
| 48 | import vnsim.network.dsrc.WirelessPhy; |
---|
| 49 | import vnsim.network.propagation.Ricean; |
---|
| 50 | import vnsim.network.scft.CarRunningSCFT; |
---|
| 51 | import vnsim.vehicular.emissions.EmissionsResults; |
---|
| 52 | import vnsim.vehicular.generator.EntryFlowVariation; |
---|
| 53 | import vnsim.vehicular.generator.Mobility; |
---|
| 54 | import vnsim.vehicular.generator.NewCarEvent; |
---|
| 55 | import vnsim.vehicular.routePlan.RoutingConstants; |
---|
| 56 | import vnsim.vehicular.routePlan.cityRouting.CarToIntersectionComm; |
---|
| 57 | import vnsim.vehicular.routePlan.cityRouting.CityTrafficLight; |
---|
| 58 | import vnsim.vehicular.routePlan.cityRouting.IntersectionNode; |
---|
| 59 | import vnsim.vehicular.routePlan.cityRouting.RoadSegmentCost; |
---|
| 60 | import vnsim.vehicular.routePlan.cityRouting.Server; |
---|
| 61 | import vnsim.vehicular.routePlan.infrastructureRouted.DelayRecord; |
---|
| 62 | import vnsim.vehicular.routePlan.infrastructureRouted.GuidanceRequest; |
---|
| 63 | import vnsim.vehicular.routePlan.infrastructureRouted.InfrastructureNode; |
---|
| 64 | import vnsim.vehicular.routePlan.selfRouted.QueryGeneration; |
---|
| 65 | import vnsim.vehicular.scenarios.EntryExitScenario; |
---|
| 66 | import vnsim.vehicular.scenarios.EntryScenario; |
---|
| 67 | import vnsim.vehicular.scenarios.Route; |
---|
| 68 | import vnsim.vehicular.simulator.AggresivePersonality; |
---|
| 69 | import vnsim.vehicular.simulator.CalmPersonality; |
---|
| 70 | import vnsim.vehicular.simulator.CarInstance; |
---|
| 71 | import vnsim.vehicular.simulator.Personality; |
---|
| 72 | import vnsim.vehicular.simulator.RegularPersonality; |
---|
| 73 | import vnsim.vehicular.simulator.SortedCarVector; |
---|
| 74 | import vnsim.vehicular.simulator.intersections.Intersection; |
---|
| 75 | import vnsim.vehicular.simulator.intersections.IntersectionWithTrafficLights; |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * |
---|
| 81 | * The Engine class is the main class that controlls the simulation. It manages |
---|
| 82 | * the list of entities (cars, trafficlights) and the list of events and passes the |
---|
| 83 | * control to the mobility layer or communication layer when needed. |
---|
| 84 | * |
---|
| 85 | * @author Victor Gradinescu |
---|
| 86 | * |
---|
| 87 | */ |
---|
| 88 | |
---|
| 89 | public class Engine { |
---|
| 90 | |
---|
| 91 | /* EZCab - file informations about the client */ |
---|
| 92 | public static BufferedWriter bwClient, bwCab; |
---|
| 93 | |
---|
| 94 | public static double WIRELESS_RANGE = 0.1; |
---|
| 95 | public static double EXTENDED_WIRELESS_RANGE = 1.0; |
---|
| 96 | |
---|
| 97 | // Petroaca - simulation protocol type ( 802.11 or DSRC ) |
---|
| 98 | public static int simulationProtocol=Globals.PROTOCOL_80211; |
---|
| 99 | |
---|
| 100 | //Petroaca - propagation type ( tworay or shadowing ) |
---|
| 101 | public static int propagationModel=Globals.TWO_RAY_GROUND; |
---|
| 102 | |
---|
| 103 | //Petroaca - the Ricean propagation module |
---|
| 104 | public static Ricean riceanModule; |
---|
| 105 | |
---|
| 106 | DataInputStream dis = null; |
---|
| 107 | |
---|
| 108 | public int fps; |
---|
| 109 | |
---|
| 110 | ArrayList<Event> eventQueue = null; |
---|
| 111 | |
---|
| 112 | //Petroaca - the DSRC emergency channel |
---|
| 113 | ArrayList<Event> eventQueueEmergency = null; |
---|
| 114 | |
---|
| 115 | public boolean pauseFlag = true; |
---|
| 116 | |
---|
| 117 | boolean doneFlag = false,doneFlagEmergency=false; |
---|
| 118 | |
---|
| 119 | public ArrayList<SimulatedCarInfo> cars = null; |
---|
| 120 | |
---|
| 121 | public int crtTime = 0; |
---|
| 122 | |
---|
| 123 | public long t0 = 0L; |
---|
| 124 | |
---|
| 125 | public boolean withGUI, startedWithGUI; |
---|
| 126 | |
---|
| 127 | boolean runtimeMobility; |
---|
| 128 | |
---|
| 129 | public boolean disableComm = false; |
---|
| 130 | |
---|
| 131 | File fText = null, fBin = null; |
---|
| 132 | |
---|
| 133 | PrintWriter pwText = null; |
---|
| 134 | |
---|
| 135 | DataOutputStream outBin = null; |
---|
| 136 | |
---|
| 137 | long prevRealSec = 0; |
---|
| 138 | double lastSimSecond = 0.0; |
---|
| 139 | |
---|
| 140 | int eventsTime = 0, mobilityTime = 0, cleanupTime = 0, netTime = 0, codeTime = 0; |
---|
| 141 | int schedTime = 0, moveTime = 0, semTime = 0; |
---|
| 142 | |
---|
| 143 | public double maxControlDelay = 0; |
---|
| 144 | |
---|
| 145 | public int mycnt = 0; |
---|
| 146 | public long fincars; |
---|
| 147 | public long crossedCars; |
---|
| 148 | public double totalkm, totalControlDelay; |
---|
| 149 | public EmissionsResults totalEM = new EmissionsResults(); |
---|
| 150 | private long totalTime; |
---|
| 151 | |
---|
| 152 | public static int created = 0; |
---|
| 153 | |
---|
| 154 | // Routing |
---|
| 155 | public static int simulationType = 0; |
---|
| 156 | public static int applicationType = 0; |
---|
| 157 | int jammed; |
---|
| 158 | |
---|
| 159 | public ArrayList<SimulatedCarInfo> infrastructure = null; |
---|
| 160 | |
---|
| 161 | Object event; |
---|
| 162 | |
---|
| 163 | //City Routing |
---|
| 164 | private Server server= new Server(); |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | * |
---|
| 168 | * |
---|
| 169 | * @param withGUI If the simulator is run at the same time with GUI, the code |
---|
| 170 | * will have to be synchronized |
---|
| 171 | * @param runtimeMobility |
---|
| 172 | */ |
---|
| 173 | public Engine(boolean withGUI, boolean runtimeMobility) |
---|
| 174 | { |
---|
| 175 | try { |
---|
| 176 | FileWriter fwClient = new FileWriter("client_request.txt"); |
---|
| 177 | bwClient = new BufferedWriter(fwClient); |
---|
| 178 | FileWriter fwCab = new FileWriter("client_answers.txt"); |
---|
| 179 | bwCab = new BufferedWriter(fwCab); |
---|
| 180 | } catch (IOException e1) { |
---|
| 181 | e1.printStackTrace(); |
---|
| 182 | } |
---|
| 183 | this.withGUI = withGUI; |
---|
| 184 | this.startedWithGUI = withGUI; |
---|
| 185 | this.runtimeMobility = runtimeMobility; |
---|
| 186 | // init(); |
---|
| 187 | prevRealSec = t0 = System.currentTimeMillis(); |
---|
| 188 | |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /* |
---|
| 192 | * Load the map, initialize eventqueue, init |
---|
| 193 | */ |
---|
| 194 | public void init() { |
---|
| 195 | |
---|
| 196 | // try{ |
---|
| 197 | // File f=new File(".//maps//map//apaca.map"); |
---|
| 198 | // if(!f.exists()) { |
---|
| 199 | // System.out.println("NOT EXIST!"); |
---|
| 200 | // Globals.map = new trafficview.object.Map(".//maps\\rt\\apaca.RT1",".\\maps\\rt\\apaca.RT2"); |
---|
| 201 | // |
---|
| 202 | //// Globals.map = new trafficview.object.Map(".\\maps\\rt\\Intersectie.RT1","Intersectie.RT2"); |
---|
| 203 | // // Globals.map = new trafficview.object.Map("Autostrada.RT1","Autostrada.RT2"); |
---|
| 204 | // |
---|
| 205 | //// Globals.map = new trafficview.object.Map("TGR34023_2.RT1","TGR34023_2.RT2"); |
---|
| 206 | // ObjectOutputStream oos=new ObjectOutputStream(new |
---|
| 207 | // FileOutputStream(".\\maps\\map\\apaca.map")); |
---|
| 208 | // oos.writeObject(Globals.map); |
---|
| 209 | // } else { |
---|
| 210 | // System.out.println("EXIST!"); |
---|
| 211 | // ObjectInputStream ois=new ObjectInputStream(new |
---|
| 212 | // FileInputStream(".\\maps\\map\\apaca.map")); |
---|
| 213 | // Globals.map=(trafficview.object.Map)ois.readObject(); |
---|
| 214 | // } |
---|
| 215 | // }catch(Exception e){ |
---|
| 216 | // System.err.println("Error reading map;"); |
---|
| 217 | // e.printStackTrace(); |
---|
| 218 | // System.exit(0); |
---|
| 219 | // } |
---|
| 220 | // MapLoader.loadMap(); |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | Map map = Globals.map; |
---|
| 224 | if (map.roads.get(0).getName().startsWith("Iuliu") || map.roads.get(0).getName().startsWith("Dr")) |
---|
| 225 | { |
---|
| 226 | |
---|
| 227 | Globals.monitorWTL = (WirelessTrafficLight)map.allIntersections.get(1); |
---|
| 228 | } |
---|
| 229 | pauseFlag = false; |
---|
| 230 | doneFlag = false; |
---|
| 231 | |
---|
| 232 | // the positions of the cars may be updated by a mobility module by |
---|
| 233 | // recomputing positions at each step or can be updated by reading a |
---|
| 234 | // traces file |
---|
| 235 | if (!runtimeMobility) |
---|
| 236 | { |
---|
| 237 | try |
---|
| 238 | { |
---|
| 239 | if (dis != null) { |
---|
| 240 | dis.close(); |
---|
| 241 | } |
---|
| 242 | dis = new DataInputStream(new FileInputStream(Globals.inFile)); |
---|
| 243 | } |
---|
| 244 | catch (IOException e) |
---|
| 245 | { |
---|
| 246 | System.err.println("Error reading scenario; file error" |
---|
| 247 | + Globals.inFile); |
---|
| 248 | e.printStackTrace(); |
---|
| 249 | System.exit(0); |
---|
| 250 | } |
---|
| 251 | try |
---|
| 252 | { |
---|
| 253 | fps = dis.readInt(); |
---|
| 254 | } |
---|
| 255 | catch (IOException e) |
---|
| 256 | { |
---|
| 257 | System.err.println("Error reading file " + Globals.inFile); |
---|
| 258 | e.printStackTrace(); |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | else |
---|
| 262 | { |
---|
| 263 | fps = Globals.executionFPS; |
---|
| 264 | |
---|
| 265 | Globals.WIRELESS_TRANSMISSION_FRAMES = (int) Math |
---|
| 266 | .ceil((double) Globals.WIRELESS_TRANSMISSION_TIME |
---|
| 267 | / (1000 / Globals.executionFPS)); |
---|
| 268 | |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | // init cars and events |
---|
| 272 | eventQueue = new ArrayList<Event>(); |
---|
| 273 | |
---|
| 274 | cars = new ArrayList<SimulatedCarInfo>(); |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | //Petroaca - if DSRC simulation then initialize the emergency event queue |
---|
| 279 | if(this.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
| 280 | eventQueueEmergency = new ArrayList<Event>(); |
---|
| 281 | |
---|
| 282 | crtTime = 0; |
---|
| 283 | |
---|
| 284 | if (!runtimeMobility) { |
---|
| 285 | readScenarioPhase(); |
---|
| 286 | } else { |
---|
| 287 | // Mobility.initCarsTest(this); |
---|
| 288 | // Mobility.initCarsAutostrada(this); |
---|
| 289 | // Mobility.initCars(this); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | synchronized (eventQueue) |
---|
| 293 | { |
---|
| 294 | eventQueue.add(new GPSEvent(0)); |
---|
| 295 | |
---|
| 296 | eventQueue.add(new CleanupEvent(1)); |
---|
| 297 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 298 | while (it.hasNext()) |
---|
| 299 | { |
---|
| 300 | SimulatedCarInfo r = it.next(); |
---|
| 301 | int t = 4 + random.nextInt(Globals.executionFPS / 3); |
---|
| 302 | schedEvent(new SendEvent(4, r, |
---|
| 303 | SimulatedCarInfo.STANDARD_MESSAGE_ID)); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | /* Szekeres A. START_MODIFY - add broadcast event for each traffic light*/ |
---|
| 307 | for(int i=0;i<Globals.map.allIntersections.size();i++) { |
---|
| 308 | Intersection sender = Globals.map.allIntersections.get(i); |
---|
| 309 | if (sender instanceof EmissionsTrafficLight) |
---|
| 310 | { |
---|
| 311 | ((EmissionsTrafficLight)sender).init(); |
---|
| 312 | schedEvent(new SendEvent(4, (EmissionsTrafficLight)sender, (int)Globals.PROT_TL_FEEDBACK)); |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | /* Szekeres A. STOP_MODIFY */ |
---|
| 316 | |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | try |
---|
| 320 | { |
---|
| 321 | if (runtimeMobility && outputToFile) |
---|
| 322 | { |
---|
| 323 | |
---|
| 324 | fText = new File(Globals.inFile + ".txt"); |
---|
| 325 | fBin = new File(Globals.inFile); |
---|
| 326 | fText.createNewFile(); |
---|
| 327 | fBin.createNewFile(); |
---|
| 328 | pwText = new PrintWriter(new FileOutputStream(fText)); |
---|
| 329 | outBin = new DataOutputStream(new FileOutputStream(fBin)); |
---|
| 330 | |
---|
| 331 | pwText.println("" + Globals.FPS); |
---|
| 332 | outBin.writeInt(Globals.FPS); |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | catch (IOException e) |
---|
| 336 | { |
---|
| 337 | System.err.println("Error reading scenario; file error" |
---|
| 338 | + Globals.inFile); |
---|
| 339 | e.printStackTrace(); |
---|
| 340 | System.exit(0); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | if (this.simulationType == Globals.routePlanConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { |
---|
| 344 | DelayRecord.initDelays(); |
---|
| 345 | this.infrastructure=new ArrayList<SimulatedCarInfo>(); |
---|
| 346 | //create a fixed node for every intersection |
---|
| 347 | InfrastructureNode.createInfrastructureNodes(); |
---|
| 348 | } |
---|
| 349 | /*if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
| 350 | RoadSegmentCost.initRoadSegmentsCost(); |
---|
| 351 | this.infrastructure=new ArrayList<SimulatedCarInfo>(); |
---|
| 352 | //create a fixed node for every intersection |
---|
| 353 | IntersectionNode.createIntersectionNodes(); |
---|
| 354 | }*/ |
---|
| 355 | |
---|
| 356 | //Petroaca - instantiate the ricean propagation module |
---|
| 357 | try |
---|
| 358 | { |
---|
| 359 | if(Engine.propagationModel==Globals.RICEAN) |
---|
| 360 | this.riceanModule=new Ricean(Globals.RICEAN_FILE_NAME,true); |
---|
| 361 | } |
---|
| 362 | catch(IOException ioe){System.out.println("Ricean file io error:"+ioe.getMessage());System.exit(1);} |
---|
| 363 | // System.out.println("Engine initialized - "+eventQueue.size()+" |
---|
| 364 | // events"); |
---|
| 365 | // System.out.println("CARS:"); |
---|
| 366 | // System.out.println(cars); |
---|
| 367 | // System.out.println("Events:"); |
---|
| 368 | // System.out.println(eventQueue); |
---|
| 369 | |
---|
| 370 | // Road r = Globals.map.roads.get(5); |
---|
| 371 | // Point p = r.points.get(10900); |
---|
| 372 | // int i = 10899; |
---|
| 373 | // while (true){ |
---|
| 374 | // Point px = r.points.get(i); |
---|
| 375 | // i--; |
---|
| 376 | // if (p.getDistance() - px.getDistance() > 10) |
---|
| 377 | // break; |
---|
| 378 | // } |
---|
| 379 | // System.out.println(i + " " +(p.getDistance() - r.points.get(i).getDistance())); |
---|
| 380 | |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | /** |
---|
| 384 | * Reset simulation |
---|
| 385 | */ |
---|
| 386 | public void reset() |
---|
| 387 | { |
---|
| 388 | eventQueue = new ArrayList<Event>(); |
---|
| 389 | cars = new ArrayList<SimulatedCarInfo>(); |
---|
| 390 | |
---|
| 391 | //Petroaca - if DSRC simulation also reset the emergency channel |
---|
| 392 | if(this.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
| 393 | eventQueueEmergency = new ArrayList<Event>(); |
---|
| 394 | |
---|
| 395 | crtTime = 0; |
---|
| 396 | |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | /** |
---|
| 400 | * Turn on communication by scheduling transmission events for each car. |
---|
| 401 | * |
---|
| 402 | */ |
---|
| 403 | public void restartCommunication() { |
---|
| 404 | disableComm = false; |
---|
| 405 | synchronized (eventQueue) { |
---|
| 406 | // eventQueue.add(new CleanupEvent(crtTime+1)); |
---|
| 407 | |
---|
| 408 | schedEvent(new CleanupEvent(crtTime + 1)); |
---|
| 409 | |
---|
| 410 | synchronized (cars) |
---|
| 411 | { |
---|
| 412 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 413 | while (it.hasNext()) { |
---|
| 414 | SimulatedCarInfo r = it.next(); |
---|
| 415 | schedEvent(new SendEvent(crtTime + 2, r, |
---|
| 416 | SimulatedCarInfo.STANDARD_MESSAGE_ID)); |
---|
| 417 | } |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | // Petroaca - if DSRC simulation also reset the emergency channel |
---|
| 422 | if(this.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
| 423 | eventQueueEmergency = new ArrayList<Event>(); |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | /** |
---|
| 427 | * Start simulation, execute events from queue continously |
---|
| 428 | * |
---|
| 429 | */ |
---|
| 430 | public void play() |
---|
| 431 | { |
---|
| 432 | // supposed to be in the init() |
---|
| 433 | // between init and play() Globals.map.allIntersections is changing |
---|
| 434 | // need to find a fix |
---|
| 435 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
| 436 | CityTrafficLight.upgradeToCityTL(); |
---|
| 437 | RoadSegmentCost.initRoadSegmentsCost(); |
---|
| 438 | this.infrastructure=new ArrayList<SimulatedCarInfo>(); |
---|
| 439 | //create a fixed node for every intersection |
---|
| 440 | IntersectionNode.createIntersectionNodes(server); |
---|
| 441 | server.init(); |
---|
| 442 | } |
---|
| 443 | // |
---|
| 444 | prevRealSec = t0 = System.currentTimeMillis(); |
---|
| 445 | pauseFlag = false; |
---|
| 446 | while (!pauseFlag && !doneFlag) { |
---|
| 447 | step(); |
---|
| 448 | } |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | /** |
---|
| 452 | * Execute all the events for the following <seconds> s |
---|
| 453 | * |
---|
| 454 | */ |
---|
| 455 | public void play(int seconds) { |
---|
| 456 | int frames = seconds * fps; |
---|
| 457 | pauseFlag = false; |
---|
| 458 | for (int i = 0; i < frames && !pauseFlag && !doneFlag; i++) { |
---|
| 459 | step(); |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | /** |
---|
| 464 | * Request the interruption of the continous execution of events |
---|
| 465 | * |
---|
| 466 | */ |
---|
| 467 | public void pause() { |
---|
| 468 | pauseFlag = true; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | /** |
---|
| 472 | * Execute all the events for the current moment of time (virtual time) |
---|
| 473 | * |
---|
| 474 | */ |
---|
| 475 | double dec1(double x){ |
---|
| 476 | return (double)((int)(x * 10)) / 10; |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | |
---|
| 480 | long lastTime = -1; |
---|
| 481 | |
---|
| 482 | /** |
---|
| 483 | * Process the events for the current step of the simulation |
---|
| 484 | * |
---|
| 485 | */ |
---|
| 486 | public void step() { |
---|
| 487 | int cnt = 0; |
---|
| 488 | |
---|
| 489 | if(Globals.SOCIALNETWORK == 1){ |
---|
| 490 | double sec = vnsim.socialNet.GlobalNetwork.nextReconfigurationTime*Globals.SECOND; |
---|
| 491 | // if(crtTime == sec){ |
---|
| 492 | // int ret=JOptionPane.showConfirmDialog(null, "au trecut "+sec+" secunde"); |
---|
| 493 | // //vnsim.vehicular.generator.Mobility.events = new SortedEventsVector(); |
---|
| 494 | // Scenario s = vnsim.socialNet.Main.recofigurare(); |
---|
| 495 | // try { |
---|
| 496 | // vnsim.socialNet.Main.addCarEvents(s,crtTime); |
---|
| 497 | // } catch (Exception ex) { |
---|
| 498 | // JOptionPane.showMessageDialog(null, "<html>ERROR! Map structure has been modified<br> Try rebuilding the map " + s.mapFileName +"!</html>"); |
---|
| 499 | // } |
---|
| 500 | // } |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | if (crtTime > 3.5 * Globals.HOUR){ |
---|
| 504 | doneFlag = true; |
---|
| 505 | System.exit(0); |
---|
| 506 | return; |
---|
| 507 | } |
---|
| 508 | if (withGUI) { |
---|
| 509 | synchronized (Globals.mutex) { |
---|
| 510 | if (Globals.flag == 1) { |
---|
| 511 | try { |
---|
| 512 | Globals.mutex.wait(); |
---|
| 513 | } catch (Exception e) { |
---|
| 514 | |
---|
| 515 | } |
---|
| 516 | } |
---|
| 517 | Globals.flag = 0; |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | long t1 = System.currentTimeMillis(); |
---|
| 522 | if (crtTime % Globals.SECOND == 0) |
---|
| 523 | { |
---|
| 524 | lastSimSecond = (double)(t1-prevRealSec)/1000; |
---|
| 525 | prevRealSec = t1; |
---|
| 526 | if (lastSimSecond < 1) |
---|
| 527 | try{ |
---|
| 528 | Thread.sleep(100);//(int)((1.0 - lastSimSecond)*1000)); |
---|
| 529 | }catch(Exception e){ |
---|
| 530 | |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | if (crtTime % Globals.MINUTE == 0){ |
---|
| 535 | System.out.println((crtTime / Globals.MINUTE) + " sim min;\t" + |
---|
| 536 | "packets: "+ Globals.packets +"\t" + |
---|
| 537 | " collided: "+Globals.collided + |
---|
| 538 | " lost: " + Globals.lost+ |
---|
| 539 | " interfered: " + Globals.interfered+ |
---|
| 540 | " max range: " + Globals.maxRange); |
---|
| 541 | |
---|
| 542 | Globals.pw.println((crtTime / Globals.MINUTE) + " sim min;\t" + |
---|
| 543 | "packets: "+ Globals.packets +"\t" + |
---|
| 544 | " collided: "+Globals.collided + |
---|
| 545 | " lost: " + Globals.lost+ |
---|
| 546 | " interfered: " + Globals.interfered+ |
---|
| 547 | " max range: " + Globals.maxRange); |
---|
| 548 | Globals.packets = 0; |
---|
| 549 | Globals.collided = 0; |
---|
| 550 | Globals.lost = 0; |
---|
| 551 | |
---|
| 552 | // System.out.println((crtTime / Globals.MINUTE) + " sim min;\t" + |
---|
| 553 | // "simulation time: "+ (System.currentTimeMillis() - t0) +"\t" + |
---|
| 554 | // " cars: "+cars.size() + |
---|
| 555 | // " created " + created); |
---|
| 556 | Globals.pw.println((crtTime / Globals.MINUTE) + |
---|
| 557 | " global volume: " + Globals.volume * 60+ |
---|
| 558 | " global demand: " + Globals.demand * 60); |
---|
| 559 | Globals.volume = 0; |
---|
| 560 | Globals.demand = 0; |
---|
| 561 | |
---|
| 562 | //Petroaca |
---|
| 563 | if(this.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
| 564 | { |
---|
| 565 | /*System.out.print("\n"); |
---|
| 566 | System.out.println("DSRC DATA------------------------------"); |
---|
| 567 | System.out.println((crtTime / Globals.MINUTE)+ " min\t" + "weak :"+Globals.DSRC_PACKETS_LOST_WEAK+ |
---|
| 568 | " collided :"+Globals.DSRC_PACKETS_LOST_COLLISION+" ok: "+Globals.DSRC_PACKETS_RECEIVED_OK+ |
---|
| 569 | " lost Tx :"+Globals.DSRC_PACKETS_LOST_TX+" lost Rx :"+Globals.DSRC_PACKETS_LOST_RX+ |
---|
| 570 | "lost per :"+Globals.DSRC_PACKETS_LOST_PER+" lost corrupted :"+Globals.DSRC_PACKETS_LOST_CORRUPTED |
---|
| 571 | +" total packets: "+Globals.DSRC_PACKETS_TOTAL); |
---|
| 572 | System.out.println("send faliure Rx:"+Globals.DSRC_PACKETS_SEND_FALIURE_RX+" send faliure noise:"+Globals.DSRC_PACKETS_SEND_FALIURE_NOISE |
---|
| 573 | ); |
---|
| 574 | */ |
---|
| 575 | DSRCStatistics.addStatistic(new DSRCStatisticComponent(0,Globals.DSRC_PACKETS_LOST_WEAK,Globals.DSRC_PACKETS_TOTAL,Globals.DSRC_PACKETS_LOST_CORRUPTED,Globals.DSRC_PACKETS_LOST_COLLISION,Globals.DSRC_PACKETS_LOST_TX,Globals.DSRC_PACKETS_LOST_RX,Globals.DSRC_PACKETS_LOST_PER,Globals.DSRC_PACKETS_RECEIVED_OK |
---|
| 576 | ,0,0,Globals.CUMMULATIVE_PACKETS_FV,Globals.CUMMULATIVE_PACKETS_NFV,Globals.CUMMULATIVE_PACKETS_LAV,Globals.CUMMULATIVE_PACKETS_RAV,Globals.IRT_FV,Globals.IRT_NFV,Globals.IRT_LAV,Globals.IRT_RAV,0,0,0,0,this.cars.size(),Globals.EMERGENCY_RECEIVED,Globals.EMERGENCY_SENT)); |
---|
| 577 | |
---|
| 578 | |
---|
| 579 | Globals.DSRC_PACKETS_LOST_WEAK=0; |
---|
| 580 | Globals.DSRC_PACKETS_LOST_COLLISION=0; |
---|
| 581 | Globals.DSRC_PACKETS_LOST_TX=0; |
---|
| 582 | Globals.DSRC_PACKETS_LOST_RX=0; |
---|
| 583 | Globals.DSRC_PACKETS_LOST_PER=0; |
---|
| 584 | Globals.DSRC_PACKETS_LOST_CORRUPTED=0; |
---|
| 585 | Globals.DSRC_PACKETS_RECEIVED_OK=0; |
---|
| 586 | Globals.DSRC_PACKETS_TOTAL=0; |
---|
| 587 | Globals.DSRC_PACKETS_SEND_FALIURE_RX=0; |
---|
| 588 | Globals.DSRC_PACKETS_SEND_FALIURE_NOISE=0; |
---|
| 589 | Globals.DSRC_SEND=0; |
---|
| 590 | |
---|
| 591 | System.out.println((crtTime / Globals.MINUTE)+ " min\t"+"FV:"+Globals.CUMMULATIVE_PACKETS_FV+" NFV:"+Globals.CUMMULATIVE_PACKETS_NFV+" LAV:"+Globals.CUMMULATIVE_PACKETS_LAV+" RAV:"+Globals.CUMMULATIVE_PACKETS_RAV+" EMERGENCY_RECV:"+Globals.EMERGENCY_RECEIVED+" EMERGENCY_SENT:"+Globals.EMERGENCY_SENT); |
---|
| 592 | |
---|
| 593 | Globals.CUMMULATIVE_PACKETS_RAV=0; |
---|
| 594 | Globals.CUMMULATIVE_PACKETS_LAV=0; |
---|
| 595 | Globals.CUMMULATIVE_PACKETS_FV=0; |
---|
| 596 | Globals.CUMMULATIVE_PACKETS_NFV=0; |
---|
| 597 | Globals.IRT_FV=0; |
---|
| 598 | Globals.IRT_NFV=0; |
---|
| 599 | Globals.IRT_LAV=0; |
---|
| 600 | Globals.IRT_RAV=0; |
---|
| 601 | Globals.EMERGENCY_RECEIVED=0; |
---|
| 602 | Globals.EMERGENCY_SENT=0; |
---|
| 603 | |
---|
| 604 | /*System.out.println("END DSRC DATA------------------------------");*/ |
---|
| 605 | } |
---|
| 606 | } |
---|
| 607 | if (crtTime % (5 * Globals.MINUTE) == 0){ |
---|
| 608 | if (lastTime == -1) |
---|
| 609 | lastTime = t0; |
---|
| 610 | long realTime = t1 - lastTime; |
---|
| 611 | lastTime = t1; |
---|
| 612 | // Globals.pw.println("\n-----"); |
---|
| 613 | // Globals.pw.println("5 virtual minutes = " + (double)realTime/1000); |
---|
| 614 | // Globals.pw.println("density [veh/km] = " + (double) cars.size()/10); |
---|
| 615 | // Globals.pw.println("events: " + (double) (100 * (eventsTime - codeTime - cleanupTime)) |
---|
| 616 | // / realTime); |
---|
| 617 | // Globals.pw.println("mobility: " + (double) (100 * mobilityTime) |
---|
| 618 | // / realTime); |
---|
| 619 | // Globals.pw.println("code: " + (double) (100 * codeTime + cleanupTime) / realTime); |
---|
| 620 | // Globals.pw.println("-----"); |
---|
| 621 | |
---|
| 622 | eventsTime = 0; |
---|
| 623 | codeTime = 0; |
---|
| 624 | cleanupTime = 0; |
---|
| 625 | mobilityTime = 0; |
---|
| 626 | |
---|
| 627 | Globals.pw.println( "\n" + (crtTime / Globals.MINUTE) + " " + |
---|
| 628 | " " + fincars + |
---|
| 629 | " "+ (totalControlDelay/fincars) + |
---|
| 630 | " "+ (maxControlDelay) + |
---|
| 631 | " "+ ( ( totalEM.fc * 100) / (1000 * totalkm)) + |
---|
| 632 | " "+ (totalEM.fc / fincars) + |
---|
| 633 | " "+ (totalEM.co2 / fincars) + |
---|
| 634 | " "+ (totalEM.co / fincars) + |
---|
| 635 | " "+ (totalEM.hc / fincars) + |
---|
| 636 | " "+ (totalEM.nox / fincars) |
---|
| 637 | ); |
---|
| 638 | Globals.pw.flush(); |
---|
| 639 | |
---|
| 640 | Globals.demo.st.clearInfo(); |
---|
| 641 | Globals.demo.st.addInfo("Avg Delay "+ dec1(totalControlDelay/(fincars*60))+" min"); |
---|
| 642 | Globals.demo.st.addInfo("Max Delay "+dec1(maxControlDelay / 60)+" min"); |
---|
| 643 | Globals.demo.st.addInfo("Fuel: " + dec1( ( totalEM.fc * 100) / (1000 * totalkm)) + " L/100 km"); |
---|
| 644 | Globals.demo.st.addInfo("CO2: " + dec1((totalEM.co2 * 20 )/ 1000)+" kg/h"); |
---|
| 645 | Globals.demo.st.addInfo("CO: " + dec1((totalEM.co * 20 )/ 1000)+" kg/h"); |
---|
| 646 | Globals.demo.st.addInfo("HC: " + dec1((totalEM.hc * 20 )/ 1000)+" kg/h"); |
---|
| 647 | Globals.demo.st.addInfo("NOx: " + dec1((totalEM.nox * 20 )/ 1000)+" kg/h"); |
---|
| 648 | |
---|
| 649 | |
---|
| 650 | |
---|
| 651 | Globals.pwxc.println( (crtTime / Globals.MINUTE) + " " + |
---|
| 652 | " " + fincars + |
---|
| 653 | " "+ (totalControlDelay/fincars) + |
---|
| 654 | " "+ (maxControlDelay) + |
---|
| 655 | " "+ ( (totalEM.fc * 100) / ( 1000 * totalkm)) + |
---|
| 656 | " "+ (totalEM.fc / crossedCars) + |
---|
| 657 | " "+ (totalEM.co2 / crossedCars) + |
---|
| 658 | " "+ (totalEM.co / crossedCars) + |
---|
| 659 | " "+ (totalEM.hc / crossedCars) + |
---|
| 660 | " "+ (totalEM.nox / crossedCars) + |
---|
| 661 | " "+ crossedCars |
---|
| 662 | ); |
---|
| 663 | Globals.pwxc.flush(); |
---|
| 664 | if (fincars>0) { |
---|
| 665 | Globals.pwxc.println( "min " + (crtTime / Globals.MINUTE) + |
---|
| 666 | " cars: " + fincars + |
---|
| 667 | " total time: " + totalTime + |
---|
| 668 | " total km: " + totalkm + |
---|
| 669 | " time/car: " + totalTime / fincars + |
---|
| 670 | " L/100: "+ ( (totalEM.fc * 100) / ( 1000 * totalkm)) + |
---|
| 671 | " fc: " + (totalEM.fc / fincars) + |
---|
| 672 | " co2: " + (totalEM.co2 / fincars) + |
---|
| 673 | " co: " + (totalEM.co / fincars) + |
---|
| 674 | " hc: " + (totalEM.hc / fincars) + |
---|
| 675 | " nox: " + (totalEM.nox / fincars) |
---|
| 676 | ); |
---|
| 677 | Globals.pwxc.flush(); |
---|
| 678 | } |
---|
| 679 | //fincars = 0; |
---|
| 680 | //crossedCars = 0; |
---|
| 681 | //totalkm = 0; |
---|
| 682 | //totalEM = new EmissionsResults(); |
---|
| 683 | //totalControlDelay = 0; |
---|
| 684 | //maxControlDelay = 0; |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | |
---|
| 688 | // if (crtTime % Globals.SECOND == 0) |
---|
| 689 | // |
---|
| 690 | // if (crtTime % (Globals.executionFPS / Globals.FPS) == 0) |
---|
| 691 | if (startedWithGUI) { |
---|
| 692 | Globals.demo.st.setCount(crtTime); |
---|
| 693 | Globals.demo.st.setCurrentTime(System.currentTimeMillis() - t0); |
---|
| 694 | Globals.demo.st.setSimulationTime((float) ((crtTime * 1000) / fps)); |
---|
| 695 | //Globals.demo.st.setSemTime(mycnt); |
---|
| 696 | Globals.demo.st.setNrEvents(eventQueue.size()); |
---|
| 697 | Globals.demo.st.setCarsNo(cars.size()); |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | long t2 = System.currentTimeMillis(); |
---|
| 701 | |
---|
| 702 | // Petroaca - if DSRC simualtion play the events in emergency channel as well |
---|
| 703 | |
---|
| 704 | if(this.simulationProtocol==Globals.PROTOCOL_DSRC && Globals.MULTI_CHANNEL) |
---|
| 705 | { |
---|
| 706 | while (true) { |
---|
| 707 | if (eventQueueEmergency.size() == 0) { |
---|
| 708 | doneFlagEmergency = true; |
---|
| 709 | break; |
---|
| 710 | } |
---|
| 711 | |
---|
| 712 | Event e = (Event) eventQueueEmergency.get(0); |
---|
| 713 | if (e.getTime() == crtTime) { |
---|
| 714 | playEventEmergency(); |
---|
| 715 | //cnt++; |
---|
| 716 | } else |
---|
| 717 | break; |
---|
| 718 | } |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | while (true) { |
---|
| 722 | if (eventQueue.size() == 0) { |
---|
| 723 | doneFlag = true; |
---|
| 724 | return; |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | Event e = (Event) eventQueue.get(0); |
---|
| 728 | if (e.getTime() == crtTime) { |
---|
| 729 | playEvent(); |
---|
| 730 | cnt++; |
---|
| 731 | } else |
---|
| 732 | break; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | |
---|
| 736 | //increment simulation time |
---|
| 737 | crtTime++; |
---|
| 738 | |
---|
| 739 | long t3 = System.currentTimeMillis(); |
---|
| 740 | if (crtTime % (Globals.executionFPS / Globals.FPS) == 0) |
---|
| 741 | if (!runtimeMobility) |
---|
| 742 | readScenarioPhase(); |
---|
| 743 | else |
---|
| 744 | generateScenarioPhase(); |
---|
| 745 | checkCreateNewCars(); |
---|
| 746 | |
---|
| 747 | long t4 = System.currentTimeMillis(); |
---|
| 748 | eventsTime += t3 - t2; |
---|
| 749 | mobilityTime += t4 - t3; |
---|
| 750 | |
---|
| 751 | if (this.simulationType == Globals.routePlanConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { |
---|
| 752 | if (crtTime % 100 == 0) { |
---|
| 753 | for (int i = 0; i < this.infrastructure.size(); i++) { |
---|
| 754 | |
---|
| 755 | ((InfrastructureNode) this.infrastructure.get(i)) |
---|
| 756 | .updateDelays(); |
---|
| 757 | } |
---|
| 758 | } |
---|
| 759 | } |
---|
| 760 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
| 761 | for (int i = 0; i < this.infrastructure.size(); i++) { |
---|
| 762 | ((IntersectionNode) infrastructure.get(i)).step(crtTime); |
---|
| 763 | } |
---|
| 764 | server.step(crtTime); |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | // if (crtTime % Globals.MINUTE == 0){ |
---|
| 768 | // System.out.println("[Engine - step()] "); |
---|
| 769 | // System.out.println("\ntotal: " + (double)(t4 - t0)/1000); |
---|
| 770 | // System.out.println("events: " + (double) (100 * eventsTime) |
---|
| 771 | // / (t4 - t0)); |
---|
| 772 | // System.out.println("mobility: " + (double) (100 * mobilityTime) |
---|
| 773 | // / (t4 - t0)); |
---|
| 774 | // System.out.println("code: " + (double) (100 * codeTime) |
---|
| 775 | // / (t4 - t0)); |
---|
| 776 | // |
---|
| 777 | // } |
---|
| 778 | // System.out.println(); |
---|
| 779 | // System.out.println("crtTime "+crtTime); |
---|
| 780 | // System.out.println("CARS:"); |
---|
| 781 | // System.out.println(cars); |
---|
| 782 | // System.out.println("Events:"); |
---|
| 783 | // System.out.println(eventQueue); |
---|
| 784 | |
---|
| 785 | //checkInjectQueries(); |
---|
| 786 | |
---|
| 787 | // if (crtTime % Globals.SECOND == 0){ |
---|
| 788 | // |
---|
| 789 | // if (crtTime % (Globals.executionFPS / Globals.FPS) == 0) |
---|
| 790 | // if (withGUI) { |
---|
| 791 | // synchronized (Globals.mutex){ |
---|
| 792 | // Globals.flag = 1; |
---|
| 793 | // Globals.mutex.notify(); |
---|
| 794 | // } |
---|
| 795 | // } |
---|
| 796 | // }else |
---|
| 797 | // synchronized (Globals.mutex){ |
---|
| 798 | // Globals.mutex.notify(); |
---|
| 799 | // } |
---|
| 800 | |
---|
| 801 | } |
---|
| 802 | |
---|
| 803 | /** |
---|
| 804 | * VITP. |
---|
| 805 | * |
---|
| 806 | */ |
---|
| 807 | private void checkInjectQueries() { |
---|
| 808 | // should inject some queries? |
---|
| 809 | vnsim.applications.vitp.predefinedqueries.Query q=PredefinedQueries.queries.get(crtTime); |
---|
| 810 | if(q!=null) |
---|
| 811 | { |
---|
| 812 | if(q instanceof QueryByVehicleId) { |
---|
| 813 | //look for the vehicle with the specified id |
---|
| 814 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 815 | while (it.hasNext()) { |
---|
| 816 | SimulatedCarInfo r = it.next(); |
---|
| 817 | if(r.getVehicleId()==((QueryByVehicleId)q).vehicleId) |
---|
| 818 | { |
---|
| 819 | //inject the query |
---|
| 820 | CarRunningVITP x = (CarRunningVITP)r; |
---|
| 821 | if(!x.isEquipped) |
---|
| 822 | continue; //look for an equipped vehicle |
---|
| 823 | x.p.postMessageFromAbove(q.req); |
---|
| 824 | System.out.println("QUERY INJECTED; CRTTIME="+crtTime); |
---|
| 825 | break; |
---|
| 826 | } |
---|
| 827 | } |
---|
| 828 | } else if(q instanceof QueryByLocation) { |
---|
| 829 | //look for a vehicle in the specified location |
---|
| 830 | int roadIdx=((QueryByLocation)q).roadIdx; |
---|
| 831 | int pointIdx=((QueryByLocation)q).pointIdx; |
---|
| 832 | |
---|
| 833 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 834 | while (it.hasNext()) { |
---|
| 835 | SimulatedCarInfo r = it.next(); |
---|
| 836 | if(r.getRoadIdx()==roadIdx && r.getPointIdx()<pointIdx+5 && r.getPointIdx()>pointIdx-5) { |
---|
| 837 | CarRunningVITP x = (CarRunningVITP)r; |
---|
| 838 | if(!x.isEquipped) |
---|
| 839 | continue; //look for an equipped vehicle |
---|
| 840 | x.p.postMessageFromAbove(q.req); |
---|
| 841 | System.out.println("QUERY INJECTED; CRTTIME="+crtTime); |
---|
| 842 | |
---|
| 843 | // Road road=Globals.map.roads.get(r.getRoadIdx()); |
---|
| 844 | // |
---|
| 845 | // Point p1=road.points.get(r.getPointIdx()); |
---|
| 846 | // Point p2=road.points.get(q.req.getDest().pt1); |
---|
| 847 | // |
---|
| 848 | // // System.out.println("P1="+r.getPointIdx()); |
---|
| 849 | // // System.out.println("P2="+q.req.getDest().pt1); |
---|
| 850 | // // |
---|
| 851 | // // System.out.println("AWAY="+(p1.getDistance()-p2.getDistance())); |
---|
| 852 | // |
---|
| 853 | // Statistics.setQueryDistance(MessageIndexModule.index, (p1.getDistance()-p2.getDistance())); |
---|
| 854 | // Statistics.setInitialTime(MessageIndexModule.index, crtTime); |
---|
| 855 | break; |
---|
| 856 | } |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | } |
---|
| 860 | } |
---|
| 861 | |
---|
| 862 | static Random random = new Random(); |
---|
| 863 | |
---|
| 864 | public static int lastCarID = 0; |
---|
| 865 | |
---|
| 866 | /** |
---|
| 867 | * Insert new cars at the map entry points according to the scheduled flow. |
---|
| 868 | * |
---|
| 869 | */ |
---|
| 870 | private void checkCreateNewCars() |
---|
| 871 | { |
---|
| 872 | |
---|
| 873 | int percent; |
---|
| 874 | // should create some new cars? |
---|
| 875 | NewCarEvent first=Mobility.events.first(); |
---|
| 876 | while(first!=null && first.timestamp==crtTime) { |
---|
| 877 | //create this one |
---|
| 878 | EntryScenario es=Mobility.entryScenarios.get(first.index); |
---|
| 879 | |
---|
| 880 | int roadIndexSrc=es.entry.roadIdx; |
---|
| 881 | int pointIndexSrc=es.entry.ptIdx; |
---|
| 882 | |
---|
| 883 | int ID=lastCarID; |
---|
| 884 | lastCarID++; |
---|
| 885 | |
---|
| 886 | //which exit is the car going towards? |
---|
| 887 | double rand; |
---|
| 888 | rand=(random.nextDouble())*100; |
---|
| 889 | int i=0; |
---|
| 890 | int sum=0; |
---|
| 891 | while(i<es.entryExits.size()) { |
---|
| 892 | sum+=es.entryExits.elementAt(i).percentOfFlow; |
---|
| 893 | if(rand<sum) { |
---|
| 894 | //this is the chosen exit |
---|
| 895 | break; |
---|
| 896 | } |
---|
| 897 | i++; |
---|
| 898 | } |
---|
| 899 | EntryExitScenario ees=null; |
---|
| 900 | if(i==es.entryExits.size()) { |
---|
| 901 | //last exit chosen |
---|
| 902 | ees=es.entryExits.lastElement(); |
---|
| 903 | } else |
---|
| 904 | ees=es.entryExits.elementAt(i); |
---|
| 905 | |
---|
| 906 | int roadIndexDst=ees.exit.roadIdx; |
---|
| 907 | int pointIndexDst=ees.exit.ptIdx; |
---|
| 908 | |
---|
| 909 | //which route is the car choosing? |
---|
| 910 | rand=(random.nextDouble())*100; |
---|
| 911 | i=0; |
---|
| 912 | sum=0; |
---|
| 913 | while(i<ees.percentsEachRoute.size()) { |
---|
| 914 | sum+=ees.percentsEachRoute.elementAt(i); |
---|
| 915 | if(rand<sum) { |
---|
| 916 | //this is the chosen route |
---|
| 917 | break; |
---|
| 918 | } |
---|
| 919 | i++; |
---|
| 920 | } |
---|
| 921 | Route r=null; |
---|
| 922 | if(i==ees.percentsEachRoute.size()) { |
---|
| 923 | //last route chosen |
---|
| 924 | r=ees.routes.lastElement(); |
---|
| 925 | } else |
---|
| 926 | r=ees.routes.elementAt(i); |
---|
| 927 | |
---|
| 928 | for(i=0;i<Globals.map.roads.get(roadIndexSrc).laneNo;i++) { |
---|
| 929 | //one vehicle on each lane |
---|
| 930 | |
---|
| 931 | // double offset=4*random.nextDouble(); |
---|
| 932 | // System.out.println("OFFSET="+offset); |
---|
| 933 | |
---|
| 934 | Personality personality; |
---|
| 935 | rand=(random.nextDouble())*100; |
---|
| 936 | if(rand<es.percentsDriverTypes.elementAt(0)) |
---|
| 937 | { |
---|
| 938 | personality=new CalmPersonality(); |
---|
| 939 | } else if(rand<es.percentsDriverTypes.elementAt(0)+es.percentsDriverTypes.elementAt(1)) { |
---|
| 940 | personality=new RegularPersonality(); |
---|
| 941 | } else { |
---|
| 942 | personality=new AggresivePersonality(); |
---|
| 943 | } |
---|
| 944 | |
---|
| 945 | CarInstance car = null; |
---|
| 946 | if (simulationType == RoutingConstants.INITIALLY_PROVIDED_ROUTE || simulationType == RoutingConstants.ROUTING_SCFT) { |
---|
| 947 | |
---|
| 948 | car = Mobility.createNewCar(roadIndexSrc, |
---|
| 949 | pointIndexSrc, 0, i + 1, 0.0, personality, /*personality.getWantedSpeed(roadIndexSrc)*/ 0.0, ID, |
---|
| 950 | roadIndexDst, pointIndexDst, r.route); |
---|
| 951 | |
---|
| 952 | System.out.println("Finished creating new car"); |
---|
| 953 | double d =0; |
---|
| 954 | if(car!=null) |
---|
| 955 | d = car.getDistanceToCarInFront(); |
---|
| 956 | if (d < 10 && d!=0) { |
---|
| 957 | // closer than 5 meters --> do not create |
---|
| 958 | // System.out.println("Skipping creation..."); |
---|
| 959 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
| 960 | .remove(car.ID); |
---|
| 961 | continue; |
---|
| 962 | } |
---|
| 963 | } |
---|
| 964 | |
---|
| 965 | if (this.simulationType == Globals.routePlanConstants.DYNAMIC_SELF_ROUTE) { |
---|
| 966 | System.out.println("Dynamic; we are here"); |
---|
| 967 | percent = random.nextInt(100); |
---|
| 968 | percent = (int) Math.abs((double) percent); |
---|
| 969 | if (percent <= Globals.routePlanConstants.percentBestRoute) { |
---|
| 970 | percent = 1; |
---|
| 971 | } else { |
---|
| 972 | percent = 0; |
---|
| 973 | } |
---|
| 974 | car = Mobility.createNewCar(roadIndexSrc, pointIndexSrc, 0, |
---|
| 975 | i + 1, 0.0, personality, 0.0, ID, roadIndexDst, |
---|
| 976 | pointIndexDst, r.route, percent); |
---|
| 977 | if (car == null) { |
---|
| 978 | |
---|
| 979 | continue; |
---|
| 980 | } |
---|
| 981 | double d = car.getDistanceToCarInFront(); |
---|
| 982 | if (d < 10) { |
---|
| 983 | // closer than 10 meters --> do not create |
---|
| 984 | |
---|
| 985 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
| 986 | .remove(car.ID); |
---|
| 987 | continue; |
---|
| 988 | } |
---|
| 989 | |
---|
| 990 | } |
---|
| 991 | if (this.simulationType == Globals.routePlanConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { |
---|
| 992 | |
---|
| 993 | car = Mobility.createNewCarCommunicatingWithInfrastucture( |
---|
| 994 | roadIndexSrc, pointIndexSrc, 0, i + 1, 0.0, |
---|
| 995 | personality, 0.0, ID, roadIndexDst, pointIndexDst, |
---|
| 996 | r.route); |
---|
| 997 | if (car == null) { |
---|
| 998 | |
---|
| 999 | continue; |
---|
| 1000 | } |
---|
| 1001 | double d = car.getDistanceToCarInFront(); |
---|
| 1002 | if (d < 10) { |
---|
| 1003 | // closer than 10 meters --> do not create |
---|
| 1004 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
| 1005 | .remove(car.ID); |
---|
| 1006 | |
---|
| 1007 | |
---|
| 1008 | continue; |
---|
| 1009 | } |
---|
| 1010 | |
---|
| 1011 | } |
---|
| 1012 | |
---|
| 1013 | //create new ezcab client (entity) |
---|
| 1014 | |
---|
| 1015 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
| 1016 | |
---|
| 1017 | car = Mobility.createNewCityCarCommunicatingWithInfrastucture( |
---|
| 1018 | roadIndexSrc, pointIndexSrc, 0, i + 1, 0.0, |
---|
| 1019 | personality, 0.0, ID, roadIndexDst, pointIndexDst, |
---|
| 1020 | r.route); |
---|
| 1021 | if (car == null) { |
---|
| 1022 | continue; |
---|
| 1023 | } |
---|
| 1024 | double d = car.getDistanceToCarInFront(); |
---|
| 1025 | if (d < 10) { |
---|
| 1026 | // closer than 10 meters --> do not create |
---|
| 1027 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
| 1028 | .remove(car.ID); |
---|
| 1029 | |
---|
| 1030 | |
---|
| 1031 | continue; |
---|
| 1032 | } |
---|
| 1033 | |
---|
| 1034 | } |
---|
| 1035 | |
---|
| 1036 | ID = lastCarID; |
---|
| 1037 | lastCarID++; |
---|
| 1038 | this.addCar(car); |
---|
| 1039 | } |
---|
| 1040 | Mobility.events.removeFirst(); |
---|
| 1041 | |
---|
| 1042 | try { |
---|
| 1043 | EntryFlowVariation efv=Mobility.entryFlowVariations.get(first.index); |
---|
| 1044 | |
---|
| 1045 | if(efv.getFirstMom()<crtTime && efv.getFirstMom()!=-1) { |
---|
| 1046 | int newFlow=efv.getFirstValue(); |
---|
| 1047 | int old=first.period; |
---|
| 1048 | first.period=(int)((double)(3600 * Globals.SECOND) / newFlow); |
---|
| 1049 | System.out.print("TIME="+crtTime+"; CHANGING PERIOD FOR ENTRY="+first.index+" FROM: "+old+" TO: "+first.period); |
---|
| 1050 | efv.removeFirst(); |
---|
| 1051 | } |
---|
| 1052 | } catch (Exception ex) { |
---|
| 1053 | |
---|
| 1054 | } |
---|
| 1055 | if(Globals.SOCIALNETWORK == 0 ) |
---|
| 1056 | Mobility.events.addEvent(new NewCarEvent(first.timestamp |
---|
| 1057 | + first.period, first.index, first.period)); |
---|
| 1058 | first = Mobility.events.first(); |
---|
| 1059 | } |
---|
| 1060 | } |
---|
| 1061 | |
---|
| 1062 | |
---|
| 1063 | |
---|
| 1064 | long messagesSize = 0; |
---|
| 1065 | long carInfosSize = 0; |
---|
| 1066 | /** |
---|
| 1067 | * Execute the following event in the eventQueue |
---|
| 1068 | * |
---|
| 1069 | */ |
---|
| 1070 | public void playEvent() { |
---|
| 1071 | if (eventQueue.size() == 0) { |
---|
| 1072 | doneFlag = true; |
---|
| 1073 | return; |
---|
| 1074 | } |
---|
| 1075 | |
---|
| 1076 | Event e = null; |
---|
| 1077 | synchronized (eventQueue) { |
---|
| 1078 | e = eventQueue.remove(0); |
---|
| 1079 | } |
---|
| 1080 | // System.out.print(e.toString()+", "); |
---|
| 1081 | // System.out.flush(); |
---|
| 1082 | // |
---|
| 1083 | if (e instanceof GPSEvent) { |
---|
| 1084 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 1085 | while (it.hasNext()) { |
---|
| 1086 | SimulatedCarInfo r = it.next(); |
---|
| 1087 | r.update(); |
---|
| 1088 | } |
---|
| 1089 | GPSEvent newEvent = new GPSEvent(crtTime + fps); |
---|
| 1090 | schedEvent(newEvent); |
---|
| 1091 | } |
---|
| 1092 | if (e instanceof CleanupEvent) { |
---|
| 1093 | |
---|
| 1094 | long t1 = System.currentTimeMillis(); |
---|
| 1095 | long deleted = 0; |
---|
| 1096 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 1097 | while (it.hasNext()) { |
---|
| 1098 | SimulatedCarInfo r = it.next(); |
---|
| 1099 | // if (r.isPromiscuousMode() && r.pointIdx < 256 && r.pointIdx > 50){ |
---|
| 1100 | // r.getDirection(); |
---|
| 1101 | // } |
---|
| 1102 | Iterator<CarInfo> j = r.trafficDB.iterator(); |
---|
| 1103 | while (j.hasNext()) { |
---|
| 1104 | CarInfo neighbor = j.next(); |
---|
| 1105 | if ( (crtTime - neighbor.getTimestamp() > Globals.NEIGHBOR_EXPIRES |
---|
| 1106 | && neighbor.state != 3) || |
---|
| 1107 | (neighbor.state == 3 && crtTime - neighbor.getTimestamp() > Globals.PROMISCUOUS_NEIGHBOR_EXPIRES)){ |
---|
| 1108 | j.remove(); |
---|
| 1109 | // deleted++; |
---|
| 1110 | } |
---|
| 1111 | } |
---|
| 1112 | carInfosSize += r.trafficDB.size(); |
---|
| 1113 | } |
---|
| 1114 | for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { |
---|
| 1115 | WirelessTrafficLight wtl = (WirelessTrafficLight) Globals.map.allIntersections |
---|
| 1116 | .get(Globals.map.lightsIndices.get(j)); |
---|
| 1117 | |
---|
| 1118 | synchronized(wtl.trafficDB){ |
---|
| 1119 | Iterator<IntersectionCarRecord> wtlit = wtl.trafficDB.iterator(); |
---|
| 1120 | while (wtlit.hasNext()) { |
---|
| 1121 | IntersectionCarRecord icr = wtlit.next(); |
---|
| 1122 | if (!icr.isPassed() && crtTime - icr.getCar().getTimestamp() > Globals.HOUR |
---|
| 1123 | && icr.getCar().state != 3){ |
---|
| 1124 | icr.segment.cars.remove(icr); |
---|
| 1125 | wtlit.remove(); |
---|
| 1126 | deleted++; |
---|
| 1127 | } |
---|
| 1128 | if (icr.isPassed() && crtTime - icr.getCar().getTimestamp() > Globals.TRAFFICLIGHT_NEIGHBOR_EXPIRES){ |
---|
| 1129 | icr.segment.cars.remove(icr); |
---|
| 1130 | wtlit.remove(); |
---|
| 1131 | } |
---|
| 1132 | } |
---|
| 1133 | } |
---|
| 1134 | } |
---|
| 1135 | long t2 = System.currentTimeMillis(); |
---|
| 1136 | cleanupTime += t2 - t1; |
---|
| 1137 | if (!disableComm) { |
---|
| 1138 | CleanupEvent newCleanupEvent = new CleanupEvent(crtTime + fps); |
---|
| 1139 | schedEvent(newCleanupEvent); |
---|
| 1140 | } |
---|
| 1141 | // if (deleted != 0) |
---|
| 1142 | // System.out.println(crtTime+")\n\ttotal cars - "+carInfosSize+"\n\ttotal cars deleted- "+deleted+"\n\ttotal bytes transfered - "+messagesSize); |
---|
| 1143 | messagesSize = 0; |
---|
| 1144 | } |
---|
| 1145 | if (e instanceof SendEvent) { |
---|
| 1146 | SendEvent se = (SendEvent) e; |
---|
| 1147 | Communicator sender = se.getSender(); |
---|
| 1148 | |
---|
| 1149 | long t1 = System.currentTimeMillis(); |
---|
| 1150 | |
---|
| 1151 | |
---|
| 1152 | if (se.getMessage() == null){ |
---|
| 1153 | byte[] message = sender.prepareMessage(se.getMessageType()); |
---|
| 1154 | se.setMessage(message); |
---|
| 1155 | if (se.getMessage() == null){ |
---|
| 1156 | // System.out.println("null message"+se.sender + " "+se.sender.getClass()); |
---|
| 1157 | if (sender.isPromiscuousMode() && sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { |
---|
| 1158 | int eventPeriod = sender.getPeriod(se.getMessageType()); |
---|
| 1159 | if (eventPeriod != -1) |
---|
| 1160 | schedEvent(new SendEvent(crtTime + eventPeriod, sender, se |
---|
| 1161 | .getMessageType())); |
---|
| 1162 | } |
---|
| 1163 | return; |
---|
| 1164 | } |
---|
| 1165 | messagesSize += message.length; |
---|
| 1166 | long t2 = System.currentTimeMillis(); |
---|
| 1167 | codeTime += t2 - t1; |
---|
| 1168 | |
---|
| 1169 | if (sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { |
---|
| 1170 | int eventPeriod = sender.getPeriod(se.getMessageType()); |
---|
| 1171 | if (eventPeriod != -1) |
---|
| 1172 | schedEvent(new SendEvent(crtTime + eventPeriod, sender, se |
---|
| 1173 | .getMessageType())); |
---|
| 1174 | } |
---|
| 1175 | } |
---|
| 1176 | |
---|
| 1177 | //Petroaca - the DSRC impl |
---|
| 1178 | if(Engine.simulationProtocol==Globals.PROTOCOL_80211) |
---|
| 1179 | { |
---|
| 1180 | // TODO To be replaced with a real MAC layer |
---|
| 1181 | if (!sender.mediumAvailable(crtTime)) { |
---|
| 1182 | e.setTime(crtTime + 1); |
---|
| 1183 | schedEvent(e); |
---|
| 1184 | |
---|
| 1185 | return; |
---|
| 1186 | } |
---|
| 1187 | |
---|
| 1188 | sender.getRadio().setRadioTxMode(); |
---|
| 1189 | simulateCommunication(se); |
---|
| 1190 | |
---|
| 1191 | } |
---|
| 1192 | else |
---|
| 1193 | { |
---|
| 1194 | if(sender instanceof CarRunningDSRC) |
---|
| 1195 | { |
---|
| 1196 | MAC80211 macs=((CarRunningDSRC)sender).getMac(); |
---|
| 1197 | WirelessPhy phys=((CarRunningDSRC)sender).getPhysical(); |
---|
| 1198 | |
---|
| 1199 | macs.refreshFrameTime(crtTime); |
---|
| 1200 | phys.refreshFrameTime(crtTime); |
---|
| 1201 | |
---|
| 1202 | if(macs.sendToPhysical(se.getMessage(),crtTime)!=null) |
---|
| 1203 | { |
---|
| 1204 | phys.sendToChannel(se.getMessage(),crtTime); |
---|
| 1205 | macs.setChannelState(Globals.CCA_BUSY); |
---|
| 1206 | simulateCommunication(se); |
---|
| 1207 | } |
---|
| 1208 | else |
---|
| 1209 | { |
---|
| 1210 | |
---|
| 1211 | e.setTime(crtTime+1); |
---|
| 1212 | schedEvent(e); |
---|
| 1213 | |
---|
| 1214 | } |
---|
| 1215 | return; |
---|
| 1216 | } |
---|
| 1217 | else |
---|
| 1218 | { |
---|
| 1219 | if (!sender.mediumAvailable(crtTime)) { |
---|
| 1220 | e.setTime(crtTime + 1); |
---|
| 1221 | schedEvent(e); |
---|
| 1222 | |
---|
| 1223 | return; |
---|
| 1224 | } |
---|
| 1225 | |
---|
| 1226 | sender.getRadio().setRadioTxMode(); |
---|
| 1227 | simulateCommunication(se); |
---|
| 1228 | } |
---|
| 1229 | |
---|
| 1230 | |
---|
| 1231 | } |
---|
| 1232 | |
---|
| 1233 | |
---|
| 1234 | |
---|
| 1235 | |
---|
| 1236 | |
---|
| 1237 | } |
---|
| 1238 | if (e instanceof ReceiveEvent) { |
---|
| 1239 | ReceiveEvent re = (ReceiveEvent) e; |
---|
| 1240 | |
---|
| 1241 | // long tmp = 0; |
---|
| 1242 | // long t3 = System.currentTimeMillis(); |
---|
| 1243 | // long t4; |
---|
| 1244 | // if (re.isUnicast()) { |
---|
| 1245 | // long t1 = System.currentTimeMillis(); |
---|
| 1246 | // re.receiver.onReceive(re.getMessage(), re.sender); |
---|
| 1247 | // long t2 = System.currentTimeMillis(); |
---|
| 1248 | // codeTime += t2 - t1; |
---|
| 1249 | // } else { |
---|
| 1250 | long t1 = System.currentTimeMillis(); |
---|
| 1251 | //Petroaca - the DSRC impl |
---|
| 1252 | if(Engine.simulationProtocol==Globals.PROTOCOL_80211) |
---|
| 1253 | { |
---|
| 1254 | RadioDev rxradio = re.receiver.getRadio(); |
---|
| 1255 | ReceiveEvent goodSignal = rxradio.receive(re.signals); |
---|
| 1256 | re.receiver.removeReceiveEventForTime(re.getTime()); |
---|
| 1257 | |
---|
| 1258 | // for (ReceiveEvent goodSignal : re.signals){ |
---|
| 1259 | // Road r1 = (Road) Globals.map.roads.get(re.sender.getRoadIdx()); |
---|
| 1260 | // Point p1 = (Point) r1.points.get(re.sender.getPointIdx()); |
---|
| 1261 | // Road r2 = (Road) Globals.map.roads.get(re.receiver.getRoadIdx()); |
---|
| 1262 | // Point p2 = (Point) r2.points.get(re.receiver.getPointIdx()); |
---|
| 1263 | // double distance = GPSutil.distance(p1, p2); |
---|
| 1264 | // if (distance <= WIRELESS_RANGE){ |
---|
| 1265 | if (goodSignal != null){ |
---|
| 1266 | re.receiver.onReceive(goodSignal.getMessage(), goodSignal.getMessageObject(), goodSignal.sender); |
---|
| 1267 | } |
---|
| 1268 | // } |
---|
| 1269 | // } |
---|
| 1270 | |
---|
| 1271 | } |
---|
| 1272 | else |
---|
| 1273 | { |
---|
| 1274 | if(re.receiver instanceof CarRunningDSRC) |
---|
| 1275 | { |
---|
| 1276 | MAC80211 macr=((CarRunningDSRC)re.receiver).getMac(); |
---|
| 1277 | WirelessPhy phyr=((CarRunningDSRC)re.receiver).getPhysical(); |
---|
| 1278 | Application appr=((CarRunningDSRC)re.receiver).getAppLayer(); |
---|
| 1279 | |
---|
| 1280 | if(Globals.DIFFERENTIAL_CW) |
---|
| 1281 | macr.recalculateCW(phyr.getTotalReceivedPackets(),crtTime); |
---|
| 1282 | |
---|
| 1283 | macr.refreshFrameTime(crtTime); |
---|
| 1284 | phyr.refreshFrameTime(crtTime); |
---|
| 1285 | appr.refreshApplication(crtTime); |
---|
| 1286 | |
---|
| 1287 | ReceiveEvent best=phyr.recvFromChannel(re.signals,crtTime); |
---|
| 1288 | re.receiver.removeReceiveEventForTime(re.getTime()); |
---|
| 1289 | |
---|
| 1290 | double sinr=phyr.getSINR(); |
---|
| 1291 | if(best!=null) |
---|
| 1292 | { |
---|
| 1293 | macr.setChannelState(Globals.CCA_BUSY); |
---|
| 1294 | |
---|
| 1295 | if(macr.recvFromPhysical(best,sinr,crtTime)!=null) |
---|
| 1296 | { |
---|
| 1297 | appr.ForwardCollisionWarning(best.getMessage(),((CarInfo)re.receiver)); |
---|
| 1298 | //appr.LaneChangeAssistance(best.getMessage(),((CarInfo)re.receiver)); |
---|
| 1299 | re.receiver.onReceive(best.getMessage(), best.getMessageObject(), best.sender); |
---|
| 1300 | |
---|
| 1301 | } |
---|
| 1302 | |
---|
| 1303 | |
---|
| 1304 | } |
---|
| 1305 | |
---|
| 1306 | |
---|
| 1307 | } |
---|
| 1308 | else |
---|
| 1309 | { |
---|
| 1310 | RadioDev rxradio = re.receiver.getRadio(); |
---|
| 1311 | ReceiveEvent goodSignal = rxradio.receive(re.signals); |
---|
| 1312 | re.receiver.removeReceiveEventForTime(re.getTime()); |
---|
| 1313 | |
---|
| 1314 | |
---|
| 1315 | if (goodSignal != null){ |
---|
| 1316 | re.receiver.onReceive(goodSignal.getMessage(), goodSignal.getMessageObject(), goodSignal.sender); |
---|
| 1317 | } |
---|
| 1318 | |
---|
| 1319 | |
---|
| 1320 | } |
---|
| 1321 | |
---|
| 1322 | |
---|
| 1323 | } |
---|
| 1324 | |
---|
| 1325 | long t2 = System.currentTimeMillis(); |
---|
| 1326 | codeTime += t2 - t1; |
---|
| 1327 | /* if (!re.sender.mediumAvailable(crtTime)) { |
---|
| 1328 | re.setTime(crtTime + 1); |
---|
| 1329 | schedEvent(re); |
---|
| 1330 | return; |
---|
| 1331 | } |
---|
| 1332 | |
---|
| 1333 | Road r1 = null; |
---|
| 1334 | Point p1 = null; |
---|
| 1335 | if (re.sender instanceof SimulatedCarInfo){ |
---|
| 1336 | SimulatedCarInfo sender = (SimulatedCarInfo)re.sender; |
---|
| 1337 | r1 = (Road) Globals.map.roads.get(sender.getRealPos() |
---|
| 1338 | .getRoadIdx()); |
---|
| 1339 | p1 = (Point) r1.points.get(sender.getRealPos().getPointIdx()); |
---|
| 1340 | }else{ |
---|
| 1341 | WirelessTrafficLight wtl = (WirelessTrafficLight)re.sender; |
---|
| 1342 | r1 = (Road) Globals.map.roads.get(wtl.segments.get(0).roadIndex); |
---|
| 1343 | p1 = (Point) r1.points.get(wtl.segments.get(0).pointIndex); |
---|
| 1344 | } |
---|
| 1345 | // recvEventsCnt++; |
---|
| 1346 | |
---|
| 1347 | long comp = 0; |
---|
| 1348 | // peanoKeys |
---|
| 1349 | int cnt2 = 0; |
---|
| 1350 | int cnt3 = 0; |
---|
| 1351 | int cnt4 = 0; |
---|
| 1352 | int pkcnt = 0; |
---|
| 1353 | PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx()); |
---|
| 1354 | |
---|
| 1355 | ListIterator<PeanoKey> pkit = Globals.map.peanoKeys |
---|
| 1356 | .listIterator(p1.getPeanoKeyIdx()); |
---|
| 1357 | PeanoKey maxpk = null; |
---|
| 1358 | |
---|
| 1359 | double wifirange; |
---|
| 1360 | if (re.sender instanceof WirelessTrafficLight){ |
---|
| 1361 | maxpk = GPSutil.getMaxSearchBoundPK(p1,Engine.EXTENDED_WIRELESS_RANGE); |
---|
| 1362 | wifirange = EXTENDED_WIRELESS_RANGE; |
---|
| 1363 | }else{ |
---|
| 1364 | wifirange = WIRELESS_RANGE; |
---|
| 1365 | maxpk = pk.getMaxpk(); |
---|
| 1366 | } |
---|
| 1367 | PeanoKey pkx = null; |
---|
| 1368 | for (pkx = pkit.next(); pkit.hasNext(); pkx = pkit.next()) { |
---|
| 1369 | pkcnt++; |
---|
| 1370 | // long tc1 = System.currentTimeMillis(); |
---|
| 1371 | // PeanoKey pkx = Globals.map.peanoKeys.get(idx); |
---|
| 1372 | if (pkx.compareTo(maxpk) > 0) |
---|
| 1373 | break; |
---|
| 1374 | // long tc2 = System.currentTimeMillis(); |
---|
| 1375 | // comp += (tc2-tc1); |
---|
| 1376 | |
---|
| 1377 | if (pkx.getCars() != null) { |
---|
| 1378 | Road r2 = (Road) Globals.map.roads.get(pkx |
---|
| 1379 | .getRoadIndex()); |
---|
| 1380 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
| 1381 | if (GPSutil.distance(p1, p2) < wifirange) { |
---|
| 1382 | cnt3 += pkx.getCars().size(); |
---|
| 1383 | Iterator<SimulatedCarInfo> it = pkx.getCars() |
---|
| 1384 | .iterator(); |
---|
| 1385 | while (it.hasNext()) { |
---|
| 1386 | SimulatedCarInfo r = it.next(); |
---|
| 1387 | if (re.sender instanceof SimulatedCarInfo) |
---|
| 1388 | if (r.getVehicleId() == ((SimulatedCarInfo)re.sender) |
---|
| 1389 | .getVehicleId()) |
---|
| 1390 | continue; |
---|
| 1391 | r.setLastMediumTransmition(crtTime); |
---|
| 1392 | t4 = System.currentTimeMillis(); |
---|
| 1393 | tmp += t4 - t3; |
---|
| 1394 | long t1 = System.currentTimeMillis(); |
---|
| 1395 | r.onReceive(re.getMessage(), re.sender); |
---|
| 1396 | long t2 = System.currentTimeMillis(); |
---|
| 1397 | codeTime += t2 - t1; |
---|
| 1398 | cnt2++; |
---|
| 1399 | t3 = System.currentTimeMillis(); |
---|
| 1400 | } |
---|
| 1401 | } |
---|
| 1402 | } |
---|
| 1403 | } |
---|
| 1404 | PeanoKey minpk = null; |
---|
| 1405 | if (re.sender instanceof WirelessTrafficLight) |
---|
| 1406 | minpk = GPSutil.getMaxSearchBoundPK(p1,-Engine.EXTENDED_WIRELESS_RANGE); |
---|
| 1407 | else |
---|
| 1408 | minpk = pk.getMinpk(); |
---|
| 1409 | |
---|
| 1410 | pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); |
---|
| 1411 | |
---|
| 1412 | if (pkit.hasPrevious()){ |
---|
| 1413 | pkx = pkit.previous(); |
---|
| 1414 | } |
---|
| 1415 | for (; pkit.hasPrevious(); pkx = pkit |
---|
| 1416 | .previous()) { |
---|
| 1417 | pkcnt++; |
---|
| 1418 | // long tc1 = System.currentTimeMillis(); |
---|
| 1419 | // PeanoKey pkx = Globals.map.peanoKeys.get(idx); |
---|
| 1420 | if (pkx.compareTo(minpk) < 0) { |
---|
| 1421 | break; |
---|
| 1422 | } |
---|
| 1423 | // long tc2 = System.currentTimeMillis(); |
---|
| 1424 | // comp += (tc2-tc1); |
---|
| 1425 | |
---|
| 1426 | if (pkx.getCars() != null) { |
---|
| 1427 | Road r2 = (Road) Globals.map.roads.get(pkx |
---|
| 1428 | .getRoadIndex()); |
---|
| 1429 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
| 1430 | if (GPSutil.distance(p1, p2) < wifirange) { |
---|
| 1431 | Iterator<SimulatedCarInfo> it = pkx.getCars() |
---|
| 1432 | .iterator(); |
---|
| 1433 | cnt3 += pkx.getCars().size(); |
---|
| 1434 | while (it.hasNext()) { |
---|
| 1435 | SimulatedCarInfo r = it.next(); |
---|
| 1436 | r.setLastMediumTransmition(crtTime); |
---|
| 1437 | |
---|
| 1438 | if (re.sender instanceof SimulatedCarInfo) |
---|
| 1439 | if (r.getVehicleId() == ((SimulatedCarInfo)re.sender) |
---|
| 1440 | .getVehicleId()) |
---|
| 1441 | continue; |
---|
| 1442 | |
---|
| 1443 | t4 = System.currentTimeMillis(); |
---|
| 1444 | tmp += t4 - t3; |
---|
| 1445 | long t1 = System.currentTimeMillis(); |
---|
| 1446 | r.onReceive(re.getMessage(), re.sender); |
---|
| 1447 | long t2 = System.currentTimeMillis(); |
---|
| 1448 | codeTime += t2 - t1; |
---|
| 1449 | cnt4++; |
---|
| 1450 | t3 = System.currentTimeMillis(); |
---|
| 1451 | } |
---|
| 1452 | } |
---|
| 1453 | } |
---|
| 1454 | } |
---|
| 1455 | |
---|
| 1456 | for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { |
---|
| 1457 | WirelessTrafficLight wtl = (WirelessTrafficLight) Globals.map.allIntersections |
---|
| 1458 | .get(Globals.map.lightsIndices.get(j)); |
---|
| 1459 | long t1sem = System.currentTimeMillis(); |
---|
| 1460 | |
---|
| 1461 | if (GPSutil.distance(p1, wtl.getMapPoint()) < WIRELESS_RANGE) { |
---|
| 1462 | wtl.setLastMediumTransmition(crtTime); |
---|
| 1463 | wtl.onReceive(re.getMessage(), re.sender); |
---|
| 1464 | } |
---|
| 1465 | long t2sem = System.currentTimeMillis(); |
---|
| 1466 | semTime += (t2sem - t1sem); |
---|
| 1467 | } |
---|
| 1468 | |
---|
| 1469 | t4 = System.currentTimeMillis(); |
---|
| 1470 | tmp += t4 - t3; |
---|
| 1471 | netTime += tmp; |
---|
| 1472 | long t5 = System.currentTimeMillis(); |
---|
| 1473 | */ |
---|
| 1474 | // if (tmp > 0) |
---|
| 1475 | // avgTime += (double) (100 * comp) / tmp; |
---|
| 1476 | // if (pkcnt > pkmax) |
---|
| 1477 | // pkmax = pkcnt; |
---|
| 1478 | // avgi++; |
---|
| 1479 | // pkavg += cnt2; |
---|
| 1480 | |
---|
| 1481 | // long t5 = System.currentTimeMillis(); |
---|
| 1482 | // |
---|
| 1483 | // if (cnt1 != cnt2 + cnt4){ |
---|
| 1484 | // System.out.println("==== "+crtTime+" car: "+re.sender+ " |
---|
| 1485 | // ======\n"+ |
---|
| 1486 | // "Count FAILED old:" + cnt1 +" - new: "+cnt2 +" + |
---|
| 1487 | // "+cnt4+"/"+pkcnt |
---|
| 1488 | // +" --- "+minpk+" - "+maxpk); |
---|
| 1489 | // } |
---|
| 1490 | // if (t5 - t4 > tmp){ |
---|
| 1491 | // System.out.println("==== "+crtTime+" car: "+re.sender+ " |
---|
| 1492 | // ======\n"+ |
---|
| 1493 | // "Time FAILED old:" + tmp+"/"+cnt1 +" - new: "+ |
---|
| 1494 | // (t5-t4)+"/"+cnt2 +"/"+ cnt3+" / "+pkcnt +" - "+comp); |
---|
| 1495 | // } |
---|
| 1496 | |
---|
| 1497 | } |
---|
| 1498 | |
---|
| 1499 | // if (Globals.demo.mv.currentCar != null && |
---|
| 1500 | // Globals.demo.mv.currentCar == re.getReceiver().car) |
---|
| 1501 | // System.out.println(i+" cars nearby"); |
---|
| 1502 | |
---|
| 1503 | // } // end if else isUnicast |
---|
| 1504 | } |
---|
| 1505 | |
---|
| 1506 | // static int recvEventsCnt = 0; |
---|
| 1507 | // |
---|
| 1508 | // static int pkmax; |
---|
| 1509 | // |
---|
| 1510 | // static long pkavg = 0; |
---|
| 1511 | // |
---|
| 1512 | // static double avgTime = 0; |
---|
| 1513 | // |
---|
| 1514 | // static int avgi = 0; |
---|
| 1515 | // |
---|
| 1516 | public void playEventEmergency() |
---|
| 1517 | { |
---|
| 1518 | if (eventQueueEmergency.size() == 0) { |
---|
| 1519 | doneFlagEmergency = true; |
---|
| 1520 | return; |
---|
| 1521 | } |
---|
| 1522 | |
---|
| 1523 | Event e = null; |
---|
| 1524 | synchronized (eventQueueEmergency) { |
---|
| 1525 | e = eventQueueEmergency.remove(0); |
---|
| 1526 | } |
---|
| 1527 | |
---|
| 1528 | if (e instanceof SendEvent) { |
---|
| 1529 | SendEvent se = (SendEvent) e; |
---|
| 1530 | Communicator sender = se.getSender(); |
---|
| 1531 | |
---|
| 1532 | long t1 = System.currentTimeMillis(); |
---|
| 1533 | |
---|
| 1534 | |
---|
| 1535 | if (se.getMessage() == null){ |
---|
| 1536 | byte[] message = sender.prepareMessage(se.getMessageType()); |
---|
| 1537 | se.setMessage(message); |
---|
| 1538 | if (se.getMessage() == null){ |
---|
| 1539 | // System.out.println("null message"+se.sender + " "+se.sender.getClass()); |
---|
| 1540 | if (sender.isPromiscuousMode() && sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { |
---|
| 1541 | int eventPeriod = sender.getPeriod(se.getMessageType()); |
---|
| 1542 | if (eventPeriod != -1) |
---|
| 1543 | schedEventEmergency(new SendEvent(crtTime + eventPeriod, sender, se |
---|
| 1544 | .getMessageType())); |
---|
| 1545 | } |
---|
| 1546 | return; |
---|
| 1547 | } |
---|
| 1548 | messagesSize += message.length; |
---|
| 1549 | long t2 = System.currentTimeMillis(); |
---|
| 1550 | codeTime += t2 - t1; |
---|
| 1551 | |
---|
| 1552 | if (sender.isPeriodicalMessage(se.getMessageType()) && !disableComm) { |
---|
| 1553 | int eventPeriod = sender.getPeriod(se.getMessageType()); |
---|
| 1554 | if (eventPeriod != -1) |
---|
| 1555 | schedEventEmergency(new SendEvent(crtTime + eventPeriod, sender, se |
---|
| 1556 | .getMessageType())); |
---|
| 1557 | } |
---|
| 1558 | } |
---|
| 1559 | |
---|
| 1560 | //Petroaca - the DSRC impl |
---|
| 1561 | if(sender instanceof CarRunningDSRC) |
---|
| 1562 | { |
---|
| 1563 | MAC80211 macs=((CarRunningDSRC)sender).getMac(); |
---|
| 1564 | WirelessPhy phys=((CarRunningDSRC)sender).getPhysical(); |
---|
| 1565 | |
---|
| 1566 | macs.refreshFrameTime(crtTime); |
---|
| 1567 | phys.refreshFrameTime(crtTime); |
---|
| 1568 | |
---|
| 1569 | if(macs.sendToPhysical(se.getMessage(),crtTime)!=null) |
---|
| 1570 | { |
---|
| 1571 | phys.sendToChannel(se.getMessage(),crtTime); |
---|
| 1572 | macs.setChannelState(Globals.CCA_BUSY); |
---|
| 1573 | simulateCommunicationEmergency(se); |
---|
| 1574 | } |
---|
| 1575 | else |
---|
| 1576 | { |
---|
| 1577 | |
---|
| 1578 | e.setTime(crtTime+1); |
---|
| 1579 | schedEventEmergency(e); |
---|
| 1580 | |
---|
| 1581 | } |
---|
| 1582 | return; |
---|
| 1583 | } |
---|
| 1584 | |
---|
| 1585 | if(sender instanceof CarRunningDSRC) |
---|
| 1586 | { |
---|
| 1587 | return; |
---|
| 1588 | } |
---|
| 1589 | else |
---|
| 1590 | { |
---|
| 1591 | return; |
---|
| 1592 | } |
---|
| 1593 | |
---|
| 1594 | |
---|
| 1595 | |
---|
| 1596 | |
---|
| 1597 | } |
---|
| 1598 | |
---|
| 1599 | |
---|
| 1600 | |
---|
| 1601 | if (e instanceof ReceiveEvent) { |
---|
| 1602 | ReceiveEvent re = (ReceiveEvent) e; |
---|
| 1603 | |
---|
| 1604 | // long tmp = 0; |
---|
| 1605 | // long t3 = System.currentTimeMillis(); |
---|
| 1606 | // long t4; |
---|
| 1607 | // if (re.isUnicast()) { |
---|
| 1608 | // long t1 = System.currentTimeMillis(); |
---|
| 1609 | // re.receiver.onReceive(re.getMessage(), re.sender); |
---|
| 1610 | // long t2 = System.currentTimeMillis(); |
---|
| 1611 | // codeTime += t2 - t1; |
---|
| 1612 | // } else { |
---|
| 1613 | long t1 = System.currentTimeMillis(); |
---|
| 1614 | //Petroaca - the DSRC impl |
---|
| 1615 | |
---|
| 1616 | if(re.receiver instanceof CarRunningDSRC) |
---|
| 1617 | { |
---|
| 1618 | MAC80211 macr=((CarRunningDSRC)re.receiver).getMac(); |
---|
| 1619 | WirelessPhy phyr=((CarRunningDSRC)re.receiver).getPhysical(); |
---|
| 1620 | Application appr=((CarRunningDSRC)re.receiver).getAppLayer(); |
---|
| 1621 | appr.refreshApplication(crtTime); |
---|
| 1622 | |
---|
| 1623 | if(Globals.DIFFERENTIAL_CW) |
---|
| 1624 | macr.recalculateCW(phyr.getTotalReceivedPackets(),crtTime); |
---|
| 1625 | |
---|
| 1626 | macr.refreshFrameTime(crtTime); |
---|
| 1627 | phyr.refreshFrameTime(crtTime); |
---|
| 1628 | |
---|
| 1629 | ReceiveEvent best=phyr.recvFromChannel(re.signals,crtTime); |
---|
| 1630 | ((SimulatedCarInfo)re.receiver).removeReceiveEventEmergencyForTime(re.getTime()); |
---|
| 1631 | |
---|
| 1632 | double sinr=phyr.getSINR(); |
---|
| 1633 | if(best!=null) |
---|
| 1634 | { |
---|
| 1635 | macr.setChannelState(Globals.CCA_BUSY); |
---|
| 1636 | |
---|
| 1637 | if(macr.recvFromPhysical(best,sinr,crtTime)!=null) |
---|
| 1638 | { |
---|
| 1639 | //appr.ForwardCollisionWarning(best.getMessage(),((CarInfo)re.receiver)); |
---|
| 1640 | //appr.LaneChangeAssistance(best.getMessage(),((CarInfo)re.receiver)); |
---|
| 1641 | re.receiver.onReceive(best.getMessage(), best.getMessageObject(), best.sender); |
---|
| 1642 | |
---|
| 1643 | } |
---|
| 1644 | |
---|
| 1645 | |
---|
| 1646 | } |
---|
| 1647 | |
---|
| 1648 | |
---|
| 1649 | } |
---|
| 1650 | long t2 = System.currentTimeMillis(); |
---|
| 1651 | codeTime += t2 - t1; |
---|
| 1652 | |
---|
| 1653 | } |
---|
| 1654 | |
---|
| 1655 | |
---|
| 1656 | } |
---|
| 1657 | |
---|
| 1658 | /** |
---|
| 1659 | * Applications may schedule dynamically communication events using this method |
---|
| 1660 | * to insert new events in the queue. |
---|
| 1661 | * |
---|
| 1662 | * @param e Event to schedule |
---|
| 1663 | */ |
---|
| 1664 | public void schedEvent(Event e) { |
---|
| 1665 | |
---|
| 1666 | long t1 = System.currentTimeMillis(); |
---|
| 1667 | |
---|
| 1668 | if (e instanceof ReceiveEvent) { |
---|
| 1669 | ReceiveEvent re = (ReceiveEvent) e; |
---|
| 1670 | ReceiveEvent existing = re.receiver.getReceiveEventForTime(e.getTime()); |
---|
| 1671 | if (existing == null){ |
---|
| 1672 | re.receiver.addReceiveEventForTime(re); |
---|
| 1673 | }else{ |
---|
| 1674 | existing.signals.add(re); |
---|
| 1675 | return; |
---|
| 1676 | } |
---|
| 1677 | } |
---|
| 1678 | |
---|
| 1679 | int idx = Collections.binarySearch(eventQueue, e); |
---|
| 1680 | synchronized (eventQueue) { |
---|
| 1681 | if (idx >= 0) { |
---|
| 1682 | // advance till the end of the sequence of equal events |
---|
| 1683 | if (eventQueue.get(idx).equals(e)) |
---|
| 1684 | idx++; |
---|
| 1685 | eventQueue.add(idx, e); |
---|
| 1686 | } else { |
---|
| 1687 | eventQueue.add(-(idx + 1), e); |
---|
| 1688 | } |
---|
| 1689 | } |
---|
| 1690 | long t2 = System.currentTimeMillis(); |
---|
| 1691 | schedTime += t2 - t1; |
---|
| 1692 | } |
---|
| 1693 | |
---|
| 1694 | //Petroaca - the message scheduler for the emergency channel |
---|
| 1695 | public void schedEventEmergency(Event e) |
---|
| 1696 | { |
---|
| 1697 | long t1 = System.currentTimeMillis(); |
---|
| 1698 | |
---|
| 1699 | if (e instanceof ReceiveEvent) { |
---|
| 1700 | ReceiveEvent re = (ReceiveEvent) e; |
---|
| 1701 | ReceiveEvent existing = ((SimulatedCarInfo)re.receiver).getReceiveEventEmergencyForTime(e.getTime()); |
---|
| 1702 | if (existing == null){ |
---|
| 1703 | ((SimulatedCarInfo)re.receiver).addReceiveEventEmergencyForTime(re); |
---|
| 1704 | }else{ |
---|
| 1705 | existing.signals.add(re); |
---|
| 1706 | return; |
---|
| 1707 | } |
---|
| 1708 | } |
---|
| 1709 | |
---|
| 1710 | int idx = Collections.binarySearch(eventQueueEmergency, e); |
---|
| 1711 | synchronized (eventQueueEmergency) { |
---|
| 1712 | if (idx >= 0) { |
---|
| 1713 | // advance till the end of the sequence of equal events |
---|
| 1714 | if (eventQueueEmergency.get(idx).equals(e)) |
---|
| 1715 | idx++; |
---|
| 1716 | eventQueueEmergency.add(idx, e); |
---|
| 1717 | } else { |
---|
| 1718 | eventQueueEmergency.add(-(idx + 1), e); |
---|
| 1719 | } |
---|
| 1720 | } |
---|
| 1721 | long t2 = System.currentTimeMillis(); |
---|
| 1722 | schedTime += t2 - t1; |
---|
| 1723 | } |
---|
| 1724 | |
---|
| 1725 | /** |
---|
| 1726 | * Generate the receive events corresponding to one send event, for the cars |
---|
| 1727 | * in the wireless range of the sending car. |
---|
| 1728 | * |
---|
| 1729 | */ |
---|
| 1730 | public void simulateCommunication(SendEvent e) { |
---|
| 1731 | |
---|
| 1732 | Road r1 = null; |
---|
| 1733 | Point p1 = null; |
---|
| 1734 | r1 = (Road) Globals.map.roads.get(e.sender.getRoadIdx()); |
---|
| 1735 | p1 = (Point) r1.points.get(e.sender.getPointIdx()); |
---|
| 1736 | |
---|
| 1737 | if (e instanceof UnicastSendEvent) { |
---|
| 1738 | long t1 = System.currentTimeMillis(); |
---|
| 1739 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 1740 | ReceiveEvent re = null; |
---|
| 1741 | while (it.hasNext()) { |
---|
| 1742 | SimulatedCarInfo r = it.next(); |
---|
| 1743 | if (r.vehicleId == ((UnicastSendEvent) e).getReceiverId()) { |
---|
| 1744 | Road r2 = (Road) Globals.map.roads.get(r.getRealPos() |
---|
| 1745 | .getRoadIdx()); |
---|
| 1746 | Point p2 = (Point) r2.points.get(r.getRealPos() |
---|
| 1747 | .getPointIdx()); |
---|
| 1748 | |
---|
| 1749 | // MODIFIED BY CRISTI!!! |
---|
| 1750 | // ReceiveEvent er = new ReceiveEvent(e.getTime() + 1, r, |
---|
| 1751 | // e.getMessage()); |
---|
| 1752 | |
---|
| 1753 | re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
| 1754 | re.setUnicast(true); |
---|
| 1755 | |
---|
| 1756 | schedEvent(re); |
---|
| 1757 | if (GPSutil.distance(p1, p2) < WIRELESS_RANGE) { |
---|
| 1758 | } else { |
---|
| 1759 | System.out.println("EROARE! DISTANCE=" |
---|
| 1760 | + GPSutil.distance(p1, p2)); |
---|
| 1761 | } |
---|
| 1762 | // END MODIFIED!!! |
---|
| 1763 | return; |
---|
| 1764 | } |
---|
| 1765 | } |
---|
| 1766 | |
---|
| 1767 | re.setSender(e.getSender()); |
---|
| 1768 | schedEvent(re); |
---|
| 1769 | |
---|
| 1770 | long t2 = System.currentTimeMillis(); |
---|
| 1771 | netTime += t2 - t1; |
---|
| 1772 | |
---|
| 1773 | } else { |
---|
| 1774 | |
---|
| 1775 | long tmp = 0; |
---|
| 1776 | long t3 = System.currentTimeMillis(); |
---|
| 1777 | long t4; |
---|
| 1778 | |
---|
| 1779 | // re = new ReceiveEvent(e.getTime() + 1, e.getSender(), e |
---|
| 1780 | // .getMessage()); |
---|
| 1781 | |
---|
| 1782 | |
---|
| 1783 | long comp = 0; |
---|
| 1784 | // peanoKeys |
---|
| 1785 | int cnt2 = 0; |
---|
| 1786 | int cnt3 = 0; |
---|
| 1787 | int cnt4 = 0; |
---|
| 1788 | int pkcnt = 0; |
---|
| 1789 | PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx()); |
---|
| 1790 | |
---|
| 1791 | ListIterator<PeanoKey> pkit = Globals.map.peanoKeys |
---|
| 1792 | .listIterator(p1.getPeanoKeyIdx()); |
---|
| 1793 | PeanoKey maxpk = null; |
---|
| 1794 | |
---|
| 1795 | double wifirange; |
---|
| 1796 | if (e.sender instanceof WirelessTrafficLight){ |
---|
| 1797 | maxpk = GPSutil.getMaxSearchBoundPK(p1,Engine.EXTENDED_WIRELESS_RANGE); |
---|
| 1798 | wifirange = EXTENDED_WIRELESS_RANGE; |
---|
| 1799 | }else{ |
---|
| 1800 | wifirange = WIRELESS_RANGE; |
---|
| 1801 | maxpk = pk.getMaxpk(); |
---|
| 1802 | } |
---|
| 1803 | PeanoKey pkx = null; |
---|
| 1804 | for (pkx = pkit.next(); pkit.hasNext(); pkx = pkit.next()) { |
---|
| 1805 | pkcnt++; |
---|
| 1806 | // long tc1 = System.currentTimeMillis(); |
---|
| 1807 | if (pkx.compareTo(maxpk) > 0) |
---|
| 1808 | break; |
---|
| 1809 | // long tc2 = System.currentTimeMillis(); |
---|
| 1810 | // comp += (tc2-tc1); |
---|
| 1811 | |
---|
| 1812 | if (pkx.getCars() != null) { |
---|
| 1813 | Road r2 = (Road) Globals.map.roads.get(pkx |
---|
| 1814 | .getRoadIndex()); |
---|
| 1815 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
| 1816 | if (GPSutil.distance(p1, p2) < 5 * wifirange) { |
---|
| 1817 | cnt3 += pkx.getCars().size(); |
---|
| 1818 | Iterator<SimulatedCarInfo> it = pkx.getCars() |
---|
| 1819 | .iterator(); |
---|
| 1820 | while (it.hasNext()) { |
---|
| 1821 | SimulatedCarInfo r = it.next(); |
---|
| 1822 | if (e.sender instanceof SimulatedCarInfo) |
---|
| 1823 | if (r.getVehicleId() == ((SimulatedCarInfo)e.sender) |
---|
| 1824 | .getVehicleId()) |
---|
| 1825 | continue; |
---|
| 1826 | r.setLastMediumTransmition(crtTime); |
---|
| 1827 | |
---|
| 1828 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
| 1829 | re.setSender(e.getSender()); |
---|
| 1830 | schedEvent(re); |
---|
| 1831 | } |
---|
| 1832 | } |
---|
| 1833 | } |
---|
| 1834 | } |
---|
| 1835 | PeanoKey minpk = null; |
---|
| 1836 | if (e.sender instanceof WirelessTrafficLight) |
---|
| 1837 | minpk = GPSutil.getMaxSearchBoundPK(p1,-Engine.EXTENDED_WIRELESS_RANGE); |
---|
| 1838 | else |
---|
| 1839 | minpk = pk.getMinpk(); |
---|
| 1840 | |
---|
| 1841 | pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); |
---|
| 1842 | |
---|
| 1843 | if (pkit.hasPrevious()){ |
---|
| 1844 | pkx = pkit.previous(); |
---|
| 1845 | } |
---|
| 1846 | for (; pkit.hasPrevious(); pkx = pkit |
---|
| 1847 | .previous()) { |
---|
| 1848 | pkcnt++; |
---|
| 1849 | // long tc1 = System.currentTimeMillis(); |
---|
| 1850 | if (pkx.compareTo(minpk) < 0) { |
---|
| 1851 | break; |
---|
| 1852 | } |
---|
| 1853 | // long tc2 = System.currentTimeMillis(); |
---|
| 1854 | // comp += (tc2-tc1); |
---|
| 1855 | |
---|
| 1856 | if (pkx.getCars() != null) { |
---|
| 1857 | Road r2 = (Road) Globals.map.roads.get(pkx |
---|
| 1858 | .getRoadIndex()); |
---|
| 1859 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
| 1860 | double distance = GPSutil.distance(p1, p2); |
---|
| 1861 | if (distance < 5 * wifirange) { |
---|
| 1862 | Iterator<SimulatedCarInfo> it = pkx.getCars() |
---|
| 1863 | .iterator(); |
---|
| 1864 | cnt3 += pkx.getCars().size(); |
---|
| 1865 | while (it.hasNext()) { |
---|
| 1866 | SimulatedCarInfo r = it.next(); |
---|
| 1867 | r.setLastMediumTransmition(crtTime); |
---|
| 1868 | |
---|
| 1869 | if (e.sender instanceof SimulatedCarInfo) |
---|
| 1870 | if (r.getVehicleId() == ((SimulatedCarInfo)e.sender) |
---|
| 1871 | .getVehicleId()) |
---|
| 1872 | continue; |
---|
| 1873 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
| 1874 | re.setSender(e.getSender()); |
---|
| 1875 | schedEvent(re); |
---|
| 1876 | } |
---|
| 1877 | } |
---|
| 1878 | } |
---|
| 1879 | } |
---|
| 1880 | |
---|
| 1881 | for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { |
---|
| 1882 | WirelessTrafficLight wtl = (WirelessTrafficLight) Globals.map.allIntersections |
---|
| 1883 | .get(Globals.map.lightsIndices.get(j)); |
---|
| 1884 | long t1sem = System.currentTimeMillis(); |
---|
| 1885 | |
---|
| 1886 | if (GPSutil.distance(p1, wtl.getMapPoint()) < 5 * WIRELESS_RANGE) { |
---|
| 1887 | wtl.setLastMediumTransmition(crtTime); |
---|
| 1888 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, wtl, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
| 1889 | re.setSender(e.getSender()); |
---|
| 1890 | schedEvent(re); |
---|
| 1891 | } |
---|
| 1892 | long t2sem = System.currentTimeMillis(); |
---|
| 1893 | semTime += (t2sem - t1sem); |
---|
| 1894 | } |
---|
| 1895 | |
---|
| 1896 | t4 = System.currentTimeMillis(); |
---|
| 1897 | tmp += t4 - t3; |
---|
| 1898 | netTime += tmp; |
---|
| 1899 | } |
---|
| 1900 | } |
---|
| 1901 | |
---|
| 1902 | //Petroaca - the communication simulation for the emergency channel |
---|
| 1903 | public void simulateCommunicationEmergency(SendEvent e) { |
---|
| 1904 | |
---|
| 1905 | Road r1 = null; |
---|
| 1906 | Point p1 = null; |
---|
| 1907 | r1 = (Road) Globals.map.roads.get(e.sender.getRoadIdx()); |
---|
| 1908 | p1 = (Point) r1.points.get(e.sender.getPointIdx()); |
---|
| 1909 | |
---|
| 1910 | if (e instanceof UnicastSendEvent) { |
---|
| 1911 | long t1 = System.currentTimeMillis(); |
---|
| 1912 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 1913 | ReceiveEvent re = null; |
---|
| 1914 | while (it.hasNext()) { |
---|
| 1915 | SimulatedCarInfo r = it.next(); |
---|
| 1916 | if (r.vehicleId == ((UnicastSendEvent) e).getReceiverId()) { |
---|
| 1917 | Road r2 = (Road) Globals.map.roads.get(r.getRealPos() |
---|
| 1918 | .getRoadIdx()); |
---|
| 1919 | Point p2 = (Point) r2.points.get(r.getRealPos() |
---|
| 1920 | .getPointIdx()); |
---|
| 1921 | |
---|
| 1922 | // MODIFIED BY CRISTI!!! |
---|
| 1923 | // ReceiveEvent er = new ReceiveEvent(e.getTime() + 1, r, |
---|
| 1924 | // e.getMessage()); |
---|
| 1925 | |
---|
| 1926 | re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
| 1927 | re.setUnicast(true); |
---|
| 1928 | |
---|
| 1929 | schedEventEmergency(re); |
---|
| 1930 | if (GPSutil.distance(p1, p2) < WIRELESS_RANGE) { |
---|
| 1931 | } else { |
---|
| 1932 | System.out.println("EROARE! DISTANCE=" |
---|
| 1933 | + GPSutil.distance(p1, p2)); |
---|
| 1934 | } |
---|
| 1935 | // END MODIFIED!!! |
---|
| 1936 | return; |
---|
| 1937 | } |
---|
| 1938 | } |
---|
| 1939 | |
---|
| 1940 | re.setSender(e.getSender()); |
---|
| 1941 | schedEventEmergency(re); |
---|
| 1942 | |
---|
| 1943 | long t2 = System.currentTimeMillis(); |
---|
| 1944 | netTime += t2 - t1; |
---|
| 1945 | |
---|
| 1946 | } else { |
---|
| 1947 | |
---|
| 1948 | long tmp = 0; |
---|
| 1949 | long t3 = System.currentTimeMillis(); |
---|
| 1950 | long t4; |
---|
| 1951 | |
---|
| 1952 | // re = new ReceiveEvent(e.getTime() + 1, e.getSender(), e |
---|
| 1953 | // .getMessage()); |
---|
| 1954 | |
---|
| 1955 | |
---|
| 1956 | long comp = 0; |
---|
| 1957 | // peanoKeys |
---|
| 1958 | int cnt2 = 0; |
---|
| 1959 | int cnt3 = 0; |
---|
| 1960 | int cnt4 = 0; |
---|
| 1961 | int pkcnt = 0; |
---|
| 1962 | PeanoKey pk = Globals.map.peanoKeys.get(p1.getPeanoKeyIdx()); |
---|
| 1963 | |
---|
| 1964 | ListIterator<PeanoKey> pkit = Globals.map.peanoKeys |
---|
| 1965 | .listIterator(p1.getPeanoKeyIdx()); |
---|
| 1966 | PeanoKey maxpk = null; |
---|
| 1967 | |
---|
| 1968 | double wifirange; |
---|
| 1969 | if (e.sender instanceof WirelessTrafficLight){ |
---|
| 1970 | maxpk = GPSutil.getMaxSearchBoundPK(p1,Engine.EXTENDED_WIRELESS_RANGE); |
---|
| 1971 | wifirange = EXTENDED_WIRELESS_RANGE; |
---|
| 1972 | }else{ |
---|
| 1973 | wifirange = WIRELESS_RANGE; |
---|
| 1974 | maxpk = pk.getMaxpk(); |
---|
| 1975 | } |
---|
| 1976 | PeanoKey pkx = null; |
---|
| 1977 | for (pkx = pkit.next(); pkit.hasNext(); pkx = pkit.next()) { |
---|
| 1978 | pkcnt++; |
---|
| 1979 | // long tc1 = System.currentTimeMillis(); |
---|
| 1980 | if (pkx.compareTo(maxpk) > 0) |
---|
| 1981 | break; |
---|
| 1982 | // long tc2 = System.currentTimeMillis(); |
---|
| 1983 | // comp += (tc2-tc1); |
---|
| 1984 | |
---|
| 1985 | if (pkx.getCars() != null) { |
---|
| 1986 | Road r2 = (Road) Globals.map.roads.get(pkx |
---|
| 1987 | .getRoadIndex()); |
---|
| 1988 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
| 1989 | if (GPSutil.distance(p1, p2) < 5 * wifirange) { |
---|
| 1990 | cnt3 += pkx.getCars().size(); |
---|
| 1991 | Iterator<SimulatedCarInfo> it = pkx.getCars() |
---|
| 1992 | .iterator(); |
---|
| 1993 | while (it.hasNext()) { |
---|
| 1994 | SimulatedCarInfo r = it.next(); |
---|
| 1995 | if (e.sender instanceof SimulatedCarInfo) |
---|
| 1996 | if (r.getVehicleId() == ((SimulatedCarInfo)e.sender) |
---|
| 1997 | .getVehicleId()) |
---|
| 1998 | continue; |
---|
| 1999 | r.setLastMediumTransmition(crtTime); |
---|
| 2000 | |
---|
| 2001 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
| 2002 | re.setSender(e.getSender()); |
---|
| 2003 | schedEventEmergency(re); |
---|
| 2004 | } |
---|
| 2005 | } |
---|
| 2006 | } |
---|
| 2007 | } |
---|
| 2008 | PeanoKey minpk = null; |
---|
| 2009 | if (e.sender instanceof WirelessTrafficLight) |
---|
| 2010 | minpk = GPSutil.getMaxSearchBoundPK(p1,-Engine.EXTENDED_WIRELESS_RANGE); |
---|
| 2011 | else |
---|
| 2012 | minpk = pk.getMinpk(); |
---|
| 2013 | |
---|
| 2014 | pkit = Globals.map.peanoKeys.listIterator(p1.getPeanoKeyIdx()); |
---|
| 2015 | |
---|
| 2016 | if (pkit.hasPrevious()){ |
---|
| 2017 | pkx = pkit.previous(); |
---|
| 2018 | } |
---|
| 2019 | for (; pkit.hasPrevious(); pkx = pkit |
---|
| 2020 | .previous()) { |
---|
| 2021 | pkcnt++; |
---|
| 2022 | // long tc1 = System.currentTimeMillis(); |
---|
| 2023 | if (pkx.compareTo(minpk) < 0) { |
---|
| 2024 | break; |
---|
| 2025 | } |
---|
| 2026 | // long tc2 = System.currentTimeMillis(); |
---|
| 2027 | // comp += (tc2-tc1); |
---|
| 2028 | |
---|
| 2029 | if (pkx.getCars() != null) { |
---|
| 2030 | Road r2 = (Road) Globals.map.roads.get(pkx |
---|
| 2031 | .getRoadIndex()); |
---|
| 2032 | Point p2 = (Point) r2.points.get(pkx.getPointIndex()); |
---|
| 2033 | double distance = GPSutil.distance(p1, p2); |
---|
| 2034 | if (distance < 5 * wifirange) { |
---|
| 2035 | Iterator<SimulatedCarInfo> it = pkx.getCars() |
---|
| 2036 | .iterator(); |
---|
| 2037 | cnt3 += pkx.getCars().size(); |
---|
| 2038 | while (it.hasNext()) { |
---|
| 2039 | SimulatedCarInfo r = it.next(); |
---|
| 2040 | r.setLastMediumTransmition(crtTime); |
---|
| 2041 | |
---|
| 2042 | if (e.sender instanceof SimulatedCarInfo) |
---|
| 2043 | if (r.getVehicleId() == ((SimulatedCarInfo)e.sender) |
---|
| 2044 | .getVehicleId()) |
---|
| 2045 | continue; |
---|
| 2046 | ReceiveEvent re = new ReceiveEvent(e.getTime() + 1, r, e.getMessage(), e.getMessageObject(), e.getMessageType()); |
---|
| 2047 | re.setSender(e.getSender()); |
---|
| 2048 | schedEventEmergency(re); |
---|
| 2049 | } |
---|
| 2050 | } |
---|
| 2051 | } |
---|
| 2052 | } |
---|
| 2053 | |
---|
| 2054 | t4 = System.currentTimeMillis(); |
---|
| 2055 | tmp += t4 - t3; |
---|
| 2056 | netTime += tmp; |
---|
| 2057 | } |
---|
| 2058 | } |
---|
| 2059 | |
---|
| 2060 | /** |
---|
| 2061 | * Adds a new car to the simulation. |
---|
| 2062 | * |
---|
| 2063 | * @param car The object that stores the physical behaviour of the vehicle to add |
---|
| 2064 | */ |
---|
| 2065 | public void addCar(CarInstance car) |
---|
| 2066 | { |
---|
| 2067 | if (car == null) |
---|
| 2068 | return; |
---|
| 2069 | |
---|
| 2070 | car.timestamp = crtTime; |
---|
| 2071 | created ++; |
---|
| 2072 | synchronized (cars) |
---|
| 2073 | { |
---|
| 2074 | SimulatedCarInfo dc = null; |
---|
| 2075 | if (crtTime == 0) { |
---|
| 2076 | // dc = new SimulatedCarInfo(car.ID, car.getLane(), |
---|
| 2077 | // car.getSpeed(), car.getRoadIdx(), car.getPointIdx(), |
---|
| 2078 | // car.getDirection(), car.getOffset()); |
---|
| 2079 | /* |
---|
| 2080 | dc = new CarRunningVITP(car.ID, car.getLane(), car.getSpeed(), |
---|
| 2081 | car.getRoadIdx(), car.getPointIdx(), |
---|
| 2082 | car.getDirection(), car.getOffset()); |
---|
| 2083 | */ |
---|
| 2084 | //EZcab car |
---|
| 2085 | if (applicationType == Globals.PROT_EZCAB){ |
---|
| 2086 | dc=new EzcabCar(car.ID, car.getLane(), car.getSpeed(), |
---|
| 2087 | car.getRoadIdx(), car.getPointIdx(), |
---|
| 2088 | car.getDirection(), car.getOffset()); |
---|
| 2089 | System.out.println("new ezcab car created"); |
---|
| 2090 | } |
---|
| 2091 | else if (simulationType == RoutingConstants.ROUTING_SCFT) |
---|
| 2092 | dc=new CarRunningSCFT(car.ID, car.getLane(), car.getSpeed(), |
---|
| 2093 | car.getRoadIdx(), car.getPointIdx(), |
---|
| 2094 | car.getDirection(), car.getOffset()); |
---|
| 2095 | else |
---|
| 2096 | dc=new CarRunningDSRC(car.ID, car.getLane(), car.getSpeed(), |
---|
| 2097 | car.getRoadIdx(), car.getPointIdx(), |
---|
| 2098 | car.getDirection(), car.getOffset()); |
---|
| 2099 | |
---|
| 2100 | dc.setTimestamp(crtTime); |
---|
| 2101 | dc.setRealPos(car); |
---|
| 2102 | cars.add(dc); |
---|
| 2103 | dc.init(); |
---|
| 2104 | } |
---|
| 2105 | else |
---|
| 2106 | { |
---|
| 2107 | int poz = cars.indexOf(new SimulatedCarInfo(car.ID)); |
---|
| 2108 | if (poz == -1) |
---|
| 2109 | { |
---|
| 2110 | /* |
---|
| 2111 | dc = new CarRunningVITP(car.ID, car.getLane(), car |
---|
| 2112 | .getSpeed(), car.getRoadIdx(), car.getPointIdx(), |
---|
| 2113 | car.getDirection(), car.getOffset()); |
---|
| 2114 | */ |
---|
| 2115 | //Ezcab |
---|
| 2116 | if (applicationType == Globals.PROT_EZCAB){ |
---|
| 2117 | if(System.currentTimeMillis() % 2 == 0){ |
---|
| 2118 | dc = new EzcabClient(car.ID, car.getLane(), 1.0, //a minimum speed |
---|
| 2119 | car.getRoadIdx(), car.getPointIdx(), |
---|
| 2120 | car.getDirection(), car.getOffset()); |
---|
| 2121 | |
---|
| 2122 | // dc=new EzcabCar(car.ID, car.getLane(), car.getSpeed(), |
---|
| 2123 | // car.getRoadIdx(), car.getPointIdx(), |
---|
| 2124 | // car.getDirection(), car.getOffset()); |
---|
| 2125 | //System.out.println("new client created"); |
---|
| 2126 | } |
---|
| 2127 | else { |
---|
| 2128 | dc=new EzcabCar(car.ID, car.getLane(), car.getSpeed(), |
---|
| 2129 | car.getRoadIdx(), car.getPointIdx(), |
---|
| 2130 | car.getDirection(), car.getOffset()); |
---|
| 2131 | |
---|
| 2132 | // dc = new EzcabClient(car.ID, car.getLane(), 1.0, //a minimum speed |
---|
| 2133 | // car.getRoadIdx(), car.getPointIdx(), |
---|
| 2134 | // car.getDirection(), car.getOffset()); |
---|
| 2135 | //System.out.println("new ezcab car created"); |
---|
| 2136 | } |
---|
| 2137 | } |
---|
| 2138 | else if (simulationType == RoutingConstants.ROUTING_SCFT) |
---|
| 2139 | dc=new CarRunningSCFT(car.ID, car.getLane(), car.getSpeed(), |
---|
| 2140 | car.getRoadIdx(), car.getPointIdx(), |
---|
| 2141 | car.getDirection(), car.getOffset()); |
---|
| 2142 | else |
---|
| 2143 | dc=new CarRunningDSRC(car.ID, car.getLane(), car.getSpeed(), |
---|
| 2144 | car.getRoadIdx(), car.getPointIdx(), |
---|
| 2145 | car.getDirection(), car.getOffset()); |
---|
| 2146 | // dc = new SimulatedCarInfo(car.ID, car.getLane(), car |
---|
| 2147 | // .getSpeed(), car.getRoadIdx(), car.getPointIdx(), |
---|
| 2148 | // car.getDirection(), car.getOffset()); |
---|
| 2149 | |
---|
| 2150 | dc.setTimestamp(crtTime); |
---|
| 2151 | dc.setRealPos(car); |
---|
| 2152 | int t = 1 + random.nextInt(fps / 2 - 5); |
---|
| 2153 | schedEvent(new SendEvent(crtTime + t, dc, |
---|
| 2154 | SimulatedCarInfo.STANDARD_MESSAGE_ID)); |
---|
| 2155 | cars.add(dc); |
---|
| 2156 | dc.init(); |
---|
| 2157 | } |
---|
| 2158 | else |
---|
| 2159 | { |
---|
| 2160 | ((SimulatedCarInfo) cars.get(poz)).setRealPos(car); |
---|
| 2161 | } |
---|
| 2162 | } |
---|
| 2163 | } |
---|
| 2164 | } |
---|
| 2165 | |
---|
| 2166 | /* |
---|
| 2167 | * Read a scenario phase from a traces file. Update the vehicle's position |
---|
| 2168 | * for the current step. |
---|
| 2169 | * |
---|
| 2170 | */ |
---|
| 2171 | public void readScenarioPhase() { |
---|
| 2172 | int nrCars = 0; |
---|
| 2173 | try { |
---|
| 2174 | int vehId, rdIdx, ptIdx; |
---|
| 2175 | byte dir, lane, speed; |
---|
| 2176 | double offset; |
---|
| 2177 | nrCars = dis.readInt(); |
---|
| 2178 | // System.out.println(nrCars+" masini"); |
---|
| 2179 | |
---|
| 2180 | synchronized (cars) |
---|
| 2181 | { |
---|
| 2182 | for (int i = 0; i < nrCars; i++) |
---|
| 2183 | { |
---|
| 2184 | vehId = dis.readInt(); |
---|
| 2185 | rdIdx = dis.readInt(); |
---|
| 2186 | ptIdx = dis.readInt(); |
---|
| 2187 | offset = dis.readDouble(); |
---|
| 2188 | dir = (byte) dis.readInt(); |
---|
| 2189 | lane = (byte) dis.readInt(); |
---|
| 2190 | speed = (byte) dis.readDouble(); |
---|
| 2191 | |
---|
| 2192 | SimulatedCarInfo dc = null; |
---|
| 2193 | if (crtTime == 0) { |
---|
| 2194 | /* |
---|
| 2195 | dc = new CarRunningVITP(vehId, lane, speed, |
---|
| 2196 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2197 | */ |
---|
| 2198 | //create ezcab car |
---|
| 2199 | if (applicationType == Globals.PROT_EZCAB){ |
---|
| 2200 | dc = new EzcabCar(vehId, lane, speed, |
---|
| 2201 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2202 | System.out.println("new ezcab car"); |
---|
| 2203 | } |
---|
| 2204 | else if (simulationType == RoutingConstants.ROUTING_SCFT) |
---|
| 2205 | dc = new CarRunningSCFT(vehId, lane, speed, |
---|
| 2206 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2207 | else |
---|
| 2208 | dc = new CarRunningDSRC(vehId, lane, speed, |
---|
| 2209 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2210 | // dc = new SimulatedCarInfo(vehId, lane, speed, |
---|
| 2211 | // (short)rdIdx, (short)ptIdx, dir, offset); |
---|
| 2212 | dc.setTimestamp(crtTime); |
---|
| 2213 | dc.setRealPos(new CarInstance((CarInfo) dc)); |
---|
| 2214 | cars.add(dc); |
---|
| 2215 | dc.init(); |
---|
| 2216 | } else { |
---|
| 2217 | int poz = cars.indexOf(new SimulatedCarInfo(vehId)); |
---|
| 2218 | if (poz == -1) { |
---|
| 2219 | /* |
---|
| 2220 | dc = new CarRunningVITP(vehId, lane, speed, |
---|
| 2221 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2222 | */ |
---|
| 2223 | //create new ezcab car or client |
---|
| 2224 | if (applicationType == Globals.PROT_EZCAB){ |
---|
| 2225 | if(System.currentTimeMillis() % 2 == 0){ |
---|
| 2226 | dc = new EzcabClient(vehId, lane, 1.0, //minimum allowed speed |
---|
| 2227 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2228 | // dc = new EzcabCar(vehId, lane, speed, |
---|
| 2229 | // (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2230 | //System.out.println("new ezcab client created"); |
---|
| 2231 | } |
---|
| 2232 | else{ |
---|
| 2233 | dc = new EzcabCar(vehId, lane, speed, |
---|
| 2234 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2235 | // dc = new EzcabClient(vehId, lane, 1.0, //minimum allowed speed |
---|
| 2236 | // (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2237 | //System.out.println("new ezcab car created"); |
---|
| 2238 | } |
---|
| 2239 | } |
---|
| 2240 | else if (simulationType == RoutingConstants.ROUTING_SCFT) |
---|
| 2241 | dc = new CarRunningSCFT(vehId, lane, speed, |
---|
| 2242 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2243 | else |
---|
| 2244 | dc = new CarRunningDSRC(vehId, lane, speed, |
---|
| 2245 | (short) rdIdx, (short) ptIdx, dir, offset); |
---|
| 2246 | // dc = new SimulatedCarInfo(vehId, lane, speed, |
---|
| 2247 | // (short)rdIdx, (short)ptIdx, dir, offset); |
---|
| 2248 | dc.setTimestamp(crtTime); |
---|
| 2249 | dc.setRealPos(new CarInstance((CarInfo) dc)); |
---|
| 2250 | cars.add(dc); |
---|
| 2251 | dc.init(); |
---|
| 2252 | } else { |
---|
| 2253 | CarInstance ci = ((SimulatedCarInfo) cars.get(poz)) |
---|
| 2254 | .getRealPos(); |
---|
| 2255 | ci.deleteCarToPointMapping(); |
---|
| 2256 | ci.setSpeed((byte) speed); |
---|
| 2257 | ci.setRoadIdx((short) rdIdx); |
---|
| 2258 | ci.setPointIdx((short) ptIdx); |
---|
| 2259 | ci.setLane(lane); |
---|
| 2260 | ci.setDirection((byte) dir); |
---|
| 2261 | ci.setOffset(offset); |
---|
| 2262 | ci.setTimestamp(crtTime); |
---|
| 2263 | ci.updateCarToPointMapping(); |
---|
| 2264 | } |
---|
| 2265 | } |
---|
| 2266 | } |
---|
| 2267 | } |
---|
| 2268 | } catch (Exception e) { |
---|
| 2269 | System.err.println("Error reading scenario file"); |
---|
| 2270 | e.printStackTrace(); |
---|
| 2271 | } |
---|
| 2272 | } |
---|
| 2273 | |
---|
| 2274 | public static boolean outputToFile = false; |
---|
| 2275 | |
---|
| 2276 | public void generateScenarioPhase() { |
---|
| 2277 | |
---|
| 2278 | long t1sem = System.currentTimeMillis(); |
---|
| 2279 | if (crtTime % Globals.SECOND == 0){ |
---|
| 2280 | for (int j = 0; j < Globals.map.lightsIndices.size(); j++) { |
---|
| 2281 | IntersectionWithTrafficLights iwtl = (IntersectionWithTrafficLights) Globals.map.allIntersections |
---|
| 2282 | .get(Globals.map.lightsIndices.get(j)); |
---|
| 2283 | iwtl.step(crtTime); |
---|
| 2284 | } |
---|
| 2285 | } |
---|
| 2286 | long t2sem = System.currentTimeMillis(); |
---|
| 2287 | semTime += (t2sem - t1sem); |
---|
| 2288 | |
---|
| 2289 | int NCARS = 0; |
---|
| 2290 | |
---|
| 2291 | for (int j = 0; j < Globals.map.roads.size(); j++) { |
---|
| 2292 | |
---|
| 2293 | //Petroaca - random car on road to send emergency message |
---|
| 2294 | Random r; |
---|
| 2295 | int randId=0; |
---|
| 2296 | if(this.simulationProtocol==Globals.PROTOCOL_DSRC) |
---|
| 2297 | { |
---|
| 2298 | r=new Random(); |
---|
| 2299 | randId=r.nextInt(Globals.map.roads.get(j).carsOnThisRoad.size()+1); |
---|
| 2300 | |
---|
| 2301 | } |
---|
| 2302 | |
---|
| 2303 | SortedCarVector newVector = new SortedCarVector(); |
---|
| 2304 | for (int k = Globals.map.roads.get(j).carsOnThisRoad.size() - 1; k >= 0; k--) { |
---|
| 2305 | CarInstance car = Globals.map.roads.get(j).carsOnThisRoad |
---|
| 2306 | .get(k); |
---|
| 2307 | if (car.finished) { |
---|
| 2308 | |
---|
| 2309 | totalControlDelay += car.totalControlDelay; |
---|
| 2310 | fincars ++; |
---|
| 2311 | if (maxControlDelay < car.totalControlDelay) |
---|
| 2312 | maxControlDelay = car.totalControlDelay; |
---|
| 2313 | |
---|
| 2314 | synchronized (cars) { |
---|
| 2315 | if (car.startMonitor) { |
---|
| 2316 | crossedCars++; |
---|
| 2317 | totalEM.addEmissionsResults(car.em, 1); |
---|
| 2318 | totalkm += car.totalDistance - car.startMonitorDistance; |
---|
| 2319 | totalTime += (crtTime - car.startTime)/Globals.SECOND; |
---|
| 2320 | } |
---|
| 2321 | Globals.monitoredCars.remove(car.getSimulatedCarInfo()); |
---|
| 2322 | car.finish(); |
---|
| 2323 | car.deleteCarToPointMapping(); |
---|
| 2324 | cars.remove(car.getSimulatedCarInfo()); |
---|
| 2325 | } |
---|
| 2326 | continue; |
---|
| 2327 | } |
---|
| 2328 | try { |
---|
| 2329 | long t1 = System.currentTimeMillis(); |
---|
| 2330 | |
---|
| 2331 | synchronized (cars) { |
---|
| 2332 | car.deleteCarToPointMapping(); |
---|
| 2333 | car.move(); |
---|
| 2334 | |
---|
| 2335 | if (this.simulationType == Globals.routePlanConstants.DYNAMIC_SELF_ROUTE) { |
---|
| 2336 | QueryGeneration.generateQueryForCar(car); |
---|
| 2337 | } |
---|
| 2338 | |
---|
| 2339 | if (this.simulationType == Globals.routePlanConstants.DYNAMIC_INFRASTRUCTURE_ROUTE) { |
---|
| 2340 | GuidanceRequest.generateRequestToInfrastructure(car); |
---|
| 2341 | } |
---|
| 2342 | |
---|
| 2343 | if (Engine.simulationType == RoutingConstants.DYNAMIC_CITY_ROUTE) { |
---|
| 2344 | CarToIntersectionComm.updateMetrics(car); |
---|
| 2345 | } |
---|
| 2346 | |
---|
| 2347 | // Petroaca - emergency messages schedule |
---|
| 2348 | |
---|
| 2349 | if(this.simulationProtocol==Globals.PROTOCOL_DSRC && car.ID==randId && crtTime%50==0) |
---|
| 2350 | { |
---|
| 2351 | EmergencyRequest.generateEmergencyMessage(car); |
---|
| 2352 | } |
---|
| 2353 | |
---|
| 2354 | car.setTimestamp(crtTime); |
---|
| 2355 | |
---|
| 2356 | |
---|
| 2357 | |
---|
| 2358 | if (car.endMonitor){ |
---|
| 2359 | crossedCars ++; |
---|
| 2360 | totalEM.addEmissionsResults(car.em, 1); |
---|
| 2361 | totalkm += car.totalDistance - car.startMonitorDistance; |
---|
| 2362 | // System.out.println("Fuel: " + car.em.fc / 1000 +" "+ car.totalDistance + " "+car.em.fc * 100.0/ (10000 * car.totalDistance) + " "+(car.totalDistance * 3600.0) / (double)(car.travelTime / Globals.SECOND)); |
---|
| 2363 | car.endMonitor = false; |
---|
| 2364 | car.startMonitor = false; |
---|
| 2365 | } |
---|
| 2366 | // 2 hardcoded exits for manhatan |
---|
| 2367 | // if (car.roadIdx == 2 && car.pointIdx < 7) |
---|
| 2368 | // car.finished = true; |
---|
| 2369 | // if (car.roadIdx == 13 && car.pointIdx < 95) |
---|
| 2370 | // car.finished = true; |
---|
| 2371 | // if (car.totalDistance > 10){ |
---|
| 2372 | // System.out.println("Fuel: "+ car.em.fc / 1000); |
---|
| 2373 | // car.finished = true; |
---|
| 2374 | // } |
---|
| 2375 | car.updateCarToPointMapping(); |
---|
| 2376 | |
---|
| 2377 | } |
---|
| 2378 | |
---|
| 2379 | long t2 = System.currentTimeMillis(); |
---|
| 2380 | moveTime += t2 - t1; |
---|
| 2381 | } catch (Exception ex) { |
---|
| 2382 | ex.printStackTrace(); |
---|
| 2383 | } |
---|
| 2384 | // if it has changed road, place it in the vector of its new |
---|
| 2385 | // road |
---|
| 2386 | if (car.getRoadIdx() != j) { |
---|
| 2387 | if (car.getRoadIdx() > j) |
---|
| 2388 | NCARS--; // it will be moved again when that road |
---|
| 2389 | // will be processed |
---|
| 2390 | Globals.map.roads.get(car.getRoadIdx()).carsOnThisRoad |
---|
| 2391 | .addCarInstance(car); |
---|
| 2392 | } else { |
---|
| 2393 | // it's still on the same road |
---|
| 2394 | newVector |
---|
| 2395 | .addCarInstance(Globals.map.roads.get(j).carsOnThisRoad |
---|
| 2396 | .get(k)); |
---|
| 2397 | } |
---|
| 2398 | NCARS++; |
---|
| 2399 | } |
---|
| 2400 | |
---|
| 2401 | // loop through the new vector and set the indices |
---|
| 2402 | for (int k = 0; k < newVector.size(); k++) { |
---|
| 2403 | newVector.get(k).index = k; |
---|
| 2404 | } |
---|
| 2405 | Globals.map.roads.get(j).carsOnThisRoad = newVector; |
---|
| 2406 | } |
---|
| 2407 | |
---|
| 2408 | // Iterator it2=cars.iterator(); |
---|
| 2409 | // int countt2=0; |
---|
| 2410 | // |
---|
| 2411 | // while(it2.hasNext()) { |
---|
| 2412 | // SimulatedCarInfo sci=(SimulatedCarInfo)it2.next(); |
---|
| 2413 | // |
---|
| 2414 | // if(sci.pulseMessage) |
---|
| 2415 | // countt2++; |
---|
| 2416 | // } |
---|
| 2417 | // System.out.println(" -- "+countt2+" / "+cars.size()); |
---|
| 2418 | |
---|
| 2419 | |
---|
| 2420 | if (!outputToFile) |
---|
| 2421 | return; |
---|
| 2422 | // output text |
---|
| 2423 | try { |
---|
| 2424 | pwText.print(NCARS + " "); |
---|
| 2425 | outBin.writeInt(NCARS); |
---|
| 2426 | for (int j = 0; j < Globals.map.roads.size(); j++) { |
---|
| 2427 | for (int k = 0; k < Globals.map.roads.get(j).carsOnThisRoad |
---|
| 2428 | .size(); k++) { |
---|
| 2429 | CarInstance ci = Globals.map.roads.get(j).carsOnThisRoad |
---|
| 2430 | .get(k); |
---|
| 2431 | pwText.print(ci.ID + " " + ci.getRoadIdx() + " " |
---|
| 2432 | + ci.getPointIdx() + " " + ci.getOffset() + " " |
---|
| 2433 | + ci.getDirection() + " " + ci.getLane() + " "); |
---|
| 2434 | outBin.writeInt(ci.ID); |
---|
| 2435 | outBin.writeInt(ci.getRoadIdx()); |
---|
| 2436 | outBin.writeInt(ci.getPointIdx()); |
---|
| 2437 | outBin.writeDouble(ci.getOffset()); |
---|
| 2438 | outBin.writeInt(ci.getDirection()); |
---|
| 2439 | outBin.writeInt(ci.getLane()); |
---|
| 2440 | outBin.writeDouble(ci.getSpeed()); |
---|
| 2441 | } |
---|
| 2442 | } |
---|
| 2443 | pwText.println(); |
---|
| 2444 | } catch (IOException e) { |
---|
| 2445 | System.err.println("Error writing traces"); |
---|
| 2446 | e.printStackTrace(); |
---|
| 2447 | System.exit(0); |
---|
| 2448 | } |
---|
| 2449 | } |
---|
| 2450 | |
---|
| 2451 | public int getCrtTime(){ |
---|
| 2452 | return crtTime; |
---|
| 2453 | } |
---|
| 2454 | |
---|
| 2455 | |
---|
| 2456 | |
---|
| 2457 | |
---|
| 2458 | public SimulatedCarInfo getCarIdx(int rdIdx, int ptIdx) |
---|
| 2459 | { |
---|
| 2460 | synchronized (cars) |
---|
| 2461 | { |
---|
| 2462 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 2463 | while (it.hasNext()) { |
---|
| 2464 | SimulatedCarInfo r = it.next(); |
---|
| 2465 | if (r.getRoadIdx() == rdIdx && r.getPointIdx() == ptIdx) |
---|
| 2466 | return r; |
---|
| 2467 | } |
---|
| 2468 | } |
---|
| 2469 | return null; |
---|
| 2470 | } |
---|
| 2471 | |
---|
| 2472 | public SimulatedCarInfo getCarIdx(int idx) |
---|
| 2473 | { |
---|
| 2474 | synchronized (cars) |
---|
| 2475 | { |
---|
| 2476 | Iterator<SimulatedCarInfo> it = cars.iterator(); |
---|
| 2477 | while (it.hasNext()) { |
---|
| 2478 | SimulatedCarInfo r = it.next(); |
---|
| 2479 | if (r.vehicleId == idx) |
---|
| 2480 | return r; |
---|
| 2481 | } |
---|
| 2482 | } |
---|
| 2483 | return null; |
---|
| 2484 | } |
---|
| 2485 | |
---|
| 2486 | public static void main(String args[]) { |
---|
| 2487 | |
---|
| 2488 | // Globals.map = new Map("Intersectie.RT1","Intersectie.RT2"); |
---|
| 2489 | |
---|
| 2490 | int tests = 1; |
---|
| 2491 | for (int i = 0; i < tests; i++){ |
---|
| 2492 | try{ |
---|
| 2493 | switch (i){ |
---|
| 2494 | case 0: Globals.pw.println("Adaptive xc = 1, variations2"); |
---|
| 2495 | break; |
---|
| 2496 | case 1:Globals.pw.println("Adaptive xc = 0.95, variations2"); |
---|
| 2497 | break; |
---|
| 2498 | case 2:break; |
---|
| 2499 | } |
---|
| 2500 | |
---|
| 2501 | // ObjectInputStream ois=new ObjectInputStream(new FileInputStream(".\\maps\\fsc\\Apaca.fsc")); |
---|
| 2502 | // Scenario sc=(Scenario)ois.readObject(); |
---|
| 2503 | // Mobility.loadScenario(sc); |
---|
| 2504 | |
---|
| 2505 | Engine e = Globals.engine = new Engine(false, true); |
---|
| 2506 | e.init(); |
---|
| 2507 | // Thread t = new Thread ( new Runnable(){ |
---|
| 2508 | // public void run(){ |
---|
| 2509 | // Globals.demo = new Display(); |
---|
| 2510 | // Globals.demo.setVisible(true); |
---|
| 2511 | // } |
---|
| 2512 | // }); |
---|
| 2513 | // t.start(); |
---|
| 2514 | // e.play(); |
---|
| 2515 | switch (i){ |
---|
| 2516 | case 0: |
---|
| 2517 | Globals.Xc = 0.95; |
---|
| 2518 | break; |
---|
| 2519 | case 1:break; |
---|
| 2520 | case 2:break; |
---|
| 2521 | } |
---|
| 2522 | |
---|
| 2523 | if (i != tests - 1){ |
---|
| 2524 | Globals.pw.close(); |
---|
| 2525 | String pwname = "rezultate"+System.currentTimeMillis()+".txt"; |
---|
| 2526 | Globals.pw = new PrintWriter(new FileWriter(pwname)); |
---|
| 2527 | } |
---|
| 2528 | } catch (Exception ex) { |
---|
| 2529 | ex.printStackTrace(); |
---|
| 2530 | System.exit(0); |
---|
| 2531 | } |
---|
| 2532 | |
---|
| 2533 | |
---|
| 2534 | } |
---|
| 2535 | |
---|
| 2536 | /* long t1 = System.currentTimeMillis(); |
---|
| 2537 | e.play(60); |
---|
| 2538 | long t2 = System.currentTimeMillis(); |
---|
| 2539 | |
---|
| 2540 | System.out.println("SimTime: " + (t2 - t1)); |
---|
| 2541 | System.out.println("events: " + (double) (100 * e.eventsTime) |
---|
| 2542 | / (t2 - t1)); |
---|
| 2543 | System.out.println("mobility: " + (double) (100 * e.mobilityTime) |
---|
| 2544 | / (t2 - t1)); |
---|
| 2545 | System.out.println("move: " + (double) (100 * e.moveTime) / (t2 - t1)); |
---|
| 2546 | System.out.println("code: " + (double) (100 * e.codeTime) / (t2 - t1)); |
---|
| 2547 | System.out.println("net: " + (double) (100 * e.netTime) / (t2 - t1)); |
---|
| 2548 | System.out.println("cleanup: " + (double) (100 * e.cleanupTime) |
---|
| 2549 | / (t2 - t1)); |
---|
| 2550 | System.out |
---|
| 2551 | .println("sched: " + (double) (100 * e.schedTime) / (t2 - t1)); |
---|
| 2552 | |
---|
| 2553 | // System.out.println("pkavg " + pkavg + "\npkmax" + pkmax |
---|
| 2554 | // + "\ncomp time " + avgTime); |
---|
| 2555 | // e.play(); |
---|
| 2556 | // e.step(); |
---|
| 2557 | // e.step(); |
---|
| 2558 | // e.step(); |
---|
| 2559 | // e.step(); |
---|
| 2560 | // e.step(); |
---|
| 2561 | System.exit(0);*/ |
---|
| 2562 | } |
---|
| 2563 | |
---|
| 2564 | |
---|
| 2565 | /* public void createDummyCars() { |
---|
| 2566 | Road r = Globals.map.roads.get(1); |
---|
| 2567 | |
---|
| 2568 | //Stopping a car on a road |
---|
| 2569 | int ID = lastCarID; |
---|
| 2570 | lastCarID++; |
---|
| 2571 | CarInstance car = Mobility.createNewCar(1, 46, 0, 1, 0.0, |
---|
| 2572 | new NotMovingPersonality(), 0.0, ID, 2, 64, null, 1); |
---|
| 2573 | if (car == null) { |
---|
| 2574 | lastCarID--; |
---|
| 2575 | System.out.println("Could not create the first stopped car"); |
---|
| 2576 | } else { |
---|
| 2577 | System.out.println("Jammed car id=" + ID); |
---|
| 2578 | jammed = ID; |
---|
| 2579 | } |
---|
| 2580 | |
---|
| 2581 | this.addCar(car); |
---|
| 2582 | } |
---|
| 2583 | */ |
---|
| 2584 | |
---|
| 2585 | |
---|
| 2586 | } |
---|