ebb_split_fsm.erl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. -module(ebb_split_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. -record(state, {size, 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(Split = #split{}, Receiver) when is_pid(Receiver) ->
  22. gen_fsm:start_link(?MODULE, {Split, 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({#split{size=Size}, Receiver}) ->
  36. State = #state{size=Size, receiver=Receiver},
  37. {ok, waiting, 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. waiting({in, 1, Val}, State = #state{size=Size, receiver=Receiver}) ->
  51. List = case {is_list(Val), is_tuple(Val)} of
  52. {true, false} -> Val;
  53. {false, true} -> tuple_to_list(Val)
  54. end,
  55. lists:foreach(fun({N, X}) -> ebb_event:out(Receiver, N, X) end,
  56. lists:zip(lists:seq(1, Size), List)),
  57. {stop, normal, State}.
  58. %%--------------------------------------------------------------------
  59. %% Function:
  60. %% state_name(Event, From, State) -> {next_state, NextStateName, NextState} |
  61. %% {next_state, NextStateName,
  62. %% NextState, Timeout} |
  63. %% {reply, Reply, NextStateName, NextState}|
  64. %% {reply, Reply, NextStateName,
  65. %% NextState, Timeout} |
  66. %% {stop, Reason, NewState}|
  67. %% {stop, Reason, Reply, NewState}
  68. %% Description: There should be one instance of this function for each
  69. %% possible state name. Whenever a gen_fsm receives an event sent using
  70. %% gen_fsm:sync_send_event/2,3, the instance of this function with the same
  71. %% name as the current state name StateName is called to handle the event.
  72. %%--------------------------------------------------------------------
  73. %%--------------------------------------------------------------------
  74. %% Function:
  75. %% handle_event(Event, StateName, State) -> {next_state, NextStateName,
  76. %% NextState} |
  77. %% {next_state, NextStateName,
  78. %% NextState, Timeout} |
  79. %% {stop, Reason, NewState}
  80. %% Description: Whenever a gen_fsm receives an event sent using
  81. %% gen_fsm:send_all_state_event/2, this function is called to handle
  82. %% the event.
  83. %%--------------------------------------------------------------------
  84. handle_event(_Event, StateName, State) ->
  85. {next_state, StateName, State}.
  86. %%--------------------------------------------------------------------
  87. %% Function:
  88. %% handle_sync_event(Event, From, StateName,
  89. %% State) -> {next_state, NextStateName, NextState} |
  90. %% {next_state, NextStateName, NextState,
  91. %% Timeout} |
  92. %% {reply, Reply, NextStateName, NextState}|
  93. %% {reply, Reply, NextStateName, NextState,
  94. %% Timeout} |
  95. %% {stop, Reason, NewState} |
  96. %% {stop, Reason, Reply, NewState}
  97. %% Description: Whenever a gen_fsm receives an event sent using
  98. %% gen_fsm:sync_send_all_state_event/2,3, this function is called to handle
  99. %% the event.
  100. %%--------------------------------------------------------------------
  101. handle_sync_event(_Event, _From, StateName, State) ->
  102. Reply = ok,
  103. {reply, Reply, StateName, State}.
  104. %%--------------------------------------------------------------------
  105. %% Function:
  106. %% handle_info(Info,StateName,State)-> {next_state, NextStateName, NextState}|
  107. %% {next_state, NextStateName, NextState,
  108. %% Timeout} |
  109. %% {stop, Reason, NewState}
  110. %% Description: This function is called by a gen_fsm when it receives any
  111. %% other message than a synchronous or asynchronous event
  112. %% (or a system message).
  113. %%--------------------------------------------------------------------
  114. handle_info(_Info, StateName, State) ->
  115. {next_state, StateName, State}.
  116. %%--------------------------------------------------------------------
  117. %% Function: terminate(Reason, StateName, State) -> void()
  118. %% Description:This function is called by a gen_fsm when it is about
  119. %% to terminate. It should be the opposite of Module:init/1 and do any
  120. %% necessary cleaning up. When it returns, the gen_fsm terminates with
  121. %% Reason. The return value is ignored.
  122. %%--------------------------------------------------------------------
  123. terminate(_Reason, _StateName, _State) ->
  124. ok.
  125. %%--------------------------------------------------------------------
  126. %% Function:
  127. %% code_change(OldVsn, StateName, State, Extra) -> {ok, StateName, NewState}
  128. %% Description: Convert process state when code is changed
  129. %%--------------------------------------------------------------------
  130. code_change(_OldVsn, StateName, State, _Extra) ->
  131. {ok, StateName, State}.
  132. %%--------------------------------------------------------------------
  133. %%% Internal functions
  134. %%--------------------------------------------------------------------