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 package org.apache.thrift.protocol;
22 import org.apache.thrift.transport.TTransport;
26 * TMultiplexedProtocol is a protocol-independent concrete decorator that allows a Thrift
27 * client to communicate with a multiplexing Thrift server, by prepending the service name
28 * to the function name during function calls.
30 * NOTE: THIS IS NOT TO BE 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());
50 class TMultiplexedProtocol extends TProtocolDecorator {
52 /** Used to delimit the service name from the function name */
53 public static inline var SEPARATOR : String = ":";
55 private var service : String;
58 * Wrap the specified protocol, allowing it to be used to communicate with a
59 * multiplexing server. The <code>serviceName</code> is required as it is
60 * prepended to the message header so that the multiplexing server can broker
61 * the function call to the proper service.
64 * protocol Your communication protocol of choice, e.g. TBinaryProtocol
65 * serviceName The service name of the service communicating via this protocol.
67 public function new( protocol : TProtocol, serviceName : String) {
69 service = serviceName;
73 * Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR.
75 * tMessage The original message.
77 public override function writeMessageBegin( message : TMessage) : Void {
80 case TMessageType.CALL:
81 super.writeMessageBegin(new TMessage(
82 service + SEPARATOR + message.name,
86 case TMessageType.ONEWAY:
87 super.writeMessageBegin(new TMessage(
88 service + SEPARATOR + message.name,
93 super.writeMessageBegin(message);