Exemplo n.º 1
0
        public override void Move(FarmingAction farmAction)
        {
            // Get user to move
            AMovingItem userToMove;

            this.listUsers.TryGetValue(farmAction.With, out userToMove);

            if (userToMove != null)
            {
                // Goat : river to boat
                var action = new ActionDetail(
                    (a, b) => a.MoveTo(b),
                    new FarmingAction
                {
                    To        = farmAction.To,
                    Direction = farmAction.Direction,
                    With      = farmAction.With
                }
                    );
                userToMove.ActionToDo = action;
                action.EndEvent.WaitOne();
            }
            this.PurgeWaitingList();

            // Farmer : river to boat
            this.MoveTo(new FarmingAction
            {
                To        = farmAction.To,
                Direction = farmAction.Direction,
                With      = farmAction.With
            });
            this.PurgeWaitingList();

            // Farmer : boat to river
            this.MoveTo(new FarmingAction
            {
                To        = farmAction.To,
                Direction = farmAction.Direction,
                With      = farmAction.With
            });
            this.PurgeWaitingList();

            if (userToMove != null)
            {
                // Goat : river to boat
                var action = new ActionDetail(
                    (a, b) => a.MoveTo(b),
                    new FarmingAction
                {
                    To        = farmAction.To,
                    Direction = farmAction.Direction,
                    With      = farmAction.With
                }
                    );
                userToMove.ActionToDo = action;
                action.EndEvent.WaitOne();
            }
            this.PurgeWaitingList();
        }
Exemplo n.º 2
0
        public void DoAction(ActionDetail action)
        {
            // Run action
            action.FunctionToCall(this, action.Parameters);

            // signal end of processing
            action.EndEvent.Set();
        }
Exemplo n.º 3
0
        public void Run()
        {
            // start all threads
            this.StartThreads();
            Thread.Sleep(500);
            Console.WriteLine();

            // Get the list of actions to perform
            var parameters = Orchestrator.GetAction();

            // Run each action
            foreach (var parameter in parameters)
            {
                AMovingItem with;
                this.listUsers.TryGetValue(parameter.With, out with);

                Console.WriteLine("Processing action to {0}, with {1}",
                                  this.listUsers[parameter.To], with);

                // Creation de l'action
                AMovingItem user = null;
                if (this.listUsers.TryGetValue(parameter.To, out user))
                {
                    ActionDetail action = new ActionDetail(
                        (a, b) => a.Move(b),
                        parameter
                        );

                    // Set the action to run
                    user.ActionToDo = action;

                    // Wait for the action to end
                    action.EndEvent.WaitOne();
                }
                else
                {
                    Console.WriteLine("Unknown user !");
                }

                Console.WriteLine();
            }

            // end all threads
            this.EndThreads();
        }
Exemplo n.º 4
0
        public ActionDetail CanTryEat(Position farmerPosition)
        {
            var action = new ActionDetail(
                (a, b) => a.TryEat(b),
                new EatAction
            {
                To             = this.Id,
                With           = Farming.Unknown,
                Direction      = Direction.Unknwon,
                FarmerPosition = farmerPosition
            }
                );

            Console.WriteLine("Ajout de l'action " + action.N + " a la waiting list");


            this.ToDoList.AddFirst(action);
            this.StartHandle.Set();

            // */
            return(action);
        }