Behaviours and Applications5. How
the
Fundamental
Pieces
Fit
Modules Send
Processes Messages
Contain Run
Func+ons Are
sent
to
7. Take
a
Look
ex1.erl
-‐>
ex2.erl
-‐>
ex2behaviour.erl
8. Example
Behaviour
• Behaviour
Interface
– init/0
– handle_msg/2
• Behaviour
Implementa3on
– ex2
module
• Behaviour
Container
– ex2behaviour
module
– 1
to
1
process
model
– contained
in
the
ex2behaviour
module
8
9. gen_server
Interface
init/1
ronous
handle_call/3
asyncronous
handle_call/3
handle_info/2
let’s
talk
later...
9
11. What’s
Calls
What,
Where?
?MODULE:store()
-‐>
gen_server:call/2
Client
process
Dispatch
to
impl
?MODULE:handle_call/3
gen_server
container
11
12. Documenta2on
-‐
EDoc
%%%
File
Scope
@author
@copyright
%%
Func+on
Scope
@doc
@spec
|
-‐spec
@end
12
13. Laying-‐Out
a
Module
{
%%%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
%%%
@author
Mar,n
Logan
<mar,njlogan@Macintosh.local>
%%%
@copyright
(C)
2010,
Mar,n
Logan
Header %%%
@doc
%%%
%%%
@end
%%%
Created
:
15
Sep
2010
by
Mar,n
Logan
<mar,njlogan@Macintosh.local>
%%%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐module(ex4).
%%
API
-‐export([]).
{
%%%===================================================================
%%%
API
%%%===================================================================
API %%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
%%
@doc
my
func,on.
%%
@end
%%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐spec
my_func(term)
-‐>
ok.
{
%%%===================================================================
Internal %%%
Internal
func,ons
%%%===================================================================
13
14. TCP
Basics
Server
1.
Bind
2.
Listen} gen_tcp:listen/2
3.
Accept gen_tcp:accept/1
4.
Send/Recv gen_tcp:send/1
handle_info/2
Client
1.
Connect gen_tcp:connect/2
2.
Send/Recv gen_tcp:send/1
handle_info/2
14
16. Tes2ng
the
Server
$ erl –pa tcp_rpc/ebin
Eshell V5.5.5 (abort with ^G)
1> {ok, LSock} = gen_tcp:listen(8080, [{active, true}, {reuseaddr, true}]).
…
2> tr_server:start_link(LSock).
…
From
your
standard
command
line
(bash,
sh,
etc...):
$? telnet localhost 8080
> io_lib:format(“~p ~p~n”, [hello, tr_server]).
20. Applica2on
Metadata
%% -*- mode: Erlang; fill-column: 75; comment-column: 50; -*-
{application, tcp_rpc,
[{description, "RPC server for Erlang and OTP in action"},
{vsn, "0.1.0"},
{modules, [tr_app,
tr_sup,
tr_server]},
{registered, [tr_sup]},
{applications, [kernel, stdlib]},
{mod, {tr_app, []}}
]}.
22. Applica2on
is
also
a
Behaviour
• Interface
start/2,
stop/1
• Implementa2on
tr_app.erl
• Container
applica2on.erl
• Container
has
a
one
to
many
process
architecture.
22
27. tcp_rpc
and
a
Concurrent
Server
“the
Erlang/OTP
way”
Parent
spawn
spawn
accept
tcp
request
for
new
tr_server
tr_server tr_server
wai+ng
to
accept
27
29. Star2ng
The
App
$ erl –pa tcp_rpc/ebin
Eshell V5.5.5 (abort with ^G)
1> application:start(sasl).
…
2> application:start(tcp_rpc).
…
3> appmon:start().