[ https://issues.apache.org/jira/browse/THRIFT-1914?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13987169#comment-13987169 ]
ASF GitHub Bot commented on THRIFT-1914: ---------------------------------------- Github user djwatson commented on a diff in the pull request: https://github.com/apache/thrift/pull/82#discussion_r12213289 --- Diff: lib/py/src/TMultiplexedProcessor.py --- @@ -0,0 +1,58 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +from thrift.Thrift import TProcessor, TMessageType, TException +from thrift.protocol import TProtocolDecorator, TMultiplexedProtocol + +class TMultiplexedProcessor(TProcessor): + def __init__(self): + self.services = {} + + def registerProcessor(self, serviceName, processor): + self.services[serviceName] = processor + + def process(self, iprot, oprot): + (name, type, seqid) = iprot.readMessageBegin(); + if type != TMessageType.CALL & type != TMessageType.ONEWAY: + raise TException("This should not have happened!?") --- End diff -- Can we get a better error message? "TMultiplex protocol only supports CALL & Oneway" or something > Python: Support for Multiplexing Services on any Transport, Protocol and > Server > ------------------------------------------------------------------------------- > > Key: THRIFT-1914 > URL: https://issues.apache.org/jira/browse/THRIFT-1914 > Project: Thrift > Issue Type: Sub-task > Components: Python - Library > Reporter: Doug MacEachern > Fix For: 1.0 > > Attachments: python3_multiplex.patch, python_multiplex.patch > > > *Motivation and Benefits* > We plan to use Thrift with a large number of functions communicating among > (at least) three languages. To keep maintainability high, we hope to avoid a > single service defined with a large number of functions. This would require > monolithic, unwieldy service implementations as the number of functions grows. > Breaking up our API into multiple IDLs gives us multiple abstract base > classes, which provides more flexibility in the object-oriented design of our > server platform. > Before our changes, the alternative was to open up additional ports with > smaller service implementations on each. We'd rather not open additional > ports. > *Our Approach* > We pursued an approach with the following in mind: > - No modifications to existing Thrift code. > - No modification to the Thrift protocol as described in the whitepaper. > - No modification to any {{TServer}}, {{TProtocol}} or {{TTransport}}. > - No need for any new {{TServer}} implementation. Works with any {{TServer}} > implementation. > - Work with any combination of {{TServer}}, {{TProtocol}} or {{TTransport}}. > - Avoid language-specific features, to ease implementation in other languages. > *Additions to Thrift* > Convenience class: > - {{TProtocolDecorator}} (extends {{TProtocol}}). This is a no-op decorator > around the {{TProtocol}} abstract class. > For use by clients: > - {{TMultiplexedProtocol}} (extends {{TProtocolDecorator}}). This decorates > any {{TProtocol}} by modifying the behaviour of > {{writeMessageBegin(TMessage)}} to change {{TMessage.name}} from > {{function_name}} to {{service_name + separator + function_name}}. > For use by the server: > - {{TMultiplexedProcessor}} (implements {{TProcessor}}). It should be used > to communicate with a client that was written using {{TMultiplexedProtocol}}. > It removes {{service_name + separator}} from {{TMessage.name}}, turning it > back into the standard message format. It then brokers the service request > to the {{TProcessor}} which is registered to handle requests for that service. > *Sample Usage - Client* > In this example, we've chosen to use {{TBinaryProtocol}} with two services: > {{Calculator}} and {{WeatherReport}}. > {code} > TSocket transport = new TSocket("localhost", 9090); > transport.open(); > TBinaryProtocol protocol = new TBinaryProtocol(transport); > TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator"); > Calculator.Client service = new Calculator.Client(mp); > TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, > "WeatherReport"); > WeatherReport.Client service2 = new WeatherReport.Client(mp2); > System.out.println(service.add(2,2)); > System.out.println(service2.getTemperature()); > {code} > *Sample Usage - Server* > {code} > TMultiplexedProcessor processor = new TMultiplexedProcessor(); > processor.registerProcessor( > "Calculator", > new Calculator.Processor(new CalculatorHandler())); > processor.registerProcessor( > "WeatherReport", > new WeatherReport.Processor(new WeatherReportHandler())); > TServerTransport t = new TServerSocket(9090); > TSimpleServer server = new TSimpleServer(processor, t); > server.serve(); > {code} -- This message was sent by Atlassian JIRA (v6.2#6252)