import umsgpack

class Message:
    def __init__(self, sender = "", data = {}}):
        self._sender = sender
        self._data = data

    @property
    def sender(self):
        return self._sender

    @property
    def data(self):
        return self._data

    @property
    def type(self):
        return self._type

    @type.setter
    def type(self, value):
        self._type = value

    def serialise(self):
        return umsgpack.packb({"type":self.type, "sender": self.sender, "data": self.data})

    def deserialise(self, obj):
        unpacked = umsgpack.unpackb(obj)
        self.type = unpacked["type"]
        self.sender = unpacked["sender"]
        self.data = unpacked["data"]
        return self

class RequestLeader(Message):
    _type = "RequestLeader"

class ProposeMessage(Message):
    _type = "Propose"

class ElectionVote(Message):
    _type = "Elect"

class Commit(Message):
    _type = "Commit"

class ConnectSwarm(Message):
    _type = "connect"

class RequestVote(Message):
    _type = "reqvote"

class ConnectResponse(Message):
    _type = "accepted"

class VoterWill(Message):
    _type = "disconnectedvoter"

class CommanderWill(Message):
    _type = "disconnectedcommander"

class SubmitVote(Message):
    _type = "vote"

    @property
    def vote(self):
        return self._data["vote"]

    @vote.setter
    def vote(self, value):
        self._data["vote"] = value

    