Skip to content

Commit

Permalink
Update cpg. Add the CloudI Service API function service_subscriptions…
Browse files Browse the repository at this point in the history
…/2. #60 #57
  • Loading branch information
okeuday committed Nov 8, 2013
1 parent 1f5e1ec commit cbf46da
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
2013-11-08 Michael Truog <mjtruog [at] gmail (dot) com>

* Make sure the cloudi module has all the types
* Update cpg
* Add the CloudI Service API function service_subscriptions/2

2013-11-07 Michael Truog <mjtruog [at] gmail (dot) com>

Expand Down
16 changes: 16 additions & 0 deletions src/lib/cloudi_core/src/cloudi_configurator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
configure/0,
acl_add/2,
acl_remove/2,
service_subscriptions/2,
services_add/2,
services_remove/2,
services_restart/2,
Expand Down Expand Up @@ -107,6 +108,11 @@ acl_remove(L, Timeout) ->
{acl_remove, L,
timeout_decr(Timeout)}, Timeout)).

service_subscriptions(ServiceId, Timeout) ->
?CATCH_EXIT(gen_server:call(?MODULE,
{service_subscriptions, ServiceId,
timeout_decr(Timeout)}, Timeout)).

services_add(L, Timeout) ->
?CATCH_EXIT(gen_server:call(?MODULE,
{services_add, L,
Expand Down Expand Up @@ -231,6 +237,16 @@ handle_call({acl_remove, L, _}, _,
{reply, Error, State}
end;

handle_call({service_subscriptions, ServiceId, Timeout}, _, State) ->
case cloudi_services_monitor:pids(ServiceId, Timeout) of
{ok, PidList} ->
L = [sets:from_list(cloudi_x_cpg:which_groups(Pid, Timeout))
|| Pid <- PidList],
{reply, {ok, lists:sort(sets:to_list(sets:union(L)))}, State};
{error, _} = Error ->
{reply, Error, State}
end;

handle_call({services_add, L, Timeout}, _,
#state{configuration = Config} = State) ->
case cloudi_configuration:services_add(L, Config, Timeout) of
Expand Down
22 changes: 21 additions & 1 deletion src/lib/cloudi_core/src/cloudi_service_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
%% external interface
-export([acl_add/2,
acl_remove/2,
service_subscriptions/2,
services_add/2,
services_remove/2,
services_restart/2,
Expand Down Expand Up @@ -227,6 +228,25 @@ acl_remove([_ | _] = L, Timeout)
(Timeout =:= infinity)) ->
cloudi_configurator:acl_remove(L, Timeout).

%%-------------------------------------------------------------------------
%% @doc
%% ===Get a list of all service subscriptions.===
%% When a subscription on the same service name pattern occurred
%% multiple times, only a single entry is returned within the list.
%% @end
%%-------------------------------------------------------------------------

-spec service_subscriptions(ServiceId :: service_id(),
Timeout :: timeout_milliseconds() | infinity) ->
{ok, list(cloudi_service:service_name_pattern())} |
{error, any()}.

service_subscriptions(ServiceId, Timeout)
when is_binary(ServiceId), byte_size(ServiceId) == 16,
((is_integer(Timeout) andalso Timeout > ?TIMEOUT_DELTA) orelse
(Timeout =:= infinity)) ->
cloudi_configurator:service_subscriptions(ServiceId, Timeout).

%%-------------------------------------------------------------------------
%% @doc
%% ===Add service instances.===
Expand Down Expand Up @@ -297,7 +317,7 @@ services_restart([_ | _] = L, Timeout)
%% @end
%%-------------------------------------------------------------------------

-spec services_search(ServiceName :: string(),
-spec services_search(ServiceName :: cloudi:service_name(),
Timeout :: timeout_milliseconds() | infinity) ->
{ok, list({service_id(), #internal{}} |
{service_id(), #external{}})} |
Expand Down
20 changes: 18 additions & 2 deletions src/lib/cloudi_core/src/cloudi_services_monitor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
%%%
%%% @author Michael Truog <mjtruog [at] gmail (dot) com>
%%% @copyright 2011-2013 Michael Truog
%%% @version 1.2.5 {@date} {@time}
%%% @version 1.3.1 {@date} {@time}
%%%------------------------------------------------------------------------

-module(cloudi_services_monitor).
Expand All @@ -59,7 +59,8 @@
monitor/7,
shutdown/2,
restart/2,
search/2]).
search/2,
pids/2]).

%% gen_server callbacks
-export([init/1,
Expand Down Expand Up @@ -125,6 +126,12 @@ search([_ | _] = PidList, Timeout) ->
{search, PidList},
Timeout)).

pids(ServiceId, Timeout)
when is_binary(ServiceId), byte_size(ServiceId) == 16 ->
?CATCH_EXIT(gen_server:call(?MODULE,
{pids, ServiceId},
Timeout)).

%%%------------------------------------------------------------------------
%%% Callback functions from gen_server
%%%------------------------------------------------------------------------
Expand Down Expand Up @@ -211,6 +218,15 @@ handle_call({search, PidList}, _,
end, [], PidList),
{reply, {ok, lists:reverse(ServiceIdList)}, State};

handle_call({pids, ServiceId}, _,
#state{services = Services} = State) ->
case cloudi_x_key2value:find1(ServiceId, Services) of
{ok, {PidList, _}} ->
{reply, {ok, PidList}, State};
error ->
{reply, {error, not_found}, State}
end;

handle_call(Request, _, State) ->
?LOG_WARN("Unknown call \"~p\"", [Request]),
{stop, cloudi_string:format("Unknown call \"~p\"", [Request]),
Expand Down

0 comments on commit cbf46da

Please sign in to comment.