Author: dreiss
Date: Tue Jun 10 15:57:21 2008
New Revision: 666376

URL: http://svn.apache.org/viewvc?rev=666376&view=rev
Log:
Make processor use a service definition and handler to determine types

Added:
    incubator/thrift/trunk/lib/alterl/src/test_handler.erl
    incubator/thrift/trunk/lib/alterl/src/test_service.erl
Modified:
    incubator/thrift/trunk/lib/alterl/src/thrift_processor.erl
    incubator/thrift/trunk/lib/alterl/src/thrift_server.erl
    incubator/thrift/trunk/lib/alterl/src/thrift_service.erl

Added: incubator/thrift/trunk/lib/alterl/src/test_handler.erl
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/alterl/src/test_handler.erl?rev=666376&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/alterl/src/test_handler.erl (added)
+++ incubator/thrift/trunk/lib/alterl/src/test_handler.erl Tue Jun 10 15:57:21 
2008
@@ -0,0 +1,7 @@
+-module(test_handler).
+
+-export([handle_function/2]).
+
+handle_function(add, Params = {A, B}) ->
+    io:format("Got params: ~p~n", [Params]),
+    {reply, A + B}.

Added: incubator/thrift/trunk/lib/alterl/src/test_service.erl
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/alterl/src/test_service.erl?rev=666376&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/alterl/src/test_service.erl (added)
+++ incubator/thrift/trunk/lib/alterl/src/test_service.erl Tue Jun 10 15:57:21 
2008
@@ -0,0 +1,10 @@
+-module(test_service).
+%
+% Test service definition
+
+-export([function_info/2]).
+
+function_info(add, params_type) ->
+    {struct, [{1, i32},
+              {2, i32}]};
+function_info(add, reply_type) -> i32.

Modified: incubator/thrift/trunk/lib/alterl/src/thrift_processor.erl
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/alterl/src/thrift_processor.erl?rev=666376&r1=666375&r2=666376&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/alterl/src/thrift_processor.erl (original)
+++ incubator/thrift/trunk/lib/alterl/src/thrift_processor.erl Tue Jun 10 
15:57:21 2008
@@ -36,35 +36,57 @@
             ok
     end.
 
-handle_function(State = #state{in_protocol = IProto,
-                               out_protocol = OProto},
-                add) ->
-    io:format("Reading struct~n"),
-    {ok, Struct} = thrift_protocol:read(IProto,
-                  {struct, [{1, i32},
-                            {2, i32}]}),
-    io:format("Struct: ~p~n", [Struct]),
+%handle_function(State = #state{in_protocol = IProto,
+%                               out_protocol = OProto},
+%                add) ->
+%    io:format("Reading struct~n"),
+%    {ok, Struct} = thrift_protocol:read(IProto,
+%                  {struct, [{1, i32},
+%                            {2, i32}]}),
+%    io:format("Struct: ~p~n", [Struct]),
     
-    {A, B} = Struct,
+%    {A, B} = Struct,
 
-    thrift_protocol:write(OProto, #protocol_message_begin{
-                            name = "addResult",
-                            type = ?tMessageType_REPLY,
-                            seqid = 0}),
-    thrift_protocol:write(OProto, {{struct, [{0, i32}]},
-                                   {A + B}}),
-    thrift_protocol:write(OProto, message_end);
+%    thrift_protocol:write(OProto, #protocol_message_begin{
+%                            name = "addResult",
+%                            type = ?tMessageType_REPLY,
+%                            seqid = 0}),
+%    thrift_protocol:write(OProto, {{struct, [{0, i32}]},
+%                                   {A + B}}),
+%    thrift_protocol:write(OProto, message_end);
 
-handle_function(State = #state{in_protocol = IProto,
-                               out_protocol = OProto},
-                complexTest) ->
-    io:format("Reading struct~n"),
-    Struct = thrift_protocol:read(
-               IProto,
-               {struct, [{1, {struct,
-                              [{1, {list, i32}},
-                               {2, {map, string, {struct, [{1, i16}]}}}]}}]}),
+%handle_function(State = #state{in_protocol = IProto,
+%                               out_protocol = OProto},
+%                complexTest) ->
+%    io:format("Reading struct~n"),
+%    Struct = thrift_protocol:read(
+%               IProto,
+%               {struct, [{1, {struct,
+%                              [{1, {list, i32}},
+%                               {2, {map, string, {struct, [{1, i16}]}}}]}}]}),
     
-    io:format("Struct: ~p~n", [Struct]).
+%    io:format("Struct: ~p~n", [Struct]);
+
 
+handle_function(State = #state{in_protocol = IProto,
+                               out_protocol = OProto,
+                               handler = Handler,
+                               service = Service},
+                Function) ->
+    InParams = Service:function_info(Function, params_type),
 
+    {ok, Params} = thrift_protocol:read(IProto, InParams),
+    Result = Handler:handle_function(Function, Params),
+    
+    ReplyType = Service:function_info(Function, reply_type),
+    
+    case Result of
+        {reply, Reply} -> 
+            thrift_protocol:write(OProto, #protocol_message_begin{
+                            name = atom_to_list(Function) ++ "_result",
+                            type = ?tMessageType_REPLY,
+                            seqid = 0}),
+            thrift_protocol:write(OProto, {{struct, [{0, ReplyType}]}, 
{Reply}}),
+            thrift_protocol:write(OProto, message_end)
+    end,
+    ok.

Modified: incubator/thrift/trunk/lib/alterl/src/thrift_server.erl
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/alterl/src/thrift_server.erl?rev=666376&r1=666375&r2=666376&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/alterl/src/thrift_server.erl (original)
+++ incubator/thrift/trunk/lib/alterl/src/thrift_server.erl Tue Jun 10 15:57:21 
2008
@@ -19,7 +19,6 @@
 -define(SERVER, ?MODULE).
 
 -record(state, {listen_socket, acceptor, service}).
--record(handler, {module}).
 
 %%====================================================================
 %% API
@@ -42,8 +41,7 @@
 %%                         {stop, Reason}
 %% Description: Initiates the server
 %%--------------------------------------------------------------------
-init({Port, Service, HandlerModule}) ->
-    Handler = #handler{module = HandlerModule},
+init({Port, Service, Handler}) ->
     {ok, Socket} = gen_tcp:listen(Port,
                                   [binary,
                                    {packet, 0},
@@ -109,7 +107,7 @@
 %%--------------------------------------------------------------------
 
 acceptor(ListenSocket, Service, Handler)
-  when is_port(ListenSocket), is_record(Handler, handler) ->
+  when is_port(ListenSocket), is_atom(Handler) ->
     {ok, Socket} = gen_tcp:accept(ListenSocket),
     error_logger:info_msg("Accepted client"),
 

Modified: incubator/thrift/trunk/lib/alterl/src/thrift_service.erl
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/alterl/src/thrift_service.erl?rev=666376&r1=666375&r2=666376&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/alterl/src/thrift_service.erl (original)
+++ incubator/thrift/trunk/lib/alterl/src/thrift_service.erl Tue Jun 10 
15:57:21 2008
@@ -3,4 +3,4 @@
 -export([behaviour_info/1]).
 
 behaviour_info(callbacks) ->
-    [{service_info, 1}].
+    [{function_info, 2}].


Reply via email to