%%%-------------------------------------------------------------------------
%%% File     : balance.erl
%%% Purpose  : TCP load balancing application.
  Author    :    , email : lhs = slf , rhs = caspiannetworks.com
  Copyright : ( c ) 2003 Caspian Networks , Inc.
%%%-------------------------------------------------------------------------

-module(balance).

-compile([verbose, report_errors, report_warnings, trace, debug_info]).

-behaviour(application).

-include("balance.hrl").

%% application callbacks
-export([start/0, start/2, stop/1]).

%%%
  As far as I know , the Erlang application & release management parts
  of the OTP ( Open Telecom Platform ) do n't allow you to run mulitple
%%% instances of the same application.  E.g. Running this application
  several times to provide HTTP , SMTP , and NNTP proxy services .
%%%
%%% It's possible to do, but you need to build some of your own
%%% application management infrastructure ... which is beyond the scope
%%% of this small hack.  Therefore, do not be surprised if you see 
  assumptions that there 's only one balance app running at a time .
%%%
%%% See , in particular
  the " Design Principles " document .
%%%

%%%----------------------------------------------------------------------
%%% Callback functions from application
%%%----------------------------------------------------------------------

%%----------------------------------------------------------------------
%% Func: start/2
  Returns : { ok , Pid }         |
           { ok , Pid , State } |
%%          {error, Reason}   
%%----------------------------------------------------------------------
start() ->
    %% io:format("XXX ~w:start/0\n", [?MODULE]),
    start(xxxwhocares, []).
start(Type, []) ->
    %% io:format("XXX ~w:start/2 []\n", [?MODULE]),
    start(xxxwhocares, [local_tcp_port(), be_conn_timeout(),
			be_inactivity_timeout()]);
start(Type, StartArgs) ->
  io : format("XXX ~w : start/2 Type = ~w ,  = ~w\n " , [ ? MODULE , Type ,  ] ) ,
    %%
    %% Application environment values: fetch via application:get_all_env et al.
    %% Sources:
     1 . ' env ' portion of ethbridge.app file
     2 . from a .config file via " -config filename " on command line
     3 . from " -ethbridge keyX valX keyY valY ... "
    %%
    bal_sup:start_link(StartArgs).    

%%----------------------------------------------------------------------
%% Func: stop/1
%% Returns: any 
%%----------------------------------------------------------------------
stop(State) ->
    ok.

%%%----------------------------------------------------------------------
  Internal functions
%%%----------------------------------------------------------------------

local_tcp_port() ->
    get_app_env(?BALANCER_APP, local_tcp_port, 3632).

be_conn_timeout() ->
    get_app_env(?BALANCER_APP, be_conn_timeout, 5*1000).

be_inactivity_timeout() ->
    get_app_env(?BALANCER_APP, be_inactivity_timeout, 180*1000).

get_app_env(App, Key, Default) ->
    case application:get_env(App, Key) of
	{ok, Val} -> Val;
	_         -> Default
    end.

-------------------------------------------------------------------------
 File     : balance.erl
 Purpose  : TCP load balancing application.
-------------------------------------------------------------------------
 application callbacks

 instances of the same application.  E.g. Running this application

 It's possible to do, but you need to build some of your own
 application management infrastructure ... which is beyond the scope
 of this small hack.  Therefore, do not be surprised if you see 

 See , in particular

----------------------------------------------------------------------
 Callback functions from application
----------------------------------------------------------------------
----------------------------------------------------------------------
 Func: start/2
          {error, Reason}   
----------------------------------------------------------------------
 io:format("XXX ~w:start/0\n", [?MODULE]),
 io:format("XXX ~w:start/2 []\n", [?MODULE]),

 Application environment values: fetch via application:get_all_env et al.
 Sources:

----------------------------------------------------------------------
 Func: stop/1
 Returns: any 
----------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------------------------
  Author    :    , email : lhs = slf , rhs = caspiannetworks.com
  Copyright : ( c ) 2003 Caspian Networks , Inc.

-module(balance).

-compile([verbose, report_errors, report_warnings, trace, debug_info]).

-behaviour(application).

-include("balance.hrl").

-export([start/0, start/2, stop/1]).

  As far as I know , the Erlang application & release management parts
  of the OTP ( Open Telecom Platform ) do n't allow you to run mulitple
  several times to provide HTTP , SMTP , and NNTP proxy services .
  assumptions that there 's only one balance app running at a time .
  the " Design Principles " document .


  Returns : { ok , Pid }         |
           { ok , Pid , State } |
start() ->
    start(xxxwhocares, []).
start(Type, []) ->
    start(xxxwhocares, [local_tcp_port(), be_conn_timeout(),
			be_inactivity_timeout()]);
start(Type, StartArgs) ->
  io : format("XXX ~w : start/2 Type = ~w ,  = ~w\n " , [ ? MODULE , Type ,  ] ) ,
     1 . ' env ' portion of ethbridge.app file
     2 . from a .config file via " -config filename " on command line
     3 . from " -ethbridge keyX valX keyY valY ... "
    bal_sup:start_link(StartArgs).    

stop(State) ->
    ok.

  Internal functions

local_tcp_port() ->
    get_app_env(?BALANCER_APP, local_tcp_port, 3632).

be_conn_timeout() ->
    get_app_env(?BALANCER_APP, be_conn_timeout, 5*1000).

be_inactivity_timeout() ->
    get_app_env(?BALANCER_APP, be_inactivity_timeout, 180*1000).

get_app_env(App, Key, Default) ->
    case application:get_env(App, Key) of
	{ok, Val} -> Val;
	_         -> Default
    end.
