blob: 420281c7de12494598f9b8fc79340093eea55d00 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
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
David Reissac549552008-06-10 22:56:59 +000020-module(thrift_transport).
21
David Reiss3b9c3422008-06-11 00:59:19 +000022-export([behaviour_info/1]).
David Reissac549552008-06-10 22:56:59 +000023
David Reiss3b9c3422008-06-11 00:59:19 +000024-export([new/2,
David Reissac549552008-06-10 22:56:59 +000025 write/2,
David Reiss90b40832008-06-10 22:58:52 +000026 read/2,
David Reissc11734e2008-06-11 00:59:48 +000027 flush/1,
28 close/1
David Reissac549552008-06-10 22:56:59 +000029 ]).
30
31behaviour_info(callbacks) ->
David Reiss3b9c3422008-06-11 00:59:19 +000032 [{read, 2},
33 {write, 2},
David Reissc11734e2008-06-11 00:59:48 +000034 {flush, 1},
35 {close, 1}
David Reiss3b9c3422008-06-11 00:59:19 +000036 ].
David Reissac549552008-06-10 22:56:59 +000037
David Reiss80664fe2008-06-11 01:00:30 +000038-record(transport, {module, data}).
David Reissac549552008-06-10 22:56:59 +000039
Bryan Duxburyd3879f82010-08-19 05:06:02 +000040new(Module, Data) when is_atom(Module) ->
David Reissf32d0fb2010-08-30 22:05:00 +000041 {ok, #transport{module = Module,
42 data = Data}}.
Bryan Duxburyd3879f82010-08-19 05:06:02 +000043
David Reiss9b7fbe32010-08-30 22:05:20 +000044-spec write(#transport{}, iolist() | binary()) -> ok | {error, _Reason}.
David Reiss914ebb42008-06-11 01:01:48 +000045write(Transport, Data) ->
David Reissac549552008-06-10 22:56:59 +000046 Module = Transport#transport.module,
David Reissf32d0fb2010-08-30 22:05:00 +000047 Module:write(Transport#transport.data, Data).
David Reissac549552008-06-10 22:56:59 +000048
David Reiss9b7fbe32010-08-30 22:05:20 +000049-spec read(#transport{}, non_neg_integer()) -> {ok, binary()} | {error, _Reason}.
David Reissac549552008-06-10 22:56:59 +000050read(Transport, Len) when is_integer(Len) ->
51 Module = Transport#transport.module,
David Reissf32d0fb2010-08-30 22:05:00 +000052 Module:read(Transport#transport.data, Len).
David Reiss90b40832008-06-10 22:58:52 +000053
David Reiss9b7fbe32010-08-30 22:05:20 +000054-spec flush(#transport{}) -> ok | {error, _Reason}.
David Reissf32d0fb2010-08-30 22:05:00 +000055flush(#transport{module = Module, data = Data}) ->
56 Module:flush(Data).
David Reissc11734e2008-06-11 00:59:48 +000057
David Reiss9b7fbe32010-08-30 22:05:20 +000058-spec close(#transport{}) -> ok | {error, _Reason}.
David Reissf32d0fb2010-08-30 22:05:00 +000059close(#transport{module = Module, data = Data}) ->
60 Module:close(Data).