blob: ddc9475576b8dec848d1cdf2102e4f47da510bf0 [file] [log] [blame]
David Robakowskiae971ce2013-08-02 12:16:00 +02001%%
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
20-module(thrift_multiplexed_protocol).
21
22-behaviour(thrift_protocol).
23
24-include("thrift_constants.hrl").
25-include("thrift_protocol.hrl").
26
Sergei Elin45764092022-09-23 23:21:31 +030027-export([
28 new/2,
29 read/2,
30 write/2,
31 flush_transport/1,
32 close_transport/1
33]).
David Robakowskiae971ce2013-08-02 12:16:00 +020034
Sergei Elin45764092022-09-23 23:21:31 +030035-record(protocol, {
36 module :: module(),
37 data :: term()
38}).
David Robakowskiae971ce2013-08-02 12:16:00 +020039-type protocol() :: #protocol{}.
40
Sergei Elin45764092022-09-23 23:21:31 +030041-record(multiplexed_protocol, {
42 protocol_module_to_decorate :: atom(),
43 protocol_data_to_decorate :: term(),
44 service_name :: nonempty_string()
45}).
David Robakowskiae971ce2013-08-02 12:16:00 +020046
Sergei Elin45764092022-09-23 23:21:31 +030047-spec new(ProtocolToDecorate :: protocol(), ServiceName :: nonempty_string()) ->
48 {ok, Protocol :: protocol()}.
49new(ProtocolToDecorate, ServiceName) when
50 is_record(ProtocolToDecorate, protocol),
51 is_list(ServiceName)
52->
53 State = #multiplexed_protocol{
54 protocol_module_to_decorate = ProtocolToDecorate#protocol.module,
55 protocol_data_to_decorate = ProtocolToDecorate#protocol.data,
56 service_name = ServiceName
57 },
David Robakowskiae971ce2013-08-02 12:16:00 +020058 thrift_protocol:new(?MODULE, State).
59
Sergei Elin45764092022-09-23 23:21:31 +030060flush_transport(
61 State = #multiplexed_protocol{
62 protocol_module_to_decorate = ProtocolModuleToDecorate,
63 protocol_data_to_decorate = State0
64 }
65) ->
David Robakowskiae971ce2013-08-02 12:16:00 +020066 {State1, ok} = ProtocolModuleToDecorate:flush_transport(State0),
67 {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}.
68
Sergei Elin45764092022-09-23 23:21:31 +030069close_transport(
70 State = #multiplexed_protocol{
71 protocol_module_to_decorate = ProtocolModuleToDecorate,
72 protocol_data_to_decorate = State0
73 }
74) ->
David Robakowskiae971ce2013-08-02 12:16:00 +020075 {State1, ok} = ProtocolModuleToDecorate:close_transport(State0),
76 {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}.
77
Sergei Elin45764092022-09-23 23:21:31 +030078write(
79 State = #multiplexed_protocol{
80 protocol_module_to_decorate = ProtocolModuleToDecorate,
81 protocol_data_to_decorate = State0,
82 service_name = ServiceName
83 },
84 Message = #protocol_message_begin{name = Name}
85) ->
86 {State1, ok} = ProtocolModuleToDecorate:write(
87 State0,
88 Message#protocol_message_begin{
89 name =
90 ServiceName ++
91 ?MULTIPLEXED_SERVICE_SEPARATOR ++
92 Name
93 }
94 ),
David Robakowskiae971ce2013-08-02 12:16:00 +020095 {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok};
Sergei Elin45764092022-09-23 23:21:31 +030096write(
97 State = #multiplexed_protocol{
98 protocol_module_to_decorate = ProtocolModuleToDecorate,
99 protocol_data_to_decorate = State0
100 },
101 Message
102) ->
David Robakowskiae971ce2013-08-02 12:16:00 +0200103 {State1, ok} = ProtocolModuleToDecorate:write(State0, Message),
104 {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}.
105
Sergei Elin45764092022-09-23 23:21:31 +0300106read(
107 State = #multiplexed_protocol{
108 protocol_module_to_decorate = ProtocolModuleToDecorate,
109 protocol_data_to_decorate = State0
110 },
111 Message
112) ->
David Robakowskiae971ce2013-08-02 12:16:00 +0200113 {State1, Result} = ProtocolModuleToDecorate:read(State0, Message),
114 {State#multiplexed_protocol{protocol_data_to_decorate = State1}, Result}.