THRIFT-1213 - make membuffer in erlang more useful

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1137121 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Anthony F. Molinaro 2011-06-18 03:54:04 +00:00
parent a1c416fbbd
commit 3d3f42103c
2 changed files with 87 additions and 56 deletions

View File

@ -22,7 +22,7 @@
-behaviour(thrift_transport).
%% API
-export([new/0, new_transport_factory/0]).
-export([new/0, new/1, new_transport_factory/0]).
%% thrift_transport callbacks
-export([write/2, read/2, flush/1, close/1]).
@ -35,6 +35,13 @@ new() ->
State = #memory_buffer{buffer = []},
thrift_transport:new(?MODULE, State).
new (Buf) when is_list (Buf) ->
State = #memory_buffer{buffer = Buf},
thrift_transport:new(?MODULE, State);
new (Buf) ->
State = #memory_buffer{buffer = [Buf]},
thrift_transport:new(?MODULE, State).
new_transport_factory() ->
{ok, fun() -> new() end}.
@ -42,8 +49,8 @@ new_transport_factory() ->
write(State = #memory_buffer{buffer = Buf}, Data) ->
{State#memory_buffer{buffer = [Buf, Data]}, ok}.
flush(State) ->
{State, ok}.
flush(State = #memory_buffer {buffer = Buf}) ->
{State#memory_buffer{buffer = []}, Buf}.
close(State) ->
{State, ok}.

View File

@ -23,10 +23,12 @@
-include("thriftTest_types.hrl").
test_data() ->
#xtruct{string_thing = <<"foobar">>,
#xtruct {
string_thing = <<"foobar">>,
byte_thing = 123,
i32_thing = 1234567,
i64_thing = 12345678900}.
i64_thing = 12345678900
}.
t1() ->
{ok, Transport} = thrift_memory_buffer:new(),
@ -38,7 +40,6 @@ t1() ->
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('xtruct'))},
'xtruct'),
Result = TestData.
@ -87,10 +88,33 @@ t4() ->
TestData = Result.
t5() ->
% test writing to a buffer, getting the bytes out, putting them
% in a new buffer and reading them
% here's the writing part
{ok, Transport0} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport0),
TestData = test_data(),
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('xtruct'))},
TestData}),
% flush now returns the buffer
{_Protocol2, Buf} = thrift_protocol:flush_transport (Protocol1),
% now the reading part
{ok, T2} = thrift_memory_buffer:new (Buf),
{ok, P2} = thrift_binary_protocol:new(T2),
{_, {ok, Result}} = thrift_protocol:read(P2,
{struct, element(2, thriftTest_types:struct_info('xtruct'))},
'xtruct'),
Result = TestData.
t() ->
t1(),
t2(),
t3(),
t4().
t4(),
t5().