Esempio n. 1
0
        public ResponseMessage RequestVote(RequestVoteMessage message)
        {
            CheckFreeze();
            if (message.Term < this.term)
            {
                return(new ResponseMessage(this.term, false, message.SharedId));
            }
            else if (message.Term == this.term && message.LogSize > this.FollowerLog.LogList.Count)
            {
                return(new ResponseMessage(this.term, true, message.SharedId));
            }
            this.term = message.Term;
            lock (votedFor)
            {
                if (!this.votedFor.ContainsKey(this.term))
                {
                    this.votedFor.Add(this.term, null);
                }
                if (votedFor[this.term] == null || message.CandidateId.Equals(votedFor[this.term]))
                {
                    votedFor[this.term] = message.CandidateId;
                    Console.WriteLine("Voted for: " + message.CandidateId + " " + this.term);
                    TimerHelper.RestartTimer(GetElectionTimeout(), ElectionTimer);
                    return(new ResponseMessage(this.term, true, message.SharedId));
                }
            }
            TimerHelper.RestartTimer(GetElectionTimeout(), ElectionTimer);

            return(new ResponseMessage(this.term, false, message.SharedId));
        }
Esempio n. 2
0
        private IAsyncResult RemoteAsyncCallRequestVote(Node otherNode, RequestVoteMessage message)
        {
            AsyncRequestVoteDelegate RemoteDel = new AsyncRequestVoteDelegate(otherNode.RequestVote);
            AsyncCallback            callback  = new AsyncCallback(VerifyVotes);
            IAsyncResult             RemAr     = RemoteDel.BeginInvoke(message, callback, null);

            return(RemAr);
        }
Esempio n. 3
0
        public ResponseMessage RequestVote(RequestVoteMessage message)
        {
            CheckFreeze();

            try
            {
                if (message.Term < this.term)
                {
                    return(new ResponseMessage(this.term, false, message.SharedId));
                }
                else if (message.Term > this.term)
                {
                    this.term = message.Term;
                    node.SetState(new Follower(node, this.term, this.leaderLog.log));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return(new ResponseMessage(this.term, false, message.SharedId));
        }
Esempio n. 4
0
        public ResponseMessage RequestVote(RequestVoteMessage message)
        {
            CheckFreeze();

            try
            {
                Console.WriteLine("Received request from: " + message.CandidateId + " " + term);
                //1.Reply false if term < currentTerm

                if (message.Term < this.term)
                {
                    return(new ResponseMessage(this.term, false, message.SharedId));
                }
                else if (message.Term == this.term && message.LogSize > this.CandidateLog.LogList.Count)
                {
                    return(new ResponseMessage(this.term, true, message.SharedId));
                }
                this.term = message.Term;
                lock (votedFor)
                {
                    if (!this.votedFor.ContainsKey(this.term))
                    {
                        this.votedFor.Add(this.term, null);
                    }

                    if (votedFor[this.term] == null || message.CandidateId.Equals(votedFor[this.term]))
                    {
                        votedFor[this.term] = message.CandidateId;
                        leaderFound         = true;
                        this.node.SetState(new Follower(this.node, this.term, this.CandidateLog, votedFor));
                        return(new ResponseMessage(this.term, true, message.SharedId));
                    }
                    return(new ResponseMessage(this.term, false, message.SharedId));
                }
            }
            catch (Exception e) {}
            return(new ResponseMessage(this.term, false, message.SharedId));
        }