Add implementation for taking votes

This commit is contained in:
DSTO\pivatom
2019-01-14 09:02:26 +10:30
parent 6444d6d3a0
commit 6bdf03abfc

View File

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