blob: 8d929ac389ca72788c8dc8f13b9b44a5f57dcabb [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
James E. King III74a3e092019-02-07 19:33:52 -050020from thrift.Thrift import TProcessor, TMessageType
Roger Meier879cab22014-05-03 17:51:21 +020021from thrift.protocol import TProtocolDecorator, TMultiplexedProtocol
James E. King III74a3e092019-02-07 19:33:52 -050022from thrift.protocol.TProtocol import TProtocolException
Roger Meier879cab22014-05-03 17:51:21 +020023
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090024
Roger Meier879cab22014-05-03 17:51:21 +020025class TMultiplexedProcessor(TProcessor):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090026 def __init__(self):
27 self.services = {}
Roger Meier879cab22014-05-03 17:51:21 +020028
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090029 def registerProcessor(self, serviceName, processor):
30 self.services[serviceName] = processor
Roger Meier879cab22014-05-03 17:51:21 +020031
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090032 def process(self, iprot, oprot):
33 (name, type, seqid) = iprot.readMessageBegin()
Helgi Kristvin Sigurbjarnarsonf2b7a482015-10-24 22:56:25 -070034 if type != TMessageType.CALL and type != TMessageType.ONEWAY:
James E. King III74a3e092019-02-07 19:33:52 -050035 raise TProtocolException(
36 TProtocolException.NOT_IMPLEMENTED,
37 "TMultiplexedProtocol only supports CALL & ONEWAY")
Roger Meier879cab22014-05-03 17:51:21 +020038
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090039 index = name.find(TMultiplexedProtocol.SEPARATOR)
40 if index < 0:
James E. King III74a3e092019-02-07 19:33:52 -050041 raise TProtocolException(
42 TProtocolException.NOT_IMPLEMENTED,
43 "Service name not found in message name: " + name + ". " +
44 "Did you forget to use TMultiplexedProtocol in your client?")
Roger Meier879cab22014-05-03 17:51:21 +020045
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090046 serviceName = name[0:index]
47 call = name[index + len(TMultiplexedProtocol.SEPARATOR):]
48 if serviceName not in self.services:
James E. King III74a3e092019-02-07 19:33:52 -050049 raise TProtocolException(
50 TProtocolException.NOT_IMPLEMENTED,
51 "Service name not found: " + serviceName + ". " +
52 "Did you forget to call registerProcessor()?")
Roger Meier879cab22014-05-03 17:51:21 +020053
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090054 standardMessage = (call, type, seqid)
James E. King III74a3e092019-02-07 19:33:52 -050055 return self.services[serviceName].process(
56 StoredMessageProtocol(iprot, standardMessage), oprot)
Roger Meier879cab22014-05-03 17:51:21 +020057
58
59class StoredMessageProtocol(TProtocolDecorator.TProtocolDecorator):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090060 def __init__(self, protocol, messageBegin):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090061 self.messageBegin = messageBegin
Roger Meier879cab22014-05-03 17:51:21 +020062
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090063 def readMessageBegin(self):
64 return self.messageBegin