Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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_memory_buffer).
21
 
22
-behaviour(gen_server).
23
-behaviour(thrift_transport).
24
 
25
%% API
26
-export([new/0, new_transport_factory/0]).
27
 
28
%% gen_server callbacks
29
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
30
         terminate/2, code_change/3]).
31
 
32
%% thrift_transport callbacks
33
-export([write/2, read/2, flush/1, close/1]).
34
 
35
-record(memory_buffer, {buffer}).
36
 
37
%%====================================================================
38
%% API
39
%%====================================================================
40
new() ->
41
    case gen_server:start_link(?MODULE, [], []) of
42
        {ok, Pid} ->
43
            thrift_transport:new(?MODULE, Pid);
44
        Else ->
45
            Else
46
    end.
47
 
48
new_transport_factory() ->
49
    {ok, fun() -> new() end}.
50
 
51
%%--------------------------------------------------------------------
52
%% Function: write(Transport, Data) -> ok
53
%%
54
%% Data = iolist()
55
%%
56
%% Description: Writes data into the buffer
57
%%--------------------------------------------------------------------
58
write(Transport, Data) ->
59
    gen_server:call(Transport, {write, Data}).
60
 
61
%%--------------------------------------------------------------------
62
%% Function: flush(Transport) -> ok
63
%%
64
%% Description: Flushes the buffer through to the wrapped transport
65
%%--------------------------------------------------------------------
66
flush(Transport) ->
67
    gen_server:call(Transport, flush).
68
 
69
%%--------------------------------------------------------------------
70
%% Function: close(Transport) -> ok
71
%%
72
%% Description: Closes the transport and the wrapped transport
73
%%--------------------------------------------------------------------
74
close(Transport) ->
75
    gen_server:cast(Transport, close).
76
 
77
%%--------------------------------------------------------------------
78
%% Function: Read(Transport, Len) -> {ok, Data}
79
%%
80
%% Data = binary()
81
%%
82
%% Description: Reads data through from the wrapped transoprt
83
%%--------------------------------------------------------------------
84
read(Transport, Len) when is_integer(Len) ->
85
    gen_server:call(Transport, {read, Len}).
86
 
87
%%====================================================================
88
%% gen_server callbacks
89
%%====================================================================
90
 
91
%%--------------------------------------------------------------------
92
%% Function: init(Args) -> {ok, State} |
93
%%                         {ok, State, Timeout} |
94
%%                         ignore               |
95
%%                         {stop, Reason}
96
%% Description: Initiates the server
97
%%--------------------------------------------------------------------
98
init([]) ->
99
    {ok, #memory_buffer{buffer = []}}.
100
 
101
%%--------------------------------------------------------------------
102
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
103
%%                                      {reply, Reply, State, Timeout} |
104
%%                                      {noreply, State} |
105
%%                                      {noreply, State, Timeout} |
106
%%                                      {stop, Reason, Reply, State} |
107
%%                                      {stop, Reason, State}
108
%% Description: Handling call messages
109
%%--------------------------------------------------------------------
110
handle_call({write, Data}, _From, State = #memory_buffer{buffer = Buf}) ->
111
    {reply, ok, State#memory_buffer{buffer = [Buf, Data]}};
112
 
113
handle_call({read, Len}, _From, State = #memory_buffer{buffer = Buf}) ->
114
    Binary = iolist_to_binary(Buf),
115
    Give = min(iolist_size(Binary), Len),
116
    {Result, Remaining} = split_binary(Binary, Give),
117
    {reply, {ok, Result}, State#memory_buffer{buffer = Remaining}};
118
 
119
handle_call(flush, _From, State) ->
120
    {reply, ok, State}.
121
 
122
%%--------------------------------------------------------------------
123
%% Function: handle_cast(Msg, State) -> {noreply, State} |
124
%%                                      {noreply, State, Timeout} |
125
%%                                      {stop, Reason, State}
126
%% Description: Handling cast messages
127
%%--------------------------------------------------------------------
128
handle_cast(close, State) ->
129
    {stop, normal, State};
130
handle_cast(Msg, State=#memory_buffer{}) ->
131
    {noreply, State}.
132
 
133
%%--------------------------------------------------------------------
134
%% Function: handle_info(Info, State) -> {noreply, State} |
135
%%                                       {noreply, State, Timeout} |
136
%%                                       {stop, Reason, State}
137
%% Description: Handling all non call/cast messages
138
%%--------------------------------------------------------------------
139
handle_info(_Info, State) ->
140
    {noreply, State}.
141
 
142
%%--------------------------------------------------------------------
143
%% Function: terminate(Reason, State) -> void()
144
%% Description: This function is called by a gen_server when it is about to
145
%% terminate. It should be the opposite of Module:init/1 and do any necessary
146
%% cleaning up. When it returns, the gen_server terminates with Reason.
147
%% The return value is ignored.
148
%%--------------------------------------------------------------------
149
terminate(_Reason, _State) ->
150
    ok.
151
 
152
%%--------------------------------------------------------------------
153
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
154
%% Description: Convert process state when code is changed
155
%%--------------------------------------------------------------------
156
code_change(_OldVsn, State, _Extra) ->
157
    {ok, State}.
158
 
159
%%--------------------------------------------------------------------
160
%%% Internal functions
161
%%--------------------------------------------------------------------
162
min(A,B) when A<B -> A;
163
min(_,B)          -> B.
164