ebb_value_fsm.erl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. -module(ebb_value_fsm).
  2. -behaviour(gen_fsm).
  3. -include("../include/ebb_prim.hrl").
  4. %% API
  5. -export([start_link/2]).
  6. %% gen_fsm callbacks
  7. -export([init/1, handle_event/3,
  8. handle_sync_event/4, handle_info/3, terminate/3, code_change/4]).
  9. %%====================================================================
  10. %% API
  11. %%====================================================================
  12. %%--------------------------------------------------------------------
  13. %% Function: start_link() -> ok,Pid} | ignore | {error,Error}
  14. %% Description:Creates a gen_fsm process which calls Module:init/1 to
  15. %% initialize. To ensure a synchronized start-up procedure, this function
  16. %% does not return until Module:init/1 has returned.
  17. %%--------------------------------------------------------------------
  18. start_link(Value = #value{}, Receiver) ->
  19. gen_fsm:start_link(?MODULE, {Value, Receiver}, []).
  20. %%====================================================================
  21. %% gen_fsm callbacks
  22. %%====================================================================
  23. %%--------------------------------------------------------------------
  24. %% Function: init(Args) -> {ok, StateName, State} |
  25. %% {ok, StateName, State, Timeout} |
  26. %% ignore |
  27. %% {stop, StopReason}
  28. %% Description:Whenever a gen_fsm is started using gen_fsm:start/[3,4] or
  29. %% gen_fsm:start_link/3,4, this function is called by the new process to
  30. %% initialize.
  31. %%--------------------------------------------------------------------
  32. init({#value{value=Value}, Receiver}) ->
  33. ebb_event:out(Receiver, 1, Value),
  34. ignore.
  35. %%--------------------------------------------------------------------
  36. %% Function:
  37. %% state_name(Event, State) -> {next_state, NextStateName, NextState}|
  38. %% {next_state, NextStateName,
  39. %% NextState, Timeout} |
  40. %% {stop, Reason, NewState}
  41. %% Description:There should be one instance of this function for each possible
  42. %% state name. Whenever a gen_fsm receives an event sent using
  43. %% gen_fsm:send_event/2, the instance of this function with the same name as
  44. %% the current state name StateName is called to handle the event. It is also
  45. %% called if a timeout occurs.
  46. %%--------------------------------------------------------------------
  47. %%--------------------------------------------------------------------
  48. %% Function:
  49. %% state_name(Event, From, State) -> {next_state, NextStateName, NextState} |
  50. %% {next_state, NextStateName,
  51. %% NextState, Timeout} |
  52. %% {reply, Reply, NextStateName, NextState}|
  53. %% {reply, Reply, NextStateName,
  54. %% NextState, Timeout} |
  55. %% {stop, Reason, NewState}|
  56. %% {stop, Reason, Reply, NewState}
  57. %% Description: There should be one instance of this function for each
  58. %% possible state name. Whenever a gen_fsm receives an event sent using
  59. %% gen_fsm:sync_send_event/2,3, the instance of this function with the same
  60. %% name as the current state name StateName is called to handle the event.
  61. %%--------------------------------------------------------------------
  62. %%--------------------------------------------------------------------
  63. %% Function:
  64. %% handle_event(Event, StateName, State) -> {next_state, NextStateName,
  65. %% NextState} |
  66. %% {next_state, NextStateName,
  67. %% NextState, Timeout} |
  68. %% {stop, Reason, NewState}
  69. %% Description: Whenever a gen_fsm receives an event sent using
  70. %% gen_fsm:send_all_state_event/2, this function is called to handle
  71. %% the event.
  72. %%--------------------------------------------------------------------
  73. handle_event(_Event, StateName, State) ->
  74. {next_state, StateName, State}.
  75. %%--------------------------------------------------------------------
  76. %% Function:
  77. %% handle_sync_event(Event, From, StateName,
  78. %% State) -> {next_state, NextStateName, NextState} |
  79. %% {next_state, NextStateName, NextState,
  80. %% Timeout} |
  81. %% {reply, Reply, NextStateName, NextState}|
  82. %% {reply, Reply, NextStateName, NextState,
  83. %% Timeout} |
  84. %% {stop, Reason, NewState} |
  85. %% {stop, Reason, Reply, NewState}
  86. %% Description: Whenever a gen_fsm receives an event sent using
  87. %% gen_fsm:sync_send_all_state_event/2,3, this function is called to handle
  88. %% the event.
  89. %%--------------------------------------------------------------------
  90. handle_sync_event(_Event, _From, StateName, State) ->
  91. Reply = ok,
  92. {reply, Reply, StateName, State}.
  93. %%--------------------------------------------------------------------
  94. %% Function:
  95. %% handle_info(Info,StateName,State)-> {next_state, NextStateName, NextState}|
  96. %% {next_state, NextStateName, NextState,
  97. %% Timeout} |
  98. %% {stop, Reason, NewState}
  99. %% Description: This function is called by a gen_fsm when it receives any
  100. %% other message than a synchronous or asynchronous event
  101. %% (or a system message).
  102. %%--------------------------------------------------------------------
  103. handle_info(_Info, StateName, State) ->
  104. {next_state, StateName, State}.
  105. %%--------------------------------------------------------------------
  106. %% Function: terminate(Reason, StateName, State) -> void()
  107. %% Description:This function is called by a gen_fsm when it is about
  108. %% to terminate. It should be the opposite of Module:init/1 and do any
  109. %% necessary cleaning up. When it returns, the gen_fsm terminates with
  110. %% Reason. The return value is ignored.
  111. %%--------------------------------------------------------------------
  112. terminate(_Reason, _StateName, _State) ->
  113. ok.
  114. %%--------------------------------------------------------------------
  115. %% Function:
  116. %% code_change(OldVsn, StateName, State, Extra) -> {ok, StateName, NewState}
  117. %% Description: Convert process state when code is changed
  118. %%--------------------------------------------------------------------
  119. code_change(_OldVsn, StateName, State, _Extra) ->
  120. {ok, StateName, State}.
  121. %%--------------------------------------------------------------------
  122. %%% Internal functions
  123. %%--------------------------------------------------------------------