# Path: taskloaf/protocol.py
# class Protocol:
#     def __init__(self):
#         self.msg_types = []
# 
#     def add_msg_type(self, name, *, serializer=CloudPickleMsg, handler=None):
# 
#         type_code = len(self.msg_types)
#         setattr(self, name, type_code)
#         self.msg_types.append((serializer, handler, name))
#         return type_code
# 
#     def encode(self, source_name, type_code, *args):
#         serializer = self.msg_types[type_code][0]
#         m = serializer.serialize(*args)
#         m.typeCode = type_code
#         m.sourceName = source_name
#         return memoryview(m.to_bytes())
# 
#     def handle(self, msg_buf, *args, **kwargs):
#         msg = taskloaf.message_capnp.Message.from_bytes(msg_buf)
#         serializer, handler, _ = self.msg_types[msg.typeCode]
#         data = serializer.deserialize(msg)
#         log.info(
#             f"handling from {msg.sourceName}"
#             f"a {self.get_name(msg.typeCode)} with data: {str(data)}"
#         )
#         handler(data, *args, **kwargs)
# 
#     def get_name(self, type_code):
#         return self.msg_types[type_code][2]


from .protocol import Protocol
import logging
class JoinMeetMessenger:
    """
    A cluster view is a pairing of a communicator, ownership of the protocol,
    and the glue to attach the two. That includes control over finding other
    messengers over the network.

        MEET = send a message asking to JOIN
               receiver sends a JOIN reply

        JOIN = send a message with tuples of form (name, hostname, port) for
               all friends receiver then JOIN all new friends (possibly
               including the one they received the JOIN from)

        A worker will MEET then receive the JOIN replies and JOIN all the new
        friends.

        A client will MEET then receive the JOIN replies and MEET all the new
        friends. As a result, a client learns about the whole cluster but the
        cluster doesn't know about the client or send the client info. Is
        this a good idea? Then how to send info out to the client? By
        hostname/port?
    """

    def __init__(self, name, comm, should_join):
        self.name = name
        self.comm = comm
        self.should_join = should_join

        self.endpts = dict()


# Path: taskloaf/protocol.py
# class Protocol:
#     def __init__(self):
#         self.msg_types = []
# 
#     def add_msg_type(self, name, *, serializer=CloudPickleMsg, handler=None):
# 
#         type_code = len(self.msg_types)
#         setattr(self, name, type_code)
#         self.msg_types.append((serializer, handler, name))
#         return type_code
# 
#     def encode(self, source_name, type_code, *args):
#         serializer = self.msg_types[type_code][0]
#         m = serializer.serialize(*args)
#         m.typeCode = type_code
#         m.sourceName = source_name
#         return memoryview(m.to_bytes())
# 
#     def handle(self, msg_buf, *args, **kwargs):
#         msg = taskloaf.message_capnp.Message.from_bytes(msg_buf)
#         serializer, handler, _ = self.msg_types[msg.typeCode]
#         data = serializer.deserialize(msg)
#         log.info(
#             f"handling from {msg.sourceName}"
#             f"a {self.get_name(msg.typeCode)} with data: {str(data)}"
#         )
#         handler(data, *args, **kwargs)
# 
#     def get_name(self, type_code):
#         return self.msg_types[type_code][2]


# Path: taskloaf/messenger.py
from .protocol import Protocol
import logging

class JoinMeetMessenger:
    """
    A cluster view is a pairing of a communicator, ownership of the protocol,
    and the glue to attach the two. That includes control over finding other
    messengers over the network.

        MEET = send a message asking to JOIN
               receiver sends a JOIN reply

        JOIN = send a message with tuples of form (name, hostname, port) for
               all friends receiver then JOIN all new friends (possibly
               including the one they received the JOIN from)

        A worker will MEET then receive the JOIN replies and JOIN all the new
        friends.

        A client will MEET then receive the JOIN replies and MEET all the new
        friends. As a result, a client learns about the whole cluster but the
        cluster doesn't know about the client or send the client info. Is
        this a good idea? Then how to send info out to the client? By
        hostname/port?
    """

    def __init__(self, name, comm, should_join):
        self.name = name
        self.comm = comm
        self.should_join = should_join

        self.endpts = dict()


        self.protocol = Protocol()