ebb_offset_fsm.erl 7.2 KB

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