Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: erlang/otp
base: maint
...
head fork: cipriancraciun/otp
compare: patches/erl_epmd-as-proper-gen_server
Checking mergeability… Don’t worry, you can still create the pull request.
  • 3 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Commits on Oct 25, 2011
@cipriancraciun cipriancraciun Rename `erl_epmd` internal functions `port_please1` to `get_port1`, a…
…nd `names1` to `get_names1`.

The functions `get_port` and `get_names` expect to receive as argument an `ip_address()`.

The renamed functions are actually simple wrappers for `get_port` and `get_names` respectively,
as their sole purpose is to take a hostname, call `inet:gethostbyname`, and delegate work.

The rename has been done in order to assist with enabling the overriding of the default
`erl_epmd` module behaviour, as described in the following email:
    http://erlang.org/pipermail/erlang-questions/2011-October/061975.html
d37811c
@cipriancraciun cipriancraciun Add timeout argument for `erl_epmd:names`.
So far `erl_epmd:port_please` allowed to specify a third argument as the timeout of the operation.

This patch adds the same option also to `erl_epmd:names` function.

The update has been done in order to assist with enabling the overriding of the default
`erl_epmd` module behaviour, as described in the following email:
    http://erlang.org/pipermail/erlang-questions/2011-October/061975.html
9166a4e
@cipriancraciun cipriancraciun Change `erl_epmd` functions `port_please` and `names` to send message…
…s to the `erl_epmd` registered process.

So far only `register_node` delegated work to the `gen_server` process registered under `erl_epmd`.
The other two functions were implemented in-line, which made impossible to override the default implementation.

This patch makes the following simple updates:
 * renames the in-line implementation as `do_port_please` and `do_names` respectively;
 * add `handle_call` clauses to handle the new messages `{port_please, ...}` and `{names, ...}`;
 * these new handlers `spawn_link` a background process that actually does the work and replies;
(All the changes were made by taking the example of `register_nodes`.)

The update has been done in order to assist with enabling the overriding of the default
`erl_epmd` module behaviour, as described in the following email:
    http://erlang.org/pipermail/erlang-questions/2011-October/061975.html
05899c2
Showing with 64 additions and 32 deletions.
  1. +64 −32 lib/kernel/src/erl_epmd.erl
