1<?php
2/*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 * @package thrift.protocol
21 */
22
23namespace Thrift\Protocol;
24
25use Thrift\Type\TMessageType;
26
27/**
28 * <code>TMultiplexedProtocol</code> is a protocol-independent concrete decorator
29 * that allows a Thrift client to communicate with a multiplexing Thrift server,
30 * by prepending the service name to the function name during function calls.
31 *
32 * @package Thrift\Protocol
33 */
34class TMultiplexedProtocol extends TProtocolDecorator
35{
36    /**
37     * Separator between service name and function name.
38     * Should be the same as used at multiplexed Thrift server.
39     *
40     * @var string
41     */
42    const SEPARATOR = ":";
43
44    /**
45     * The name of service.
46     *
47     * @var string
48     */
49    private $serviceName_;
50
51    /**
52     * Constructor of <code>TMultiplexedProtocol</code> class.
53     *
54     * Wrap the specified protocol, allowing it to be used to communicate with a
55     * multiplexing server.  The <code>$serviceName</code> is required as it is
56     * prepended to the message header so that the multiplexing server can broker
57     * the function call to the proper service.
58     *
59     * @param TProtocol $protocol
60     * @param string    $serviceName The name of service.
61     */
62    public function __construct(TProtocol $protocol, $serviceName)
63    {
64        parent::__construct($protocol);
65        $this->serviceName_ = $serviceName;
66    }
67
68    /**
69     * Writes the message header.
70     * Prepends the service name to the function name, separated by <code>TMultiplexedProtocol::SEPARATOR</code>.
71     *
72     * @param string $name  Function name.
73     * @param int    $type  Message type.
74     * @param int    $seqid The sequence id of this message.
75     */
76    public function writeMessageBegin($name, $type, $seqid)
77    {
78        if ($type == TMessageType::CALL || $type == TMessageType::ONEWAY) {
79            $nameWithService = $this->serviceName_ . self::SEPARATOR . $name;
80            parent::writeMessageBegin($nameWithService, $type, $seqid);
81        } else {
82            parent::writeMessageBegin($name, $type, $seqid);
83        }
84    }
85}
86