Beginner OTP question

Bill Mill <>
Fri Feb 18 14:06:12 CET 2005


On Fri, 18 Feb 2005 06:43:32 +0100, Ulf Wiger <> wrote:
> Den 2005-02-18 04:25:18 skrev Bill Mill <>:
> 
> 
> > 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).
> 

starting to make sense now...

> > %%%%%%%%%%%%
> > /c/code/erlang/myserver$ erl -sname hello
> > Erlang (BEAM) emulator version 5.4.3 [source] [hipe]
> >
> > Eshell V5.4.3  (abort with ^G)
> > ()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)
> > ()1> {myserver, } ! 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, } ! 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.
> 

I figured that it did, I just couldn't figure out what it was. Seeing
that it's just a gen_server call is (I think, I can't test until after
work) the key insight I was looking for.

> A {myserver, } ! 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().
> 

I probably tried about a thousand variations on this (without the
parameterized send() function, obviously) before I settled on trying
to send a signal with ! .

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

I hope so too. As I said, can't test until after work, but I think I
understand a lot more now. I don't like to ask questions, and I've
probably read the 'getting started' section more times than anyone by
now. That and the gen_server section of the OTP design principles
page, I could probably recite in my sleep.

It would have been really helpful to me if there had been a really
simple OTP server example *that worked*. The one given in the
gen_server section of the OTP design principles document isn't even
vaguely close to compiling. Instead, I was left trying to distill how
OTP worked from the httpd module.

I plan to write up my really simple OTP server on my blog, so maybe
it'll help others in a similar situation.

Thanks a lot for the help.

Peace
Bill Mill
bill.mill at gmail.com



More information about the erlang-questions mailing list