def buildProtocol(self, addr): pfactory = TBinaryProtocol.TBinaryProtocolFactory() p = TTwisted.ThriftClientProtocol(self._client_class, pfactory) wrapper = _LossNotifyingWrapperProtocol(p, self._on_connectionLost) return wrapper
def setUp(self): self.handler = TestHandler() self.processor = ThriftTest.Processor(self.handler) self.pfactory = TBinaryProtocol.TBinaryProtocolFactory() self.server = reactor.listenTCP( 0, TTwisted.ThriftServerFactory(self.processor, self.pfactory), interface="127.0.0.1") self.portNo = self.server.getHost().port self.txclient = yield ClientCreator(reactor, TTwisted.ThriftClientProtocol, ThriftTest.Client, self.pfactory).connectTCP("127.0.0.1", self.portNo) self.client = self.txclient.client
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 start_service(api_class, service_class, log, services=None, host='localhost', port=9100): """Start a Data API service. Args: api_class (BaseService): The custom API service class, e.g., `doekbase.data_api.taxonomy.taxon.api.TaxonService` service_class (type): The Thrift auto-generated service class log (logging.Logger): Logging object services (dict): Service configuration dictionary, passed to constructor of the `api_class`. host (str): Service host (will default to 'localhost') port (int): Service port, e.g. 9101 """ assert issubclass(api_class, BaseService), \ 'Invalid "api_class": must be a subclass of ' \ 'doekbase.data_api.service_core.BaseService' assert hasattr(service_class, 'Processor'), 'Invalid "service_class": ' \ 'missing "Processor" attribute' assert isinstance(port, int), 'The "port" must be an integer' log.debug('method=start_service state=begin host={host} port={port:d}' .format(host=host, port=port)) # Create server services = services or SERVICES_DICT handler = api_class(services) processor = service_class.Processor(handler) pfactory = TBinaryProtocol.TBinaryProtocolFactory() resource = TTwisted.ThriftResource(processor, pfactory, pfactory) site = twisted.web.server.Site(resource=resource) twisted.internet.reactor.listenTCP(port, site, interface=host) # Run server sname = api_class.__name__ shost = host or 'localhost' log.info('msg="Starting {} server at {}:{}"'.format(sname, shost, port)) log.debug('method=twisted.internet.reactor.run state=begin') try: twisted.internet.reactor.run() except Exception as err: log.error('msg="Abort {} server on error"'.format(sname)) log.error('method=twisted.internet.reactor.run state=error ' 'error_message={e}'.format(e=err)) raise finally: log.debug('method=twisted.internet.reactor.run state=end') return 0
def startService(self): if scribe.Iface.providedBy(self._handler): handler = self._handler else: handler = _SimpleLogHandler(self._handler) self._processor = scribe.Processor(handler) thriftFactory = TTwisted.ThriftServerFactory( processor=self._processor, iprot_factory=TBinaryProtocol.TBinaryProtocolFactory()) d = self._endpoint.listen(thriftFactory) def _listening(port): self._port = port d.addCallback(_listening)
def make_application(): ssetings = settings.get('SERVER') port = ssetings['port'] pkg_size = settings.getint("SEED_PKG_SIZE") seed_servie = MemoryBasedSeedsService(pkg_size) handler = RequestHandler(seed_servie) processor = Scheduler.Processor(handler) factory = TTwisted.ThriftServerFactory(processor, TBinaryProtocol.TBinaryProtocolFactory()) tcp_service = internet.TCPServer(port, factory) application = service.Application(ssetings['name']) logfile = DailyLogFile(settings.get('LOG_FILE'), settings.get('LOG_DIR')) application.setComponent(ILogObserver, FileLogObserver(logfile).emit) multiService = service.MultiService() tcp_service.setServiceParent(multiService) multiService.setServiceParent(application) return application
def main(): if len(sys.argv) < 2: print 'Usage: %s config_file' % sys.argv[0] sys.exit() log.startLogging(sys.stdout) Config.init(sys.argv[1]) if Config.debug: log.startLogging(sys.stdout) else: log.startLogging(DailyLogFile.fromFullPath(Config.get('log.file'))) handler = SnowflakeServiceHandler(Config.getint('worker.id'), Config.getint('datacenter.id')) processor = SnowflakeService.Processor(handler) server = TTwisted.ThriftServerFactory( processor=processor, iprot_factory=TBinaryProtocol.TBinaryProtocolFactory()) reactor.listenTCP(Config.getint('port', default=9999), server, interface=Config.get('listen', default="0.0.0.0")) reactor.run()
else: x = InvalidOperation() x.what = work.op x.why = 'Invalid operation' raise x log = SharedStruct() log.key = logid log.value = '%d' % (val) self.log[logid] = log return val def getStruct(self, key): print 'getStruct(%d)' % (key) return self.log[key] def zip(self): print 'zip()' if __name__ == '__main__': handler = CalculatorHandler() processor = Calculator.Processor(handler) pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = reactor.listenTCP(9090, TTwisted.ThriftServerFactory( processor, pfactory), interface="127.0.0.1") reactor.run()
def start_service(api_class, service_class, log, services=None, host='localhost', port=9100, killprocgrp=False): """Start a Data API service. Args: api_class (BaseService): The custom API service class, e.g., `doekbase.data_api.taxonomy.taxon.api.TaxonService` service_class (type): The Thrift auto-generated service class log (logging.Logger): Logging object services (dict): Service configuration dictionary, passed to constructor of the `api_class`. host (str): Service host (will default to 'localhost') port (int): Service port, e.g. 9101 killprocgrp (bool): if True, kill process group on exit """ assert issubclass(api_class, BaseService), \ 'Invalid "api_class": must be a subclass of ' \ 'doekbase.data_api.service_core.BaseService' assert hasattr(service_class, 'Processor'), 'Invalid "service_class": ' \ 'missing "Processor" attribute' assert isinstance(port, int), 'The "port" must be an integer' svc_t0 = util.log_start(log, 'start_service', kvp=dict(host=host, port=port)) # Create server services = services or SERVICES_DICT handler = api_class(services) processor = service_class.Processor(handler) pfactory = TBinaryProtocol.TBinaryProtocolFactory() resource = TTwisted.ThriftResource(processor, pfactory, pfactory) site = twisted.web.server.Site(resource=resource) twisted.internet.reactor.listenTCP(port, site, interface=host) # Kill entire process group on shutdown if killprocgrp: twisted.internet.reactor.addSystemEventTrigger( 'before', 'shutdown', functools.partial(kill_process_group, log=log)) # Run server sname = api_class.__name__ shost = host or 'localhost' util.log_start(log, 'server', kvp=dict(name=sname, host=shost, port=port)) t0 = util.log_start(log, 'twisted.internet.reactor.run', level=logging.DEBUG) try: twisted.internet.reactor.run() except Exception as err: log.error('msg="Abort {} server on error"'.format(sname)) util.log_end(log, t0, 'twisted.internet.reactor.run', status_code=1, level=logging.ERROR, kvp=dict(msg=err)) raise finally: util.log_end(log, t0, 'twisted.internet.reactor.run') util.log_end(log, svc_t0, 'start_service', kvp=dict(host=host, port=port)) return 0
def FdsFactory(server): processor = fdsapi.Processor(FdsApiHandler(server)) pfactory = TBinaryProtocol.TBinaryProtocolFactory() return TTwisted.ThriftServerFactory(processor, pfactory)
def OtsApiFactory(topka): processor = otsapi.Processor(OtsApiHandler(topka)) pfactory = TBinaryProtocol.TBinaryProtocolFactory() return TTwisted.ThriftServerFactory(processor, pfactory)
def build_factory(self): return TTwisted.ThriftServerFactory( processor=self.handler, iprot_factory=TBinaryProtocol.TBinaryProtocolFactory())
def build_factory(self): return TTwisted.ThriftClientFactory( self.service_class, iprot_factory=TBinaryProtocol.TBinaryProtocolFactory())