syn_backbone.erl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. -module(syn_backbone).
  27. -behaviour(gen_server).
  28. %% API
  29. -export([start_link/0]).
  30. -export([get_event_handler_module/0]).
  31. %% gen_server callbacks
  32. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  33. %% macros
  34. -define(DEFAULT_EVENT_HANDLER_MODULE, syn_event_handler).
  35. %% records
  36. -record(state, {}).
  37. %% includes
  38. -include("syn.hrl").
  39. %% ===================================================================
  40. %% API
  41. %% ===================================================================
  42. -spec start_link() -> {ok, pid()} | {error, any()}.
  43. start_link() ->
  44. Options = [],
  45. gen_server:start_link({local, ?MODULE}, ?MODULE, [], Options).
  46. -spec get_event_handler_module() -> module().
  47. get_event_handler_module() ->
  48. %% get handler
  49. CustomEventHandler = application:get_env(syn, event_handler, ?DEFAULT_EVENT_HANDLER_MODULE),
  50. %% ensure that is it loaded (not using code:ensure_loaded/1 to support embedded mode)
  51. catch CustomEventHandler:module_info(exports),
  52. %% return
  53. CustomEventHandler.
  54. %% ===================================================================
  55. %% Callbacks
  56. %% ===================================================================
  57. %% ----------------------------------------------------------------------------------------------------------
  58. %% Init
  59. %% ----------------------------------------------------------------------------------------------------------
  60. -spec init([]) ->
  61. {ok, #state{}} |
  62. {ok, #state{}, Timeout :: non_neg_integer()} |
  63. ignore |
  64. {stop, Reason :: any()}.
  65. init([]) ->
  66. %% create ETS tables
  67. %% entries have structure {Name, Pid, Meta, MonitorRef, Node}
  68. ets:new(syn_registry_by_name, [set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
  69. %% entries have format {{Pid, Name}, Meta, MonitorRef, Node}
  70. ets:new(syn_registry_by_pid, [ordered_set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
  71. %% entries have format {{GroupName, Pid}, Meta, MonitorRef, Node}
  72. ets:new(syn_groups_by_name, [ordered_set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
  73. %% entries have format {{Pid, GroupName}, Meta, MonitorRef, Node}
  74. ets:new(syn_groups_by_pid, [ordered_set, public, named_table, {read_concurrency, true}, {write_concurrency, true}]),
  75. %% init
  76. {ok, #state{}}.
  77. %% ----------------------------------------------------------------------------------------------------------
  78. %% Call messages
  79. %% ----------------------------------------------------------------------------------------------------------
  80. -spec handle_call(Request :: any(), From :: any(), #state{}) ->
  81. {reply, Reply :: any(), #state{}} |
  82. {reply, Reply :: any(), #state{}, Timeout :: non_neg_integer()} |
  83. {noreply, #state{}} |
  84. {noreply, #state{}, Timeout :: non_neg_integer()} |
  85. {stop, Reason :: any(), Reply :: any(), #state{}} |
  86. {stop, Reason :: any(), #state{}}.
  87. handle_call(Request, From, State) ->
  88. error_logger:warning_msg("Syn(~p): Received from ~p an unknown call message: ~p~n", [node(), Request, From]),
  89. {reply, undefined, State}.
  90. %% ----------------------------------------------------------------------------------------------------------
  91. %% Cast messages
  92. %% ----------------------------------------------------------------------------------------------------------
  93. -spec handle_cast(Msg :: any(), #state{}) ->
  94. {noreply, #state{}} |
  95. {noreply, #state{}, Timeout :: non_neg_integer()} |
  96. {stop, Reason :: any(), #state{}}.
  97. handle_cast(Msg, State) ->
  98. error_logger:warning_msg("Syn(~p): Received an unknown cast message: ~p~n", [node(), Msg]),
  99. {noreply, State}.
  100. %% ----------------------------------------------------------------------------------------------------------
  101. %% All non Call / Cast messages
  102. %% ----------------------------------------------------------------------------------------------------------
  103. -spec handle_info(Info :: any(), #state{}) ->
  104. {noreply, #state{}} |
  105. {noreply, #state{}, Timeout :: non_neg_integer()} |
  106. {stop, Reason :: any(), #state{}}.
  107. handle_info(Info, State) ->
  108. error_logger:warning_msg("Syn(~p): Received an unknown info message: ~p~n", [node(), Info]),
  109. {noreply, State}.
  110. %% ----------------------------------------------------------------------------------------------------------
  111. %% Terminate
  112. %% ----------------------------------------------------------------------------------------------------------
  113. -spec terminate(Reason :: any(), #state{}) -> terminated.
  114. terminate(Reason, _State) ->
  115. error_logger:info_msg("Syn(~p): Terminating with reason: ~p~n", [node(), Reason]),
  116. %% delete ETS tables
  117. ets:delete(syn_registry_by_name),
  118. ets:delete(syn_registry_by_pid),
  119. ets:delete(syn_groups_by_name),
  120. ets:delete(syn_groups_by_pid),
  121. %% return
  122. terminated.
  123. %% ----------------------------------------------------------------------------------------------------------
  124. %% Convert process state when code is changed.
  125. %% ----------------------------------------------------------------------------------------------------------
  126. -spec code_change(OldVsn :: any(), #state{}, Extra :: any()) -> {ok, #state{}}.
  127. code_change(_OldVsn, State, _Extra) ->
  128. {ok, State}.