ebb_switch_fsm.erl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. -module(ebb_switch_fsm).
  2. -behaviour(gen_fsm).
  3. -include("../include/ebb_prim.hrl").
  4. %% API
  5. -export([start_link/3]).
  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, forwarding/2]).
  11. -define(DICT, orddict).
  12. -record(state, {arity, map, vals, run, 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(Switch = #switch{}, Run, Receiver)
  23. when is_function(Run), is_pid(Receiver) ->
  24. gen_fsm:start_link(?MODULE, {Switch, Run, Receiver}, []).
  25. %%====================================================================
  26. %% gen_fsm callbacks
  27. %%====================================================================
  28. %%--------------------------------------------------------------------
  29. %% Function: init(Args) -> {ok, StateName, State} |
  30. %% {ok, StateName, State, Timeout} |
  31. %% ignore |
  32. %% {stop, StopReason}
  33. %% Description:Whenever a gen_fsm is started using gen_fsm:start/[3,4] or
  34. %% gen_fsm:start_link/3,4, this function is called by the new process to
  35. %% initialize.
  36. %%--------------------------------------------------------------------
  37. init({#switch{in=Arity, map=Map}, Run, Receiver}) ->
  38. State = #state{arity=Arity, map=?DICT:from_list(Map), vals=?DICT:new(),
  39. run=Run, receiver=Receiver},
  40. {ok, waiting, State}.
  41. %%--------------------------------------------------------------------
  42. %% Function:
  43. %% state_name(Event, State) -> {next_state, NextStateName, NextState}|
  44. %% {next_state, NextStateName,
  45. %% NextState, Timeout} |
  46. %% {stop, Reason, NewState}
  47. %% Description:There should be one instance of this function for each possible
  48. %% state name. Whenever a gen_fsm receives an event sent using
  49. %% gen_fsm:send_event/2, the instance of this function with the same name as
  50. %% the current state name StateName is called to handle the event. It is also
  51. %% called if a timeout occurs.
  52. %%--------------------------------------------------------------------
  53. waiting({in, 1, Tag},
  54. State = #state{map=Map, vals=Vals, run=Run, receiver=Receiver}) ->
  55. Path = ?DICT:fetch(Tag, Map),
  56. {ok, Target} = Run(Path, Receiver),
  57. State2 = State#state{receiver=Target},
  58. forward_all(Vals, Target),
  59. case is_done(State2) of
  60. true -> {stop, normal, State2};
  61. false -> {next_state, forwarding, State2}
  62. end;
  63. waiting({in, N, Val}, State = #state{arity=Arity, vals=Vals})
  64. when N > 0, N =< Arity ->
  65. Vals2 = ?DICT:store(N, Val, Vals),
  66. State2 = State#state{vals=Vals2},
  67. {next_state, waiting, State2}.
  68. forwarding({in, N, Val},
  69. State = #state{arity=Arity, vals=Vals, receiver=Receiver})
  70. when N > 1, N =< Arity ->
  71. Vals2 = ?DICT:store(N, Val, Vals),
  72. State2 = State#state{vals=Vals2},
  73. forward(N, Val, Receiver),
  74. case is_done(State2) of
  75. true -> {stop, normal, State2};
  76. false -> {next_state, forwarding, State2}
  77. end.
  78. %%--------------------------------------------------------------------
  79. %% Function:
  80. %% state_name(Event, From, State) -> {next_state, NextStateName, NextState} |
  81. %% {next_state, NextStateName,
  82. %% NextState, Timeout} |
  83. %% {reply, Reply, NextStateName, NextState}|
  84. %% {reply, Reply, NextStateName,
  85. %% NextState, Timeout} |
  86. %% {stop, Reason, NewState}|
  87. %% {stop, Reason, Reply, NewState}
  88. %% Description: There should be one instance of this function for each
  89. %% possible state name. Whenever a gen_fsm receives an event sent using
  90. %% gen_fsm:sync_send_event/2,3, the instance of this function with the same
  91. %% name as the current state name StateName is called to handle the event.
  92. %%--------------------------------------------------------------------
  93. %%--------------------------------------------------------------------
  94. %% Function:
  95. %% handle_event(Event, StateName, State) -> {next_state, NextStateName,
  96. %% NextState} |
  97. %% {next_state, NextStateName,
  98. %% NextState, Timeout} |
  99. %% {stop, Reason, NewState}
  100. %% Description: Whenever a gen_fsm receives an event sent using
  101. %% gen_fsm:send_all_state_event/2, this function is called to handle
  102. %% the event.
  103. %%--------------------------------------------------------------------
  104. handle_event(_Event, StateName, State) ->
  105. {next_state, StateName, State}.
  106. %%--------------------------------------------------------------------
  107. %% Function:
  108. %% handle_sync_event(Event, From, StateName,
  109. %% State) -> {next_state, NextStateName, NextState} |
  110. %% {next_state, NextStateName, NextState,
  111. %% Timeout} |
  112. %% {reply, Reply, NextStateName, NextState}|
  113. %% {reply, Reply, NextStateName, NextState,
  114. %% Timeout} |
  115. %% {stop, Reason, NewState} |
  116. %% {stop, Reason, Reply, NewState}
  117. %% Description: Whenever a gen_fsm receives an event sent using
  118. %% gen_fsm:sync_send_all_state_event/2,3, this function is called to handle
  119. %% the event.
  120. %%--------------------------------------------------------------------
  121. handle_sync_event(_Event, _From, StateName, State) ->
  122. Reply = ok,
  123. {reply, Reply, StateName, State}.
  124. %%--------------------------------------------------------------------
  125. %% Function:
  126. %% handle_info(Info,StateName,State)-> {next_state, NextStateName, NextState}|
  127. %% {next_state, NextStateName, NextState,
  128. %% Timeout} |
  129. %% {stop, Reason, NewState}
  130. %% Description: This function is called by a gen_fsm when it receives any
  131. %% other message than a synchronous or asynchronous event
  132. %% (or a system message).
  133. %%--------------------------------------------------------------------
  134. handle_info(_Info, StateName, State) ->
  135. {next_state, StateName, State}.
  136. %%--------------------------------------------------------------------
  137. %% Function: terminate(Reason, StateName, State) -> void()
  138. %% Description:This function is called by a gen_fsm when it is about
  139. %% to terminate. It should be the opposite of Module:init/1 and do any
  140. %% necessary cleaning up. When it returns, the gen_fsm terminates with
  141. %% Reason. The return value is ignored.
  142. %%--------------------------------------------------------------------
  143. terminate(_Reason, _StateName, _State) ->
  144. ok.
  145. %%--------------------------------------------------------------------
  146. %% Function:
  147. %% code_change(OldVsn, StateName, State, Extra) -> {ok, StateName, NewState}
  148. %% Description: Convert process state when code is changed
  149. %%--------------------------------------------------------------------
  150. code_change(_OldVsn, StateName, State, _Extra) ->
  151. {ok, StateName, State}.
  152. %%--------------------------------------------------------------------
  153. %%% Internal functions
  154. %%--------------------------------------------------------------------
  155. forward_all(Vals, Receiver) ->
  156. lists:foreach(fun({N, Val}) -> forward(N, Val, Receiver) end,
  157. ?DICT:to_list(Vals)).
  158. forward(N, Val, Receiver) ->
  159. ebb_event:in(Receiver, N-1, Val).
  160. is_done(#state{arity=Arity, vals=Vals}) ->
  161. ?DICT:size(Vals) == Arity-1.