[zeromq-dev] design communication protocols

andrea crotti andrea.crotti.0 at gmail.com
Fri Aug 17 18:20:51 CEST 2012


It's starting to slowly make more sense (even if I'm far from
convinced), the following code for example has a protocol object that
takes a message, and knows how to serialise and unserialise.

Usign a simple list I go over all the possible message types to
construct the right type, and potentially in the message types I can do
more fancy parsing..

The thing I don't understand now for example is how would I actually
convert automagically this code to be tested via SPIN?
Because here there still is no logic like "if get X answer Y to the
channel".

I'm also thinking that even if not the same with heavy unit testing I
might even not need a real model checker, also because there might be
bugs in the conversion code -> SPIN, while unit tests run on actual
code.

But in case I wanted that then I would need to pass the communication
channels to the protocol somehow, and not sure how I should do that, any
advice?


class Message:
    def __init__(self, message):
        self.message = message

    def dump(self):
        return self.message

    def __eq__(self, other):
        return self.message == other.message


class TaskStarting(Message):
    key = 'T_START'
    def __init__(self, message):
        super(TaskStarting, self).__init__(message)
        # create some extra fields from the message


class TaskOver(Message):
    key = 'T_OVER'



class WorkerSinkProtocol:
    """Communication protocol to communicate between the worker and
    the sink, sending results and the actual status.
    TODO:Does it make sense to send also the actual status?
    """
    ACTIONS = [TaskStarting, TaskOver]

    def __init__(self, message):
        self.message = message

    def __eq__(self, other):
        return self.message == other.message

    def serialise(self):
        return DELIMITER.join([type(self.message).key, self.message.dump()])

    @classmethod
    def unserialise(cls, st):
        act, msg = st.split(DELIMITER)
        for a in cls.ACTIONS:
            if a.key == act:
                return cls(a(msg))



More information about the zeromq-dev mailing list