ebb_operation_fsm.erl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. -module(ebb_operation_fsm).
  2. -behaviour(gen_fsm).
  3. -include("../include/ebb_prim.hrl").
  4. %% API
  5. -export([start_link/1, start_link/2]).
  6. -export([send_in/2, send_in/3, return_out/1, cleanup/1]).
  7. %% gen_fsm callbacks
  8. -export([init/1, handle_event/3, handle_sync_event/4, handle_info/3,
  9. terminate/3, code_change/4]).
  10. %% state callbacks
  11. -export([running/2, running/3, finished/2, finished/3]).
  12. -define(DICT, orddict).
  13. -record(state, {mode, operation, out_arity, output,
  14. return_requests, pending_cleanup}).
  15. %%====================================================================
  16. %% API
  17. %%====================================================================
  18. %%--------------------------------------------------------------------
  19. %% Function: start_link() -> ok,Pid} | ignore | {error,Error}
  20. %% Description:Creates a gen_fsm process which calls Module:init/1 to
  21. %% initialize. To ensure a synchronized start-up procedure, this function
  22. %% does not return until Module:init/1 has returned.
  23. %%--------------------------------------------------------------------
  24. start_link(Op) ->
  25. start_link(Op, local).
  26. start_link(Op, Mode) ->
  27. gen_fsm:start_link(?MODULE, {Op, Mode}, []).
  28. send_in(Op, N, Arg) ->
  29. ebb_event:in(Op, N, Arg).
  30. send_in(Op, Args) ->
  31. lists:foreach(fun({N, Arg}) -> send_in(Op, N, Arg) end,
  32. lists:zip(lists:seq(1, length(Args)), Args)).
  33. return_out(Op) ->
  34. gen_fsm:sync_send_event(Op, return_out, infinity).
  35. cleanup(Op) ->
  36. gen_fsm:send_event(Op, cleanup).
  37. %%====================================================================
  38. %% gen_fsm callbacks
  39. %%====================================================================
  40. %%--------------------------------------------------------------------
  41. %% Function: init(Args) -> {ok, StateName, State} |
  42. %% {ok, StateName, State, Timeout} |
  43. %% ignore |
  44. %% {stop, StopReason}
  45. %% Description:Whenever a gen_fsm is started using gen_fsm:start/[3,4] or
  46. %% gen_fsm:start_link/3,4, this function is called by the new process to
  47. %% initialize.
  48. %%--------------------------------------------------------------------
  49. init({Op, Mode}) ->
  50. State = #state{mode=Mode, out_arity=ebb_prim:out_arity(Op),
  51. output=?DICT:new(), return_requests=[],
  52. pending_cleanup=false},
  53. case start_operation(Op, Mode, self()) of
  54. {ok, Pid} -> {ok, running, State#state{operation=Pid}};
  55. ignore -> {ok, running, State};
  56. Error -> Error
  57. end.
  58. %%--------------------------------------------------------------------
  59. %% Function:
  60. %% state_name(Event, State) -> {next_state, NextStateName, NextState}|
  61. %% {next_state, NextStateName,
  62. %% NextState, Timeout} |
  63. %% {stop, Reason, NewState}
  64. %% Description:There should be one instance of this function for each possible
  65. %% state name. Whenever a gen_fsm receives an event sent using
  66. %% gen_fsm:send_event/2, the instance of this function with the same name as
  67. %% the current state name StateName is called to handle the event. It is also
  68. %% called if a timeout occurs.
  69. %%--------------------------------------------------------------------
  70. running({out, N, Val},
  71. State = #state{out_arity=Arity, output=Output,
  72. return_requests=Requests, pending_cleanup=Clean})
  73. when N > 0, N =< Arity ->
  74. Output2 = ?DICT:store(N, Val, Output),
  75. State2 = State#state{output=Output2},
  76. case is_done(State2) of
  77. true -> send_out_requests(Requests, Output2),
  78. State3 = State2#state{return_requests=[]},
  79. case Clean of
  80. true -> {stop, normal, State3};
  81. false -> {next_state, finished, State3}
  82. end;
  83. false -> {next_state, running, State2}
  84. end;
  85. running({in, N, Arg}, State = #state{operation=Pid}) ->
  86. ebb_event:in(Pid, N, Arg),
  87. {next_state, running, State};
  88. running(cleanup, State) ->
  89. State2 = State#state{pending_cleanup=true},
  90. {next_state, running, State2}.
  91. finished(cleanup, State) ->
  92. {stop, normal, State}.
  93. %%--------------------------------------------------------------------
  94. %% Function:
  95. %% state_name(Event, From, State) -> {next_state, NextStateName, NextState} |
  96. %% {next_state, NextStateName,
  97. %% NextState, Timeout} |
  98. %% {reply, Reply, NextStateName, NextState}|
  99. %% {reply, Reply, NextStateName,
  100. %% NextState, Timeout} |
  101. %% {stop, Reason, NewState}|
  102. %% {stop, Reason, Reply, NewState}
  103. %% Description: There should be one instance of this function for each
  104. %% possible state name. Whenever a gen_fsm receives an event sent using
  105. %% gen_fsm:sync_send_event/2,3, the instance of this function with the same
  106. %% name as the current state name StateName is called to handle the event.
  107. %%--------------------------------------------------------------------
  108. running(return_out, From,
  109. State = #state{out_arity=Arity, return_requests=Requests}) ->
  110. case Arity of
  111. 0 -> {reply, [], running, State};
  112. _ -> State2 = State#state{return_requests=[From|Requests]},
  113. {next_state, running, State2}
  114. end.
  115. finished(return_out, _From, State = #state{output=Output}) ->
  116. Reply = format_output(Output),
  117. {reply, Reply, finished, State}.
  118. %%--------------------------------------------------------------------
  119. %% Function:
  120. %% handle_event(Event, StateName, State) -> {next_state, NextStateName,
  121. %% NextState} |
  122. %% {next_state, NextStateName,
  123. %% NextState, Timeout} |
  124. %% {stop, Reason, NewState}
  125. %% Description: Whenever a gen_fsm receives an event sent using
  126. %% gen_fsm:send_all_state_event/2, this function is called to handle
  127. %% the event.
  128. %%--------------------------------------------------------------------
  129. handle_event(_Event, StateName, State) ->
  130. {next_state, StateName, State}.
  131. %%--------------------------------------------------------------------
  132. %% Function:
  133. %% handle_sync_event(Event, From, StateName,
  134. %% State) -> {next_state, NextStateName, NextState} |
  135. %% {next_state, NextStateName, NextState,
  136. %% Timeout} |
  137. %% {reply, Reply, NextStateName, NextState}|
  138. %% {reply, Reply, NextStateName, NextState,
  139. %% Timeout} |
  140. %% {stop, Reason, NewState} |
  141. %% {stop, Reason, Reply, NewState}
  142. %% Description: Whenever a gen_fsm receives an event sent using
  143. %% gen_fsm:sync_send_all_state_event/2,3, this function is called to handle
  144. %% the event.
  145. %%--------------------------------------------------------------------
  146. handle_sync_event(_Event, _From, StateName, State) ->
  147. Reply = ok,
  148. {reply, Reply, StateName, State}.
  149. %%--------------------------------------------------------------------
  150. %% Function:
  151. %% handle_info(Info,StateName,State)-> {next_state, NextStateName, NextState}|
  152. %% {next_state, NextStateName, NextState,
  153. %% Timeout} |
  154. %% {stop, Reason, NewState}
  155. %% Description: This function is called by a gen_fsm when it receives any
  156. %% other message than a synchronous or asynchronous event
  157. %% (or a system message).
  158. %%--------------------------------------------------------------------
  159. handle_info(_Info, StateName, State) ->
  160. {next_state, StateName, State}.
  161. %%--------------------------------------------------------------------
  162. %% Function: terminate(Reason, StateName, State) -> void()
  163. %% Description:This function is called by a gen_fsm when it is about
  164. %% to terminate. It should be the opposite of Module:init/1 and do any
  165. %% necessary cleaning up. When it returns, the gen_fsm terminates with
  166. %% Reason. The return value is ignored.
  167. %%--------------------------------------------------------------------
  168. terminate(_Reason, _StateName, _State) ->
  169. ok.
  170. %%--------------------------------------------------------------------
  171. %% Function:
  172. %% code_change(OldVsn, StateName, State, Extra) -> {ok, StateName, NewState}
  173. %% Description: Convert process state when code is changed
  174. %%--------------------------------------------------------------------
  175. code_change(_OldVsn, StateName, State, _Extra) ->
  176. {ok, StateName, State}.
  177. %%--------------------------------------------------------------------
  178. %%% Internal functions
  179. %%--------------------------------------------------------------------
  180. start_operation(Func = #func{}, Mode, Receiver) ->
  181. ebb_func_fsm:start_link(Func, Mode, Receiver);
  182. start_operation(Value = #value{}, _Mode, Receiver) ->
  183. ebb_value_fsm:start_link(Value, Receiver);
  184. start_operation(Pipe = #pipe{}, Mode, Receiver) ->
  185. ebb_pipe_fsm:start_link(
  186. Pipe, fun(Op, Ret) -> start_operation(Op, Mode, Ret) end, Receiver);
  187. start_operation(Par = #par{}, Mode, Receiver) ->
  188. ebb_par_fsm:start_link(
  189. Par, fun(Op, Ret) -> start_operation(Op, Mode, Ret) end, Receiver);
  190. start_operation(Route = #route{}, _Mode, Receiver) ->
  191. ebb_route_fsm:start_link(Route, Receiver);
  192. start_operation(Sync = #sync{}, _Mode, Receiver) ->
  193. ebb_sync_fsm:start_link(Sync, Receiver);
  194. start_operation(Split = #split{}, _Mode, Receiver) ->
  195. ebb_split_fsm:start_link(Split, Receiver);
  196. start_operation(Merge = #merge{}, _Mode, Receiver) ->
  197. ebb_merge_fsm:start_link(Merge, Receiver);
  198. start_operation(Switch = #switch{}, Mode, Receiver) ->
  199. ebb_switch_fsm:start_link(
  200. Switch, fun(Op, Ret) -> start_operation(Op, Mode, Ret) end, Receiver).
  201. is_done(#state{out_arity=Arity, output=Output}) ->
  202. ?DICT:size(Output) == Arity.
  203. format_output(Output) ->
  204. [ V || {_, V} <- lists:keysort(1, ?DICT:to_list(Output)) ].
  205. send_out_requests(Requests, Output) ->
  206. Out = format_output(Output),
  207. lists:foreach(fun(From) -> gen_fsm:reply(From, Out) end, Requests).