2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
20 from thrift.Thrift import TProcessor, TMessageType, TException
21 from thrift.protocol import TProtocolDecorator, TMultiplexedProtocol
23 class TMultiplexedProcessor(TProcessor):
27 def registerProcessor(self, serviceName, processor):
28 self.services[serviceName] = processor
30 def process(self, iprot, oprot):
31 (name, type, seqid) = iprot.readMessageBegin();
32 if type != TMessageType.CALL & type != TMessageType.ONEWAY:
33 raise TException("TMultiplex protocol only supports CALL & ONEWAY")
35 index = name.find(TMultiplexedProtocol.SEPARATOR)
37 raise TException("Service name not found in message name: " + name + ". Did you forget to use TMultiplexProtocol in your client?")
39 serviceName = name[0:index]
40 call = name[index+len(TMultiplexedProtocol.SEPARATOR):]
41 if not serviceName in self.services:
42 raise TException("Service name not found: " + serviceName + ". Did you forget to call registerProcessor()?")
49 return self.services[serviceName].process(StoredMessageProtocol(iprot, standardMessage), oprot)
52 class StoredMessageProtocol(TProtocolDecorator.TProtocolDecorator):
53 def __init__(self, protocol, messageBegin):
54 TProtocolDecorator.TProtocolDecorator.__init__(self, protocol)
55 self.messageBegin = messageBegin
57 def readMessageBegin(self):
58 return self.messageBegin