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 | |
| 32 | /** |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 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 |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 35 | * to the function name during function calls. |
| 36 | * |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 37 | * NOTE: THIS IS NOT TO BE USED BY SERVERS. |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 38 | * 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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 44 | * |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 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 | * |
| 56 | */ |
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 | |
| 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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 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 | |
| 81 | /** |
| 82 | * Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR. |
| 83 | * Args: |
| 84 | * tMessage The original message. |
| 85 | */ |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 86 | public override void WriteMessageBegin(TMessage tMessage) |
Jens Geyer | 7203424 | 2013-05-08 18:46:57 +0200 | [diff] [blame] | 87 | { |
| 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 | } |