Add implementation for taking votes
This commit is contained in:
@@ -8,7 +8,8 @@ class Commander:
|
||||
# Stores voters that connect to maintain a majority.
|
||||
# Voters who do not vote in latest round are removed.
|
||||
_connectedVoters = []
|
||||
_votes = []
|
||||
# Dict has format: {clientId: vote}
|
||||
_votes = {}
|
||||
_taking_votes = False
|
||||
|
||||
def __init__(self, timeout = 60):
|
||||
@@ -50,9 +51,11 @@ class Commander:
|
||||
def get_votes(self):
|
||||
message = {"type": "reqVote"}
|
||||
message_packed = umsgpack.packb(message)
|
||||
self._taking_votes = True
|
||||
# Publish a message that votes are needed.
|
||||
self.client.publish("swarm1/voters", message_packed, qos)
|
||||
time.sleep(self.timeout)
|
||||
self._taking_votes = False
|
||||
self.make_decision()
|
||||
|
||||
# Shouldn't be needed anymore.
|
||||
@@ -72,8 +75,13 @@ class Commander:
|
||||
self._connectedVoters.append(messageDict["client"])
|
||||
elif messageDict["type"] == "vote":
|
||||
# Voter is sending in their vote.
|
||||
if self._taking_votes:
|
||||
# Commander must have requested their taking votes, and the timeout
|
||||
# has not occurred.
|
||||
# Only add vote to list if the client has not already voted.
|
||||
pass
|
||||
if messageDict["client"] not in self._votes.keys:
|
||||
self._votes[messageDict["client"]] = messageDict["vote"]
|
||||
|
||||
elif messageDict["type"] == "disconnected":
|
||||
self._connectedVoters.remove(messageDict["type"])
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user