Add message implementations and make private variables have underscore
This commit is contained in:
@@ -7,9 +7,9 @@ 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 = []
|
_votes = []
|
||||||
taking_votes = False
|
_taking_votes = False
|
||||||
|
|
||||||
def __init__(self, timeout = 60):
|
def __init__(self, timeout = 60):
|
||||||
'''
|
'''
|
||||||
@@ -47,7 +47,7 @@ class Commander:
|
|||||||
|
|
||||||
return max_vote
|
return max_vote
|
||||||
|
|
||||||
def get_votes():
|
def get_votes(self):
|
||||||
message = {"type": "reqVote"}
|
message = {"type": "reqVote"}
|
||||||
message_packed = umsgpack.packb(message)
|
message_packed = umsgpack.packb(message)
|
||||||
# Publish a message that votes are needed.
|
# Publish a message that votes are needed.
|
||||||
@@ -64,19 +64,21 @@ class Commander:
|
|||||||
|
|
||||||
def on_message(self, client, userdata, message):
|
def on_message(self, client, userdata, message):
|
||||||
messageDict = umsgpack.unpackb()
|
messageDict = umsgpack.unpackb()
|
||||||
|
|
||||||
|
|
||||||
if "type" in messageDict.keys:
|
if "type" in messageDict.keys:
|
||||||
|
# Need to consider that a malicious message may have a type with incorrect subtypes.
|
||||||
if messageDict["type"] == "connect":
|
if messageDict["type"] == "connect":
|
||||||
# Voter just connected/reconnnected.
|
# Voter just connected/reconnnected.
|
||||||
pass
|
if not messageDict["client"] in self._connectedVoters:
|
||||||
|
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.
|
||||||
if messageDict[""]
|
# Only add vote to list if the client has not already voted.
|
||||||
|
pass
|
||||||
|
elif messageDict["type"] == "disconnected":
|
||||||
|
self._connectedVoters.remove(messageDict["type"])
|
||||||
else:
|
else:
|
||||||
# Not a message we should be using.
|
# Not a message we should be using.
|
||||||
NotImplementedError
|
pass
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(self, client, userdata, flags, rc):
|
||||||
self.client.subscribe("swarm1/commander")
|
self.client.subscribe("swarm1/commander")
|
||||||
Reference in New Issue
Block a user