blob: 605aa1f252ab8c8acb228c74c4958a5fa2d8fe77 [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
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090023
Roger Meier879cab22014-05-03 17:51:21 +020024class TMultiplexedProcessor(TProcessor):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090025 def __init__(self):
26 self.services = {}
Roger Meier879cab22014-05-03 17:51:21 +020027
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090028 def registerProcessor(self, serviceName, processor):
29 self.services[serviceName] = processor
Roger Meier879cab22014-05-03 17:51:21 +020030
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090031 def process(self, iprot, oprot):
32 (name, type, seqid) = iprot.readMessageBegin()
Helgi Kristvin Sigurbjarnarsonf2b7a482015-10-24 22:56:25 -070033 if type != TMessageType.CALL and type != TMessageType.ONEWAY:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090034 raise TException("TMultiplex protocol only supports CALL & ONEWAY")
Roger Meier879cab22014-05-03 17:51:21 +020035
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090036 index = name.find(TMultiplexedProtocol.SEPARATOR)
37 if index < 0:
38 raise TException("Service name not found in message name: " + name + ". Did you forget to use TMultiplexProtocol in your client?")
Roger Meier879cab22014-05-03 17:51:21 +020039
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090040 serviceName = name[0:index]
41 call = name[index + len(TMultiplexedProtocol.SEPARATOR):]
42 if serviceName not in self.services:
43 raise TException("Service name not found: " + serviceName + ". Did you forget to call registerProcessor()?")
Roger Meier879cab22014-05-03 17:51:21 +020044
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090045 standardMessage = (call, type, seqid)
46 return self.services[serviceName].process(StoredMessageProtocol(iprot, standardMessage), oprot)
Roger Meier879cab22014-05-03 17:51:21 +020047
48
49class StoredMessageProtocol(TProtocolDecorator.TProtocolDecorator):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090050 def __init__(self, protocol, messageBegin):
51 TProtocolDecorator.TProtocolDecorator.__init__(self, protocol)
52 self.messageBegin = messageBegin
Roger Meier879cab22014-05-03 17:51:21 +020053
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090054 def readMessageBegin(self):
55 return self.messageBegin