blob: a9b6e217a1a51051e3747e7f3df7e412dad35db6 [file] [log] [blame]
David Reiss0c8cb4a2008-06-11 01:12:52 +00001%%%-------------------------------------------------------------------
2%%% File : thrift_server.erl
3%%% Author : <todd@lipcon.org>
4%%% Description :
5%%%
6%%% Created : 28 Jan 2008 by <todd@lipcon.org>
7%%%-------------------------------------------------------------------
8-module(thrift_server).
9
10-behaviour(gen_server).
11
12%% API
13-export([start_link/3, stop/1, take_socket/2]).
14
15%% gen_server callbacks
16-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
17 terminate/2, code_change/3]).
18
19-define(SERVER, ?MODULE).
20
21-record(state, {listen_socket, acceptor_ref, service, handler}).
22
23%%====================================================================
24%% API
25%%====================================================================
26%%--------------------------------------------------------------------
27%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
28%% Description: Starts the server
29%%--------------------------------------------------------------------
30start_link(Port, Service, HandlerModule) when is_integer(Port), is_atom(HandlerModule) ->
31 gen_server:start_link({local, ?SERVER}, ?MODULE, {Port, Service, HandlerModule}, []).
32
33%%--------------------------------------------------------------------
34%% Function: stop(Pid) -> ok, {error, Reason}
35%% Description: Stops the server.
36%%--------------------------------------------------------------------
37stop(Pid) when is_pid(Pid) ->
38 gen_server:call(Pid, stop).
39
40
41take_socket(Server, Socket) ->
42 gen_server:call(Server, {take_socket, Socket}).
43
44
45%%====================================================================
46%% gen_server callbacks
47%%====================================================================
48
49%%--------------------------------------------------------------------
50%% Function: init(Args) -> {ok, State} |
51%% {ok, State, Timeout} |
52%% ignore |
53%% {stop, Reason}
54%% Description: Initiates the server
55%%--------------------------------------------------------------------
56init({Port, Service, Handler}) ->
57 {ok, Socket} = gen_tcp:listen(Port,
58 [binary,
59 {packet, 0},
60 {active, false},
61 {nodelay, true},
62 {reuseaddr, true}]),
63 {ok, Ref} = prim_inet:async_accept(Socket, -1),
64 {ok, #state{listen_socket = Socket,
65 acceptor_ref = Ref,
66 service = Service,
67 handler = Handler}}.
68
69%%--------------------------------------------------------------------
70%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
71%% {reply, Reply, State, Timeout} |
72%% {noreply, State} |
73%% {noreply, State, Timeout} |
74%% {stop, Reason, Reply, State} |
75%% {stop, Reason, State}
76%% Description: Handling call messages
77%%--------------------------------------------------------------------
78handle_call(stop, _From, State) ->
79 {stop, stopped, ok, State};
80
81handle_call({take_socket, Socket}, {FromPid, _Tag}, State) ->
82 Result = gen_tcp:controlling_process(Socket, FromPid),
83 {reply, Result, State}.
84
85%%--------------------------------------------------------------------
86%% Function: handle_cast(Msg, State) -> {noreply, State} |
87%% {noreply, State, Timeout} |
88%% {stop, Reason, State}
89%% Description: Handling cast messages
90%%--------------------------------------------------------------------
91handle_cast(_Msg, State) ->
92 {noreply, State}.
93
94%%--------------------------------------------------------------------
95%% Function: handle_info(Info, State) -> {noreply, State} |
96%% {noreply, State, Timeout} |
97%% {stop, Reason, State}
98%% Description: Handling all non call/cast messages
99%%--------------------------------------------------------------------
100handle_info({inet_async, ListenSocket, Ref, {ok, ClientSocket}},
101 State = #state{listen_socket = ListenSocket,
102 acceptor_ref = Ref,
103 service = Service,
104 handler = Handler}) ->
105 case set_sockopt(ListenSocket, ClientSocket) of
106 ok ->
107 %% New client connected - start processor
108 start_processor(ClientSocket, Service, Handler),
109 {ok, NewRef} = prim_inet:async_accept(ListenSocket, -1),
110 {noreply, State#state{acceptor_ref = NewRef}};
111 {error, Reason} ->
112 error_logger:error_msg("Couldn't set socket opts: ~p~n",
113 [Reason]),
114 {stop, Reason, State}
115 end;
116
117handle_info({inet_async, ListenSocket, Ref, Error}, State) ->
118 error_logger:error_msg("Error in acceptor: ~p~n", [Error]),
119 {stop, Error, State};
120
121handle_info(_Info, State) ->
122 {noreply, State}.
123
124%%--------------------------------------------------------------------
125%% Function: terminate(Reason, State) -> void()
126%% Description: This function is called by a gen_server when it is about to
127%% terminate. It should be the opposite of Module:init/1 and do any necessary
128%% cleaning up. When it returns, the gen_server terminates with Reason.
129%% The return value is ignored.
130%%--------------------------------------------------------------------
131terminate(_Reason, _State) ->
132 ok.
133
134%%--------------------------------------------------------------------
135%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
136%% Description: Convert process state when code is changed
137%%--------------------------------------------------------------------
138code_change(_OldVsn, State, _Extra) ->
139 {ok, State}.
140
141%%--------------------------------------------------------------------
142%%% Internal functions
143%%--------------------------------------------------------------------
144set_sockopt(ListenSocket, ClientSocket) ->
145 true = inet_db:register_socket(ClientSocket, inet_tcp),
146 case prim_inet:getopts(ListenSocket,
147 [active, nodelay, keepalive, delay_send, priority, tos]) of
148 {ok, Opts} ->
149 case prim_inet:setopts(ClientSocket, Opts) of
150 ok -> ok;
151 Error -> gen_tcp:close(ClientSocket),
152 Error
153 end;
154 Error ->
155 gen_tcp:close(ClientSocket),
156 Error
157 end.
158
159start_processor(Socket, Service, Handler) ->
160 Server = self(),
161
162 ProtoGen = fun() ->
163 % Become the controlling process
164 ok = take_socket(Server, Socket),
165 {ok, SocketTransport} = thrift_socket_transport:new(Socket),
166 {ok, BufferedTransport} = thrift_buffered_transport:new(SocketTransport),
167 {ok, Protocol} = thrift_binary_protocol:new(BufferedTransport),
168 {ok, Protocol, Protocol}
169 end,
170
David Reiss37dbfef2008-06-11 01:16:01 +0000171 spawn(thrift_processor, init, [{Server, ProtoGen, Service, Handler}]).