You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2010/08/31 00:05:47 UTC

svn commit: r990992 - /incubator/thrift/trunk/lib/erl/src/thrift_framed_transport.erl

Author: dreiss
Date: Mon Aug 30 22:05:47 2010
New Revision: 990992

URL: http://svn.apache.org/viewvc?rev=990992&view=rev
Log:
erlang: Don't use a separate process for framed_transport

Modified:
    incubator/thrift/trunk/lib/erl/src/thrift_framed_transport.erl

Modified: incubator/thrift/trunk/lib/erl/src/thrift_framed_transport.erl
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/erl/src/thrift_framed_transport.erl?rev=990992&r1=990991&r2=990992&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/erl/src/thrift_framed_transport.erl (original)
+++ incubator/thrift/trunk/lib/erl/src/thrift_framed_transport.erl Mon Aug 30 22:05:47 2010
@@ -19,16 +19,11 @@
 
 -module(thrift_framed_transport).
 
--behaviour(gen_server).
 -behaviour(thrift_transport).
 
 %% API
 -export([new/1]).
 
-%% gen_server callbacks
--export([init/1, handle_call/3, handle_cast/2, handle_info/2,
-         terminate/2, code_change/3]).
-
 %% thrift_transport callbacks
 -export([write/2, read/2, flush/1, close/1]).
 
@@ -36,90 +31,41 @@
                            read_buffer, % iolist()
                            write_buffer % iolist()
                           }).
--type state() :: pid().
+-type state() :: #framed_transport{}.
 -include("thrift_transport_impl.hrl").
 
-%%====================================================================
-%% API
-%%====================================================================
-%%--------------------------------------------------------------------
-%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
-%% Description: Starts the server
-%%--------------------------------------------------------------------
 new(WrappedTransport) ->
-    case gen_server:start_link(?MODULE, [WrappedTransport], []) of
-        {ok, Pid} ->
-            thrift_transport:new(?MODULE, Pid);
-        Else ->
-            Else
-    end.
-
-%%--------------------------------------------------------------------
-%% Function: write(Transport, Data) -> ok
-%%
-%% Data = iolist()
-%%
-%% Description: Writes data into the buffer
-%%--------------------------------------------------------------------
-write(Transport, Data) ->
-    {Transport, gen_server:call(Transport, {write, Data})}.
-
-%%--------------------------------------------------------------------
-%% Function: flush(Transport) -> ok
-%%
-%% Description: Flushes the buffer through to the wrapped transport
-%%--------------------------------------------------------------------
-flush(Transport) ->
-    {Transport, gen_server:call(Transport, flush)}.
+    State = #framed_transport{wrapped = WrappedTransport,
+                              read_buffer = [],
+                              write_buffer = []},
+    thrift_transport:new(?MODULE, State).
+
+%% Writes data into the buffer
+write(State = #framed_transport{write_buffer = WBuf}, Data) ->
+    {State#framed_transport{write_buffer = [WBuf, Data]}, ok}.
 
-%%--------------------------------------------------------------------
-%% Function: close(Transport) -> ok
-%%
-%% Description: Closes the transport and the wrapped transport
-%%--------------------------------------------------------------------
-close(Transport) ->
-    {Transport, gen_server:cast(Transport, close)}.
+%% Flushes the buffer through to the wrapped transport
+flush(State0 = #framed_transport{write_buffer = Buffer,
+                                   wrapped = Wrapped0}) ->
+    FrameLen = iolist_size(Buffer),
+    Data     = [<<FrameLen:32/integer-signed-big>>, Buffer],
 
-%%--------------------------------------------------------------------
-%% Function: Read(Transport, Len) -> {ok, Data}
-%%
-%% Data = binary()
-%%
-%% Description: Reads data through from the wrapped transoprt
-%%--------------------------------------------------------------------
-read(Transport, Len) when is_integer(Len) ->
-    {Transport, gen_server:call(Transport, {read, Len})}.
+    {Wrapped1, Response} = thrift_transport:write(Wrapped0, Data),
 
-%%====================================================================
-%% gen_server callbacks
-%%====================================================================
+    {Wrapped2, _} = thrift_transport:flush(Wrapped1),
 
-%%--------------------------------------------------------------------
-%% Function: init(Args) -> {ok, State} |
-%%                         {ok, State, Timeout} |
-%%                         ignore               |
-%%                         {stop, Reason}
-%% Description: Initiates the server
-%%--------------------------------------------------------------------
-init([Wrapped]) ->
-    {ok, #framed_transport{wrapped = Wrapped,
-                           read_buffer = [],
-                           write_buffer = []}}.
+    State1 = State0#framed_transport{wrapped = Wrapped2, write_buffer = []},
+    {State1, Response}.
 
-%%--------------------------------------------------------------------
-%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
-%%                                      {reply, Reply, State, Timeout} |
-%%                                      {noreply, State} |
-%%                                      {noreply, State, Timeout} |
-%%                                      {stop, Reason, Reply, State} |
-%%                                      {stop, Reason, State}
-%% Description: Handling call messages
-%%--------------------------------------------------------------------
-handle_call({write, Data}, _From, State = #framed_transport{write_buffer = WBuf}) ->
-    {reply, ok, State#framed_transport{write_buffer = [WBuf, Data]}};
-
-handle_call({read, Len}, _From, State = #framed_transport{wrapped = Wrapped0,
-                                                          read_buffer = RBuf}) ->
+%% Closes the transport and the wrapped transport
+close(State = #framed_transport{wrapped = Wrapped0}) ->
+    {Wrapped1, Result} = thrift_transport:close(Wrapped0),
+    NewState = State#framed_transport{wrapped = Wrapped1},
+    {NewState, Result}.
+
+%% Reads data through from the wrapped transoprt
+read(State0 = #framed_transport{wrapped = Wrapped0, read_buffer = RBuf},
+     Len) when is_integer(Len) ->
     {Wrapped1, {RBuf1, RBuf1Size}} =
         %% if the read buffer is empty, read another frame
         %% otherwise, just read from what's left in the buffer
@@ -141,68 +87,13 @@ handle_call({read, Len}, _From, State = 
     <<Data:Give/binary, RBuf2/binary>> = iolist_to_binary(RBuf1),
 
     Response = {ok, Data},
-    State1 = State#framed_transport{wrapped = Wrapped1, read_buffer=RBuf2},
-
-    {reply, Response, State1};
-
-handle_call(flush, _From, State) ->
-    {Response, State1} = do_flush(State),
-    {reply, Response, State1}.
-
-%%--------------------------------------------------------------------
-%% Function: handle_cast(Msg, State) -> {noreply, State} |
-%%                                      {noreply, State, Timeout} |
-%%                                      {stop, Reason, State}
-%% Description: Handling cast messages
-%%--------------------------------------------------------------------
-handle_cast(close, State) ->
-    %% Wrapped is closed by terminate/2
-    %%  error_logger:info_msg("thrift_framed_transport ~p: closing", [self()]),
-    {stop, normal, State};
-handle_cast(Msg, State=#framed_transport{}) ->
-    {noreply, State}.
-
-%%--------------------------------------------------------------------
-%% Function: handle_info(Info, State) -> {noreply, State} |
-%%                                       {noreply, State, Timeout} |
-%%                                       {stop, Reason, State}
-%% Description: Handling all non call/cast messages
-%%--------------------------------------------------------------------
-handle_info(_Info, State) ->
-    {noreply, State}.
-
-%%--------------------------------------------------------------------
-%% Function: terminate(Reason, State) -> void()
-%% Description: This function is called by a gen_server when it is about to
-%% terminate. It should be the opposite of Module:init/1 and do any necessary
-%% cleaning up. When it returns, the gen_server terminates with Reason.
-%% The return value is ignored.
-%%--------------------------------------------------------------------
-terminate(_Reason, State = #framed_transport{wrapped=Wrapped}) ->
-    thrift_transport:close(Wrapped),
-    ok.
+    State1 = State0#framed_transport{wrapped = Wrapped1, read_buffer=RBuf2},
 
-%%--------------------------------------------------------------------
-%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
-%% Description: Convert process state when code is changed
-%%--------------------------------------------------------------------
-code_change(_OldVsn, State, _Extra) ->
-    {ok, State}.
+    {State1, Response}.
 
 %%--------------------------------------------------------------------
-%%% Internal functions
+%% Internal functions
 %%--------------------------------------------------------------------
-do_flush(State = #framed_transport{write_buffer = Buffer,
-                                   wrapped = Wrapped0}) ->
-    FrameLen = iolist_size(Buffer),
-    Data     = [<<FrameLen:32/integer-signed-big>>, Buffer],
-
-    {Wrapped1, Response} = thrift_transport:write(Wrapped0, Data),
-
-    {Wrapped2, _} = thrift_transport:flush(Wrapped1),
-
-    State1 = State#framed_transport{wrapped = Wrapped2, write_buffer = []},
-    {Response, State1}.
 
 min(A,B) when A<B -> A;
 min(_,B)          -> B.