David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 1 | %% |
| 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 Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 27 | -export([ |
| 28 | new/2, |
| 29 | read/2, |
| 30 | write/2, |
| 31 | flush_transport/1, |
| 32 | close_transport/1 |
| 33 | ]). |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 34 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 35 | -record(protocol, { |
| 36 | module :: module(), |
| 37 | data :: term() |
| 38 | }). |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 39 | -type protocol() :: #protocol{}. |
| 40 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 41 | -record(multiplexed_protocol, { |
| 42 | protocol_module_to_decorate :: atom(), |
| 43 | protocol_data_to_decorate :: term(), |
| 44 | service_name :: nonempty_string() |
| 45 | }). |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 46 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 47 | -spec new(ProtocolToDecorate :: protocol(), ServiceName :: nonempty_string()) -> |
| 48 | {ok, Protocol :: protocol()}. |
| 49 | new(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 Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 58 | thrift_protocol:new(?MODULE, State). |
| 59 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 60 | flush_transport( |
| 61 | State = #multiplexed_protocol{ |
| 62 | protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 63 | protocol_data_to_decorate = State0 |
| 64 | } |
| 65 | ) -> |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 66 | {State1, ok} = ProtocolModuleToDecorate:flush_transport(State0), |
| 67 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}. |
| 68 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 69 | close_transport( |
| 70 | State = #multiplexed_protocol{ |
| 71 | protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 72 | protocol_data_to_decorate = State0 |
| 73 | } |
| 74 | ) -> |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 75 | {State1, ok} = ProtocolModuleToDecorate:close_transport(State0), |
| 76 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}. |
| 77 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 78 | write( |
| 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 Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 95 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}; |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 96 | write( |
| 97 | State = #multiplexed_protocol{ |
| 98 | protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 99 | protocol_data_to_decorate = State0 |
| 100 | }, |
| 101 | Message |
| 102 | ) -> |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 103 | {State1, ok} = ProtocolModuleToDecorate:write(State0, Message), |
| 104 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}. |
| 105 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 106 | read( |
| 107 | State = #multiplexed_protocol{ |
| 108 | protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 109 | protocol_data_to_decorate = State0 |
| 110 | }, |
| 111 | Message |
| 112 | ) -> |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 113 | {State1, Result} = ProtocolModuleToDecorate:read(State0, Message), |
| 114 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, Result}. |