Christopher Piro | 2f5afce | 2007-06-29 07:17:33 +0000 | [diff] [blame^] | 1 | -module(tServer). |
| 2 | |
| 3 | -include("thrift/thrift.hrl"). |
| 4 | -include("thrift/protocol/tProtocol.hrl"). |
| 5 | -include("thrift/protocol/tBinaryProtocol.hrl"). |
| 6 | % -include("thrift/transport/tTransport.hrl"). |
| 7 | -include("tServer.hrl"). |
| 8 | |
| 9 | -export([new/3, serve/1]). |
| 10 | |
| 11 | % now processor is the module with process_*, not an object |
| 12 | |
| 13 | new(ProcessorModule, HandlerModule, ServerTransport) -> |
| 14 | #tServer{processorModule=ProcessorModule, |
| 15 | handlerModule=HandlerModule, |
| 16 | serverTransport=ServerTransport}. |
| 17 | |
| 18 | serverTransport(This) -> |
| 19 | This#tServer.serverTransport. |
| 20 | |
| 21 | serve(This) -> |
| 22 | ST1 = ?M0(serverTransport(This), listen_MUTABLE), |
| 23 | This1 = This#tServer{serverTransport=ST1}, |
| 24 | serve_loop(This1). |
| 25 | |
| 26 | processorModule(This) -> |
| 27 | This#tServer.processorModule. |
| 28 | |
| 29 | handlerModule(This) -> |
| 30 | This#tServer.handlerModule. |
| 31 | |
| 32 | serve_loop(This) -> |
| 33 | io:format("~nready.~n", []), |
| 34 | Client = ?M0(serverTransport(This), accept_MUTABLE), |
| 35 | This1 = This#tServer{serverTransport=Client}, |
| 36 | |
| 37 | Trans = Client, % factory |
| 38 | Prot = tBinaryProtocol:new(Trans), |
| 39 | serve_loop_loop(This1, Prot), % giggle loop? |
| 40 | ?M0(Trans, close_MUTABLE), % don't "assign" ... discard |
| 41 | serve_loop(This). |
| 42 | |
| 43 | serve_loop_loop(This, Prot) -> |
| 44 | Next = try |
| 45 | Val = (processorModule(This)):process(handlerModule(This), Prot, Prot), |
| 46 | io:format("request processed: rv=~p~n", [Val]), |
| 47 | loop |
| 48 | catch |
| 49 | %% TODO(cpiro) case when is_record(...) to pick out our exception |
| 50 | %% records vs. normal erlang throws |
| 51 | {tTransportException,_,_} -> |
| 52 | io:format("tTransportException (normal-ish?)~n", []), |
| 53 | close; |
| 54 | E -> |
| 55 | io:format("EXCEPTION: ~p~n", [E]), |
| 56 | close |
| 57 | end, |
| 58 | case Next of |
| 59 | loop -> serve_loop_loop(This, Prot); |
| 60 | close -> ok |
| 61 | end. |
| 62 | |