Upload
ericbmerritt
View
243
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Strange Loop 2011 Erlang Workshop ->Code is available -> https://github.com/ericbmerritt/strangeloop-behaviours-and-applications
Citation preview
Behaviours and Applica2ons
gen_server, supervisor and applica2on
What You Will Build
The Protocol
Module:Func+on(Arg1, ..., ArgN).
<func+on return as text>
In Broad Strokes
4
Step 1: tr_server Step 2: OTP packaged
How the Fundamental Pieces Fit
Func+ons
Modules
Contain Run
ProcessesSend
Are sent to
Messages
Behaviour Basics
• Behaviour Interface• Behaviour Implementa2on
• Behaviour Container
6
Take a Look
ex1.erl -‐> ex2.erl -‐> ex2behaviour.erl
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
gen_server Interface
9
handle_call/3
ronous
handle_info/2let’s talk later...
asyncronous
init/1
handle_call/3
gen_server Container
10
gen_server:start_link/4
gen_server:call/2
gen_server:cast/2
init/1
handle_call/3
handle_cast/2
What’s Calls What, Where?
11
Client process
gen_servercontainer
Dispatchto impl
?MODULE:store() -‐> gen_server:call/2
?MODULE:handle_call/3
Documenta2on -‐ EDoc
12
%%% File Scope@author@copyright
%% Func+on Scope@doc@spec | -‐spec@end
Laying-‐Out a Module
13
%%%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ %%% @author Mar,n Logan <mar,[email protected]> %%% @copyright (C) 2010, Mar,n Logan %%% @doc %%% %%% @end %%% Created : 15 Sep 2010 by Mar,n Logan <mar,[email protected]> %%%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ -‐module(ex4).
%% API -‐export([]).
%%%=================================================================== %%% API %%%===================================================================
%%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ %% @doc my func,on. %% @end %%-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ -‐spec my_func(term) -‐> ok.
%%%=================================================================== %%% Internal func,ons %%%===================================================================
{
{{
Header
API
Internal
TCP Basics
14
Client1. Connect2. Send/Recv
Server1. Bind2. Listen3. Accept4. Send/Recv
} gen_tcp:listen/2
gen_tcp:accept/1gen_tcp:send/1handle_info/2
gen_tcp:connect/2
gen_tcp:send/1handle_info/2
Our TCP Handling gen_server
tcp_rpc/src/tr_server.erl
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]).
Applica2ons
LibraryAc+ve
Applica2on Layout
The Directory Layout
<application-name>[-<version>] | |- doc |- ebin |- include |- priv |- src
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, []}} ]}.
Applica2on Behavior
tcp_rpc/src/tr_app.erl
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
Supervisors
Supervisors Hierarchy
Supervisors Restarts
Supervisor Behaviour
tcp_rpc/src/tr_sup.erl
1. tr_app needs to start this top level supervisor
tcp_rpc and a Concurrent Server“the Erlang/OTP way”
27
Parent
tr_server
spawn
accept tcp
request for newtr_server
tr_serverwai+ng to accept
spawn
Let’s Play
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().
Wrapping Up
30