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; }
void CarsStart() { for (int i = 0; i < countCars; i++) { var busCar = new BusCar(); busCar.LocationVertex = transportMotion.GetHomeVertex(); cars.TryAdd(busCar.CarId, busCar); busCar.CarTools = new CarTools() { AirplaneResponse = new AutoResetEvent(false), WakeEvent = new AutoResetEvent(false), StorageResponse = new AutoResetEvent(false), TokenSource = new CancellationTokenSource() }; DoWork(busCar, cars[busCar.CarId].CarTools.WakeEvent).Start(); } }
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; }
Task DoWork(BusCar car, AutoResetEvent wakeEvent) //car work { return(new Task(() => { while (true) { if (commands.TryDequeue(out var command)) { if (command.Action == TransferAction.Give) { GetPassengersToAirplane(car, command); } else { TakePassengersFromAirplane(car, command); } completionEvents[command.PlaneId].Signal(); } if (!IsHome(car.LocationVertex)) //if car is not home go home { Console.WriteLine($"{car.CarId} is going home"); car.IsGoingHome = true; transportMotion.GoPathFree(car, transportMotion.GetHomeVertex(), car.CarTools.TokenSource.Token); } if (!car.CarTools.TokenSource.IsCancellationRequested) //if going home was not cancelled wait for task { car.IsGoingHome = false; wakeEvent.WaitOne(); } else { car.CarTools.TokenSource = new CancellationTokenSource(); } } })); }