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 
20 #ifndef THRIFT_TMULTIPLEXEDPROTOCOL_H_
21 #define THRIFT_TMULTIPLEXEDPROTOCOL_H_ 1
22 
23 #include <thrift/protocol/TProtocolDecorator.h>
24 
25 namespace apache {
26 namespace thrift {
27 namespace protocol {
28 using std::shared_ptr;
29 
30 /**
31  * <code>TMultiplexedProtocol</code> is a protocol-independent concrete decorator
32  * that allows a Thrift client to communicate with a multiplexing Thrift server,
33  * by prepending the service name to the function name during function calls.
34  *
35  * \note THIS IS NOT USED BY SERVERS.  On the server, use
36  * {@link apache::thrift::TMultiplexedProcessor TMultiplexedProcessor} to handle requests
37  * from a multiplexing client.
38  *
39  * This example uses a single socket transport to invoke two services:
40  *
41  * <blockquote><code>
42  *     shared_ptr<TSocket> transport(new TSocket("localhost", 9090));
43  *     transport->open();
44  *
45  *     shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
46  *
47  *     shared_ptr<TMultiplexedProtocol> mp1(new TMultiplexedProtocol(protocol, "Calculator"));
48  *     shared_ptr<CalculatorClient> service1(new CalculatorClient(mp1));
49  *
50  *     shared_ptr<TMultiplexedProtocol> mp2(new TMultiplexedProtocol(protocol, "WeatherReport"));
51  *     shared_ptr<WeatherReportClient> service2(new WeatherReportClient(mp2));
52  *
53  *     service1->add(2,2);
54  *     int temp = service2->getTemperature();
55  * </code></blockquote>
56  *
57  * @see apache::thrift::protocol::TProtocolDecorator
58  */
59 class TMultiplexedProtocol : public TProtocolDecorator {
60 public:
61   /**
62    * Wrap the specified protocol, allowing it to be used to communicate with a
63    * multiplexing server.  The <code>serviceName</code> is required as it is
64    * prepended to the message header so that the multiplexing server can broker
65    * the function call to the proper service.
66    *
67    * \param _protocol    Your communication protocol of choice, e.g. <code>TBinaryProtocol</code>.
68    * \param _serviceName The service name of the service communicating via this protocol.
69    */
TMultiplexedProtocol(shared_ptr<TProtocol> _protocol,const std::string & _serviceName)70   TMultiplexedProtocol(shared_ptr<TProtocol> _protocol, const std::string& _serviceName)
71     : TProtocolDecorator(_protocol), serviceName(_serviceName), separator(":") {}
72   ~TMultiplexedProtocol() override = default;
73 
74   /**
75    * Prepends the service name to the function name, separated by TMultiplexedProtocol::SEPARATOR.
76    *
77    * \param [in] _name   The name of the method to be called in the service.
78    * \param [in] _type   The type of message
79    * \param [in] _name   The sequential id of the message
80    *
81    * \throws TException  Passed through from wrapped <code>TProtocol</code> instance.
82    */
83   uint32_t writeMessageBegin_virt(const std::string& _name,
84                                   const TMessageType _type,
85                                   const int32_t _seqid) override;
86 
87 private:
88   const std::string serviceName;
89   const std::string separator;
90 };
91 }
92 }
93 }
94 
95 #endif // THRIFT_TMULTIPLEXEDPROTOCOL_H_
96