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 | |
| 27 | -include("thrift_protocol_behaviour.hrl"). |
| 28 | |
| 29 | -export([new/2, |
| 30 | read/2, |
| 31 | write/2, |
| 32 | flush_transport/1, |
| 33 | close_transport/1 |
| 34 | ]). |
| 35 | |
| 36 | -record(protocol, {module, data}). |
| 37 | -type protocol() :: #protocol{}. |
| 38 | |
| 39 | -record (multiplexed_protocol, {protocol_module_to_decorate::atom(), |
| 40 | protocol_data_to_decorate::term(), |
| 41 | service_name::nonempty_string()}). |
| 42 | |
| 43 | -type state() :: #multiplexed_protocol{}. |
| 44 | |
| 45 | -spec new(ProtocolToDecorate::protocol(), ServiceName::nonempty_string()) -> {ok, Protocol::protocol()}. |
| 46 | new(ProtocolToDecorate, ServiceName) when is_record(ProtocolToDecorate, protocol), |
| 47 | is_list(ServiceName) -> |
| 48 | State = #multiplexed_protocol{protocol_module_to_decorate = ProtocolToDecorate#protocol.module, |
| 49 | protocol_data_to_decorate = ProtocolToDecorate#protocol.data, |
| 50 | service_name = ServiceName}, |
| 51 | thrift_protocol:new(?MODULE, State). |
| 52 | |
| 53 | flush_transport(State = #multiplexed_protocol{protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 54 | protocol_data_to_decorate = State0}) -> |
| 55 | {State1, ok} = ProtocolModuleToDecorate:flush_transport(State0), |
| 56 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}. |
| 57 | |
| 58 | close_transport(State = #multiplexed_protocol{protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 59 | protocol_data_to_decorate = State0}) -> |
| 60 | {State1, ok} = ProtocolModuleToDecorate:close_transport(State0), |
| 61 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}. |
| 62 | |
| 63 | write(State = #multiplexed_protocol{protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 64 | protocol_data_to_decorate = State0, |
| 65 | service_name = ServiceName}, |
| 66 | Message = #protocol_message_begin{name = Name}) -> |
| 67 | {State1, ok} = ProtocolModuleToDecorate:write(State0, |
| 68 | Message#protocol_message_begin{name=ServiceName ++ |
| 69 | ?MULTIPLEXED_SERVICE_SEPARATOR ++ |
| 70 | Name}), |
| 71 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}; |
| 72 | |
| 73 | write(State = #multiplexed_protocol{protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 74 | protocol_data_to_decorate = State0}, |
| 75 | Message) -> |
| 76 | {State1, ok} = ProtocolModuleToDecorate:write(State0, Message), |
| 77 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, ok}. |
| 78 | |
| 79 | read(State = #multiplexed_protocol{protocol_module_to_decorate = ProtocolModuleToDecorate, |
| 80 | protocol_data_to_decorate = State0}, |
| 81 | Message) -> |
| 82 | {State1, Result} = ProtocolModuleToDecorate:read(State0, Message), |
| 83 | {State#multiplexed_protocol{protocol_data_to_decorate = State1}, Result}. |