Add ability to propogate result of vote and making a vote.
This commit is contained in:
@@ -1,33 +1,19 @@
|
||||
import json
|
||||
from DecisionSystem.messages import ConnectSwarm, SubmitVote, Message, deserialise, RequestVote
|
||||
from DecisionSystem.messages import ConnectSwarm, SubmitVote, Message, deserialise, RequestVote, ClientVoteRequest, VoteResult
|
||||
from multiprocessing import Pool
|
||||
from messenger import Messenger
|
||||
|
||||
class BallotVoter:
|
||||
def __init__(self, on_vote, messenger: Messenger):
|
||||
|
||||
# Load config file
|
||||
cfg = None
|
||||
with open('config.json') as json_config:
|
||||
cfg = json.load(json_config)
|
||||
self._cfg = cfg
|
||||
|
||||
def __init__(self, on_vote, handle_agreement, messenger: Messenger):
|
||||
self.messenger = messenger
|
||||
self.messenger.add_message_callback(self.on_message)
|
||||
self.messenger.add_connect(self.on_connect)
|
||||
self.on_vote = on_vote
|
||||
self.handle_agreement = handle_agreement
|
||||
|
||||
def on_connect(self, rc):
|
||||
print("Connected with result code " + str(rc))
|
||||
|
||||
# Should subscribes be handled by the messenger?
|
||||
#self.client.subscribe('swarm1/voters', qos=1)
|
||||
|
||||
# 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"}
|
||||
|
||||
# Tell commander we are now connected.
|
||||
self.send_connect()
|
||||
|
||||
@@ -41,12 +27,8 @@ class BallotVoter:
|
||||
self.submit_vote()
|
||||
elif messageD.type == "listening":
|
||||
self.send_connect()
|
||||
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
|
||||
elif messageD.type == VoteResult.type:
|
||||
self.handle_agreement(messageD.data["vote"])
|
||||
|
||||
def submit_vote(self):
|
||||
v = self.on_vote()
|
||||
@@ -59,15 +41,11 @@ class BallotVoter:
|
||||
self.messenger.broadcast_message(self.messenger.swarm, vote.serialise())
|
||||
print('published vote')
|
||||
|
||||
def submit_vote_multicore(self):
|
||||
'''
|
||||
Uses multiple processes to work on multiple frames at the same time
|
||||
to take an average for this voter, or if it could not find a hand
|
||||
in one frame but can in another.
|
||||
'''
|
||||
pass
|
||||
|
||||
def send_connect(self):
|
||||
# Send a connected message to let any commanders know that
|
||||
# it is available.
|
||||
self.messenger.broadcast_message(self.messenger.swarm, ConnectSwarm(self.messenger.id).serialise())
|
||||
self.messenger.broadcast_message(self.messenger.swarm, ConnectSwarm(self.messenger.id).serialise())
|
||||
|
||||
def request_vote(self):
|
||||
"""Sends a request to the leader to start collecting votes."""
|
||||
self.messenger.broadcast_message(self.messenger.swarm, ClientVoteRequest(self.messenger.id).serialise())
|
||||
Reference in New Issue
Block a user