Пример #1
0
        public void OnHandleCommunication(object source, RobotEventArgs e)
        {
            // Case where MasterRobot asks HelperRobot to help out and HelperRobot is available to help
            if (e.RequestHelp && !isWorking)
            {
                isWorking = true;
                Console.WriteLine(Name + ": I am willing to help out... Busy working");
                base.OnHandleCommunication(new RobotEventArgs()
                {
                    Robot = this, Message = ""
                });
            }

            // Case where a request to help comes in but the HelperRobot is already busy
            else if (e.RequestHelp && isWorking)
            {
                Console.WriteLine(Name + ": I am willing to help but I am currently busy");
                base.OnHandleCommunication(new RobotEventArgs()
                {
                    Robot = this, Message = "I cannot accept the job"
                });
            }

            // Notification if other robot is finished working
            if (!e.Robot.isWorking)
            {
                Console.WriteLine(Name + ": " + e.Robot.Name + " Just Finished Work");
            }
        }
Пример #2
0
 protected virtual void OnHandleCommunication(RobotEventArgs args)
 {
     if (CommunicateWithOtherRobot != null)
     {
         CommunicateWithOtherRobot(this, args);
     }
 }
Пример #3
0
        public void OnHandleCommunication(object source, RobotEventArgs e)
        {
            // Handle case when HelperRobot isn't able to take on more tasks
            if (e.Message.Length > 0 && e.Robot.isWorking)
            {
                Console.WriteLine(Name + ": Received a message from " +
                                  e.Robot.Name + " saying " +
                                  e.Message);

                NumberOfJobsQueued += 1;
            }

            // Handle case when HelperRobot is able to work on a task
            else if (e.Robot.isWorking)
            {
                Console.WriteLine(Name + ": Adding task from " + e.Robot.Name);
                NumberOfJobsWorking += 1;
            }

            // Handle when HelperRobot finishes up a task
            if (!e.Robot.isWorking && e.RequestTask)
            {
                Console.WriteLine(Name + ": Received a task completion from " + e.Robot.Name);
                NumberOfJobsWorking -= 1;

                // Give HelperRobot another task from Queue if available
                if (NumberOfJobsQueued > 0)
                {
                    Console.WriteLine(Name + String.Format(": Giving {0} another task from the queue", e.Robot.Name));
                    base.OnHandleCommunication(new RobotEventArgs()
                    {
                        Robot = this, RequestHelp = true
                    });
                    NumberOfJobsQueued -= 1;
                }
            }
        }