Skip to content

Commit

Permalink
Add ranch:get_addr/1
Browse files Browse the repository at this point in the history
  • Loading branch information
essen committed Aug 25, 2015
1 parent d1cd167 commit 236d0f8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

* Allow the supervised process and the process owning the socket to be different
* Add many transport options (please refer to the documentation)
* Add function ranch:get_addr/1 to retrieve both IP and port of listener
* Don't pass Ranch-specific options down to transports
** Should make Dialyzer happy in user projects.
** New types ranch:opt(), ranch_tcp:opt(), ranch_ssl:ssl_opt() and ranch_ssl:opt()
Expand Down
8 changes: 8 additions & 0 deletions doc/src/manual/ranch.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ Return child specifications for a new listener.
This function can be used to embed a listener directly
in an application instead of letting Ranch handle it.

=== get_addr(Ref) -> {IP, Port}

Ref = ref():: Listener name.
IP = inet:ip_address():: IP of the interface used by this listener.
Port = inet:port_number():: Port number used by this listener.

Return the IP address and port for the given listener.

=== get_max_connections(Ref) -> MaxConns

Ref = ref():: Listener name.
Expand Down
8 changes: 7 additions & 1 deletion src/ranch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-export([child_spec/6]).
-export([accept_ack/1]).
-export([remove_connection/1]).
-export([get_addr/1]).
-export([get_port/1]).
-export([get_max_connections/1]).
-export([set_max_connections/2]).
Expand Down Expand Up @@ -105,9 +106,14 @@ remove_connection(Ref) ->
ConnsSup ! {remove_connection, Ref},
ok.

-spec get_addr(ref()) -> {inet:ip_address(), inet:port_number()}.
get_addr(Ref) ->
ranch_server:get_addr(Ref).

-spec get_port(ref()) -> inet:port_number().
get_port(Ref) ->
ranch_server:get_port(Ref).
{_, Port} = get_addr(Ref),
Port.

-spec get_max_connections(ref()) -> max_conns().
get_max_connections(Ref) ->
Expand Down
4 changes: 2 additions & 2 deletions src/ranch_acceptors_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ init([Ref, NbAcceptors, Transport, TransOpts]) ->
Socket ->
Socket
end,
{ok, {_, Port}} = Transport:sockname(LSocket),
ranch_server:set_port(Ref, Port),
{ok, Addr} = Transport:sockname(LSocket),
ranch_server:set_addr(Ref, Addr),
Procs = [
{{acceptor, self(), N}, {ranch_acceptor, start_link, [
LSocket, Transport, ConnsSup
Expand Down
22 changes: 11 additions & 11 deletions src/ranch_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
-export([cleanup_listener_opts/1]).
-export([set_connections_sup/2]).
-export([get_connections_sup/1]).
-export([set_port/2]).
-export([get_port/1]).
-export([set_addr/2]).
-export([get_addr/1]).
-export([set_max_connections/2]).
-export([get_max_connections/1]).
-export([set_protocol_options/2]).
Expand Down Expand Up @@ -56,7 +56,7 @@ set_new_listener_opts(Ref, MaxConns, Opts) ->

-spec cleanup_listener_opts(ranch:ref()) -> ok.
cleanup_listener_opts(Ref) ->
_ = ets:delete(?TAB, {port, Ref}),
_ = ets:delete(?TAB, {addr, Ref}),
_ = ets:delete(?TAB, {max_conns, Ref}),
_ = ets:delete(?TAB, {opts, Ref}),
ok.
Expand All @@ -70,13 +70,13 @@ set_connections_sup(Ref, Pid) ->
get_connections_sup(Ref) ->
ets:lookup_element(?TAB, {conns_sup, Ref}, 2).

-spec set_port(ranch:ref(), inet:port_number()) -> ok.
set_port(Ref, Port) ->
gen_server:call(?MODULE, {set_port, Ref, Port}).
-spec set_addr(ranch:ref(), {inet:ip_address(), inet:port_number()}) -> ok.
set_addr(Ref, Addr) ->
gen_server:call(?MODULE, {set_addr, Ref, Addr}).

-spec get_port(ranch:ref()) -> inet:port_number().
get_port(Ref) ->
ets:lookup_element(?TAB, {port, Ref}, 2).
-spec get_addr(ranch:ref()) -> {inet:ip_address(), inet:port_number()}.
get_addr(Ref) ->
ets:lookup_element(?TAB, {addr, Ref}, 2).

-spec set_max_connections(ranch:ref(), ranch:max_conns()) -> ok.
set_max_connections(Ref, MaxConnections) ->
Expand Down Expand Up @@ -119,8 +119,8 @@ handle_call({set_connections_sup, Ref, Pid}, _,
false ->
{reply, false, State}
end;
handle_call({set_port, Ref, Port}, _, State) ->
true = ets:insert(?TAB, {{port, Ref}, Port}),
handle_call({set_addr, Ref, Addr}, _, State) ->
true = ets:insert(?TAB, {{addr, Ref}, Addr}),
{reply, ok, State};
handle_call({set_max_conns, Ref, MaxConns}, _, State) ->
ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
Expand Down

0 comments on commit 236d0f8

Please sign in to comment.