Browse Source

Add ranch:get_addr/1

Loïc Hoguin 9 years ago
parent
commit
236d0f8eec
5 changed files with 29 additions and 14 deletions
  1. 1 0
      CHANGELOG.asciidoc
  2. 8 0
      doc/src/manual/ranch.asciidoc
  3. 7 1
      src/ranch.erl
  4. 2 2
      src/ranch_acceptors_sup.erl
  5. 11 11
      src/ranch_server.erl

+ 1 - 0
CHANGELOG.asciidoc

@@ -5,6 +5,7 @@
 
 
 * Allow the supervised process and the process owning the socket to be different
 * Allow the supervised process and the process owning the socket to be different
 * Add many transport options (please refer to the documentation)
 * 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
 * Don't pass Ranch-specific options down to transports
 ** Should make Dialyzer happy in user projects.
 ** Should make Dialyzer happy in user projects.
 ** New types ranch:opt(), ranch_tcp:opt(), ranch_ssl:ssl_opt() and ranch_ssl:opt()
 ** New types ranch:opt(), ranch_tcp:opt(), ranch_ssl:ssl_opt() and ranch_ssl:opt()

+ 8 - 0
doc/src/manual/ranch.asciidoc

@@ -82,6 +82,14 @@ Return child specifications for a new listener.
 This function can be used to embed a listener directly
 This function can be used to embed a listener directly
 in an application instead of letting Ranch handle it.
 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
 === get_max_connections(Ref) -> MaxConns
 
 
 Ref = ref():: Listener name.
 Ref = ref():: Listener name.

+ 7 - 1
src/ranch.erl

@@ -19,6 +19,7 @@
 -export([child_spec/6]).
 -export([child_spec/6]).
 -export([accept_ack/1]).
 -export([accept_ack/1]).
 -export([remove_connection/1]).
 -export([remove_connection/1]).
+-export([get_addr/1]).
 -export([get_port/1]).
 -export([get_port/1]).
 -export([get_max_connections/1]).
 -export([get_max_connections/1]).
 -export([set_max_connections/2]).
 -export([set_max_connections/2]).
@@ -105,9 +106,14 @@ remove_connection(Ref) ->
 	ConnsSup ! {remove_connection, Ref},
 	ConnsSup ! {remove_connection, Ref},
 	ok.
 	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().
 -spec get_port(ref()) -> inet:port_number().
 get_port(Ref) ->
 get_port(Ref) ->
-	ranch_server:get_port(Ref).
+	{_, Port} = get_addr(Ref),
+	Port.
 
 
 -spec get_max_connections(ref()) -> max_conns().
 -spec get_max_connections(ref()) -> max_conns().
 get_max_connections(Ref) ->
 get_max_connections(Ref) ->

+ 2 - 2
src/ranch_acceptors_sup.erl

@@ -37,8 +37,8 @@ init([Ref, NbAcceptors, Transport, TransOpts]) ->
 		Socket ->
 		Socket ->
 			Socket
 			Socket
 	end,
 	end,
-	{ok, {_, Port}} = Transport:sockname(LSocket),
+	{ok, Addr} = Transport:sockname(LSocket),
-	ranch_server:set_port(Ref, Port),
+	ranch_server:set_addr(Ref, Addr),
 	Procs = [
 	Procs = [
 		{{acceptor, self(), N}, {ranch_acceptor, start_link, [
 		{{acceptor, self(), N}, {ranch_acceptor, start_link, [
 			LSocket, Transport, ConnsSup
 			LSocket, Transport, ConnsSup

+ 11 - 11
src/ranch_server.erl

@@ -21,8 +21,8 @@
 -export([cleanup_listener_opts/1]).
 -export([cleanup_listener_opts/1]).
 -export([set_connections_sup/2]).
 -export([set_connections_sup/2]).
 -export([get_connections_sup/1]).
 -export([get_connections_sup/1]).
--export([set_port/2]).
+-export([set_addr/2]).
--export([get_port/1]).
+-export([get_addr/1]).
 -export([set_max_connections/2]).
 -export([set_max_connections/2]).
 -export([get_max_connections/1]).
 -export([get_max_connections/1]).
 -export([set_protocol_options/2]).
 -export([set_protocol_options/2]).
@@ -56,7 +56,7 @@ set_new_listener_opts(Ref, MaxConns, Opts) ->
 
 
 -spec cleanup_listener_opts(ranch:ref()) -> ok.
 -spec cleanup_listener_opts(ranch:ref()) -> ok.
 cleanup_listener_opts(Ref) ->
 cleanup_listener_opts(Ref) ->
-	_ = ets:delete(?TAB, {port, Ref}),
+	_ = ets:delete(?TAB, {addr, Ref}),
 	_ = ets:delete(?TAB, {max_conns, Ref}),
 	_ = ets:delete(?TAB, {max_conns, Ref}),
 	_ = ets:delete(?TAB, {opts, Ref}),
 	_ = ets:delete(?TAB, {opts, Ref}),
 	ok.
 	ok.
@@ -70,13 +70,13 @@ set_connections_sup(Ref, Pid) ->
 get_connections_sup(Ref) ->
 get_connections_sup(Ref) ->
 	ets:lookup_element(?TAB, {conns_sup, Ref}, 2).
 	ets:lookup_element(?TAB, {conns_sup, Ref}, 2).
 
 
--spec set_port(ranch:ref(), inet:port_number()) -> ok.
+-spec set_addr(ranch:ref(), {inet:ip_address(), inet:port_number()}) -> ok.
-set_port(Ref, Port) ->
+set_addr(Ref, Addr) ->
-	gen_server:call(?MODULE, {set_port, Ref, Port}).
+	gen_server:call(?MODULE, {set_addr, Ref, Addr}).
 
 
--spec get_port(ranch:ref()) -> inet:port_number().
+-spec get_addr(ranch:ref()) -> {inet:ip_address(), inet:port_number()}.
-get_port(Ref) ->
+get_addr(Ref) ->
-	ets:lookup_element(?TAB, {port, Ref}, 2).
+	ets:lookup_element(?TAB, {addr, Ref}, 2).
 
 
 -spec set_max_connections(ranch:ref(), ranch:max_conns()) -> ok.
 -spec set_max_connections(ranch:ref(), ranch:max_conns()) -> ok.
 set_max_connections(Ref, MaxConnections) ->
 set_max_connections(Ref, MaxConnections) ->
@@ -119,8 +119,8 @@ handle_call({set_connections_sup, Ref, Pid}, _,
 		false ->
 		false ->
 			{reply, false, State}
 			{reply, false, State}
 	end;
 	end;
-handle_call({set_port, Ref, Port}, _, State) ->
+handle_call({set_addr, Ref, Addr}, _, State) ->
-	true = ets:insert(?TAB, {{port, Ref}, Port}),
+	true = ets:insert(?TAB, {{addr, Ref}, Addr}),
 	{reply, ok, State};
 	{reply, ok, State};
 handle_call({set_max_conns, Ref, MaxConns}, _, State) ->
 handle_call({set_max_conns, Ref, MaxConns}, _, State) ->
 	ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
 	ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),