David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | %% |
| 2 | %% Licensed to the Apache Software Foundation (ASF) under one |
| 3 | %% or more contributor license agreements. See the NOTICE file |
| 4 | %% distributed with this work for additional information |
| 5 | %% regarding copyright ownership. The ASF licenses this file |
| 6 | %% to you under the Apache License, Version 2.0 (the |
| 7 | %% "License"); you may not use this file except in compliance |
| 8 | %% with the License. You may obtain a copy of the License at |
| 9 | %% |
| 10 | %% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | %% |
| 12 | %% Unless required by applicable law or agreed to in writing, |
| 13 | %% software distributed under the License is distributed on an |
| 14 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | %% KIND, either express or implied. See the License for the |
| 16 | %% specific language governing permissions and limitations |
| 17 | %% under the License. |
| 18 | %% |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 19 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 20 | -module(thrift_socket_server). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 21 | |
| 22 | -behaviour(gen_server). |
| 23 | |
| 24 | -export([start/1, stop/1]). |
| 25 | |
| 26 | -export([init/1, handle_call/3, handle_cast/2, terminate/2, code_change/3, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 27 | handle_info/2]). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 28 | |
| 29 | -export([acceptor_loop/1]). |
| 30 | |
| 31 | -record(thrift_socket_server, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 32 | {port, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 33 | service, |
| 34 | handler, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 35 | name, |
| 36 | max=2048, |
| 37 | ip=any, |
| 38 | listen=null, |
| 39 | acceptor=null, |
David Reiss | b42361c | 2009-09-09 17:18:57 +0000 | [diff] [blame] | 40 | socket_opts=[{recv_timeout, 500}], |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 41 | framed=false, |
| 42 | ssltransport=false, |
| 43 | ssloptions=[] |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 44 | }). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 45 | |
| 46 | start(State=#thrift_socket_server{}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 47 | start_server(State); |
| 48 | start(Options) -> |
| 49 | start(parse_options(Options)). |
| 50 | |
| 51 | stop(Name) when is_atom(Name) -> |
| 52 | gen_server:cast(Name, stop); |
| 53 | stop(Pid) when is_pid(Pid) -> |
| 54 | gen_server:cast(Pid, stop); |
| 55 | stop({local, Name}) -> |
| 56 | stop(Name); |
| 57 | stop({global, Name}) -> |
| 58 | stop(Name); |
| 59 | stop(Options) -> |
| 60 | State = parse_options(Options), |
| 61 | stop(State#thrift_socket_server.name). |
| 62 | |
| 63 | %% Internal API |
| 64 | |
| 65 | parse_options(Options) -> |
| 66 | parse_options(Options, #thrift_socket_server{}). |
| 67 | |
| 68 | parse_options([], State) -> |
| 69 | State; |
| 70 | parse_options([{name, L} | Rest], State) when is_list(L) -> |
| 71 | Name = {local, list_to_atom(L)}, |
| 72 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 73 | parse_options([{name, A} | Rest], State) when is_atom(A) -> |
| 74 | Name = {local, A}, |
| 75 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 76 | parse_options([{name, Name} | Rest], State) -> |
| 77 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 78 | parse_options([{port, L} | Rest], State) when is_list(L) -> |
| 79 | Port = list_to_integer(L), |
| 80 | parse_options(Rest, State#thrift_socket_server{port=Port}); |
| 81 | parse_options([{port, Port} | Rest], State) -> |
| 82 | parse_options(Rest, State#thrift_socket_server{port=Port}); |
| 83 | parse_options([{ip, Ip} | Rest], State) -> |
| 84 | ParsedIp = case Ip of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 85 | any -> |
| 86 | any; |
| 87 | Ip when is_tuple(Ip) -> |
| 88 | Ip; |
| 89 | Ip when is_list(Ip) -> |
| 90 | {ok, IpTuple} = inet_parse:address(Ip), |
| 91 | IpTuple |
| 92 | end, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 93 | parse_options(Rest, State#thrift_socket_server{ip=ParsedIp}); |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 94 | parse_options([{socket_opts, L} | Rest], State) when is_list(L), length(L) > 0 -> |
| 95 | parse_options(Rest, State#thrift_socket_server{socket_opts=L}); |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 96 | parse_options([{handler, Handler} | Rest], State) -> |
| 97 | parse_options(Rest, State#thrift_socket_server{handler=Handler}); |
| 98 | parse_options([{service, Service} | Rest], State) -> |
| 99 | parse_options(Rest, State#thrift_socket_server{service=Service}); |
| 100 | parse_options([{max, Max} | Rest], State) -> |
| 101 | MaxInt = case Max of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 102 | Max when is_list(Max) -> |
| 103 | list_to_integer(Max); |
| 104 | Max when is_integer(Max) -> |
| 105 | Max |
| 106 | end, |
David Reiss | b42361c | 2009-09-09 17:18:57 +0000 | [diff] [blame] | 107 | parse_options(Rest, State#thrift_socket_server{max=MaxInt}); |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 108 | |
David Reiss | b42361c | 2009-09-09 17:18:57 +0000 | [diff] [blame] | 109 | parse_options([{framed, Framed} | Rest], State) when is_boolean(Framed) -> |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 110 | parse_options(Rest, State#thrift_socket_server{framed=Framed}); |
| 111 | |
| 112 | parse_options([{ssltransport, SSLTransport} | Rest], State) when is_boolean(SSLTransport) -> |
| 113 | parse_options(Rest, State#thrift_socket_server{ssltransport=SSLTransport}); |
| 114 | parse_options([{ssloptions, SSLOptions} | Rest], State) when is_list(SSLOptions) -> |
| 115 | parse_options(Rest, State#thrift_socket_server{ssloptions=SSLOptions}). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 116 | |
| 117 | start_server(State=#thrift_socket_server{name=Name}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 118 | case Name of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 119 | undefined -> |
| 120 | gen_server:start_link(?MODULE, State, []); |
| 121 | _ -> |
| 122 | gen_server:start_link(Name, ?MODULE, State, []) |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 123 | end. |
| 124 | |
| 125 | init(State=#thrift_socket_server{ip=Ip, port=Port}) -> |
David Reiss | d74b023 | 2008-06-11 01:02:55 +0000 | [diff] [blame] | 126 | process_flag(trap_exit, true), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 127 | BaseOpts = [binary, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 128 | {reuseaddr, true}, |
| 129 | {packet, 0}, |
| 130 | {backlog, 4096}, |
| 131 | {recbuf, 8192}, |
| 132 | {active, false}], |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 133 | Opts = case Ip of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 134 | any -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 135 | BaseOpts; |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 136 | Ip -> |
| 137 | [{ip, Ip} | BaseOpts] |
| 138 | end, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 139 | case gen_tcp_listen(Port, Opts, State) of |
| 140 | {stop, eacces} -> |
| 141 | %% fdsrv module allows another shot to bind |
| 142 | %% ports which require root access |
| 143 | case Port < 1024 of |
| 144 | true -> |
| 145 | case fdsrv:start() of |
| 146 | {ok, _} -> |
| 147 | case fdsrv:bind_socket(tcp, Port) of |
| 148 | {ok, Fd} -> |
| 149 | gen_tcp_listen(Port, [{fd, Fd} | Opts], State); |
| 150 | _ -> |
| 151 | {stop, fdsrv_bind_failed} |
| 152 | end; |
| 153 | _ -> |
| 154 | {stop, fdsrv_start_failed} |
| 155 | end; |
| 156 | false -> |
| 157 | {stop, eacces} |
| 158 | end; |
| 159 | Other -> |
| 160 | error_logger:info_msg("thrift service listening on port ~p", [Port]), |
| 161 | Other |
| 162 | end. |
| 163 | |
| 164 | gen_tcp_listen(Port, Opts, State) -> |
| 165 | case gen_tcp:listen(Port, Opts) of |
| 166 | {ok, Listen} -> |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 167 | {ok, ListenPort} = inet:port(Listen), |
| 168 | {ok, new_acceptor(State#thrift_socket_server{listen=Listen, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 169 | port=ListenPort})}; |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 170 | {error, Reason} -> |
| 171 | {stop, Reason} |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 172 | end. |
| 173 | |
| 174 | new_acceptor(State=#thrift_socket_server{max=0}) -> |
| 175 | error_logger:error_msg("Not accepting new connections"), |
| 176 | State#thrift_socket_server{acceptor=null}; |
David Reiss | 5ed313d | 2010-08-30 22:05:57 +0000 | [diff] [blame] | 177 | new_acceptor(State=#thrift_socket_server{listen=Listen, |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 178 | service=Service, handler=Handler, |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 179 | socket_opts=Opts, framed=Framed, |
| 180 | ssltransport=SslTransport, ssloptions=SslOptions |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 181 | }) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 182 | Pid = proc_lib:spawn_link(?MODULE, acceptor_loop, |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 183 | [{self(), Listen, Service, Handler, Opts, Framed, SslTransport, SslOptions}]), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 184 | State#thrift_socket_server{acceptor=Pid}. |
| 185 | |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 186 | acceptor_loop({Server, Listen, Service, Handler, SocketOpts, Framed, SslTransport, SslOptions}) |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 187 | when is_pid(Server), is_list(SocketOpts) -> |
David Reiss | d74b023 | 2008-06-11 01:02:55 +0000 | [diff] [blame] | 188 | case catch gen_tcp:accept(Listen) of % infinite timeout |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 189 | {ok, Socket} -> |
| 190 | gen_server:cast(Server, {accepted, self()}), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 191 | ProtoGen = fun() -> |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 192 | {ok, SocketTransport} = case SslTransport of |
| 193 | true -> thrift_sslsocket_transport:new(Socket, SocketOpts, SslOptions); |
| 194 | false -> thrift_socket_transport:new(Socket, SocketOpts) |
| 195 | end, |
| 196 | {ok, Transport} = case Framed of |
| 197 | true -> thrift_framed_transport:new(SocketTransport); |
| 198 | false -> thrift_buffered_transport:new(SocketTransport) |
| 199 | end, |
| 200 | {ok, Protocol} = thrift_binary_protocol:new(Transport), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 201 | {ok, Protocol} |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 202 | end, |
| 203 | thrift_processor:init({Server, ProtoGen, Service, Handler}); |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 204 | {error, closed} -> |
| 205 | exit({error, closed}); |
| 206 | Other -> |
| 207 | error_logger:error_report( |
| 208 | [{application, thrift}, |
| 209 | "Accept failed error", |
| 210 | lists:flatten(io_lib:format("~p", [Other]))]), |
| 211 | exit({error, accept_failed}) |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 212 | end. |
| 213 | |
| 214 | handle_call({get, port}, _From, State=#thrift_socket_server{port=Port}) -> |
| 215 | {reply, Port, State}; |
| 216 | handle_call(_Message, _From, State) -> |
| 217 | Res = error, |
| 218 | {reply, Res, State}. |
| 219 | |
| 220 | handle_cast({accepted, Pid}, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 221 | State=#thrift_socket_server{acceptor=Pid, max=Max}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 222 | % io:format("accepted ~p~n", [Pid]), |
| 223 | State1 = State#thrift_socket_server{max=Max - 1}, |
| 224 | {noreply, new_acceptor(State1)}; |
| 225 | handle_cast(stop, State) -> |
| 226 | {stop, normal, State}. |
| 227 | |
| 228 | terminate(_Reason, #thrift_socket_server{listen=Listen, port=Port}) -> |
| 229 | gen_tcp:close(Listen), |
| 230 | case Port < 1024 of |
| 231 | true -> |
| 232 | catch fdsrv:stop(), |
| 233 | ok; |
| 234 | false -> |
| 235 | ok |
| 236 | end. |
| 237 | |
| 238 | code_change(_OldVsn, State, _Extra) -> |
| 239 | State. |
| 240 | |
| 241 | handle_info({'EXIT', Pid, normal}, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 242 | State=#thrift_socket_server{acceptor=Pid}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 243 | {noreply, new_acceptor(State)}; |
| 244 | handle_info({'EXIT', Pid, Reason}, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 245 | State=#thrift_socket_server{acceptor=Pid}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 246 | error_logger:error_report({?MODULE, ?LINE, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 247 | {acceptor_error, Reason}}), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 248 | timer:sleep(100), |
| 249 | {noreply, new_acceptor(State)}; |
| 250 | handle_info({'EXIT', _LoopPid, Reason}, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 251 | State=#thrift_socket_server{acceptor=Pid, max=Max}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 252 | case Reason of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 253 | normal -> ok; |
David Reiss | 4cf5a6a | 2008-06-11 01:00:59 +0000 | [diff] [blame] | 254 | shutdown -> ok; |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 255 | _ -> error_logger:error_report({?MODULE, ?LINE, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 256 | {child_error, Reason, erlang:get_stacktrace()}}) |
| 257 | end, |
| 258 | State1 = State#thrift_socket_server{max=Max + 1}, |
| 259 | State2 = case Pid of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 260 | null -> new_acceptor(State1); |
| 261 | _ -> State1 |
| 262 | end, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 263 | {noreply, State2}; |
| 264 | handle_info(Info, State) -> |
| 265 | error_logger:info_report([{'INFO', Info}, {'State', State}]), |
| 266 | {noreply, State}. |