30
Behaviours and Applica2ons gen_server, supervisor and applica2on

Behaviours and Applications

Embed Size (px)

DESCRIPTION

Strange Loop 2011 Erlang Workshop ->Code is available -> https://github.com/ericbmerritt/strangeloop-behaviours-and-applications

Citation preview

Page 1: Behaviours and Applications

Behaviours  and  Applica2ons

gen_server,  supervisor  and  applica2on

Page 2: Behaviours and Applications

What  You  Will  Build

Page 3: Behaviours and Applications

The  Protocol

Module:Func+on(Arg1,  ...,  ArgN).

<func+on  return  as  text>

Page 4: Behaviours and Applications

In  Broad  Strokes

4

Step  1:  tr_server Step  2:  OTP  packaged

Page 5: Behaviours and Applications

How  the  Fundamental  Pieces  Fit

Func+ons

Modules

Contain Run

ProcessesSend

Are  sent  to

Messages

Page 6: Behaviours and Applications

Behaviour  Basics

• Behaviour  Interface• Behaviour  Implementa2on

• Behaviour  Container

6

Page 7: Behaviours and Applications

Take  a  Look

ex1.erl  -­‐>  ex2.erl  -­‐>  ex2behaviour.erl

Page 8: Behaviours and Applications

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

Page 9: Behaviours and Applications

gen_server  Interface

9

handle_call/3

ronous

handle_info/2let’s  talk  later...

asyncronous

init/1

handle_call/3

Page 10: Behaviours and Applications

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

Page 11: Behaviours and Applications

What’s  Calls  What,  Where?

11

Client  process

gen_servercontainer

Dispatchto  impl

?MODULE:store()  -­‐>  gen_server:call/2

?MODULE:handle_call/3

Page 12: Behaviours and Applications

Documenta2on  -­‐  EDoc

12

%%%  File  Scope@author@copyright

%%  Func+on  Scope@doc@spec  |  -­‐spec@end

Page 13: Behaviours and Applications

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

Page 14: Behaviours and Applications

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

Page 15: Behaviours and Applications

Our  TCP  Handling  gen_server

tcp_rpc/src/tr_server.erl

Page 16: Behaviours and Applications

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]).

Page 17: Behaviours and Applications

Applica2ons

LibraryAc+ve

Page 18: Behaviours and Applications

Applica2on  Layout  

Page 19: Behaviours and Applications

The  Directory  Layout  

<application-name>[-<version>] | |- doc |- ebin |- include |- priv |- src

Page 20: Behaviours and Applications

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, []}} ]}.

Page 21: Behaviours and Applications

Applica2on  Behavior

tcp_rpc/src/tr_app.erl

Page 22: Behaviours and Applications

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

Page 23: Behaviours and Applications

Supervisors

Page 24: Behaviours and Applications

Supervisors  Hierarchy

Page 25: Behaviours and Applications

Supervisors  Restarts

Page 26: Behaviours and Applications

Supervisor  Behaviour

tcp_rpc/src/tr_sup.erl

1.  tr_app  needs  to  start  this  top  level  supervisor

Page 27: Behaviours and Applications

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

Page 28: Behaviours and Applications

Let’s  Play

Page 29: Behaviours and Applications

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().

Page 30: Behaviours and Applications

Wrapping  Up

30