]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | ||
29 | namespace Thrift.Protocol | |
30 | { | |
31 | ||
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> | |
57 | public class TMultiplexedProtocol : TProtocolDecorator | |
58 | { | |
59 | ||
60 | /// <summary> | |
61 | /// Used to delimit the service name from the function name. | |
62 | /// </summary> | |
63 | public static string SEPARATOR = ":"; | |
64 | ||
65 | private string ServiceName; | |
66 | ||
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) | |
76 | : base(protocol) | |
77 | { | |
78 | ServiceName = serviceName; | |
79 | } | |
80 | ||
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> | |
85 | public override void WriteMessageBegin(TMessage tMessage) | |
86 | { | |
87 | switch (tMessage.Type) | |
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 | } | |
103 | } |