  Copyright ( c ) 2012   ,   and   ,  
  See the file license.txt for copying permission .

%%%-------------------------------------------------------------------
  @author  
%%% @doc
  Dynamic supervisor for player
%%% @end
%%%-------------------------------------------------------------------
-module(player_sup).

-behaviour(supervisor).

%% API
-export([start_link/0, start_player/2, stop_player/1]).

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

-define(SERVER, ?MODULE).

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

%%--------------------------------------------------------------------
%% @doc
%% Starts the supervisor
%%
    ( ) - > { ok , Pid } | ignore | { error , Error }
%% @end
%%--------------------------------------------------------------------
start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).


%%--------------------------------------------------------------------
%% @doc
%% Starts the player with name Name 
%%
%% @end
%%--------------------------------------------------------------------
-spec start_player(string(), pid()) -> pid() | {error, term()}.
start_player(Name, Console) ->
    Child = {{player, Name}, {player, start_link,[Name, Console]}, permanent,
	     2000, worker, [player]},
    case supervisor:start_child(?SERVER, Child) of
	{ok, Pid} -> Pid;	  
	{ok, Pid, _} -> Pid;
	{error, Info} -> {error, Info}
    end.

%%--------------------------------------------------------------------
%% @doc
%% Stop the player with name Name
%%
%% @end
%%--------------------------------------------------------------------
-spec stop_player(string()) -> ok | {error, term()}.
stop_player(Name) ->
    supervisor:terminate_child(?SERVER, {player, Name}),
    supervisor:delete_child(?SERVER, {player, Name}).

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

%%--------------------------------------------------------------------
  @private
%% @doc
%% Whenever a supervisor is started using supervisor:start_link/[2,3],
%% this function is called by the new process to find out about
%% restart strategy, maximum restart frequency and child
%% specifications.
%%
    ) - > { ok , { SupFlags , [ ChildSpec ] } } |
%%                     ignore |
%%                     {error, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
    RestartStrategy = one_for_one,
    MaxRestarts = 1000,
    MaxSecondsBetweenRestarts = 3600,

    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},

    
    {ok, {SupFlags, []}}.

%%%===================================================================
  Internal functions
%%%===================================================================

-------------------------------------------------------------------
 @doc
 @end
-------------------------------------------------------------------
 API
 Supervisor callbacks
===================================================================
 API functions
===================================================================
--------------------------------------------------------------------
 @doc
 Starts the supervisor

 @end
--------------------------------------------------------------------
--------------------------------------------------------------------
 @doc
 Starts the player with name Name 

 @end
--------------------------------------------------------------------
--------------------------------------------------------------------
 @doc
 Stop the player with name Name

 @end
--------------------------------------------------------------------
===================================================================
 Supervisor callbacks
===================================================================
--------------------------------------------------------------------
 @doc
 Whenever a supervisor is started using supervisor:start_link/[2,3],
 this function is called by the new process to find out about
 restart strategy, maximum restart frequency and child
 specifications.

                     ignore |
                     {error, Reason}
 @end
--------------------------------------------------------------------
===================================================================
===================================================================
  Copyright ( c ) 2012   ,   and   ,  
  See the file license.txt for copying permission .

  @author  
  Dynamic supervisor for player
-module(player_sup).

-behaviour(supervisor).

-export([start_link/0, start_player/2, stop_player/1]).

-export([init/1]).

-define(SERVER, ?MODULE).


    ( ) - > { ok , Pid } | ignore | { error , Error }
start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).


-spec start_player(string(), pid()) -> pid() | {error, term()}.
start_player(Name, Console) ->
    Child = {{player, Name}, {player, start_link,[Name, Console]}, permanent,
	     2000, worker, [player]},
    case supervisor:start_child(?SERVER, Child) of
	{ok, Pid} -> Pid;	  
	{ok, Pid, _} -> Pid;
	{error, Info} -> {error, Info}
    end.

-spec stop_player(string()) -> ok | {error, term()}.
stop_player(Name) ->
    supervisor:terminate_child(?SERVER, {player, Name}),
    supervisor:delete_child(?SERVER, {player, Name}).


  @private
    ) - > { ok , { SupFlags , [ ChildSpec ] } } |
init([]) ->
    RestartStrategy = one_for_one,
    MaxRestarts = 1000,
    MaxSecondsBetweenRestarts = 3600,

    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},

    
    {ok, {SupFlags, []}}.

  Internal functions
