Add method to get swarm participants for new leader, fix some comments

This commit is contained in:
DSTO\pivatom
2019-01-14 11:19:27 +10:30
parent 90d096a918
commit 3c978b3ca3

View File

@@ -27,13 +27,13 @@ class Commander:
# If using apache zookeeper this won't be needed. # If using apache zookeeper this won't be needed.
def make_decision(self): def make_decision(self):
# Should change this to be a strategy, for different implementations of making # Should change this to follow strategy pattern, for different implementations of
# a decision on the votes. # making a decision on the votes.
votes = self._votes votes = self._votes
dif_votes = {} dif_votes = {}
for vote in votes: for vote in votes:
# Get the average/max, etc. # Get the count of different votes.
if str(vote) in dif_votes: if str(vote) in dif_votes:
dif_votes[str(vote)] = dif_votes[str(vote)] + 1 dif_votes[str(vote)] = dif_votes[str(vote)] + 1
else: else:
@@ -49,6 +49,7 @@ class Commander:
return max_vote return max_vote
def get_votes(self): def get_votes(self):
# Should consider abstracting these messages to another class for portability.
message = {"type": "reqVote"} message = {"type": "reqVote"}
message_packed = umsgpack.packb(message) message_packed = umsgpack.packb(message)
self._taking_votes = True self._taking_votes = True
@@ -72,7 +73,7 @@ class Commander:
# Commander must have requested their taking votes, and the timeout # Commander must have requested their taking votes, and the timeout
# has not occurred. # has not occurred.
# Only add vote to list if the client has not already voted. # Only add vote to list if the client has not already voted.
if messageDict["client"] not in self._votes.keys: if messageDict["client"] not in self._votes:
self._votes[messageDict["client"]] = messageDict["vote"] self._votes[messageDict["client"]] = messageDict["vote"]
elif messageDict["type"] == "disconnected": elif messageDict["type"] == "disconnected":
@@ -82,4 +83,8 @@ class Commander:
pass pass
def on_connect(self, client, userdata, flags, rc): def on_connect(self, client, userdata, flags, rc):
self.client.subscribe("swarm1/commander") self.client.subscribe("swarm1/commander")
def get_participants(self):
self.client.publish("swarm1/voters")