blob: bd10d9b6accfd182bd02b40e2e574e29cb2b97a1 [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):
James E. King III9804ab92019-02-07 16:59:05 -050027 self.defaultProcessor = None
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090028 self.services = {}
Roger Meier879cab22014-05-03 17:51:21 +020029
James E. King III9804ab92019-02-07 16:59:05 -050030 def registerDefault(self, processor):
31 """
32 If a non-multiplexed processor connects to the server and wants to
33 communicate, use the given processor to handle it. This mechanism
34 allows servers to upgrade from non-multiplexed to multiplexed in a
35 backwards-compatible way and still handle old clients.
36 """
37 self.defaultProcessor = processor
38
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090039 def registerProcessor(self, serviceName, processor):
40 self.services[serviceName] = processor
Roger Meier879cab22014-05-03 17:51:21 +020041
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090042 def process(self, iprot, oprot):
43 (name, type, seqid) = iprot.readMessageBegin()
Helgi Kristvin Sigurbjarnarsonf2b7a482015-10-24 22:56:25 -070044 if type != TMessageType.CALL and type != TMessageType.ONEWAY:
James E. King III74a3e092019-02-07 19:33:52 -050045 raise TProtocolException(
46 TProtocolException.NOT_IMPLEMENTED,
47 "TMultiplexedProtocol only supports CALL & ONEWAY")
Roger Meier879cab22014-05-03 17:51:21 +020048
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090049 index = name.find(TMultiplexedProtocol.SEPARATOR)
50 if index < 0:
James E. King III9804ab92019-02-07 16:59:05 -050051 if self.defaultProcessor:
52 return self.defaultProcessor.process(
53 StoredMessageProtocol(iprot, (name, type, seqid)), oprot)
54 else:
55 raise TProtocolException(
56 TProtocolException.NOT_IMPLEMENTED,
57 "Service name not found in message name: " + name + ". " +
58 "Did you forget to use TMultiplexedProtocol in your client?")
Roger Meier879cab22014-05-03 17:51:21 +020059
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090060 serviceName = name[0:index]
61 call = name[index + len(TMultiplexedProtocol.SEPARATOR):]
62 if serviceName not in self.services:
James E. King III74a3e092019-02-07 19:33:52 -050063 raise TProtocolException(
64 TProtocolException.NOT_IMPLEMENTED,
65 "Service name not found: " + serviceName + ". " +
66 "Did you forget to call registerProcessor()?")
Roger Meier879cab22014-05-03 17:51:21 +020067
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090068 standardMessage = (call, type, seqid)
James E. King III74a3e092019-02-07 19:33:52 -050069 return self.services[serviceName].process(
70 StoredMessageProtocol(iprot, standardMessage), oprot)
Roger Meier879cab22014-05-03 17:51:21 +020071
72
73class StoredMessageProtocol(TProtocolDecorator.TProtocolDecorator):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090074 def __init__(self, protocol, messageBegin):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090075 self.messageBegin = messageBegin
Roger Meier879cab22014-05-03 17:51:21 +020076
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090077 def readMessageBegin(self):
78 return self.messageBegin