/** * Create a proxy vehicle * * @param msg the PV update message * @return the proxy vehicle */ private ProxyVehicleSimView makeProxyVehicle(Real2ProxyPVUpdate msg) { ProxyVehicleSimView vehicle = new ProxyVehicle( msg.position, msg.heading, msg.steeringAngle, msg.velocity, msg.targetVelocity, msg.acceleration, msg.receivedTime); vehicle.setDriver(new ProxyDriver(vehicle, sim.getMap())); assert vehicle.getDriver() != null; return vehicle; }
/** * The main function for processing the incoming datagram. * * @param dp the datagram. */ private void processIncomingDatagram(DatagramPacket dp) { synchronized (sim) { SocketAddress sa = dp.getSocketAddress(); Real2ProxyMsg msg = convertDatagramToReal2ProxyMsg(dp); if (Debug.SHOW_PROXY_VEHICLE_DEBUG_MSG) { if (Debug.SHOW_PROXY_VEHICLE_PVUPDATE_MSG || !(msg instanceof Real2ProxyPVUpdate)) { System.err.printf("Proxy vehicle received a Real2Proxy msg: %s\n", msg); } } if (msg == null) { System.err.println("Error: cannot parse the datagram package."); return; } if (sa2ProxyVehicle.containsKey(sa)) { // The datagram came from a real vehicle we're already tracking. // Simply forward the datagram to the corresponding proxy vehicle sa2ProxyVehicle.get(sa).processReal2ProxyMsg(msg); } else { // We haven't seem this SA before. This must be coming from // a new real vehicle that we're not tracking // If it is a PV_UPDATE message, instantiate the proxy vehicle and // associate the socket address to this proxy vehicle. // If not, ignore the message. if (msg.messageType == Real2ProxyMsg.Type.PV_UPDATE) { Real2ProxyPVUpdate pvUpdateMsg = (Real2ProxyPVUpdate) msg; // create a proxy vehicle for this real vehicle ProxyVehicleSimView vehicle = makeProxyVehicle(pvUpdateMsg); // check the VIN number if (VinRegistry.registerVehicleWithExistingVIN(vehicle, pvUpdateMsg.vin)) { // update the socket address of the proxy vehicle // pull out just the IP <xxx.xxx.xxx.xxx> from the address only String address = sa.toString(); address = address.substring(1, address.indexOf(':')); vehicle.setSa(new InetSocketAddress(address, DEFAULT_VEHICLE_UDP_PORT)); // record the proxy vehicle sa2ProxyVehicle.put(sa, vehicle); // add the proxy vehicle to the simulator sim.addProxyVehicle(vehicle); if (Debug.SHOW_PROXY_VEHICLE_DEBUG_MSG) { System.err.printf( "A proxy vehicle is created at time %.2f " + "(vin=%d).\n", sim.getSimulationTime(), vehicle.getVIN()); } } else { System.err.println( "Warning: the VIN of the UPD message has " + "already been used by other vehicles."); // don't add the proxy vehicle to the simulator. } } else { // Ignore the message if (Debug.SHOW_PROXY_VEHICLE_DEBUG_MSG) { System.err.println( "Warning: first message from a new real " + "vehicle must be a PVUpdate."); } } } } }