def serialize(frugal_object, protocol_factory=TBinaryProtocol.TBinaryProtocolFactory()): """Serialize a frugal entity to bytes.""" transport = TTransport.TMemoryBuffer() fprotocolFactory = FProtocolFactory(protocol_factory) protocol = fprotocolFactory.get_protocol(transport) frugal_object.write(protocol) return transport.getvalue()
def deserialize(base, buf, protocol_factory=TBinaryProtocol.TBinaryProtocolFactory()): """Deserialize a frugal object into a base instance of a frugal object.""" transport = TTransport.TMemoryBuffer(buf) fprotocolFactory = FProtocolFactory(protocol_factory) protocol = fprotocolFactory.get_protocol(transport) base.read(protocol) return base
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between clients and servers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a NATS connection to send requests nats_client = NatsClient() options = {"verbose": True, "servers": ["nats://127.0.0.1:4222"]} await nats_client.connect(**options) # Create a nats transport using the connected client # The transport sends data on the music-service NATS topic nats_transport = FNatsTransport(nats_client, "music-service") try: await nats_transport.open() except TTransportException as ex: root.error(ex) return # Using the configured transport and protocol, create a client # to talk to the music store service. store_client = FStoreClient(FServiceProvider(nats_transport, prot_factory), middleware=logging_middleware) album = await store_client.buyAlbum(FContext(), str(uuid.uuid4()), "ACT-12345") root.info("Bought an album %s\n", album.tracks[0].title) await store_client.enterAlbumGiveaway(FContext(), "*****@*****.**", "Kevin") # Close transport and nats client await nats_transport.close() await nats_client.close()
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between publishers and subscribers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a aiostomp connection, default stomp port in activemq is 61613 stomp_client = AioStomp('localhost', 61613) await stomp_client.connect() # Create a pub sub scope using the configured transport and protocol transport_factory = FStompPublisherTransportFactory(stomp_client) provider = FScopeProvider(transport_factory, None, prot_factory) # Create a publisher publisher = AlbumWinnersPublisher(provider) await publisher.open() # Publish an album win event album = Album() album.ASIN = str(uuid.uuid4()) album.duration = 12000 album.tracks = [ Track(title="Comme des enfants", artist="Coeur de pirate", publisher="Grosse Boîte", composer="Béatrice Martin", duration=169, pro=PerfRightsOrg.ASCAP) ] await publisher.publish_Winner(FContext(), album) await publisher.publish_ContestStart(FContext(), [album, album]) # Close publisher and nats client await publisher.close() stomp_client.close()
def setUp(self): self.processor = mock.Mock() prot_factory = FProtocolFactory( TBinaryProtocol.TBinaryProtocolFactory()) self.handler = new_http_handler(self.processor, prot_factory) super().setUp()
def get_app(self): prot_factory = FProtocolFactory( TBinaryProtocol.TBinaryProtocolFactory()) return Application([('/frugal', FHttpHandler, { 'processor': self.processor, 'protocol_factory': prot_factory, })])
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between publishers and subscribers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a aiostomp connection, default stomp port in activemq is 61613 stomp_client = AioStomp('localhost', 61613) await stomp_client.connect() # Create a pub sub scope using the configured transport and protocol transport_factory = FStompSubscriberTransportFactory(stomp_client) provider = FScopeProvider(None, transport_factory, prot_factory) subscriber = AlbumWinnersSubscriber(provider) def event_handler(ctx, req): raise ValueError('BLAH') root.info("You won! {}".format(req.ASIN)) def start_contest_handler(ctx, albums): root.info("Contest started, available albums: {}".format(albums)) await subscriber.subscribe_Winner(event_handler) await subscriber.subscribe_ContestStart(start_contest_handler) root.info("Subscriber starting...")
def main(): logging.info("Starting...") # Declare the protocol stack used for serialization. # Protocol stacks must match between clients and servers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Create an HTTP transport for the server URL transport = THttpTransport(URL) transport.open() # Using the configured transport and protocol, create a client # to talk to the music store service. store_client = FStoreClient(FServiceProvider(transport, prot_factory)) album = store_client.buyAlbum(FContext(), str(uuid.uuid4()), "ACT-12345") root.info("Bought an album %s\n", album) store_client.enterAlbumGiveaway(FContext(), "*****@*****.**", "Kevin") # Close the transport transport.close()
def get_protocol_factory(protocol): """ Returns a protocol factory associated with the string protocol passed in as a command line argument to the cross runner :param protocol: string :return: Protocol factory """ if protocol == "binary": return FProtocolFactory(TBinaryProtocolFactory()) elif protocol == "compact": return FProtocolFactory(TCompactProtocolFactory()) elif protocol == "json": return FProtocolFactory(TJSONProtocolFactory()) else: logging.error("Unknown protocol type: %s", protocol) sys.exit(1)
def test_process_missing_function(self): processor = FBaseProcessor() frame = bytearray( b'\x00\x00\x00\x004\x00\x00\x00\x04_cid\x00\x00\x00\x06someid' b'\x00\x00\x00\x05_opid\x00\x00\x00\x011\x00\x00\x00\x08_timeout' b'\x00\x00\x00\x045000' # End of context b'\x80\x01\x00\x02\x00\x00\x00\x08basePing\x00\x00\x00\x00\x00') itrans = TMemoryBuffer(value=frame) iprot = FProtocolFactory(TBinaryProtocolFactory()).get_protocol(itrans) otrans = TMemoryOutputBuffer(1000) oprot = FProtocolFactory(TBinaryProtocolFactory()).get_protocol(otrans) yield processor.process(iprot, oprot) expected_response = bytearray( b'\x80\x01\x00\x03\x00\x00\x00\x08basePing\x00\x00' b'\x00\x00\x0b\x00\x01\x00\x00\x00\x1aUnknown function: basePing' b'\x08\x00\x02\x00\x00\x00\x01\x00') self.assertEqual(otrans.getvalue()[41:], expected_response)
def test_process_processor_exception(self): processor = FBaseProcessor() proc = Mock() e = TException(message='foo bar exception') proc.process.side_effect = e processor.add_to_processor_map("basePing", proc) frame = bytearray( b'\x00\x00\x00\x00\x0e\x00\x00\x00\x05_opid\x00\x00\x00\x011' b'\x80\x01\x00\x02\x00\x00\x00\x08basePing\x00\x00\x00\x00\x00') itrans = TMemoryBuffer(value=frame) iprot = FProtocolFactory(TBinaryProtocolFactory()).get_protocol(itrans) oprot = Mock() processor.process(iprot, oprot)
def test_process(self): processor = FBaseProcessor() proc = Mock() processor.add_to_processor_map("basePing", proc) frame = bytearray( b'\x00\x00\x00\x00\x0e\x00\x00\x00\x05_opid\x00\x00\x00\x011' b'\x80\x01\x00\x02\x00\x00\x00\x08basePing\x00\x00\x00\x00\x00') itrans = TMemoryBuffer(value=frame) iprot = FProtocolFactory(TBinaryProtocolFactory()).get_protocol(itrans) oprot = Mock() processor.process(iprot, oprot) assert (proc.process.call_args) args, _ = proc.process.call_args self.assertEqual(args[0].get_response_header(_OPID_HEADER), '1') assert (args[1] == iprot) assert (args[2] == oprot)
def setUp(self): self.mock_processor = mock.Mock() prot_factory = FProtocolFactory( TBinaryProtocol.TBinaryProtocolFactory()) app = webapp2.WSGIApplication([ ('/frugal', new_webapp2_handler(self.mock_processor, prot_factory)) ]) self.test_app = webtest.TestApp(app) self.request_data = bytearray([2, 3, 4]) self.request_frame = bytearray([0, 0, 0, 3]) + self.request_data self.request_payload = base64.b64encode(self.request_frame) self.response_data = bytearray([6, 7, 8, 9, 10, 11]) self.response_frame = bytearray([0, 0, 0, 6]) + self.response_data def process_data(_, oprot): oprot.get_transport().write(self.response_data) self.mock_processor.process.side_effect = process_data
def test_process(self): processor = FBaseProcessor() proc = Mock() future = Future() future.set_result(None) proc.process.return_value = future processor.add_to_processor_map("basePing", proc) frame = bytearray( b'\x00\x00\x00\x00\x0e\x00\x00\x00\x05_opid\x00\x00\x00\x011' b'\x80\x01\x00\x02\x00\x00\x00\x08basePing\x00\x00\x00\x00\x00') itrans = TMemoryBuffer(value=frame) iprot = FProtocolFactory(TBinaryProtocolFactory()).get_protocol(itrans) oprot = Mock() yield processor.process(iprot, oprot) assert (proc.process.call_args) args, _ = proc.process.call_args assert (args[0]._get_op_id() == 1) assert (args[1] == iprot) assert (args[2] == oprot)
def main(): logging.info("Starting...") # Declare the protocol stack used for serialization. # Protocol stacks must match between clients and servers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Create a new server processor. # Incoming requests to the processor are passed to the handler. # Results from the handler are returned back to the client. processor = FStoreProcessor(StoreHandler()) # Optionally add middleware to the processor before starting the server. # add_middleware can take a list or single middleware. processor.add_middleware(logging_middleware) # Create a new music store server using the processor, # The sever will listen on the configured URL server = FHttpServer(processor, ('', 9090), prot_factory) server.serve()
def test_new_provider(self, mock_pub_transport_factory, mock_sub_transport_factory, mock_thrift_protocol): pub_transport = FTransport() sub_transport = FTransport() protocol_factory = FProtocolFactory(None) mock_pub_transport_factory.get_transport.return_value = pub_transport mock_sub_transport_factory.get_transport.return_value = sub_transport provider = FScopeProvider(mock_pub_transport_factory, mock_sub_transport_factory, protocol_factory) trans, prot_factory = provider.new_publisher() self.assertEqual(pub_transport, trans) self.assertEqual(protocol_factory, prot_factory) trans, prot_factory = provider.new_subscriber() self.assertEqual(sub_transport, trans) self.assertEqual(protocol_factory, prot_factory)
def test_it_works(self): processor_factory = mock.Mock() mock_processor = mock.Mock() processor_factory.get_processor.return_value = mock_processor proto_factory = FProtocolFactory(TJSONProtocolFactory()) server_trans = TServerSocket(host='localhost', port=5536) server = FSimpleServer(processor_factory, server_trans, proto_factory) thread = Thread(target=lambda: server.serve()) thread.start() time.sleep(0.1) transport = TSocket(host='localhost', port=5536) transport.open() transport.write(bytearray([0, 0, 0, 3, 1, 2, 3])) transport.flush() time.sleep(0.1) server.stop() processor_factory.get_processor.assert_called_once_with(mock.ANY) mock_processor.process.assert_called_with(mock.ANY, mock.ANY)
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between clients and servers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a NATS connection to receive requests nats_client = NATS() options = {"verbose": True, "servers": ["nats://127.0.0.1:4222"]} await nats_client.connect(**options) # Create a new server processor. # Incoming requests to the processor are passed to the handler. # Results from the handler are returned back to the client. processor = FStoreProcessor(StoreHandler()) # Create a new music store server using the processor, # The sever will listen on the music-service NATS topic server = FNatsServer(nats_client, "music-service", processor, prot_factory) root.info("Starting Nats server...") await server.serve()
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between clients and servers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Create an HTTP to query the configured server URL transport = FHttpTransport("http://*****:*****@workiva.com", "Kevin") await transport.close()
async def main(): # Declare the protocol stack used for serialization. # Protocol stacks must match between publishers and subscribers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Open a NATS connection to receive requests nats_client = NatsClient() options = { "verbose": True, "servers": ["nats://127.0.0.1:4222"] } await nats_client.connect(**options) # Create a pub sub scope using the configured transport and protocol transport_factory = FNatsPublisherTransportFactory(nats_client) provider = FScopeProvider(transport_factory, None, prot_factory) # Create a publisher publisher = AlbumWinnersPublisher(provider) await publisher.open() # Publish an album win event album = Album() album.ASIN = str(uuid.uuid4()) album.duration = 12000 album.tracks = [Track(title="Comme des enfants", artist="Coeur de pirate", publisher="Grosse Boîte", composer="Béatrice Martin", duration=169, pro=PerfRightsOrg.ASCAP)] await publisher.publish_Winner(FContext(), album) await publisher.publish_ContestStart(FContext(), [album, album]) # Close publisher and nats client await publisher.close() await nats_client.close()
album = Album() album.ASIN = ASIN album.duration = 12000 return album def enterAlbumGiveaway(self, ctx, email, name): """ Always return success (true) """ return True if __name__ == '__main__': # Declare the protocol stack used for serialization. # Protocol stacks must match between clients and servers. prot_factory = FProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()) # Create a new server processor. # Incoming requests to the processor are passed to the handler. # Results from the handler are returned back to the client. processor = FStoreProcessor(StoreHandler()) # Optionally add middleware to the processor before starting the server. # add_middleware can take a list or single middleware. processor.add_middleware(logging_middleware) store_handler = new_http_handler(processor, prot_factory) app = web.Application() app.router.add_route("*", "/frugal", store_handler) web.run_app(app, port=9090)
def main(): parser = argparse.ArgumentParser(description="Run a tornado python server") parser.add_argument('--port', dest='port', default='9090') parser.add_argument('--protocol', dest='protocol_type', default="binary", choices="binary, compact, json") parser.add_argument('--transport', dest="transport_type", default="stateless", choices="stateless, http") args = parser.parse_args() if args.protocol_type == "binary": protocol_factory = FProtocolFactory(TBinaryProtocolFactory()) elif args.protocol_type == "compact": protocol_factory = FProtocolFactory(TCompactProtocolFactory()) elif args.protocol_type == "json": protocol_factory = FProtocolFactory(TJSONProtocolFactory()) else: logging.error("Unknown protocol type: %s", args.protocol_type) sys.exit(1) nats_client = NATS() options = {"verbose": True, "servers": ["nats://127.0.0.1:4222"]} yield nats_client.connect(**options) global port port = args.port handler = FrugalTestHandler() subject = "frugal.*.*.rpc.{}".format(args.port) processor = Processor(handler) if args.transport_type == "stateless": server = FNatsServer(nats_client, [subject], processor, protocol_factory) # start healthcheck so the test runner knows the server is running thread.start_new_thread(healthcheck, (port, )) print("Starting {} server...".format(args.transport_type)) yield server.serve() elif args.transport_type == "http": factories = { 'processor': processor, 'protocol_factory': protocol_factory } server = Application([(r'/', FHttpHandler, factories)]) print("Starting {} server...".format(args.transport_type)) server.listen(port) else: logging.error("Unknown transport type: %s", args.transport_type) sys.exit(1) # Setup subscriber, send response upon receipt pub_transport_factory = FNatsPublisherTransportFactory(nats_client) sub_transport_factory = FNatsSubscriberTransportFactory(nats_client) provider = FScopeProvider(pub_transport_factory, sub_transport_factory, protocol_factory) global publisher publisher = EventsPublisher(provider) yield publisher.open() @gen.coroutine def response_handler(context, event): print("received {} : {}".format(context, event)) preamble = context.get_request_header(PREAMBLE_HEADER) if preamble is None or preamble == "": logging.error("Client did not provide preamble header") return ramble = context.get_request_header(RAMBLE_HEADER) if ramble is None or ramble == "": logging.error("Client did not provide ramble header") return response_event = Event(Message="Sending Response") response_context = FContext("Call") global publisher global port yield publisher.publish_EventCreated(response_context, preamble, ramble, "response", "{}".format(port), response_event) print("Published event={}".format(response_event)) subscriber = EventsSubscriber(provider) yield subscriber.subscribe_EventCreated("*", "*", "call", "{}".format(args.port), response_handler)