# Path: nanoservice/pubsub.py
# class Subscriber(Endpoint, Process):
#     """ A Subscriber executes various functions in response to
#     different subscriptions it is subscribed to """
# 
#     # pylint: disable=too-many-arguments
#     # pylint: disable=no-member
#     def __init__(self, address, encoder=None, authenticator=None,
#                  socket=None, bind=True, timeouts=(None, None)):
# 
#         # Defaults
#         socket = socket or nanomsg.Socket(nanomsg.SUB)
#         encoder = encoder or MsgPackEncoder()
# 
#         super(Subscriber, self).__init__(
#             socket, address, bind, encoder, authenticator, timeouts)
# 
#         self.methods = {}
#         self.descriptions = {}
# 
#     def parse(self, subscription):
#         """ Fetch the function registered for a certain subscription """
# 
#         for name in self.methods:
#             tag = bytes(name.encode('utf-8'))
#             if subscription.startswith(tag):
#                 fun = self.methods.get(name)
#                 message = subscription[len(tag):]
#                 return tag, message, fun
#         return None, None, None
# 
#     def register(self, name, fun, description=None):
#         raise SubscriberError('Operation not allowed on this type of service')
# 
#     # pylint: disable=no-member
#     def subscribe(self, tag, fun, description=None):
#         """ Subscribe to something and register a function """
#         self.methods[tag] = fun
#         self.descriptions[tag] = description
#         self.socket.set_string_option(nanomsg.SUB, nanomsg.SUB_SUBSCRIBE, tag)
# 
#     # pylint: disable=logging-format-interpolation
#     # pylint: disable=duplicate-code
#     def process(self):
#         """ Receive a subscription from the socket and process it """
# 
#         subscription = None
#         result = None
# 
#         try:
#             subscription = self.socket.recv()
# 
#         except AuthenticateError as exception:
#             logging.error(
#                 'Subscriber error while authenticating request: {}'
#                 .format(exception), exc_info=1)
# 
#         except AuthenticatorInvalidSignature as exception:
#             logging.error(
#                 'Subscriber error while authenticating request: {}'
#                 .format(exception), exc_info=1)
# 
#         except DecodeError as exception:
#             logging.error(
#                 'Subscriber error while decoding request: {}'
#                 .format(exception), exc_info=1)
# 
#         except RequestParseError as exception:
#             logging.error(
#                 'Subscriber error while parsing request: {}'
#                 .format(exception), exc_info=1)
# 
#         else:
#             logging.debug(
#                 'Subscriber received payload: {}'
#                 .format(subscription))
# 
#         _tag, message, fun = self.parse(subscription)
#         message = self.verify(message)
#         message = self.decode(message)
# 
#         try:
#             result = fun(message)
#         except Exception as exception:
#             logging.error(exception, exc_info=1)
# 
#         # Return result to check successful execution of `fun` when testing
#         return result


from nanoservice import Subscriber


def log(message):
    print('Subscriber got new message: {}'.format(message))

s = Subscriber('ipc:///tmp/pub1.sock', bind=False)
s.socket.connect('ipc:///tmp/pub2.sock')
s.subscribe('log', log)

# Path: nanoservice/pubsub.py
# class Subscriber(Endpoint, Process):
#     """ A Subscriber executes various functions in response to
#     different subscriptions it is subscribed to """
# 
#     # pylint: disable=too-many-arguments
#     # pylint: disable=no-member
#     def __init__(self, address, encoder=None, authenticator=None,
#                  socket=None, bind=True, timeouts=(None, None)):
# 
#         # Defaults
#         socket = socket or nanomsg.Socket(nanomsg.SUB)
#         encoder = encoder or MsgPackEncoder()
# 
#         super(Subscriber, self).__init__(
#             socket, address, bind, encoder, authenticator, timeouts)
# 
#         self.methods = {}
#         self.descriptions = {}
# 
#     def parse(self, subscription):
#         """ Fetch the function registered for a certain subscription """
# 
#         for name in self.methods:
#             tag = bytes(name.encode('utf-8'))
#             if subscription.startswith(tag):
#                 fun = self.methods.get(name)
#                 message = subscription[len(tag):]
#                 return tag, message, fun
#         return None, None, None
# 
#     def register(self, name, fun, description=None):
#         raise SubscriberError('Operation not allowed on this type of service')
# 
#     # pylint: disable=no-member
#     def subscribe(self, tag, fun, description=None):
#         """ Subscribe to something and register a function """
#         self.methods[tag] = fun
#         self.descriptions[tag] = description
#         self.socket.set_string_option(nanomsg.SUB, nanomsg.SUB_SUBSCRIBE, tag)
# 
#     # pylint: disable=logging-format-interpolation
#     # pylint: disable=duplicate-code
#     def process(self):
#         """ Receive a subscription from the socket and process it """
# 
#         subscription = None
#         result = None
# 
#         try:
#             subscription = self.socket.recv()
# 
#         except AuthenticateError as exception:
#             logging.error(
#                 'Subscriber error while authenticating request: {}'
#                 .format(exception), exc_info=1)
# 
#         except AuthenticatorInvalidSignature as exception:
#             logging.error(
#                 'Subscriber error while authenticating request: {}'
#                 .format(exception), exc_info=1)
# 
#         except DecodeError as exception:
#             logging.error(
#                 'Subscriber error while decoding request: {}'
#                 .format(exception), exc_info=1)
# 
#         except RequestParseError as exception:
#             logging.error(
#                 'Subscriber error while parsing request: {}'
#                 .format(exception), exc_info=1)
# 
#         else:
#             logging.debug(
#                 'Subscriber received payload: {}'
#                 .format(subscription))
# 
#         _tag, message, fun = self.parse(subscription)
#         message = self.verify(message)
#         message = self.decode(message)
# 
#         try:
#             result = fun(message)
#         except Exception as exception:
#             logging.error(exception, exc_info=1)
# 
#         # Return result to check successful execution of `fun` when testing
#         return result


# Path: scenarios/one-sub-to-many-pubs/a-multisub.py
from nanoservice import Subscriber



def log(message):
    print('Subscriber got new message: {}'.format(message))

s = Subscriber('ipc:///tmp/pub1.sock', bind=False)
s.socket.connect('ipc:///tmp/pub2.sock')
s.subscribe('log', log)

s.start()