Esempio n. 1
0
 Task TransferAirplane(FollowMeCar followme, AirplaneTransferCommand cmd)
 {
     return(new Task(() =>
     {
         GoPath(GoToVertexAlone, followme, cmd.PlaneLocationVertex);
         GoPath(GoToVertexWithAirplane, followme, cmd.DestinationVertex);
         MqClient.Send <ServiceCompletionMessage>(queuesTo[Component.GroundService], new ServiceCompletionMessage()
         {
             Component = Component.FollowMe,
             PlaneId = followme.PlaneId
         });
         SendToLogs("Completed transfering airplane ID " + cmd.PlaneId);
         Console.WriteLine($"FollowMe {followme.FollowMeId} completed transfering airplane ID "
                           + cmd.PlaneId);
         followme.Status = Status.Free;
         Console.WriteLine($"FollowMe {followme.FollowMeId} is free now and going home");
         var source = new CancellationTokenSource();     //adds token and remove it after went home/new cmd
         tokens.TryAdd(followme.FollowMeId, source);
         GoPathHome(followme, GetHomeVertex(), source.Token);
         if (source.Token.IsCancellationRequested)
         {
             Console.WriteLine($"FollowMe {followme.FollowMeId} is going on new task");
         }
         else
         {
             Console.WriteLine($"FollowMe {followme.FollowMeId} is in garage now");
         }
         tokens.Remove(followme.FollowMeId, out source);
     }));
 }
Esempio n. 2
0
        public Task GotTransferRequest(AirplaneTransferCommand cmd)
        {
            Console.WriteLine($"Got transfer command of airplane {cmd.PlaneId} from vertex {cmd.PlaneLocationVertex} " +
                              $"to {cmd.DestinationVertex}");
            var  followme = cars.Values.FirstOrDefault(car => car.Status == Status.Free);
            Task task     = new Task(() =>
            {
                while (followme == null)       //waits for a free car
                {
                    source.CreateToken().Sleep(100);
                    followme = cars.Values.FirstOrDefault(car => car.Status == Status.Free);
                }

                if (tokens.TryGetValue(followme.FollowMeId, out var cancellationToken))
                {
                    cancellationToken.Cancel();                     //cancel going home
                    carTasks[followme.FollowMeId].Wait();           //wait for the task end
                }

                followme.Status  = Status.Busy;
                followme.PlaneId = cmd.PlaneId;
                var t            = TransferAirplane(followme, cmd);
                carTasks.AddOrUpdate(followme.FollowMeId, t, (key, value) => value = t);  //update task or add if not exists
                t.Start();
                Console.WriteLine($"FollowMe {followme.FollowMeId} go transfer airplane {cmd.PlaneId}");
            });

            return(task);
        }