blob: a8d5565c36290b25285b421cc5ca82726e4d14cf [file] [log] [blame]
Roger Meier879cab22014-05-03 17:51:21 +02001#
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
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
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
17# under the License.
18#
19
20from thrift.Thrift import TProcessor, TMessageType, TException
21from thrift.protocol import TProtocolDecorator, TMultiplexedProtocol
22
23class TMultiplexedProcessor(TProcessor):
24 def __init__(self):
25 self.services = {}
26
27 def registerProcessor(self, serviceName, processor):
28 self.services[serviceName] = processor
29
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")
34
35 index = name.find(TMultiplexedProtocol.SEPARATOR)
36 if index < 0:
37 raise TException("Service name not found in message name: " + name + ". Did you forget to use TMultiplexProtocol in your client?")
38
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()?")
43
44 standardMessage = (
45 call,
46 type,
47 seqid
48 )
49 return self.services[serviceName].process(StoredMessageProtocol(iprot, standardMessage), oprot)
50
51
52class StoredMessageProtocol(TProtocolDecorator.TProtocolDecorator):
53 def __init__(self, protocol, messageBegin):
54 TProtocolDecorator.TProtocolDecorator.__init__(self, protocol)
55 self.messageBegin = messageBegin
56
57 def readMessageBegin(self):
58 return self.messageBegin