/// <summary> /// Associate the vehicle with a enw client /// </summary> /// <param name="vehicleId"></param> /// <returns></returns> public bool ReBind(SimVehicleId vehicleId) { // make sure that the vehicle is not already associated with a client if (!VehicleToClientMap.ContainsKey(vehicleId)) { // loop over all clients looking for an open client foreach (string s in this.AvailableClients.Keys) { // make sure the client is not associated with a vehicle if (!ClientToVehicleMap.ContainsKey(s)) { try { // get the client SimulatorClientFacade scf = this.AvailableClients[s]; // set the vehicle in the client scf.SetVehicle(vehicleId); // set vehicle as having client VehicleToClientMap.Add(vehicleId, s); // set client as having vehicle ClientToVehicleMap.Add(s, vehicleId); // success SimulatorOutput.WriteLine("Vehicle: " + vehicleId.ToString() + " Bound to client: " + s); // return that the rebind was successful return true; } catch (Exception e) { // there was an error SimulatorOutput.WriteLine("Error binding vehicle: " + vehicleId.ToString() + " to client: " + s + "\n" + e.ToString()); // cleanup vehicle to client map if (this.VehicleToClientMap.ContainsKey(vehicleId)) this.VehicleToClientMap.Remove(vehicleId); // cleanup client to vehicle map if (this.ClientToVehicleMap.ContainsKey(s)) this.ClientToVehicleMap.Remove(s); // return that the attempt was unsuccessful return false; } } } // success SimulatorOutput.WriteLine("Vehicle: " + vehicleId.ToString() + " Could not be bound to any clients, lack of availability"); // notify that we never got to this point return false; } else return true; }
/// <summary> /// Remove a client /// </summary> /// <param name="vehicleId"></param> /// <returns></returns> public bool Remove(SimVehicleId vehicleId) { // check to see if this vehicle is associated with a client if (VehicleToClientMap.ContainsKey(vehicleId)) { // get name of client vehicle associated with string client = VehicleToClientMap[vehicleId]; try { // remove vehicle from client to vehicle map this.ClientToVehicleMap.Remove(client); // remove client from vehicle to client map this.VehicleToClientMap.Remove(vehicleId); // remove vehicle from client this.AvailableClients[client].SetVehicle(null); // notify success SimulatorOutput.WriteLine("Successfully removed vehicle: " + vehicleId.ToString() + " from clients"); // return success return true; } catch (Exception e) { // there was an error SimulatorOutput.WriteLine("Error removing vehicle: " + vehicleId.ToString() + " from clients: \n" + e.ToString()); // cleanup vehicle to client map if (this.VehicleToClientMap.ContainsKey(vehicleId)) this.VehicleToClientMap.Remove(vehicleId); // cleanup client to vehicle map if (this.ClientToVehicleMap.ContainsKey(client)) this.ClientToVehicleMap.Remove(client); // return unsuccessful return false; } } else { return true; } }
/// <summary> /// Sets the vehicle /// </summary> /// <param name="vehicleId"></param> public override void SetVehicle(SimVehicleId vehicleId) { if (vehicleId != null) { this.ClientVehicle = new ClientVehicle(); this.ClientVehicle.VehicleId = vehicleId; Console.WriteLine(DateTime.Now.ToString() + ": Registered Vehicle with Vehicle Id: " + vehicleId.ToString()); } else { if(this.ClientVehicle != null) Console.WriteLine(DateTime.Now.ToString() + ": Deregistered Vehicle with Vehicle Id: " + this.ClientVehicle.VehicleId.ToString()); this.ClientVehicle = null; } }