예제 #1
0
        public void Act(Election message)
        {
            // Every time a process sends or forwards an election message, the process also marks itself as a participant.
            if (ProcessHeader(message.Header, message))
            {
                return;
            }

            //If the UID in the election message is smaller, the process unconditionally forwards the election message in a clockwise direction.
            if (string.Compare(message.Node, _options.Address) < 0)
            {
                isParticipant = true;
                var e = new Election
                {
                    Node   = message.Node,
                    Header = CreateHeader()
                };
                PassMessage(node => node.StartElection(e));
            }

            //If the UID in the election message is larger
            else if (string.Compare(message.Node, _options.Address) > 0)
            {
                //and the process is not yet a participant, the process replaces the UID in the message with its own UID, sends the updated election message in a clockwise direction.
                if (!isParticipant)
                {
                    isParticipant = true;
                    var e = new Election
                    {
                        Node   = _options.Address,
                        Header = CreateHeader()
                    };
                    PassMessage(node => node.StartElection(e));
                }
                //If the UID in the election message is larger, and the process is already a participant
                //(i.e., the process has already sent out an election message with a UID at least as large as its own UID),
                //the process discards the election message.
            }

            else
            {
                //If the UID in the incoming election message is the same as the UID of the process, that process starts acting as the leader.
                //The leader process marks itself as non-participant and sends an elected message to its neighbour announcing its election and UID.
                isParticipant = false;
                leaderId      = _options.Address;
                var e = new Elected
                {
                    Header = CreateHeader(),
                    Node   = _options.Address
                };
                PassMessage(node => node.WonElection(e));
            }
        }
예제 #2
0
 public void Act(Elected message)
 {
     if (ProcessHeader(message.Header, message))
     {
         return;
     }
     //When a process receives an elected message, it marks itself as non-participant, records the elected UID, and forwards the elected message unchanged.
     if (message.Node != _options.Address)
     {
         isParticipant = false;
         leaderId      = message.Node;
         //When the elected message reaches the newly elected leader, the leader discards that message, and the election is over.
         PassMessage(node => node.WonElection(new Elected
         {
             Header = CreateHeader(),
             Node   = message.Node
         }));
     }
 }
 public override string ToString() =>
 $"Elected: {Elected.FullName()}. Other: {Other.FullName()}";