blob: 83f0ddd13e7cd389dd267dc113f42994446b8639 [file] [log] [blame]
Chris Simpsona9b6c702018-04-08 07:11:37 -04001/*
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
Alexander Edgea89036c2020-02-05 17:03:53 +000020extension String {
21 static let multiplexSeparator = ":"
22}
23
24/**
25 `TMultiplexedProtocol` is a protocol-independent concrete decorator
26 that allows a Thrift client to communicate with a multiplexing Thrift server,
27 by prepending the service name to the function name during function calls.
28
29 - Note: THIS IS NOT USED BY SERVERS. On the server, use `TMultiplexedProcessor` to handle request
30 from a multiplexing client.
31 */
Chris Simpsona9b6c702018-04-08 07:11:37 -040032public class TMultiplexedProtocol<Protocol: TProtocol>: TWrappedProtocol<Protocol> {
Chris Simpsona9b6c702018-04-08 07:11:37 -040033
34 public var serviceName = ""
35
36 public convenience init(on transport: TTransport, serviceName: String) {
37 self.init(on: transport)
38 self.serviceName = serviceName
39 }
40
41 override public func writeMessageBegin(name: String,
42 type messageType: TMessageType,
43 sequenceID: Int32) throws {
44 switch messageType {
45 case .call, .oneway:
46 var serviceFunction = serviceName
Alexander Edgea89036c2020-02-05 17:03:53 +000047 serviceFunction += serviceName == "" ? "" : .multiplexSeparator
Chris Simpsona9b6c702018-04-08 07:11:37 -040048 serviceFunction += name
49 return try super.writeMessageBegin(name: serviceFunction,
50 type: messageType,
51 sequenceID: sequenceID)
52 default:
53 return try super.writeMessageBegin(name: name,
54 type: messageType,
55 sequenceID: sequenceID)
56 }
57 }
58}