Example #1
0
        void CarsStart()
        {
            for (int i = 0; i < countCars; i++)
            {
                wakeEvents.Add(new AutoResetEvent(false));
                var fuelTruckCar = new FuelTruckCar(i);
                fuelTruckCar.LocationVertex = transportMotion.GetHomeVertex();
                cars.TryAdd(fuelTruckCar.CarId, fuelTruckCar);
                tokens.TryAdd(fuelTruckCar.CarId, new CancellationTokenSource());
                DoRefuel(fuelTruckCar, wakeEvents[i]).Start();
            }

            SendLogMessage(String.Format("Создали {0} машинок!", countCars));
        }
Example #2
0
 Task DoRefuel(FuelTruckCar car, AutoResetEvent wakeEvent)      //car work
 {
     return(new Task(() =>
     {
         while (true)
         {                                                           //waits for common command
             Console.WriteLine($"Fueltrack {car.CarId} trying to get command");
             if (commands.TryDequeue(out var command))
             {
                 Console.WriteLine($"Fueltruck {car.CarId} is going to fuel airplane {command.PlaneId}" +
                                   $"with {command.Fuel} litres of fuel");
                 transportMotion.GoPath(car, command.PlaneLocationVertex);
                 Console.WriteLine($"Fueltruck {car.CarId} is fueling plane {command.PlaneId}");
                 playDelaySource.CreateToken().Sleep(2 * 60 * 1000);
                 mqClient.Send <RefuelCompletion>(queuesTo[Component.Airplane], new RefuelCompletion()
                 {
                     Fuel = command.Fuel,
                     PlaneId = command.PlaneId
                 });
                 SendLogMessage(String.Format("{0} заправила самолёт {1} и поехала домой", car.CarId, command.PlaneId));
                 completionEvents[command.PlaneId].Signal();
             }
             if (!IsHome(car.LocationVertex))            //if car is not home go home
             {
                 car.IsGoingHome = true;
                 transportMotion.GoPathFree(car, transportMotion.GetHomeVertex(), tokens[car.CarId].Token);
             }
             if (!tokens[car.CarId].IsCancellationRequested)
             {
                 car.IsGoingHome = false;
                 wakeEvent.WaitOne();
             }
             else
             {
                 Console.WriteLine($"Fueltruck { car.CarId} going home was cancelled");
                 tokens[car.CarId] = new CancellationTokenSource();
             }
         }
         bool IsHome(int locationVertex)
         {
             List <int> homeVertexes = new List <int>()
             {
                 4, 10, 16, 19
             };
             return homeVertexes.Contains(locationVertex);
         }
     }));
 }