void GetPassengersToAirplane(BusCar car, PassengersServiceCommand cmd) { Console.WriteLine($"{car.CarId} is going to storage to get {cmd.PassengersCount} passengers to {cmd.PlaneId}"); transportMotion.GoPath(car, 25); mqClient.Send <PassengersFromStorageRequest>(queuesTo[Component.Storage], new PassengersFromStorageRequest() { BusId = car.CarId, Capacity = BusCar.PassengersMaxCount, FlightId = cmd.FlightId }); Console.WriteLine($"{car.CarId} send message to storage"); car.CarTools.StorageResponse.WaitOne(); Console.WriteLine($"{car.CarId} took passengers from storage and is going to {cmd.PlaneId}"); transportMotion.GoPath(car, cmd.PlaneLocationVertex); playDelaySource.CreateToken().Sleep(2 * 60 * 1000); //get passengers to airplane mqClient.Send <PassengerTransferRequest>(queuesTo[Component.Airplane], new PassengerTransferRequest() { Action = TransferAction.Give, BusId = car.CarId, PassengersCount = car.Passengers, PlaneId = cmd.PlaneId }); mqClient.Send <PassengerPassMessage>(queuesTo[Component.Passenger], new PassengerPassMessage() { ObjectId = car.PlaneId, PassengersIds = car.PassengersList, Status = PassengerStatus.InAirplane }); Console.WriteLine($"{car.CarId} gave {cmd.PassengersCount} passengers to {cmd.PlaneId}"); car.Passengers = 0; }
int DoSmallCommands(PassengersServiceCommand cmd) { var count = 0; while (cmd.PassengersCount > 0) { count++; cmd.PassengersCount -= BusCar.PassengersMaxCount; //how many passengers left if (cmd.PassengersCount > 0) { commands.Enqueue(new PassengersServiceCommand() { Action = cmd.Action, FlightId = cmd.FlightId, PassengersCount = BusCar.PassengersMaxCount, PlaneId = cmd.PlaneId, PlaneLocationVertex = cmd.PlaneLocationVertex }); } else { commands.Enqueue(new PassengersServiceCommand() { Action = cmd.Action, FlightId = cmd.FlightId, PassengersCount = cmd.PassengersCount + BusCar.PassengersMaxCount, PlaneId = cmd.PlaneId, PlaneLocationVertex = cmd.PlaneLocationVertex }); } } return(count); }
Task GotCommand(PassengersServiceCommand cmd) { Console.WriteLine($"{cmd.PlaneId} new command: {cmd.Action} {cmd.PassengersCount} passengers"); var count = DoSmallCommands(cmd); //breaking a command on small commands Console.WriteLine($"{cmd.PlaneId} needs {count} buses"); var cde = new CountdownEvent(count); completionEvents.TryAdd(cmd.PlaneId, cde); foreach (var car in cars.Values) //cancel going home { if (car.IsGoingHome) { car.CarTools.TokenSource.Cancel(); } } foreach (var car in cars.Values) //wake up the cars in garage { car.CarTools.WakeEvent.Set(); } return(new Task(() => { cde.Wait(); Console.WriteLine($"{cmd.PlaneId}: servicing completed"); completionEvents.Remove(cmd.PlaneId, out cde); mqClient.Send <ServiceCompletionMessage>(queuesTo[Component.GroundService], new ServiceCompletionMessage() { Component = Component.Bus, PlaneId = cmd.PlaneId }); })); }
void TakePassengersFromAirplane(BusCar car, PassengersServiceCommand cmd) { Console.WriteLine($"{car.CarId} is going to take passengers from {cmd.PlaneId}"); transportMotion.GoPath(car, cmd.PlaneLocationVertex); playDelaySource.CreateToken().Sleep(2 * 60 * 1000); //take passengers from airplane mqClient.Send <PassengerTransferRequest>(queuesTo[Component.Airplane], new PassengerTransferRequest() { Action = TransferAction.Take, BusId = car.CarId, PassengersCount = BusCar.PassengersMaxCount, PlaneId = cmd.PlaneId }); car.CarTools.AirplaneResponse.WaitOne(); Console.WriteLine($"{car.CarId} is going to storage"); transportMotion.GoPath(car, 25); playDelaySource.CreateToken().Sleep(2 * 60 * 1000); //just throw passengers in the rain Console.WriteLine($"{car.CarId} has transfered passengers to storage"); car.Passengers = 0; }