def __get_processor(self): ''' get muti processor instance ''' #muti processor mprocessor = TMultiplexedProcessor.TMultiplexedProcessor() #register handlers for handler in self.server_config.get_service_processors(): #get handler super class bases = handler.__bases__ if len(bases) <= 0: raise HandlerException(handler.__name__) #get iface name iface_string = util.get_clazz_string(bases[0]) if not iface_string.endswith("Iface"): raise HandlerException(handler.__name__) #get Processor split = iface_string.split(".") name = split[-2] module = __import__(".".join(split[:-1]), globals={}, locals={}, fromlist=[name]) processor = getattr(module, "Processor") #register processor mprocessor.registerProcessor(name, processor(handler())) #return return mprocessor
def _initialize_thrift_service(self): """ Initialize the thrift server. """ mux_processor = TMultiplexedProcessor.TMultiplexedProcessor() for plugin in thrift_services(): self._logger.info("Load thrift services %s (num_threads: %d)", plugin.name, plugin.num_threads) handler = plugin.handler processor = plugin.service.Processor(handler) mux_processor.registerProcessor(plugin.name, processor) self._logger.info( "Initialize SSLSocket using certfile=%s, keyfile=%s, capath=%s" % (SSL_CERT_FILE, SSL_KEY_FILE, CA_PATH)) transport = TSSLSocket.TSSLServerSocket(port=self._config.host_port, certfile=SSL_CERT_FILE, keyfile=SSL_KEY_FILE, capath=CA_PATH, ciphers=SSL_CIPHERS) protocol_factory = TCompactProtocol.TCompactProtocolFactory() server = TNonblockingServer.TNonblockingServer( mux_processor, transport, protocol_factory, protocol_factory, self._config.host_service_threads) self._server = server
def start_server(self): """ Registers the host and root scheduler processors and starts the thrift server. """ mux_processor = TMultiplexedProcessor.TMultiplexedProcessor() if self._hostHandler: self._register_processer("Host", mux_processor) if self._leafSchedulerHandler: self._register_processer("Scheduler", mux_processor) if self._rootSchedulerHandler: self._register_processer("RootScheduler", mux_processor) if self._enable_ping: self._register_processer("AgentControl", mux_processor) transport = TSocket.TServerSocket(self._address, self._port) protocol_factory = TCompactProtocol.TCompactProtocolFactory() self._server = TNonblockingServer.TNonblockingServer( mux_processor, transport, protocol_factory, protocol_factory) self._thread = threading.Thread(target=self._server.serve) self._thread.daemon = True self._thread.start() self._server_started = True self._logger.info( "[ThriftServer] Thrift server started. Address: {0} Port: {1}". format(self._address, self._port))
def echo_server(): mp = TMultiplexedProcessor.TMultiplexedProcessor() mp.registerProcessor("echo", Echoer.Processor(EchoHandler())) transport = TSocket.TServerSocket(port=PORT) tf = TTransport.TFramedTransportFactory() pf = TCompactProtocol.TCompactProtocolFactory() return TServer.TThreadedServer(mp, transport, tf, pf, daemon=True)
def create_server(server_reactor, port): model_base.setup_db() mux = TMultiplexedProcessor.TMultiplexedProcessor() for service_name, service in services.handlers.iteritems(): processor = service.processor() mux.registerProcessor(service_name, processor(service())) server_factory = TTwisted.ThriftServerFactory( processor=mux, iprot_factory=TBinaryProtocol.TBinaryProtocolFactory() ) server_endpoint = endpoints.TCP4ServerEndpoint(server_reactor, port) server_endpoint.listen(server_factory) server_reactor.run()
def _initialize_thrift_service(self): """ Initialize the thrift server. """ mux_processor = TMultiplexedProcessor.TMultiplexedProcessor() for plugin in thrift_services(): self._logger.info("Load thrift services %s (num_threads: %d)", plugin.name, plugin.num_threads) handler = plugin.handler processor = plugin.service.Processor(handler) mux_processor.registerProcessor(plugin.name, processor) transport = TSocket.TServerSocket(port=self._config.host_port) protocol_factory = TCompactProtocol.TCompactProtocolFactory() server = TNonblockingServer.TNonblockingServer( mux_processor, transport, protocol_factory, protocol_factory, self._config.host_service_threads) self._server = server
def main(cfg): if cfg.unix: if cfg.addr == "": sys.exit("invalid listener unix domain socket: {}".format(cfg.addr)) else: try: (host, port) = cfg.addr.rsplit(":", 1) port = int(port) except ValueError: sys.exit("invalid listener address: {}".format(cfg.addr)) if cfg.response == "success": handler = SuccessHandler() elif cfg.response == "idl-exception": handler = IDLExceptionHandler() elif cfg.response == "exception": # squelch traceback for the exception we throw logging.getLogger().setLevel(logging.CRITICAL) handler = ExceptionHandler() else: sys.exit("unknown server response mode {0}".format(cfg.response)) processor = Example.Processor(handler) if cfg.service is not None: # wrap processor with multiplexor multi = TMultiplexedProcessor.TMultiplexedProcessor() multi.registerProcessor(cfg.service, processor) processor = multi if cfg.unix: transport = TSocket.TServerSocket(unix_socket=cfg.addr) else: transport = TSocket.TServerSocket(host=host, port=port) if cfg.transport == "framed": transport_factory = TTransport.TFramedTransportFactory() elif cfg.transport == "unframed": transport_factory = TTransport.TBufferedTransportFactory() elif cfg.transport == "header": if cfg.protocol == "binary": transport_factory = THeaderTransport.THeaderTransportFactory( THeaderTransport.T_BINARY_PROTOCOL) elif cfg.protocol == "compact": transport_factory = THeaderTransport.THeaderTransportFactory( THeaderTransport.T_COMPACT_PROTOCOL) else: sys.exit("header transport cannot be used with protocol {0}".format(cfg.protocol)) else: sys.exit("unknown transport {0}".format(cfg.transport)) if cfg.protocol == "binary": protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() elif cfg.protocol == "compact": protocol_factory = TCompactProtocol.TCompactProtocolFactory() elif cfg.protocol == "json": protocol_factory = TJSONProtocol.TJSONProtocolFactory() else: sys.exit("unknown protocol {0}".format(cfg.protocol)) print( "Thrift Server listening on {0} for {1} {2} requests".format( cfg.addr, cfg.transport, cfg.protocol)) if cfg.service is not None: print("Thrift Server service name {0}".format(cfg.service)) if cfg.response == "idl-exception": print("Thrift Server will throw IDL exceptions when defined") elif cfg.response == "exception": print("Thrift Server will throw Thrift exceptions for all messages") server = TServer.TThreadedServer(processor, transport, transport_factory, protocol_factory) try: server.serve() except KeyboardInterrupt: print
pfactory = THeaderProtocol.THeaderProtocolFactory( True, [ CLIENT_TYPE.HEADER, CLIENT_TYPE.FRAMED_DEPRECATED, CLIENT_TYPE.UNFRAMED_DEPRECATED, CLIENT_TYPE.HTTP_SERVER ]) else: pfactory = TBinaryProtocol.TBinaryProtocolFactory() if options.context: processor = ThriftTest.ContextProcessor( TestContextHandler(options.port)) else: processor = ThriftTest.Processor(TestHandler()) if options.multiple: processor = TMultiplexedProcessor.TMultiplexedProcessor() if options.context: processor.registerProcessor( "ThriftTest", ThriftTest.ContextProcessor(TestContextHandler(options.port))) processor.registerProcessor( "SecondService", SecondService.ContextProcessor(SecondContextHandler())) else: processor.registerProcessor("ThriftTest", ThriftTest.Processor(TestHandler())) processor.registerProcessor( "SecondService", SecondService.Processor(SecondHandler())) server = TCppServer.TCppServer(processor) server.setPort(options.port)