Пример #1
0
        private void HandleElectionProposal(ElectionProposalMessage msg)
        {
            Debug.WriteLine("HandleElectionProposal");

            _proposals.Add(msg.SourceID, msg);

            bool ack = false;

            if (_proposals.ContainsKey(_config.ID))
            {
                Debug.WriteLine("Proposals contains my id");
                if (!_proposals[_config.ID].IsGreaterThan(msg))
                {
                    Debug.WriteLine("This id is larger than mine");
                    ack = true;
                }
            }
            else
            {
                Debug.WriteLine("Proposals does not contain my id");
                ack = true;
            }

            if (ack)
            {
                _haveAcked = true;
                var ackMsg = new ElectionAckMessage()
                {
                    SourceID      = _config.ID,
                    DestinationID = msg.SourceID
                };

                Debug.WriteLine(string.Format("TX {0}", ackMsg.Type));
                SendMessage(ackMsg, _group);
            }
        }
Пример #2
0
        private void HandleElectionAck(ElectionAckMessage msg)
        {
            Debug.WriteLine("HandleElectionAck");
            _acksRx++;

            if (_acksRx == _nodeCount - 1)
            {
                Debug.WriteLine(string.Format("Enter if: _acksRx = {0}, _nodeCount = {1}", _acksRx, _nodeCount));
                var myProposal = _proposals[_config.ID];

                bool isLeader = true;
                foreach (var proposal in _proposals.Values)
                {
                    if (proposal.SourceID != _config.ID)
                    {
                        if (proposal.IsGreaterThan(myProposal))
                        {
                            isLeader = false;
                            break;
                        }
                    }
                }

                if (isLeader)
                {
                    var joinEnd = new ElectionEndMessage()
                    {
                        SourceID      = _config.ID,
                        DestinationID = Constants.NULL_DESTINATION,
                    };

                    Debug.WriteLine(string.Format("TX {0}", joinEnd.Type));
                    SendMessage(joinEnd, _group);
                }
            }
        }