blob: 8a1704f62f5e8eb37a9cfaacdaaccac85a4224b1 [file] [log] [blame]
David Reissac549552008-06-10 22:56:59 +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
David Reiss8cf694d2008-06-11 00:57:54 +000013-export([start_link/3, stop/1, take_socket/2]).
David Reissac549552008-06-10 22:56:59 +000014
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
David Reiss8cf694d2008-06-11 00:57:54 +000021-record(state, {listen_socket, acceptor_ref, service, handler}).
David Reissac549552008-06-10 22:56:59 +000022
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
David Reissc308d692008-06-11 00:56:25 +000033
34%%--------------------------------------------------------------------
35%% Function: stop(Pid) -> ok, {error, Reason}
36%% Description: Stops the server.
37%%--------------------------------------------------------------------
38stop(Pid) when is_pid(Pid) ->
39 gen_server:call(Pid, stop).
40
41
David Reiss8cf694d2008-06-11 00:57:54 +000042take_socket(Server, Socket) ->
43 gen_server:call(Server, {take_socket, Socket}).
44
45
David Reissac549552008-06-10 22:56:59 +000046%%====================================================================
47%% gen_server callbacks
48%%====================================================================
49
50%%--------------------------------------------------------------------
51%% Function: init(Args) -> {ok, State} |
52%% {ok, State, Timeout} |
53%% ignore |
54%% {stop, Reason}
55%% Description: Initiates the server
56%%--------------------------------------------------------------------
David Reiss1c1ca742008-06-10 22:57:21 +000057init({Port, Service, Handler}) ->
David Reissac549552008-06-10 22:56:59 +000058 {ok, Socket} = gen_tcp:listen(Port,
59 [binary,
60 {packet, 0},
61 {active, false},
62 {nodelay, true},
63 {reuseaddr, true}]),
David Reiss8cf694d2008-06-11 00:57:54 +000064 {ok, Ref} = prim_inet:async_accept(Socket, -1),
David Reissac549552008-06-10 22:56:59 +000065 {ok, #state{listen_socket = Socket,
David Reiss8cf694d2008-06-11 00:57:54 +000066 acceptor_ref = Ref,
67 service = Service,
68 handler = Handler}}.
David Reissac549552008-06-10 22:56:59 +000069
70%%--------------------------------------------------------------------
71%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
72%% {reply, Reply, State, Timeout} |
73%% {noreply, State} |
74%% {noreply, State, Timeout} |
75%% {stop, Reason, Reply, State} |
76%% {stop, Reason, State}
77%% Description: Handling call messages
78%%--------------------------------------------------------------------
David Reissc308d692008-06-11 00:56:25 +000079handle_call(stop, _From, State) ->
David Reiss8cf694d2008-06-11 00:57:54 +000080 {stop, stopped, ok, State};
81
82handle_call({take_socket, Socket}, {FromPid, _Tag}, State) ->
83 Result = gen_tcp:controlling_process(Socket, FromPid),
84 {reply, Result, State}.
David Reissac549552008-06-10 22:56:59 +000085
86%%--------------------------------------------------------------------
87%% Function: handle_cast(Msg, State) -> {noreply, State} |
88%% {noreply, State, Timeout} |
89%% {stop, Reason, State}
90%% Description: Handling cast messages
91%%--------------------------------------------------------------------
92handle_cast(_Msg, State) ->
93 {noreply, State}.
94
95%%--------------------------------------------------------------------
96%% Function: handle_info(Info, State) -> {noreply, State} |
97%% {noreply, State, Timeout} |
98%% {stop, Reason, State}
99%% Description: Handling all non call/cast messages
100%%--------------------------------------------------------------------
David Reiss8cf694d2008-06-11 00:57:54 +0000101handle_info({inet_async, ListenSocket, Ref, {ok, ClientSocket}},
102 State = #state{listen_socket = ListenSocket,
103 acceptor_ref = Ref,
104 service = Service,
105 handler = Handler}) ->
106 case set_sockopt(ListenSocket, ClientSocket) of
107 ok ->
108 %% New client connected - start processor
109 start_processor(ClientSocket, Service, Handler),
110 {ok, NewRef} = prim_inet:async_accept(ListenSocket, -1),
111 {noreply, State#state{acceptor_ref = NewRef}};
112 {error, Reason} ->
113 error_logger:error_msg("Couldn't set socket opts: ~p~n",
114 [Reason]),
115 {stop, Reason, State}
116 end;
117
118handle_info({inet_async, ListenSocket, Ref, Error}, State) ->
119 error_logger:error_msg("Error in acceptor: ~p~n", [Error]),
120 {stop, Error, State};
121
David Reissac549552008-06-10 22:56:59 +0000122handle_info(_Info, State) ->
123 {noreply, State}.
124
125%%--------------------------------------------------------------------
126%% Function: terminate(Reason, State) -> void()
127%% Description: This function is called by a gen_server when it is about to
128%% terminate. It should be the opposite of Module:init/1 and do any necessary
129%% cleaning up. When it returns, the gen_server terminates with Reason.
130%% The return value is ignored.
131%%--------------------------------------------------------------------
132terminate(_Reason, _State) ->
133 ok.
134
135%%--------------------------------------------------------------------
136%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
137%% Description: Convert process state when code is changed
138%%--------------------------------------------------------------------
139code_change(_OldVsn, State, _Extra) ->
David Reissac549552008-06-10 22:56:59 +0000140 {ok, State}.
141
142%%--------------------------------------------------------------------
143%%% Internal functions
144%%--------------------------------------------------------------------
David Reiss8cf694d2008-06-11 00:57:54 +0000145set_sockopt(ListenSocket, ClientSocket) ->
146 true = inet_db:register_socket(ClientSocket, inet_tcp),
147 case prim_inet:getopts(ListenSocket,
148 [active, nodelay, keepalive, delay_send, priority, tos]) of
149 {ok, Opts} ->
150 case prim_inet:setopts(ClientSocket, Opts) of
151 ok -> ok;
152 Error -> gen_tcp:close(ClientSocket),
153 Error
154 end;
155 Error ->
156 gen_tcp:close(ClientSocket),
157 Error
158 end.
David Reissac549552008-06-10 22:56:59 +0000159
David Reiss8cf694d2008-06-11 00:57:54 +0000160
161
162start_processor(Socket, Service, Handler) ->
163 Server = self(),
David Reissac549552008-06-10 22:56:59 +0000164
David Reissf4761e32008-06-11 00:56:49 +0000165 ProtoGen = fun() ->
David Reiss8cf694d2008-06-11 00:57:54 +0000166 % Become the controlling process
167 ok = take_socket(Server, Socket),
David Reissf4761e32008-06-11 00:56:49 +0000168 {ok, SocketTransport} = thrift_socket_transport:new(Socket),
169 {ok, BufferedTransport} = thrift_buffered_transport:new(SocketTransport),
170 {ok, Protocol} = thrift_binary_protocol:new(BufferedTransport),
171 {ok, Protocol, Protocol}
172 end,
David Reiss8cf694d2008-06-11 00:57:54 +0000173
174 thrift_processor:start(ProtoGen, Service, Handler).