# Path: nanoservice/reqrep.py
# class Responder(Endpoint, Process):
#     """ A service which responds to requests """
# 
#     # 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.REP)
#         encoder = encoder or MsgPackEncoder()
# 
#         super(Responder, self).__init__(
#             socket, address, bind, encoder, authenticator, timeouts)
# 
#         self.methods = {}
#         self.descriptions = {}
# 
#     def execute(self, method, args, ref):
#         """ Execute the method with args """
# 
#         response = {'result': None, 'error': None, 'ref': ref}
#         fun = self.methods.get(method)
#         if not fun:
#             response['error'] = 'Method `{}` not found'.format(method)
#         else:
#             try:
#                 response['result'] = fun(*args)
#             except Exception as exception:
#                 logging.error(exception, exc_info=1)
#                 response['error'] = str(exception)
#         return response
# 
#     def register(self, name, fun, description=None):
#         """ Register function on this service """
#         self.methods[name] = fun
#         self.descriptions[name] = description
# 
#     @classmethod
#     def parse(cls, payload):
#         """ Parse client request """
#         try:
#             method, args, ref = payload
#         except Exception as exception:
#             raise RequestParseError(exception)
#         else:
#             return method, args, ref
# 
#     # pylint: disable=logging-format-interpolation
#     def process(self):
#         """ Receive data from socket and process request """
# 
#         response = None
# 
#         try:
#             payload = self.receive()
#             method, args, ref = self.parse(payload)
#             response = self.execute(method, args, ref)
# 
#         except AuthenticateError as exception:
#             logging.error(
#                 'Service error while authenticating request: {}'
#                 .format(exception), exc_info=1)
# 
#         except AuthenticatorInvalidSignature as exception:
#             logging.error(
#                 'Service error while authenticating request: {}'
#                 .format(exception), exc_info=1)
# 
#         except DecodeError as exception:
#             logging.error(
#                 'Service error while decoding request: {}'
#                 .format(exception), exc_info=1)
# 
#         except RequestParseError as exception:
#             logging.error(
#                 'Service error while parsing request: {}'
#                 .format(exception), exc_info=1)
# 
#         else:
#             logging.debug('Service received payload: {}'.format(payload))
# 
#         if response:
#             self.send(response)
#         else:
#             self.send('')


import logging
from nanoservice import Responder


def greet(name):
    return 'Hello {}'.format(name)


def add(x, y):
    return x + y

s = Responder('ipc:///tmp/service.sock')
s.register('greet', greet)

# Path: nanoservice/reqrep.py
# class Responder(Endpoint, Process):
#     """ A service which responds to requests """
# 
#     # 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.REP)
#         encoder = encoder or MsgPackEncoder()
# 
#         super(Responder, self).__init__(
#             socket, address, bind, encoder, authenticator, timeouts)
# 
#         self.methods = {}
#         self.descriptions = {}
# 
#     def execute(self, method, args, ref):
#         """ Execute the method with args """
# 
#         response = {'result': None, 'error': None, 'ref': ref}
#         fun = self.methods.get(method)
#         if not fun:
#             response['error'] = 'Method `{}` not found'.format(method)
#         else:
#             try:
#                 response['result'] = fun(*args)
#             except Exception as exception:
#                 logging.error(exception, exc_info=1)
#                 response['error'] = str(exception)
#         return response
# 
#     def register(self, name, fun, description=None):
#         """ Register function on this service """
#         self.methods[name] = fun
#         self.descriptions[name] = description
# 
#     @classmethod
#     def parse(cls, payload):
#         """ Parse client request """
#         try:
#             method, args, ref = payload
#         except Exception as exception:
#             raise RequestParseError(exception)
#         else:
#             return method, args, ref
# 
#     # pylint: disable=logging-format-interpolation
#     def process(self):
#         """ Receive data from socket and process request """
# 
#         response = None
# 
#         try:
#             payload = self.receive()
#             method, args, ref = self.parse(payload)
#             response = self.execute(method, args, ref)
# 
#         except AuthenticateError as exception:
#             logging.error(
#                 'Service error while authenticating request: {}'
#                 .format(exception), exc_info=1)
# 
#         except AuthenticatorInvalidSignature as exception:
#             logging.error(
#                 'Service error while authenticating request: {}'
#                 .format(exception), exc_info=1)
# 
#         except DecodeError as exception:
#             logging.error(
#                 'Service error while decoding request: {}'
#                 .format(exception), exc_info=1)
# 
#         except RequestParseError as exception:
#             logging.error(
#                 'Service error while parsing request: {}'
#                 .format(exception), exc_info=1)
# 
#         else:
#             logging.debug('Service received payload: {}'.format(payload))
# 
#         if response:
#             self.send(response)
#         else:
#             self.send('')


# Path: examples/req_rep/simple_service.py
import logging
from nanoservice import Responder



def greet(name):
    return 'Hello {}'.format(name)


def add(x, y):
    return x + y

s = Responder('ipc:///tmp/service.sock')
s.register('greet', greet)

s.register('add', add)