Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 1 | (* |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 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 | |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 20 | unit Thrift.Protocol.Multiplex; |
| 21 | |
| 22 | interface |
| 23 | |
| 24 | uses Thrift.Protocol; |
| 25 | |
| 26 | { TMultiplexedProtocol is a protocol-independent concrete decorator |
| 27 | that allows a Thrift client to communicate with a multiplexing Thrift server, |
| 28 | by prepending the service name to the function name during function calls. |
| 29 | |
| 30 | NOTE: THIS IS NOT USED BY SERVERS. |
| 31 | On the server, use TMultiplexedProcessor to handle requests from a multiplexing client. |
| 32 | |
| 33 | This example uses a single socket transport to invoke two services: |
| 34 | |
| 35 | TSocket transport = new TSocket("localhost", 9090); |
| 36 | transport.open(); |
| 37 | |
| 38 | TBinaryProtocol protocol = new TBinaryProtocol(transport); |
| 39 | |
| 40 | TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator"); |
| 41 | Calculator.Client service = new Calculator.Client(mp); |
| 42 | |
| 43 | TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport"); |
| 44 | WeatherReport.Client service2 = new WeatherReport.Client(mp2); |
| 45 | |
| 46 | System.out.println(service.add(2,2)); |
| 47 | System.out.println(service2.getTemperature()); |
| 48 | |
| 49 | } |
| 50 | |
| 51 | type |
| 52 | TMultiplexedProtocol = class( TProtocolDecorator) |
| 53 | public const |
| 54 | { Used to delimit the service name from the function name } |
| 55 | SEPARATOR = ':'; |
| 56 | |
| 57 | private |
| 58 | FServiceName : String; |
| 59 | |
| 60 | public |
| 61 | { Wrap the specified protocol, allowing it to be used to communicate with a multiplexing server. |
| 62 | The serviceName is required as it is prepended to the message header so that the multiplexing |
| 63 | server can broker the function call to the proper service. |
| 64 | |
| 65 | Args: |
| 66 | protocol ....... Your communication protocol of choice, e.g. TBinaryProtocol. |
| 67 | serviceName .... The service name of the service communicating via this protocol. |
| 68 | } |
| 69 | constructor Create( const aProtocol : IProtocol; const aServiceName : string); |
| 70 | |
| 71 | { Prepends the service name to the function name, separated by SEPARATOR. |
| 72 | Args: The original message. |
| 73 | } |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 74 | procedure WriteMessageBegin( const msg: TThriftMessage); override; |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 75 | end; |
| 76 | |
| 77 | |
| 78 | implementation |
| 79 | |
| 80 | |
| 81 | constructor TMultiplexedProtocol.Create(const aProtocol: IProtocol; const aServiceName: string); |
| 82 | begin |
| 83 | ASSERT( aServiceName <> ''); |
| 84 | inherited Create(aProtocol); |
| 85 | FServiceName := aServiceName; |
| 86 | end; |
| 87 | |
| 88 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 89 | procedure TMultiplexedProtocol.WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 90 | // Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR. |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 91 | var newMsg : TThriftMessage; |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 92 | begin |
| 93 | case msg.Type_ of |
| 94 | TMessageType.Call, |
| 95 | TMessageType.Oneway : begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame^] | 96 | Init( newMsg, FServiceName + SEPARATOR + msg.Name, msg.Type_, msg.SeqID); |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 97 | inherited WriteMessageBegin( newMsg); |
| 98 | end; |
| 99 | |
| 100 | else |
| 101 | inherited WriteMessageBegin( msg); |
| 102 | end; |
| 103 | end; |
| 104 | |
| 105 | |
| 106 | end. |
| 107 | |