# Path: src/lifeRaft/states/voter.py
# class Voter(State):
# 
#     def __init__(self, timeout=1.0):
#         super(Voter, self).__init__(timeout=timeout)
#         self._last_vote = None
# 
#     def on_vote_request(self, message):
#         if(self._last_vote is None and
#            message.data["lastLogIndex"] >= self._server._lastLogIndex):
#             self._last_vote = message.sender
#             self._send_vote_response_message(message)
#         else:
#             self._send_vote_response_message(message, yes=False)
# 
#         return self, None
# 
#     def _send_vote_response_message(self, msg, yes=True):
#         voteResponse = RequestVoteResponseMessage(
#             self._server._name,
#             msg.sender,
#             msg.term,
#             {"response": yes})
#         self._server.send_message_response(voteResponse)
#     
#     def on_client_message(self, message):
#         ''' return the message response for the client '''
#         return ClientFollowerResponse(message.sender, self._last_vote)
# 
# Path: src/lifeRaft/messages/request_vote.py
# class RequestVoteMessage(BaseMessage):
# 
#     _type = BaseMessage.RequestVote
# 
#     def __init__(self, sender, receiver, term, data):
#         BaseMessage.__init__(self, sender, receiver, term, data)


from .voter import Voter
from ..messages.request_vote import RequestVoteMessage
                from .leader import Leader
        from .follower import Follower

    def on_leader_timeout(self):
        ''' this event triggers an election '''
        self._votes = {}
        self._start_election()
        return self, None

    def on_vote_request(self, message):
        return self, None

    def on_vote_received(self, message):
        if message.sender not in self._votes:
            self._votes[message.sender] = message

            if(len(self._votes.keys()) > (self._server._total_nodes - 1) / 2):
                leader = Leader(timeout=self._timeout)
                leader.set_server(self._server)
                leader._last_vote = self._last_vote
                leader._send_heart_beat()
                return leader, None
        return self, None

    def on_append_entries(self, message):
        ''' revert to follower, call on_append_entries '''
        follower = Follower(timeout=self._timeout)
        follower.set_server(self._server)
        return follower.on_append_entries(message)

    def _start_election(self):
        self._server._currentTerm += 1

# Path: src/lifeRaft/states/voter.py
# class Voter(State):
# 
#     def __init__(self, timeout=1.0):
#         super(Voter, self).__init__(timeout=timeout)
#         self._last_vote = None
# 
#     def on_vote_request(self, message):
#         if(self._last_vote is None and
#            message.data["lastLogIndex"] >= self._server._lastLogIndex):
#             self._last_vote = message.sender
#             self._send_vote_response_message(message)
#         else:
#             self._send_vote_response_message(message, yes=False)
# 
#         return self, None
# 
#     def _send_vote_response_message(self, msg, yes=True):
#         voteResponse = RequestVoteResponseMessage(
#             self._server._name,
#             msg.sender,
#             msg.term,
#             {"response": yes})
#         self._server.send_message_response(voteResponse)
#     
#     def on_client_message(self, message):
#         ''' return the message response for the client '''
#         return ClientFollowerResponse(message.sender, self._last_vote)
# 
# Path: src/lifeRaft/messages/request_vote.py
# class RequestVoteMessage(BaseMessage):
# 
#     _type = BaseMessage.RequestVote
# 
#     def __init__(self, sender, receiver, term, data):
#         BaseMessage.__init__(self, sender, receiver, term, data)


# Path: src/lifeRaft/states/candidate.py
from .voter import Voter
from ..messages.request_vote import RequestVoteMessage
                from .leader import Leader
        from .follower import Follower


    def on_leader_timeout(self):
        ''' this event triggers an election '''
        self._votes = {}
        self._start_election()
        return self, None

    def on_vote_request(self, message):
        return self, None

    def on_vote_received(self, message):
        if message.sender not in self._votes:
            self._votes[message.sender] = message

            if(len(self._votes.keys()) > (self._server._total_nodes - 1) / 2):
                leader = Leader(timeout=self._timeout)
                leader.set_server(self._server)
                leader._last_vote = self._last_vote
                leader._send_heart_beat()
                return leader, None
        return self, None

    def on_append_entries(self, message):
        ''' revert to follower, call on_append_entries '''
        follower = Follower(timeout=self._timeout)
        follower.set_server(self._server)
        return follower.on_append_entries(message)

    def _start_election(self):
        self._server._currentTerm += 1

        election = RequestVoteMessage(