Beginner OTP question

Ulf Wiger ulf@REDACTED
Fri Feb 18 06:43:32 CET 2005


Den 2005-02-18 04:25:18 skrev Bill Mill <bill.mill@REDACTED>:


> send() ->
>     gen_server:call(myserver, send).

You may want to parameterize this function, if
you want to be able to send a message to another
instance of the gen_server than the one running
locally, for example:

send(Node) ->
    gen_server:call({myserver, Node}, send).

> %%%%%%%%%%%%
> /c/code/erlang/myserver$ erl -sname hello
> Erlang (BEAM) emulator version 5.4.3 [source] [hipe]
>
> Eshell V5.4.3  (abort with ^G)
> (hello@REDACTED)1> myserver:start().
> {ok,<0.37.0>}
> %%%%%%%%%%%%
>
> How would I go about starting another process and getting it to
> communicate with this server? Here's what I expect to work, which
> obviously doesn't:
>
> %%%%%%%%%%%%
> /c/code/erlang/test$ erl -sname gbye
> Erlang (BEAM) emulator version 5.4.3 [source] [hipe]
>
> Eshell V5.4.3  (abort with ^G)
> (gbye@REDACTED)1> {myserver, myserver@REDACTED} ! send.
> send
> %%%%%%%%%%%%

The node names are visible within parentheses in the
Erlang shell prompt. So to reach the processs myserver
on the "hello" node, you would have to write:

{myserver, hello@REDACTED} ! send.

But in your program, the myserver process expected
a gen_server call, and that has a special format
which gen_server:call/2 knows about.

A {myserver, hello@REDACTED} ! send would not be
recognized by the server as a gen_server call message
and would in fact crash your server (the gen_server
module would dispatch the unknown message to the
funcion myserver:handle_info(Msg, State), which doesn't
exist.)

Given the parameterized send(Node) function above,
you would instead write:

myserver:send(hello@REDACTED).

Hope this helps you get started.
Make sure to also look in the 'getting started' and
'examples' sections at erlang.org.

/Uffe



More information about the erlang-questions mailing list