Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 1 | # |
| 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 III | 74a3e09 | 2019-02-07 19:33:52 -0500 | [diff] [blame^] | 20 | from thrift.Thrift import TProcessor, TMessageType |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 21 | from thrift.protocol import TProtocolDecorator, TMultiplexedProtocol |
James E. King III | 74a3e09 | 2019-02-07 19:33:52 -0500 | [diff] [blame^] | 22 | from thrift.protocol.TProtocol import TProtocolException |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 23 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 24 | |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 25 | class TMultiplexedProcessor(TProcessor): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 26 | def __init__(self): |
| 27 | self.services = {} |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 28 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 29 | def registerProcessor(self, serviceName, processor): |
| 30 | self.services[serviceName] = processor |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 31 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 32 | def process(self, iprot, oprot): |
| 33 | (name, type, seqid) = iprot.readMessageBegin() |
Helgi Kristvin Sigurbjarnarson | f2b7a48 | 2015-10-24 22:56:25 -0700 | [diff] [blame] | 34 | if type != TMessageType.CALL and type != TMessageType.ONEWAY: |
James E. King III | 74a3e09 | 2019-02-07 19:33:52 -0500 | [diff] [blame^] | 35 | raise TProtocolException( |
| 36 | TProtocolException.NOT_IMPLEMENTED, |
| 37 | "TMultiplexedProtocol only supports CALL & ONEWAY") |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 38 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 39 | index = name.find(TMultiplexedProtocol.SEPARATOR) |
| 40 | if index < 0: |
James E. King III | 74a3e09 | 2019-02-07 19:33:52 -0500 | [diff] [blame^] | 41 | 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 Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 45 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 46 | serviceName = name[0:index] |
| 47 | call = name[index + len(TMultiplexedProtocol.SEPARATOR):] |
| 48 | if serviceName not in self.services: |
James E. King III | 74a3e09 | 2019-02-07 19:33:52 -0500 | [diff] [blame^] | 49 | raise TProtocolException( |
| 50 | TProtocolException.NOT_IMPLEMENTED, |
| 51 | "Service name not found: " + serviceName + ". " + |
| 52 | "Did you forget to call registerProcessor()?") |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 53 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 54 | standardMessage = (call, type, seqid) |
James E. King III | 74a3e09 | 2019-02-07 19:33:52 -0500 | [diff] [blame^] | 55 | return self.services[serviceName].process( |
| 56 | StoredMessageProtocol(iprot, standardMessage), oprot) |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 57 | |
| 58 | |
| 59 | class StoredMessageProtocol(TProtocolDecorator.TProtocolDecorator): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 60 | def __init__(self, protocol, messageBegin): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 61 | self.messageBegin = messageBegin |
Roger Meier | 879cab2 | 2014-05-03 17:51:21 +0200 | [diff] [blame] | 62 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 63 | def readMessageBegin(self): |
| 64 | return self.messageBegin |