blob: e5e0cd9fe3521c43b9b4be466c50ce912c0993d5 [file] [log] [blame]
Jens Geyer8a701962013-03-25 01:28:12 +02001(*
Jens Geyerd5436f52014-10-03 19:50:38 +02002 * 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 Geyer8a701962013-03-25 01:28:12 +020020unit Thrift.Protocol.Multiplex;
21
22interface
23
24uses 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
51type
52 TMultiplexedProtocol = class( TProtocolDecorator)
53 public const
54 { Used to delimit the service name from the function name }
55 SEPARATOR = ':';
56
Jens Geyerfad7fd32019-11-09 23:24:52 +010057 strict private
Jens Geyer8a701962013-03-25 01:28:12 +020058 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 Geyer17c3ad92017-09-05 20:31:27 +020074 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyer8a701962013-03-25 01:28:12 +020075 end;
76
77
78implementation
79
80
81constructor TMultiplexedProtocol.Create(const aProtocol: IProtocol; const aServiceName: string);
82begin
83 ASSERT( aServiceName <> '');
84 inherited Create(aProtocol);
85 FServiceName := aServiceName;
86end;
87
88
Jens Geyer17c3ad92017-09-05 20:31:27 +020089procedure TMultiplexedProtocol.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyer8a701962013-03-25 01:28:12 +020090// Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR.
Jens Geyer17c3ad92017-09-05 20:31:27 +020091var newMsg : TThriftMessage;
Jens Geyer8a701962013-03-25 01:28:12 +020092begin
93 case msg.Type_ of
94 TMessageType.Call,
95 TMessageType.Oneway : begin
Jens Geyer17c3ad92017-09-05 20:31:27 +020096 Init( newMsg, FServiceName + SEPARATOR + msg.Name, msg.Type_, msg.SeqID);
Jens Geyer8a701962013-03-25 01:28:12 +020097 inherited WriteMessageBegin( newMsg);
98 end;
99
100 else
101 inherited WriteMessageBegin( msg);
102 end;
103end;
104
105
106end.
107