Beginner OTP question

Serge Aleynikov serge@REDACTED
Fri Feb 18 04:53:38 CET 2005


Bill,

You should call the myserver:send() method from another process.
If you modify slightly the printing statement in your
handle_call(send, ...) procedure like this:

   io:format("~p - send called from ~p", [self(), From]),

it'll be more apparent that From is a different process from the one 
running gen_server.

The important distinction of init/1 and handle_call/3 from start/0 and 
send/0 is that the first two are required callbacks of gen_server, 
whereas the last two are the interface functions to be used by another 
process.

Regards,

Serge

Bill Mill wrote:
> Hey everyone, all I want to do is get a very simple example of two
> erlang processes talking to each other. The only additional
> complication is that I want one of them to be a gen_server.
> 
> I have written a minimal OTP gen_server that looks like:
> 
> %%%%%%%%%%%  myserver.erl %%%%%%%%%%
> -module(myserver).
> 
> -behaviour(gen_server).
> 
> -export([start/0, init/1, send/0, handle_call/3]).
> 
> start() ->
>     gen_server:start({global, myserver}, myserver, [], []).
> 
> init(_Args) ->
>     {ok, ""}.
> 
> send() ->
>     gen_server:call(myserver, send).
> 
> handle_call(send, From, _State) ->
>     io:format("send called from ~p", [From]),
>     {reply, "Thanks for calling", ""}.
> %%%%%%%% end myserver.erl %%%%%%%%%
> 
> and then started it like:
> 
> %%%%%%%%%%%%
> /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
> %%%%%%%%%%%%
> 
> Am i missing something fundamental here? How could I get "gbye" to
> talk to "hello"? Is there something wrong with the server?
> 
> Peace
> Bill Mill
> bill.mill at gmail.com



More information about the erlang-questions mailing list