Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame^] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System.Threading; |
| 19 | using System.Threading.Tasks; |
| 20 | using Thrift.Protocol.Entities; |
| 21 | |
| 22 | namespace Thrift.Protocol |
| 23 | { |
| 24 | /** |
| 25 | * TMultiplexedProtocol is a protocol-independent concrete decorator that allows a Thrift |
| 26 | * client to communicate with a multiplexing Thrift server, by prepending the service name |
| 27 | * to the function name during function calls. |
| 28 | * |
| 29 | * NOTE: THIS IS NOT TO BE USED BY SERVERS. |
| 30 | * On the server, use TMultiplexedProcessor to handle requests from a multiplexing client. |
| 31 | * |
| 32 | * This example uses a single socket transport to invoke two services: |
| 33 | * |
| 34 | * TSocketTransport transport = new TSocketTransport("localhost", 9090); |
| 35 | * transport.open(); |
| 36 | * |
| 37 | * TBinaryProtocol protocol = new TBinaryProtocol(transport); |
| 38 | * |
| 39 | * TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator"); |
| 40 | * Calculator.Client service = new Calculator.Client(mp); |
| 41 | * |
| 42 | * TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport"); |
| 43 | * WeatherReport.Client service2 = new WeatherReport.Client(mp2); |
| 44 | * |
| 45 | * System.out.println(service.add(2,2)); |
| 46 | * System.out.println(service2.getTemperature()); |
| 47 | * |
| 48 | */ |
| 49 | |
| 50 | //TODO: implementation of TProtocol |
| 51 | |
| 52 | // ReSharper disable once InconsistentNaming |
| 53 | public class TMultiplexedProtocol : TProtocolDecorator |
| 54 | { |
| 55 | /** Used to delimit the service name from the function name */ |
| 56 | public const string Separator = ":"; |
| 57 | |
| 58 | private readonly string _serviceName; |
| 59 | |
| 60 | /** |
| 61 | * Wrap the specified protocol, allowing it to be used to communicate with a |
| 62 | * multiplexing server. The <code>serviceName</code> is required as it is |
| 63 | * prepended to the message header so that the multiplexing server can broker |
| 64 | * the function call to the proper service. |
| 65 | * |
| 66 | * Args: |
| 67 | * protocol Your communication protocol of choice, e.g. TBinaryProtocol |
| 68 | * serviceName The service name of the service communicating via this protocol. |
| 69 | */ |
| 70 | |
| 71 | public TMultiplexedProtocol(TProtocol protocol, string serviceName) |
| 72 | : base(protocol) |
| 73 | { |
| 74 | _serviceName = serviceName; |
| 75 | } |
| 76 | |
| 77 | public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) |
| 78 | { |
| 79 | switch (message.Type) |
| 80 | { |
| 81 | case TMessageType.Call: |
| 82 | case TMessageType.Oneway: |
| 83 | await base.WriteMessageBeginAsync(new TMessage($"{_serviceName}{Separator}{message.Name}", message.Type, message.SeqID), cancellationToken); |
| 84 | break; |
| 85 | default: |
| 86 | await base.WriteMessageBeginAsync(message, cancellationToken); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |