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:52 UTC

svn commit: r990996 - /incubator/thrift/trunk/lib/erl/src/thrift_http_transport.erl

Author: dreiss
Date: Mon Aug 30 22:05:51 2010
New Revision: 990996

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

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

Modified: incubator/thrift/trunk/lib/erl/src/thrift_http_transport.erl
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/erl/src/thrift_http_transport.erl?rev=990996&r1=990995&r2=990996&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/erl/src/thrift_http_transport.erl (original)
+++ incubator/thrift/trunk/lib/erl/src/thrift_http_transport.erl Mon Aug 30 22:05:51 2010
@@ -19,20 +19,11 @@
 
 -module(thrift_http_transport).
 
--behaviour(gen_server).
 -behaviour(thrift_transport).
 
 %% API
 -export([new/2, new/3]).
 
-%% 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]).
 
@@ -46,13 +37,6 @@
 -type state() :: pid().
 -include("thrift_transport_impl.hrl").
 
-%%====================================================================
-%% API
-%%====================================================================
-%%--------------------------------------------------------------------
-%% Function: new() -> {ok, Transport} | ignore | {error,Error}
-%% Description: Starts the server
-%%--------------------------------------------------------------------
 new(Host, Path) ->
     new(Host, Path, _Options = []).
 
@@ -62,54 +46,6 @@ new(Host, Path) ->
 %%   {extra_headers, ExtraHeaders}  = List of extra HTTP headers
 %%--------------------------------------------------------------------
 new(Host, Path, Options) ->
-    case gen_server:start_link(?MODULE, {Host, Path, Options}, []) 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, making a request
-%%--------------------------------------------------------------------
-flush(Transport) ->
-    {Transport, gen_server:call(Transport, flush)}.
-
-%%--------------------------------------------------------------------
-%% Function: close(Transport) -> ok
-%%
-%% Description: Closes the transport
-%%--------------------------------------------------------------------
-close(Transport) ->
-    {Transport, gen_server:cast(Transport, close)}.
-
-%%--------------------------------------------------------------------
-%% 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})}.
-
-%%====================================================================
-%% gen_server callbacks
-%%====================================================================
-
-init({Host, Path, Options}) ->
     State1 = #http_transport{host = Host,
                              path = Path,
                              read_buffer = [],
@@ -129,50 +65,17 @@ init({Host, Path, Options}) ->
         end,
     case lists:foldl(ApplyOption, State1, Options) of
         State2 = #http_transport{} ->
-            {ok, State2};
+            thrift_transport:new(?MODULE, State2);
         Else ->
-            {stop, Else}
+            {error, Else}
     end.
 
-handle_call({write, Data}, _From, State = #http_transport{write_buffer = WBuf}) ->
-    {reply, ok, State#http_transport{write_buffer = [WBuf, Data]}};
-
-handle_call({read, Len}, _From, State = #http_transport{read_buffer = RBuf}) ->
-    %% Pull off Give bytes, return them to the user, leave the rest in the buffer.
-    Give = min(iolist_size(RBuf), Len),
-    case iolist_to_binary(RBuf) of
-        <<Data:Give/binary, RBuf1/binary>> ->
-            Response = {ok, Data},
-            State1 = State#http_transport{read_buffer=RBuf1},
-            {reply, Response, State1};
-        _ ->
-            {reply, {error, 'EOF'}, State}
-    end;
-
-handle_call(flush, _From, State) ->
-    {Response, State1} = do_flush(State),
-    {reply, Response, State1}.
+%% Writes data into the buffer
+write(State = #http_transport{write_buffer = WBuf}, Data) ->
+    {State#http_transport{write_buffer = [WBuf, Data]}, ok}.
 
-handle_cast(close, State) ->
-    {_, State1} = do_flush(State),
-    {stop, normal, State1};
-
-handle_cast(_Msg, State=#http_transport{}) ->
-    {noreply, State}.
-
-handle_info(_Info, State) ->
-    {noreply, State}.
-
-terminate(_Reason, _State) ->
-    ok.
-
-code_change(_OldVsn, State, _Extra) ->
-    {ok, State}.
-
-%%--------------------------------------------------------------------
-%%% Internal functions
-%%--------------------------------------------------------------------
-do_flush(State = #http_transport{host = Host,
+%% Flushes the buffer, making a request
+flush(State = #http_transport{host = Host,
                                  path = Path,
                                  read_buffer = Rbuf,
                                  write_buffer = Wbuf,
@@ -181,7 +84,7 @@ do_flush(State = #http_transport{host = 
     case iolist_to_binary(Wbuf) of
         <<>> ->
             %% Don't bother flushing empty buffers.
-            {ok, State};
+            {State, ok};
         WBinary ->
             {ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}} =
               http:request(post,
@@ -194,7 +97,22 @@ do_flush(State = #http_transport{host = 
 
             State1 = State#http_transport{read_buffer = [Rbuf, Body],
                                           write_buffer = []},
-            {ok, State1}
+            {State1, ok}
+    end.
+
+close(State) ->
+    {State, ok}.
+
+read(State = #http_transport{read_buffer = RBuf}, Len) when is_integer(Len) ->
+    %% Pull off Give bytes, return them to the user, leave the rest in the buffer.
+    Give = min(iolist_size(RBuf), Len),
+    case iolist_to_binary(RBuf) of
+        <<Data:Give/binary, RBuf1/binary>> ->
+            Response = {ok, Data},
+            State1 = State#http_transport{read_buffer=RBuf1},
+            {State1, Response};
+        _ ->
+            {State, {error, 'EOF'}}
     end.
 
 min(A,B) when A<B -> A;