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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 unit Thrift.Protocol.Multiplex;
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.
30 NOTE: THIS IS NOT USED BY SERVERS.
31 On the server, use TMultiplexedProcessor to handle requests from a multiplexing client.
33 This example uses a single socket transport to invoke two services:
35 TSocket transport = new TSocket("localhost", 9090);
38 TBinaryProtocol protocol = new TBinaryProtocol(transport);
40 TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator");
41 Calculator.Client service = new Calculator.Client(mp);
43 TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport");
44 WeatherReport.Client service2 = new WeatherReport.Client(mp2);
46 System.out.println(service.add(2,2));
47 System.out.println(service2.getTemperature());
52 TMultiplexedProtocol = class( TProtocolDecorator)
54 { Used to delimit the service name from the function name }
58 FServiceName : String;
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.
66 protocol ....... Your communication protocol of choice, e.g. TBinaryProtocol.
67 serviceName .... The service name of the service communicating via this protocol.
69 constructor Create( const aProtocol : IProtocol; const aServiceName : string);
71 { Prepends the service name to the function name, separated by SEPARATOR.
72 Args: The original message.
74 procedure WriteMessageBegin( const msg: TThriftMessage); override;
81 constructor TMultiplexedProtocol.Create(const aProtocol: IProtocol; const aServiceName: string);
83 ASSERT( aServiceName <> '');
84 inherited Create(aProtocol);
85 FServiceName := aServiceName;
89 procedure TMultiplexedProtocol.WriteMessageBegin( const msg: TThriftMessage);
90 // Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR.
91 var newMsg : TThriftMessage;
95 TMessageType.Oneway : begin
96 Init( newMsg, FServiceName + SEPARATOR + msg.Name, msg.Type_, msg.SeqID);
97 inherited WriteMessageBegin( newMsg);
101 inherited WriteMessageBegin( msg);