class RPCServer(object):
    def __init__(self,port,host=''):
        self.handler = RPCHandler(self)
        self.port = port
        self.host = host
        self.tcpserver = None
        self.serviceMap = {}

    def registerService(self, service):
        '''Register an RPC service.'''
        self.serviceMap[service.GetDescriptor().full_name] = service

    def run(self):
        log.info('Running server on port %d' % self.port)
        rpcprotocol = RPCProtocol()
        self.tcpserver = TCPServer(self.handler,rpcprotocol)
        self.tcpserver.bind(self.port, self.host)
        self.tcpserver.start(0)
        ioloop.IOLoop.instance().start()

    def stop(self):
        self.tcpserver.stop()
        ioloop.IOLoop.instance().stop()
# An example showing the use of httpserver.TCPServer
# Reverses the data received from the client and send it back

import sys

# Import httpserver in the python import path
sys.path.insert(0, "./httpserver")

from tcpserver import TCPServer


def reverse_data(queue, data):
    queue.put(data[::-1])
    print(data)


if __name__ == '__main__':
    server = TCPServer('127.0.0.1', 8000, callback=reverse_data)
    server.start()
    server.run()