blob: 1e2af834ba535d5cca7eef8c2cbb2a6b4e9d2f43 [file] [log] [blame]
Jens Geyer72034242013-05-08 18:46:57 +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 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
22 */
23
24using System;
25using System.Text;
26using Thrift.Transport;
27using System.Collections.Generic;
28
Jens Geyerd5436f52014-10-03 19:50:38 +020029namespace Thrift.Protocol
Jens Geyer72034242013-05-08 18:46:57 +020030{
31
32 /**
Jens Geyerd5436f52014-10-03 19:50:38 +020033 * TMultiplexedProtocol is a protocol-independent concrete decorator that allows a Thrift
34 * client to communicate with a multiplexing Thrift server, by prepending the service name
Jens Geyer72034242013-05-08 18:46:57 +020035 * to the function name during function calls.
36 *
Jens Geyerd5436f52014-10-03 19:50:38 +020037 * NOTE: THIS IS NOT TO BE USED BY SERVERS.
Jens Geyer72034242013-05-08 18:46:57 +020038 * On the server, use TMultiplexedProcessor to handle requests from a multiplexing client.
39 *
40 * This example uses a single socket transport to invoke two services:
41 *
42 * TSocket transport = new TSocket("localhost", 9090);
43 * transport.open();
Jens Geyerd5436f52014-10-03 19:50:38 +020044 *
Jens Geyer72034242013-05-08 18:46:57 +020045 * TBinaryProtocol protocol = new TBinaryProtocol(transport);
46 *
47 * TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator");
48 * Calculator.Client service = new Calculator.Client(mp);
49 *
50 * TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport");
51 * WeatherReport.Client service2 = new WeatherReport.Client(mp2);
52 *
53 * System.out.println(service.add(2,2));
54 * System.out.println(service2.getTemperature());
55 *
56 */
Jens Geyerd5436f52014-10-03 19:50:38 +020057 public class TMultiplexedProtocol : TProtocolDecorator
Jens Geyer72034242013-05-08 18:46:57 +020058 {
59
60 /** Used to delimit the service name from the function name */
61 public static String SEPARATOR = ":";
62
63 private String ServiceName;
64
65 /**
66 * Wrap the specified protocol, allowing it to be used to communicate with a
67 * multiplexing server. The <code>serviceName</code> is required as it is
68 * prepended to the message header so that the multiplexing server can broker
69 * the function call to the proper service.
70 *
71 * Args:
72 * protocol Your communication protocol of choice, e.g. TBinaryProtocol
73 * serviceName The service name of the service communicating via this protocol.
74 */
Jens Geyerd5436f52014-10-03 19:50:38 +020075 public TMultiplexedProtocol(TProtocol protocol, String serviceName)
Jens Geyer72034242013-05-08 18:46:57 +020076 : base(protocol)
77 {
78 ServiceName = serviceName;
79 }
80
81 /**
82 * Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR.
83 * Args:
84 * tMessage The original message.
85 */
Jens Geyerd5436f52014-10-03 19:50:38 +020086 public override void WriteMessageBegin(TMessage tMessage)
Jens Geyer72034242013-05-08 18:46:57 +020087 {
88 switch(tMessage.Type)
89 {
90 case TMessageType.Call:
91 case TMessageType.Oneway:
92 base.WriteMessageBegin(new TMessage(
93 ServiceName + SEPARATOR + tMessage.Name,
94 tMessage.Type,
95 tMessage.SeqID));
96 break;
97
98 default:
99 base.WriteMessageBegin(tMessage);
100 break;
101 }
102 }
103 }
104
105}