syn.erl 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2021 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).
  27. %% API
  28. -export([start/0, stop/0]).
  29. -export([get_node_scopes/0, add_node_to_scope/1, add_node_to_scopes/1]).
  30. -export([lookup/1]).
  31. -export([register/2, register/3, register/4]).
  32. -export([unregister/1, unregister/2]).
  33. %% ===================================================================
  34. %% API
  35. %% ===================================================================
  36. -spec start() -> ok.
  37. start() ->
  38. {ok, _} = application:ensure_all_started(syn),
  39. ok.
  40. -spec stop() -> ok | {error, Reason :: any()}.
  41. stop() ->
  42. application:stop(syn).
  43. %% ----- \/ scopes ---------------------------------------------------
  44. -spec get_node_scopes() -> [atom()].
  45. get_node_scopes() ->
  46. syn_scopes_sup:get_node_scopes().
  47. -spec add_node_to_scope(Scope :: atom()) -> ok.
  48. add_node_to_scope(Scope) ->
  49. syn_scopes_sup:add_node_to_scope(Scope).
  50. -spec add_node_to_scopes(Scopes :: [atom()]) -> ok.
  51. add_node_to_scopes(Scopes) ->
  52. lists:foreach(fun(Scope) ->
  53. syn_scopes_sup:add_node_to_scope(Scope)
  54. end, Scopes).
  55. %% ----- \/ registry -------------------------------------------------
  56. -spec lookup(Name :: any()) -> {pid(), Meta :: any()} | undefined.
  57. lookup(Name) ->
  58. syn_registry:lookup(Name).
  59. -spec register(Name :: any(), Pid :: pid()) -> ok | {error, Reason :: any()}.
  60. register(Name, Pid) ->
  61. syn_registry:register(Name, Pid).
  62. -spec register(NameOrScope :: any(), PidOrName :: any(), MetaOrPid :: any()) -> ok | {error, Reason :: any()}.
  63. register(NameOrScope, PidOrName, MetaOrPid) ->
  64. syn_registry:register(NameOrScope, PidOrName, MetaOrPid).
  65. -spec register(Scope :: atom(), Name :: any(), Pid :: pid(), Meta :: any()) -> ok | {error, Reason :: any()}.
  66. register(Scope, Name, Pid, Meta) ->
  67. syn_registry:register(Scope, Name, Pid, Meta).
  68. -spec unregister(Name :: any()) -> ok | {error, Reason :: any()}.
  69. unregister(Name) ->
  70. syn_registry:unregister(Name).
  71. -spec unregister(Scope :: atom(), Name :: any()) -> ok | {error, Reason :: any()}.
  72. unregister(Scope, Name) ->
  73. syn_registry:unregister(Scope, Name).