• 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
    

      

  • 相关阅读:
    Visual Studio的输出窗口上输出调试信息的函数
    std::min error C2059: 语法错误:“::” 的解决方法
    error C2872: “flann”: 不明确的符号 --- PCL 与OpenCV2 的flann命名空间冲突问题的解决方法
    VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置
    nginx.conf的完整配置说明
    Nginx基本配置、性能优化指南
    Apache手册
    Apache 配置虚拟主机三种方式
    Linux常用命令汇总
    Linux下安装Apache
  • 原文地址:https://www.cnblogs.com/tudou008/p/14733863.html
Copyright © 2020-2023  润新知