/// <summary>
        /// Register a client with the simulation
        /// </summary>
        /// <param name="clientName"></param>
        /// <returns></returns>
        public override bool Register(string clientName)
        {
            try
            {
                // resolve the client
                SimulatorClientFacade scf = (SimulatorClientFacade)objectDirectory.Resolve(clientName);

                // client name
                string name = scf.Name();

                // invoke changes
                this.simulation.BeginInvoke(new MethodInvoker(delegate()
                {
                    // notify
                    this.simulation.clientHandler.AddClient(name, scf, this.simulation.simEngine.Vehicles);
                    this.simulation.OnClientsChanged();
                }));

                // return success
                return(true);
            }
            catch (Exception e)
            {
                this.simulation.BeginInvoke(new MethodInvoker(delegate()
                {
                    // notify
                    SimulatorOutput.WriteLine("Error Registering Client: " + clientName + ". \n" + e.ToString());
                }));

                // return error
                return(false);
            }
        }
        public DebuggingService(bool registerWithSim)
        {
            SetupSequencerEvents();

            if (registerWithSim)
            {
                SimulatorClientFacade simClient = (SimulatorClientFacade)CommBuilder.GetObject("SimulationClient");
                simClient.RegisterSteppableClient(this, ClientRunControlFacade.OperationalStepOrder);
            }
        }
        /// <summary>
        /// Begin simulation
        /// </summary>
        public void BeginSimulation(ArbiterRoadNetwork roadNetwork, ArbiterMissionDescription mission)
        {
            try
            {
                // startup clients
                foreach (KeyValuePair <SimVehicleId, SimVehicle> vhcs in this.Vehicles)
                {
                    // set road and mission
                    SimulatorClientFacade scf = this.simulationMain.clientHandler.AvailableClients[this.simulationMain.clientHandler.VehicleToClientMap[vhcs.Key]];

                    // check if we need to randomize mission
                    if (vhcs.Value.RandomMission)
                    {
                        // create random mission
                        Queue <ArbiterCheckpoint> checks = new Queue <ArbiterCheckpoint>(60);
                        int num = mission.MissionCheckpoints.Count - 1;
                        ArbiterCheckpoint[] checkPointArray = mission.MissionCheckpoints.ToArray();
                        Random r = new Random();
                        for (int i = 0; i < 60; i++)
                        {
                            checks.Enqueue(checkPointArray[r.Next(num)]);
                        }
                        ArbiterMissionDescription amd = new ArbiterMissionDescription(checks, mission.SpeedLimits);

                        // set road , mission
                        scf.SetRoadNetworkAndMission(roadNetwork, amd);
                    }
                    // otherwise no random mission
                    else
                    {
                        // set road , mission
                        scf.SetRoadNetworkAndMission(roadNetwork, mission);
                    }

                    // startup ai
                    bool b = scf.StartupVehicle();

                    // check for false
                    if (!b)
                    {
                        Console.WriteLine("Error starting simulation for vehicle id: " + vhcs.Key.ToString());
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error starting simulation: \n" + e.ToString());
                return;
            }

            // run sim
            this.RunSimulation();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add a client to the simulation
        /// </summary>
        /// <param name="client"></param>
        /// <param name="scf"></param>
        /// <param name="vehicles"></param>
        /// <returns></returns>
        public bool AddClient(string client, SimulatorClientFacade scf, Dictionary <SimVehicleId, SimVehicle> vehicles)
        {
            try
            {
                // check if already a client
                if (this.AvailableClients.ContainsKey(client))
                {
                    // remove if already bound for some reason
                    this.Remove(client);
                }

                // lock
                lock (((ICollection)this.AvailableClients).SyncRoot)
                {
                    // add to available clients
                    this.AvailableClients.Add(client, scf);
                }

                // notify success
                SimulatorOutput.WriteLine("Successfully added client: " + client);

                // attempt to rebind the vehicles
                this.ReBindAll(vehicles);

                // return success
                return(true);
            }
            catch (Exception e)
            {
                // notify failure
                SimulatorOutput.WriteLine("Error adding client: " + client + ": \n" + e.ToString());

                // return false
                return(false);
            }
        }
Exemplo n.º 5
0
        /// <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);
            }
        }