Ejemplo n.º 1
0
 public override void OnMessage(Message message)
 {
     if (Status == State.Failed)
     {
         return;
     }
     if (message.ReceiverAgentId == AgentId)
     {
         ProcessMessage(message);
     }
 }
Ejemplo n.º 2
0
 public void OnTimeEvent()
 {
     if (RulerAgent != null && _iStatus == 1)
     {
         _pingTime = Time.GlobalSimulationTime;
         var tempMessage = new Message()
         {
             SenderAgent        = this,
             SenderAgentId      = AgentId,
             CurrentSenderAgent = this,
             ReceiverAgent      = RulerAgent,
             ReceiverAgentId    = RulerAgent.AgentId,
             MessageType        = BroadcastType.SendReceive,
             MessageContent     = MessagesContent.Ping,
             DataMessageText    = ""
         };
         SendMessage(tempMessage);
         _iStatus = 3;
     }
     else if (_pingTime != -1 && Time.GlobalSimulationTime - _pingTime > 100 && _iStatus == 3)
     {
         _iStatus = 2;
         _startPartialAdaptationTime = Time.GlobalSimulationTime;
         SendBroadcastMessage(this, this, BroadcastType.MessengerToLeaderBroadcast,
                              MessagesContent.LostRuler, 1);
     }
     else if (RulerAgent != null && _iStatus == 1)
     {
         _pingTime = Time.GlobalSimulationTime;
         var message = new Message()
         {
             SenderAgent        = this,
             SenderAgentId      = this.AgentId,
             CurrentSenderAgent = this,
             ReceiverAgent      = RulerAgent,
             ReceiverAgentId    = RulerAgent.AgentId,
             MessageType        = BroadcastType.SendReceive,
             MessageContent     = MessagesContent.Ping,
             DataMessageText    = ""
         };
         SendMessage(message);
         _iStatus = 3;
     }
     else if (_iStatus == 2 && Time.GlobalSimulationTime - _startPartialAdaptationTime > 200)
     {
         _startPartialAdaptationTime = Time.GlobalSimulationTime;
         SendBroadcastMessage(this, this,
                              BroadcastType.MessengerToLeaderBroadcast,
                              MessagesContent.LostRuler, 2);
     }
 }
Ejemplo n.º 3
0
        private void SendBroadcastMessage(Agent senderAgent,
                                          Agent currentSenderAgent,
                                          BroadcastType messageType,
                                          MessagesContent messageContent, int iBroadcastNum)
        {
            if (Status == State.Failed)
            {
                return;
            }

            var message = new Message
            {
                CurrentSenderAgent   = currentSenderAgent,
                CurrentSenderAgentId = currentSenderAgent.AgentId,
                SenderAgentId        = senderAgent.AgentId,
                SenderAgent          = senderAgent,
                ReceiverAgent        = null,
                ReceiverAgentId      = "-1",
                MessageContent       = messageContent,
                MessageType          = messageType,
                NumOfBroadcastSteps  = iBroadcastNum
            };
            var messengerAgent = FindNearestMessenger(Position);

            if (messengerAgent == null)
            {
                RadioRange += 50;
                SendBroadcastMessage(senderAgent, currentSenderAgent, messageType, messageContent, iBroadcastNum);
                return;
            }

            message.CurrentReceiverAgent   = messengerAgent;
            message.CurrentReceiverAgentId = messengerAgent.AgentId;

            Container.ContainerMedia.SendMessage(message.Copy());
        }
Ejemplo n.º 4
0
        private void ProcessMessage(Message message)
        {
            if (message.MessageContent == MessagesContent.PingReply)
            {
                _pingTime = -1;
                _iStatus  = 1;
            }
            else if (message.MessageContent == MessagesContent.LostRuler)
            {
                if (_iStatus == 0 || _iStatus == 2)
                {
                    var tempMessage = new Message()
                    {
                        SenderAgent        = this,
                        SenderAgentId      = AgentId,
                        CurrentSenderAgent = this,
                        ReceiverAgent      = message.SenderAgent,
                        ReceiverAgentId    = message.SenderAgent.AgentId,
                        MessageType        = BroadcastType.SingleCast,
                        MessageContent     = MessagesContent.ReplyRulerNum,
                        DataMessageText    = "-1"
                    };
                    SendMessage(tempMessage);
                }
                else
                {
                    var tempMessage = new Message()
                    {
                        SenderAgent        = this,
                        SenderAgentId      = AgentId,
                        CurrentSenderAgent = this,
                        ReceiverAgent      = message.SenderAgent,
                        ReceiverAgentId    = message.SenderAgent.AgentId,
                        MessageType        = BroadcastType.SingleCast,
                        MessageContent     = MessagesContent.ReplyRulerNum,
                        RulerPingReply     = RulerAgent
                    };
                    SendMessage(tempMessage);
                }
            }
            else if (message.MessageContent == MessagesContent.ReplyRulerNum)
            {
                if (message.DataMessageText != "-1")
                {
                    var repliedRuler = message.RulerPingReply;
                    if (repliedRuler != RulerAgent)
                    {
                        RulerAgent = repliedRuler;

                        _iStatus = 1;
                        _startPartialAdaptationTime = long.MaxValue;
                        _pingTime = -1;
                        MeasureAdaptingTime();
                    }
                }
            }
            else if (message.MessageContent == MessagesContent.Ping)
            {
                var replyMessage = new Message
                {
                    MessageType          = BroadcastType.SingleCast,
                    ReceiverAgentId      = message.SenderAgentId,
                    ReceiverAgent        = message.SenderAgent,
                    SenderAgent          = this,
                    SenderAgentId        = AgentId,
                    CurrentSenderAgent   = this,
                    CurrentSenderAgentId = AgentId,
                    MessageContent       = MessagesContent.PingReply
                };
                var messengerAgent = message.CurrentSenderAgent;
                replyMessage.CurrentReceiverAgentId = messengerAgent.AgentId;
                replyMessage.CurrentReceiverAgent   = messengerAgent;
                Container.ContainerMedia.SendMessage(replyMessage);
            }
        }