View
96 lib/kernel/src/erl_epmd.erl
@@ -30,7 +30,7 @@
%% External exports
-export([start/0, start_link/0, stop/0, port_please/2,
- port_please/3, names/0, names/1,
+ port_please/3, names/0, names/1, names/2,
register_node/2, open/0, open/1, open/2]).
%% gen_server callbacks
@@ -68,41 +68,19 @@ stop() ->
port_please(Node, Host) ->
port_please(Node, Host, infinity).
-port_please(Node,HostName, Timeout) when is_atom(HostName) ->
- port_please1(Node,atom_to_list(HostName), Timeout);
-port_please(Node,HostName, Timeout) when is_list(HostName) ->
- port_please1(Node,HostName, Timeout);
-port_please(Node, EpmdAddr, Timeout) ->
- get_port(Node, EpmdAddr, Timeout).
-
+port_please(Node,HostName, Timeout) ->
+ gen_server:call(erl_epmd, {port_please, Node, HostName, Timeout}, infinity).
-port_please1(Node,HostName, Timeout) ->
- case inet:gethostbyname(HostName, inet, Timeout) of
- {ok,{hostent, _Name, _ , _Af, _Size, [EpmdAddr | _]}} ->
- get_port(Node, EpmdAddr, Timeout);
- Else ->
- Else
- end.
-
names() ->
{ok, H} = inet:gethostname(),
names(H).
-names(HostName) when is_atom(HostName) ->
- names1(atom_to_list(HostName));
-names(HostName) when is_list(HostName) ->
- names1(HostName);
-names(EpmdAddr) ->
- get_names(EpmdAddr).
+names(HostName) ->
+ names(HostName, infinity).
-names1(HostName) ->
- case inet:gethostbyname(HostName) of
- {ok,{hostent, _Name, _ , _Af, _Size, [EpmdAddr | _]}} ->
- get_names(EpmdAddr);
- Else ->
- Else
- end.
+names(HostName, Timeout) ->
+ gen_server:call(erl_epmd, {names, HostName, Timeout}, infinity).
register_node(Name, PortNo) ->
@@ -119,7 +97,8 @@ init(_) ->
%%----------------------------------------------------------------------
--type calls() :: 'client_info_req' | 'stop' | {'register', term(), term()}.
+-type calls() :: 'client_info_req' | 'stop' | {'register', term(), term()}
+ | {'port_please', term(), term(), term()} | {'names', term(), term()}.
-spec handle_call(calls(), term(), state()) ->
{'reply', term(), state()} | {'stop', 'shutdown', 'ok', state()}.
@@ -140,6 +119,28 @@ handle_call({register, Name, PortNo}, _From, State) ->
{reply, {error, already_registered}, State}
end;
+handle_call({port_please, Node, HostName, Timeout}, From, State) ->
+ spawn_link(fun() ->
+ case (catch do_port_please(Node, HostName, Timeout)) of
+ {'EXIT', Reason} ->
+ gen_server:reply(From, {error, Reason});
+ Outcome ->
+ gen_server:reply(From, Outcome)
+ end
+ end),
+ {noreply, State};
+
+handle_call({names, HostName, Timeout}, From, State) ->
+ spawn_link(fun() ->
+ case (catch do_names(HostName, Timeout)) of
+ {'EXIT', Reason} ->
+ gen_server:reply(From, {error, Reason});
+ Outcome ->
+ gen_server:reply(From, Outcome)
+ end
+ end),
+ {noreply, State};
+
handle_call(client_info_req, _From, State) ->
Reply = {ok,{r4,State#state.name,State#state.port_no}},
{reply, Reply, State};
@@ -289,6 +290,13 @@ wait_for_reg_reply(Socket, SoFar) ->
%% Lookup a node "Name" at Host
%%
+do_port_please(Node,HostName, Timeout) when is_atom(HostName) ->
+ get_port1(Node,atom_to_list(HostName), Timeout);
+do_port_please(Node,HostName, Timeout) when is_list(HostName) ->
+ get_port1(Node,HostName, Timeout);
+do_port_please(Node, EpmdAddr, Timeout) ->
+ get_port(Node, EpmdAddr, Timeout).
+
get_port(Node, EpmdAddress, Timeout) ->
case open(EpmdAddress, Timeout) of
{ok, Socket} ->
@@ -301,6 +309,14 @@ get_port(Node, EpmdAddress, Timeout) ->
noport
end.
+get_port1(Node,HostName, Timeout) ->
+ case inet:gethostbyname(HostName, inet, Timeout) of
+ {ok,{hostent, _Name, _ , _Af, _Size, [EpmdAddr | _]}} ->
+ get_port(Node, EpmdAddr, Timeout);
+ Else ->
+ Else
+ end.
+
wait_for_port_reply(Socket, SoFar) ->
receive
@@ -415,14 +431,30 @@ to_string(S) when is_list(S) -> S.
%% Find names on epmd
%%
%%
-get_names(EpmdAddress) ->
- case open(EpmdAddress) of
+
+do_names(HostName, Timeout) when is_atom(HostName) ->
+ get_names1(atom_to_list(HostName), Timeout);
+do_names(HostName, Timeout) when is_list(HostName) ->
+ get_names1(HostName, Timeout);
+do_names(EpmdAddr, Timeout) ->
+ get_names(EpmdAddr, Timeout).
+
+get_names(EpmdAddress, Timeout) ->
+ case open(EpmdAddress, Timeout) of
{ok, Socket} ->
do_get_names(Socket);
_Error ->
{error, address}
end.
+get_names1(HostName, Timeout) ->
+ case inet:gethostbyname(HostName, inet, Timeout) of
+ {ok,{hostent, _Name, _ , _Af, _Size, [EpmdAddr | _]}} ->
+ get_names(EpmdAddr, Timeout);
+ Else ->
+ Else
+ end.
+
do_get_names(Socket) ->
gen_tcp:send(Socket, [?int16(1),?EPMD_NAMES]),
receive

No commit comments for this range

Something went wrong with that request. Please try again.