ebb_chain_fsm.erl 7.1 KB

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