From 6bdf03abfc0a850068389c7c0f5554407d18951a Mon Sep 17 00:00:00 2001 From: "DSTO\\pivatom" Date: Mon, 14 Jan 2019 09:02:26 +1030 Subject: [PATCH] Add implementation for taking votes --- DecisionSystem/CentralisedDecision/commander.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/DecisionSystem/CentralisedDecision/commander.py b/DecisionSystem/CentralisedDecision/commander.py index 7df9b01..8d8f1d4 100644 --- a/DecisionSystem/CentralisedDecision/commander.py +++ b/DecisionSystem/CentralisedDecision/commander.py @@ -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. - # Only add vote to list if the client has not already voted. - pass + 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. + if messageDict["client"] not in self._votes.keys: + self._votes[messageDict["client"]] = messageDict["vote"] + elif messageDict["type"] == "disconnected": self._connectedVoters.remove(messageDict["type"]) else: