Add config file usage and messages module usage
This commit is contained in:
@@ -1,11 +1,22 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
import umsgpack
|
||||
import json
|
||||
from DecisionSystem.messages import ConnectSwarm, SubmitVote, Message, deserialise, RequestVote
|
||||
|
||||
class BallotVoter:
|
||||
def __init__(self, on_vote):
|
||||
|
||||
# Load config file
|
||||
cfg = None
|
||||
with open('../config.json') as json_config:
|
||||
cfg = json.load(json_config)
|
||||
self._cfg = cfg
|
||||
|
||||
self.client = mqtt.Client()
|
||||
self.client.on_connect = self.on_connect
|
||||
self.client.on_message = self.on_message
|
||||
# id is generated automatically.
|
||||
self.client.connect('172.16.13.128')
|
||||
self.client.connect(cfg["mqtt"]["host"], cfg["mqtt"]["port"], cfg["mqtt"]["timeout"])
|
||||
self.client.loop_start()
|
||||
self.on_vote = on_vote
|
||||
|
||||
def on_connect(self, client, userdata, flags, rc):
|
||||
@@ -13,33 +24,41 @@ class BallotVoter:
|
||||
|
||||
self.client.subscribe('swarm1/voters', qos=1)
|
||||
|
||||
# Create the message to send that it is now part of the swarm.
|
||||
|
||||
# Need to set a will as well to broadcast on unexpected disconnection, so commander
|
||||
# knows it is no longer part of the set of voters.
|
||||
# Leaving this until core centralised system is working.
|
||||
#will_message = {"type": "UDisconnect"}
|
||||
|
||||
self.send_connect()
|
||||
# Tell commander we are now connected.
|
||||
# self.send_connect()
|
||||
|
||||
def on_message(self, client, userdata, message):
|
||||
messageDict = umsgpack.unpackb(message)
|
||||
if "type" in messageDict.keys:
|
||||
print("Message Received!")
|
||||
messageD = deserialise(message.payload)
|
||||
print("Message Type: " + messageD.type)
|
||||
# Ok message.
|
||||
if messageDict["type"] == "reqVote":
|
||||
if messageD.type == RequestVote().type:
|
||||
print('Received vote message')
|
||||
self.submit_vote()
|
||||
if messageDict["type"] == "listening":
|
||||
elif messageD.type == "listening":
|
||||
self.send_connect()
|
||||
else:
|
||||
# Bad message.
|
||||
elif messageD.type == "disconnectedcommander":
|
||||
# Elect new leader...
|
||||
# Might just use raft's method for electing a leader,
|
||||
# by doing a random timeout then choosing itself as the candidate,
|
||||
# and collects who is part of the swarm. This may need to be another class...
|
||||
pass
|
||||
|
||||
def submit_vote(self):
|
||||
binary = umsgpack.packb("type": "vote", "client": self.client._client_id, "vote": self.on_vote())
|
||||
self.client.publish('swarm1/commander', binary)
|
||||
v = self.on_vote()
|
||||
print("Got Vote")
|
||||
vote = SubmitVote(v, self.client._client_id)
|
||||
print('created vote')
|
||||
self.client.publish('swarm1/commander', vote.serialise())
|
||||
print('published vote')
|
||||
|
||||
def send_connect(self):
|
||||
# Send a connected message to let any commanders know that
|
||||
# it is available.
|
||||
binary = umsgpack.packb({"type": "connect", "id": self.client._client_id})
|
||||
self.client.publish("swarm/commander", binary)
|
||||
self.client.publish("swarm1/commander", ConnectSwarm(self.client._client_id).serialise())
|
||||
|
||||
Reference in New Issue
Block a user