zoukankan      html  css  js  c++  java
  • erlang的gen_event例子

    gen_event能查看的例子不多,把网上的做个标记


    curling_scoreboard_hw.erl

    -module(curling_scoreboard_hw).
    -export([add_point/1, next_round/0, set_teams/2, reset_board/0]).
    
    %% This is a 'dumb' module that's only there to replace what a real hardware
    %% controller would likely do. The real hardware controller would likely hold
    %% some state and make sure everything works right, but this one doesn't mind.
    
    %% Shows the teams on the scoreboard.
    set_teams(TeamA, TeamB) ->
        io:format("Scoreboard: Team ~s vs. Team ~s~n", [TeamA, TeamB]).
    
    next_round() ->
        io:format("Scoreboard: round over~n").
    
    add_point(Team) ->
        io:format("Scoreboard: increased score of team ~s by 1~n", [Team]).
    
    reset_board() ->
        io:format("Scoreboard: All teams are undefined and all scores are 0~n").
    

      


    curling_scoreboard.erl

    -module(curling_scoreboard).
    -behaviour(gen_event).
    
    -export([init/1, handle_event/2, handle_call/2, handle_info/2, code_change/3,
             terminate/2]).
    
    init([]) ->
        {ok, []}.
    
    handle_event({set_teams, TeamA, TeamB}, State) ->
        curling_scoreboard_hw:set_teams(TeamA, TeamB),
        {ok, State};
    handle_event({add_points, Team, N}, State) ->
        [curling_scoreboard_hw:add_point(Team) || _ <- lists:seq(1,N)],
        {ok, State};
    handle_event(next_round, State) ->
        curling_scoreboard_hw:next_round(),
        {ok, State};
    handle_event(_, State) ->
        {ok, State}.
    
    handle_call(_, State) ->
        {ok, ok, State}.
    
    handle_info(_, State) ->
        {ok, State}.
    
    code_change(_OldVsn, State, _Extra) ->
        {ok, State}.
    
    terminate(_Reason, _State) ->
        ok.
    

      

     使用方法

    1> c(curling_scoreboard_hw).
    {ok,curling_scoreboard_hw}
    2> c(curling_scoreboard).
    {ok,curling_scoreboard}
    3> {ok, Pid} = gen_event:start_link().
    {ok,<0.43.0>}
    4> gen_event:add_handler(Pid, curling_scoreboard, []).
    ok
    5> gen_event:notify(Pid, {set_teams, "Pirates", "Scotsmen"}).
    Scoreboard: Team Pirates vs. Team Scotsmen
    ok
    6> gen_event:notify(Pid, {add_points, "Pirates", 3}).
    ok
    Scoreboard: increased score of team Pirates by 1
    Scoreboard: increased score of team Pirates by 1
    Scoreboard: increased score of team Pirates by 1
    7> gen_event:notify(Pid, next_round).
    Scoreboard: round over
    ok
    8> gen_event:delete_handler(Pid, curling_scoreboard, turn_off).
    ok
    9> gen_event:notify(Pid, next_round).
    ok
    

      

  • 相关阅读:
    KMP
    KMP 算法详解
    快慢指针-链表环入口问题
    算法题——只出现一次的数字
    DECODE 与CASE WHEN 的比较
    Mybatis动态传入tableName--非预编译(STATEMENT)
    mysql数据库出现无法登录(ERROR 1045 ),预防和解决及系列问题解决方法。
    15.linux iptables防火墙规则vsftp服务
    14.LAMP服务 Linux Apache Mysql Php和防护机制 xinetd、tcp wapper
    13.mysql数据库
  • 原文地址:https://www.cnblogs.com/tudou008/p/14733863.html
Copyright © 2011-2022 走看看