
%%
  Copyright ( C ) 2011   Patrick " p2k "  <  >
%%
  This file is part of ebitcoin .
%%
%% ebitcoin is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation , either version 3 of the License , or
%% (at your option) any later version.
%%
%% ebitcoin is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%% GNU General Public License for more details.
%%
  You should have received a copy of the GNU General Public License
  along with ebitcoin .   If not , see < / > .
%%

-module(ebitcoin_sup).

-behaviour(supervisor).

%% API
-export([
    start_link/1,
    running_clients/0,
    start_client/1,
    reload_client/1,
    stop_client/1,
    crash_store/2,
    crash_fetch/1,
    crash_transfer_ets/1
]).

%% Supervisor callbacks
-export([init/1]).

%% ===================================================================
%% API functions
%% ===================================================================

start_link(DBConfig) ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, [DBConfig]).

running_clients() ->
    lists:foldl(
        fun (Spec, ClientIdAcc) ->
            case Spec of
                {{client, ClientId}, _, _, _} -> [ClientId | ClientIdAcc];
                _ -> ClientIdAcc
            end
        end,
        [],
        supervisor:which_children(?MODULE)
    ).

start_client(ClientId) ->
    case supervisor:start_child(?MODULE, {{client, ClientId}, {ebitcoin_client, start_link, [ClientId]}, transient, 5000, worker, [ebitcoin_client]}) of
        {ok, _} -> ok;
        {ok, _, _} -> ok;
        Error -> Error
    end.

reload_client(Client) ->
    ebitcoin_client:reload_config(Client).

stop_client(ClientId) ->
    case supervisor:terminate_child(?MODULE, {client, ClientId}) of
        ok -> supervisor:delete_child(?MODULE, {client, ClientId});
        Error -> Error
    end.

crash_store(Key, Value) ->
    ebitcoin_crash_repo:store(ebitcoin_crash_repo, Key, Value).

crash_fetch(Key) ->
    ebitcoin_crash_repo:fetch(ebitcoin_crash_repo, Key).

crash_transfer_ets(Key) ->
    ebitcoin_crash_repo:transfer_ets(ebitcoin_crash_repo, Key).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([DBConfig]) ->
    {ok, { {one_for_one, 5, 10}, [
        {ebitcoin_crash_repo, {ebitcoin_crash_repo, start_link, [{local, ebitcoin_crash_repo}]}, permanent, 5000, worker, [ebitcoin_crash_repo]},
        {ebitcoin_db, {ebitcoin_db_sup, start_link, [DBConfig]}, permanent, 5000, supervisor, [ebitcoin_db_sup]}
    ]} }.




 ebitcoin is free software: you can redistribute it and/or modify
 (at your option) any later version.

 ebitcoin is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.


 API
 Supervisor callbacks
 ===================================================================
 API functions
 ===================================================================
 ===================================================================
 Supervisor callbacks
 ===================================================================

  Copyright ( C ) 2011   Patrick " p2k "  <  >
  This file is part of ebitcoin .
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation , either version 3 of the License , or
  You should have received a copy of the GNU General Public License
  along with ebitcoin .   If not , see < / > .

-module(ebitcoin_sup).

-behaviour(supervisor).

-export([
    start_link/1,
    running_clients/0,
    start_client/1,
    reload_client/1,
    stop_client/1,
    crash_store/2,
    crash_fetch/1,
    crash_transfer_ets/1
]).

-export([init/1]).


start_link(DBConfig) ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, [DBConfig]).

running_clients() ->
    lists:foldl(
        fun (Spec, ClientIdAcc) ->
            case Spec of
                {{client, ClientId}, _, _, _} -> [ClientId | ClientIdAcc];
                _ -> ClientIdAcc
            end
        end,
        [],
        supervisor:which_children(?MODULE)
    ).

start_client(ClientId) ->
    case supervisor:start_child(?MODULE, {{client, ClientId}, {ebitcoin_client, start_link, [ClientId]}, transient, 5000, worker, [ebitcoin_client]}) of
        {ok, _} -> ok;
        {ok, _, _} -> ok;
        Error -> Error
    end.

reload_client(Client) ->
    ebitcoin_client:reload_config(Client).

stop_client(ClientId) ->
    case supervisor:terminate_child(?MODULE, {client, ClientId}) of
        ok -> supervisor:delete_child(?MODULE, {client, ClientId});
        Error -> Error
    end.

crash_store(Key, Value) ->
    ebitcoin_crash_repo:store(ebitcoin_crash_repo, Key, Value).

crash_fetch(Key) ->
    ebitcoin_crash_repo:fetch(ebitcoin_crash_repo, Key).

crash_transfer_ets(Key) ->
    ebitcoin_crash_repo:transfer_ets(ebitcoin_crash_repo, Key).


init([DBConfig]) ->
    {ok, { {one_for_one, 5, 10}, [
        {ebitcoin_crash_repo, {ebitcoin_crash_repo, start_link, [{local, ebitcoin_crash_repo}]}, permanent, 5000, worker, [ebitcoin_crash_repo]},
        {ebitcoin_db, {ebitcoin_db_sup, start_link, [DBConfig]}, permanent, 5000, supervisor, [ebitcoin_db_sup]}
    ]} }.
