ebb_sync_fsm.erl 7.2 KB

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