[erlang-questions] Erlang beginner questions

Attila Rajmund Nohl attila.r.nohl@REDACTED
Wed Apr 13 13:57:02 CEST 2011

2011/4/13, Mode7James <James@REDACTED>:
> 1) gen_server is a behavior.  It's a little confusing that gen_udp and
> gen_tcp are both modules, but use the same naming conventions.   I've seen
> quite a few documents on how one uses "gen_server" as a behavior - is there
> any that actually show what the text file looks like?

Very very simple example:

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3, start/1]).

init(Args) ->
    {ok, Args}.

handle_call(stop, _, _) ->
    {stop, stopped, empty};

handle_call(Request, _From, State) ->
    io:format("~p ~n", [Request]),
    io:format("slept ~p ~n", [Request]),
    {reply, Request, State}.

handle_cast(_Request, State) ->
    {noreply, State}.

handle_info(Request, State) ->
    io:format("info: ~p ~n", [Request]),
    {noreply, State}.

terminate(_, _) ->

code_change(_, _, _) ->
    {ok, nostate}.

start(Args) ->
    gen_server:start(s, ?MODULE, Args, []).

> Can I create my own
> behaviors?

Yes, just export a behaviour_info/1 function from your module which
returns the expected functions:

behaviour_info(callbacks) ->
   [{foo, 1}, {bar, 2}].

>  I come from a flash background (Java) so I'm very familiar with
> interfaces.  Can one module implement multiple behaviors, or is it only
> one-to-one direct relationship?

You can implement multiple behaviours.

>  Is there somewhere that I can see the
> complete master list of built-in OTP behaviors and what they do?

Well, the generally useful are gen_server, gen_fsm, supervisor and
maybe gen_event, they are documented. There are some
application-specific behaviours like snmpa_discovery_handler or
ssh_sftpd_file_api. They are documented with the  applications

> 3) Can someone log in via TCP connection, authenticate credentials upgrading
> to SSL,

As far as I know, this is possible.

> and then also open a UDP connection for the gameplay mechanics?

UDP is stateless, so I'm not quite sure what do you mean by "UDP
connection". Of course, you can send UDP packets whenever you want,
but you have to devise a clever authentication method that makes e.g.
replay attacks impossible.

More information about the erlang-questions mailing list