0

◦
◦




◦
◦
◦
◦

◦









◦
◦
◦
◦

◦





-module(shop).
-behaviour(gen_server).
%% API
-export([start_link/0, open_shop/0, close_shop/0, is_opened/0, stop/0]).
-ex...

◦


◦


◦

init([]) -> {ok, close}.
handle_cast(open_shop, State) ->
{noreply, open};
handle_cast(close_shop, State) ->
{noreply, clo...

◦



◦




◦



◦

1> c(shop).
{ok,shop}
2> shop:start_link().
{ok,<0.41.0>}
3> shop:is_opened().
close
4> shop:open_shop().
ok
5> shop:is_op...

◦
◦

◦
◦
◦
◦
◦
-module(shop_status).
-behaviour(gen_fsm).
-export([start_link/0, staff_comming/0, customer_coming/0])
-export([init/1, cl...

◦


◦



init(State) ->
{ok, closed, State}.
closed(staff_coming, State) ->
io:format("staff came. shop status is opened.~n"),
{nex...

◦




◦





◦

1> c(shop_status).
{ok,shop_status).
2> shop_status:start_link().
{ok,<0.41.0>}
3> shop_status:staff_coming().
staff came....

◦
◦
◦
◦
-module(shop_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_li...

◦
◦
◦



◦







◦
◦







◦
◦

◦
◦
◦
◦
◦
◦
Erlang otpを触ってみた
Upcoming SlideShare
Loading in...5
×

Erlang otpを触ってみた

250

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
250
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Erlang otpを触ってみた"

  1. 1.  ◦ ◦   
  2. 2.  ◦ ◦ ◦ ◦
  3. 3.  ◦        
  4. 4.  ◦ ◦ ◦ ◦
  5. 5.  ◦     
  6. 6. -module(shop). -behaviour(gen_server). %% API -export([start_link/0, open_shop/0, close_shop/0, is_opened/0, stop/0]). -export([init/1, handle_call/3, handle_cast/2, terminate/2]). start_link() -> gen_server:start_link({local, shop}, shop, [], []). open_shop() -> gen_server:cast(shop, open_shop). close_shop() -> gen_server:cast(shop, close_shop). is_opened() -> gen_server:call(shop, is_opened). stop() -> gen_server:cast(shop, stop).
  7. 7.  ◦   ◦   ◦ 
  8. 8. init([]) -> {ok, close}. handle_cast(open_shop, State) -> {noreply, open}; handle_cast(close_shop, State) -> {noreply, close}; handle_cast(stop, State) -> {stop, normal, State}. handle_call(is_opened, _From, State) -> {reply, State, State}. terminate(normal, State) -> ok.
  9. 9.  ◦    ◦   
  10. 10.  ◦    ◦ 
  11. 11. 1> c(shop). {ok,shop} 2> shop:start_link(). {ok,<0.41.0>} 3> shop:is_opened(). close 4> shop:open_shop(). ok 5> shop:is_opened(). open 6> shop:close_shop(). ok 7> shop:is_opened(). Close 8> shop:stop(). ok 9> shop:is_opened(). ** exception exit: {noproc, {gen_server,call,[shop,is_opened]}} in function gen_server:call/2 (gen_server.erl, line 180)
  12. 12.  ◦ ◦
  13. 13.  ◦ ◦ ◦ ◦ ◦
  14. 14. -module(shop_status). -behaviour(gen_fsm). -export([start_link/0, staff_comming/0, customer_coming/0]) -export([init/1, closed/2, opened/2, attending/2]) start_link() -> gen_fsm:start_link({local, shop_status}, shop_status, [], []). staff_coming() -> gen_fsm:send_event(shop_status, staff_coming). customer_coming() -> gen_fsm:send_event(shop_status, customer_coming).
  15. 15.  ◦   ◦   
  16. 16. init(State) -> {ok, closed, State}. closed(staff_coming, State) -> io:format("staff came. shop status is opened.~n"), {next_state, opened, State, 60000}. opened(customer_coming, State) -> io:format("customer came. shop status is attending.~n"), {next_state, attending, State, 5000}; opened(timeout, State) -> io:format("staff left. shop status is closed.~n"). attending(timeout, State) -> io:format("customer left. shop status is opened.~n"), {next_state, opened, State, 60000}.
  17. 17.  ◦     ◦    
  18. 18.  ◦ 
  19. 19. 1> c(shop_status). {ok,shop_status). 2> shop_status:start_link(). {ok,<0.41.0>} 3> shop_status:staff_coming(). staff came. shop status is opened. ok 4> shop_status:customer_coming(). customer came. shop status is attending. ok customer left. shop status is opened. Staff left. Shop status is closed.
  20. 20.  ◦ ◦ ◦ ◦
  21. 21. -module(shop_sup). -behaviour(supervisor). -export([start_link/0]). -export([init/1]). start_link() -> supervisor:start_link(shop_sup, []). init(_Args) -> {ok, {one_for_one, 1, 60}, [{shop_status, {shop_status, start_link, []}, permanent, brutal_kill, worker, [shop_status]}] }}.
  22. 22.  ◦ ◦ ◦  
  23. 23.  ◦      
  24. 24.  ◦ ◦      
  25. 25.  ◦ ◦
  26. 26.  ◦ ◦ ◦ ◦ ◦ ◦
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×