syn.erl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015 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([init/0]).
  30. %% registry
  31. -export([register/2, register/3]).
  32. -export([unregister/1]).
  33. -export([find_by_key/1, find_by_key/2]).
  34. -export([find_by_pid/1, find_by_pid/2]).
  35. -export([registry_count/0, registry_count/1]).
  36. %% groups
  37. -export([join/2]).
  38. -export([leave/2]).
  39. -export([member/2]).
  40. -export([get_members/1]).
  41. -export([publish/2]).
  42. -export([multi_call/2, multi_call/3]).
  43. -export([multi_call_reply/2]).
  44. %% ===================================================================
  45. %% API
  46. %% ===================================================================
  47. -spec start() -> ok.
  48. start() ->
  49. ok = start_application(mnesia),
  50. ok = start_application(syn),
  51. ok.
  52. -spec stop() -> ok.
  53. stop() ->
  54. ok = application:stop(syn).
  55. -spec init() -> ok.
  56. init() ->
  57. ok = syn_backbone:initdb().
  58. -spec register(Key :: any(), Pid :: pid()) -> ok | {error, taken | pid_already_registered}.
  59. register(Key, Pid) ->
  60. syn_registry:register(Key, Pid).
  61. -spec register(Key :: any(), Pid :: pid(), Meta :: any()) -> ok | {error, taken | pid_already_registered}.
  62. register(Key, Pid, Meta) ->
  63. syn_registry:register(Key, Pid, Meta).
  64. -spec unregister(Key :: any()) -> ok | {error, undefined}.
  65. unregister(Key) ->
  66. syn_registry:unregister(Key).
  67. -spec find_by_key(Key :: any()) -> pid() | undefined.
  68. find_by_key(Key) ->
  69. syn_registry:find_by_key(Key).
  70. -spec find_by_key(Key :: any(), with_meta) -> {pid(), Meta :: any()} | undefined.
  71. find_by_key(Key, with_meta) ->
  72. syn_registry:find_by_key(Key, with_meta).
  73. -spec find_by_pid(Pid :: pid()) -> Key :: any() | undefined.
  74. find_by_pid(Pid) ->
  75. syn_registry:find_by_pid(Pid).
  76. -spec find_by_pid(Pid :: pid(), with_meta) -> {Key :: any(), Meta :: any()} | undefined.
  77. find_by_pid(Pid, with_meta) ->
  78. syn_registry:find_by_pid(Pid, with_meta).
  79. -spec registry_count() -> non_neg_integer().
  80. registry_count() ->
  81. syn_registry:count().
  82. -spec registry_count(Node :: atom()) -> non_neg_integer().
  83. registry_count(Node) ->
  84. syn_registry:count(Node).
  85. -spec join(Name :: any(), Pid :: pid()) -> ok | {error, pid_already_in_group}.
  86. join(Name, Pid) ->
  87. syn_groups:join(Name, Pid).
  88. -spec leave(Name :: any(), Pid :: pid()) -> ok | {error, undefined | pid_not_in_group}.
  89. leave(Name, Pid) ->
  90. syn_groups:leave(Name, Pid).
  91. -spec member(Pid :: pid(), Name :: any()) -> boolean().
  92. member(Pid, Name) ->
  93. syn_groups:member(Pid, Name).
  94. -spec get_members(Name :: any()) -> [pid()].
  95. get_members(Name) ->
  96. syn_groups:get_members(Name).
  97. -spec publish(Name :: any(), Message :: any()) -> {ok, RecipientCount :: non_neg_integer()}.
  98. publish(Name, Message) ->
  99. syn_groups:publish(Name, Message).
  100. -spec multi_call(Name :: any(), Message :: any()) ->
  101. {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  102. multi_call(Name, Message) ->
  103. syn_groups:multi_call(Name, Message).
  104. -spec multi_call(Name :: any(), Message :: any(), Timeout :: non_neg_integer()) ->
  105. {[{pid(), Reply :: any()}], [BadPid :: pid()]}.
  106. multi_call(Name, Message, Timeout) ->
  107. syn_groups:multi_call(Name, Message, Timeout).
  108. -spec multi_call_reply(CallerPid :: pid(), Reply :: any()) -> {syn_multi_call_reply, pid(), Reply :: any()}.
  109. multi_call_reply(CallerPid, Reply) ->
  110. syn_groups:multi_call_reply(CallerPid, Reply).
  111. %% ===================================================================
  112. %% Internal
  113. %% ===================================================================
  114. -spec start_application(atom()) -> ok | {error, any()}.
  115. start_application(Application) ->
  116. case application:start(Application) of
  117. ok -> ok;
  118. {error, {already_started, Application}} -> ok;
  119. Error -> Error
  120. end.