Jens Geyer | 7203424 | 2013-05-08 18:46:57 +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 | * 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 | |
| 24 | using System; |
| 25 | using System.Text; |
| 26 | using Thrift.Transport; |
| 27 | using System.Collections.Generic; |
| 28 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 29 | namespace Thrift.Protocol |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 30 | { |
| 31 | |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame^] | 32 | /// <summary> |
| 33 | /// 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 |
| 35 | /// to the function name during function calls. |
| 36 | /// <para/> |
| 37 | /// NOTE: THIS IS NOT TO BE USED BY SERVERS. |
| 38 | /// On the server, use TMultiplexedProcessor to handle requests from a multiplexing client. |
| 39 | /// <para/> |
| 40 | /// This example uses a single socket transport to invoke two services: |
| 41 | /// <code> |
| 42 | /// TSocket transport = new TSocket("localhost", 9090); |
| 43 | /// transport.open(); |
| 44 | /// |
| 45 | /// 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 | /// </code> |
| 56 | /// </summary> |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 57 | public class TMultiplexedProtocol : TProtocolDecorator |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 58 | { |
| 59 | |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame^] | 60 | /// <summary> |
| 61 | /// Used to delimit the service name from the function name. |
| 62 | /// </summary> |
| 63 | public static string SEPARATOR = ":"; |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 64 | |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame^] | 65 | private string ServiceName; |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 66 | |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame^] | 67 | /// <summary> |
| 68 | /// Wrap the specified protocol, allowing it to be used to communicate with a |
| 69 | /// multiplexing server. The <paramref name="serviceName"/> is required as it is |
| 70 | /// prepended to the message header so that the multiplexing server can broker |
| 71 | /// the function call to the proper service. |
| 72 | /// </summary> |
| 73 | /// <param name="protocol">Your communication protocol of choice, e.g. <see cref="TBinaryProtocol"/>.</param> |
| 74 | /// <param name="serviceName">The service name of the service communicating via this protocol.</param> |
| 75 | public TMultiplexedProtocol(TProtocol protocol, string serviceName) |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 76 | : base(protocol) |
| 77 | { |
| 78 | ServiceName = serviceName; |
| 79 | } |
| 80 | |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame^] | 81 | /// <summary> |
| 82 | /// Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR. |
| 83 | /// </summary> |
| 84 | /// <param name="tMessage">The original message.</param> |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 85 | public override void WriteMessageBegin(TMessage tMessage) |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 86 | { |
Christian Weiss | 8fb719e | 2018-03-30 21:26:04 +0200 | [diff] [blame^] | 87 | switch (tMessage.Type) |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 88 | { |
| 89 | case TMessageType.Call: |
| 90 | case TMessageType.Oneway: |
| 91 | base.WriteMessageBegin(new TMessage( |
| 92 | ServiceName + SEPARATOR + tMessage.Name, |
| 93 | tMessage.Type, |
| 94 | tMessage.SeqID)); |
| 95 | break; |
| 96 | |
| 97 | default: |
| 98 | base.WriteMessageBegin(tMessage); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | } |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 103 | } |