Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d4eafd6dd4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 225 lines (204 sloc) 9.591 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
%%%-------------------------------------------------------------------
%%% File : medici_controller.erl
%%% Author : Jim McCoy <>
%%% Description :
%%%
%%% Created : 5 May 2009 by Jim McCoy <>
%%%-------------------------------------------------------------------
-module(medici_controller).

-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

-include("medici.hrl").

-record(state, {clients=[], auto_sync=nil, auto_tune=nil, auto_copy=nil}).

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
    {ok, MediciOpts} = application:get_env(options),
    MyName = proplists:get_value(controller_name, MediciOpts, ?CONTROLLER_NAME),
    gen_server:start_link({local, MyName}, ?MODULE, MediciOpts, []).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init(_ClientProps) ->
    timer:start(),
    {ok, #state{}}.

%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(_Request, _From, State) when length(State#state.clients) =:= 0 ->
    ?DEBUG_LOG("Request received by controller but no clients available~n", []),
    {reply, {error, no_connection_to_server}, State};
handle_call({task, TaskType, TaskParams}, _From, State) ->
    NewState = task(State, TaskType, TaskParams),
    {reply, ok, NewState};
handle_call({CallFunc}, From, State) ->
    %% Yes, a single atom in a tuple is "bad form", but makes the pattern matching work...
    dispatch_request({From, CallFunc}, State);
handle_call({CallFunc, Arg1}, From, State) ->
    dispatch_request({From, CallFunc, Arg1}, State);
handle_call({CallFunc, Arg1, Arg2}, From, State) ->
    dispatch_request({From, CallFunc, Arg1, Arg2}, State);
handle_call({CallFunc, Arg1, Arg2, Arg3}, From, State) ->
    dispatch_request({From, CallFunc, Arg1, Arg2, Arg3}, State);
handle_call({CallFunc, Arg1, Arg2, Arg3, Arg4}, From, State) ->
    dispatch_request({From, CallFunc, Arg1, Arg2, Arg3, Arg4}, State);
handle_call(_Request, _From, State) ->
    ?DEBUG_LOG("Unknown request received by medici controller: ~p", [_Request]),
    {reply, {error, invalid_request}, State}.

%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast({putnr, Key, Value}, State) ->
    dispatch_request({putnr, Key, Value}, State),
    {noreply, State};
handle_cast(_Msg, State) ->
    ?DEBUG_LOG("Unknown cast received by medici controller: ~p", [_Msg]),
    {noreply, State}.

%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info({client_start, Pid}, State) ->
    {noreply, State#state{clients=[Pid | State#state.clients]}};
handle_info({client_end, Pid}, State) ->
    {noreply, State#state{clients=lists:delete(Pid, State#state.clients)}};
%% The retries should either do a hard-kill of the clients it is eliminating
%% from the list, but
handle_info({retry, Pid, _OldReply, OldRequest}, State) when length(State#state.clients) > 1 ->
    dispatch_request(OldRequest, State#state{clients=lists:delete(Pid, State#state.clients)});
handle_info({retry, Pid, OldReply, OldRequest}, State) ->
    gen_server:reply(element(1, OldRequest), OldReply),
    {noreply, State#state{clients=lists:delete(Pid, State#state.clients)}};
handle_info(_Msg, State) ->
    ?DEBUG_LOG("Unknown info message received by medici controller: ~p", [_Msg]),
    {noreply, State}.


%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
    ok.

%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
dispatch_request(Request, State) when length(State#state.clients) =:= 1 ->
    gen_server:cast(hd(State#state.clients), Request),
    {noreply, State};
dispatch_request(Request, State) ->
    [TgtClient | OtherClients] = State#state.clients,
    gen_server:cast(TgtClient, Request),
    {noreply, State#state{clients=OtherClients++[TgtClient]}}.

task(State, sync, false) when State#state.auto_sync =:= nil->
    State;
task(State, sync, false) ->
    {TRef, _} = State#state.auto_sync,
    {ok, cancel} = timer:cancel(TRef),
    State#state{auto_sync=nil};
task(State, sync, true) when State#state.auto_sync =/= nil ->
    State;
task(State, sync, true) ->
    {OldTRef, OldPeriod} = State#state.auto_sync,
    case OldPeriod =/= ?DEFAULT_SYNC_PERIOD of
true ->
{ok, cancel} = timer:cancel(OldTRef),
TRef = timer:send_interval(?DEFAULT_SYNC_PERIOD, {sync});
_ ->
TRef = OldTRef
    end,
    State#state{auto_sync={TRef, ?DEFAULT_SYNC_PERIOD}};
task(State, sync, Period) when is_integer(Period), Period > 0 ->
    case State#state.auto_sync of
nil ->
TRef = timer:send_interval(Period * 1000, {sync}),
State#state{auto_sync={TRef, Period * 1000}};
{OldTRef, _} ->
{ok, cancel} = timer:cancel(OldTRef),
TRef = timer:send_interval(Period * 1000, {sync}),
State#state{auto_sync={TRef, Period * 1000}}
    end;
task(State, sync, Period) when is_integer(Period) ->
    task(State, sync, false);

task(State, tune, false) when State#state.auto_tune =:= nil->
    State;
task(State, tune, false) ->
    {TRef, _} = State#state.auto_tune,
    {ok, cancel} = timer:cancel(TRef),
    State#state{auto_tune=nil};
task(State, tune, true) when State#state.auto_tune =/= nil ->
    State;
task(State, tune, true) ->
    {OldTRef, OldPeriod} = State#state.auto_tune,
    case OldPeriod =/= ?DEFAULT_TUNE_PERIOD of
true ->
{ok, cancel} = timer:cancel(OldTRef),
TRef = timer:send_interval(?DEFAULT_TUNE_PERIOD, {tune});
_ ->
TRef = OldTRef
    end,
    State#state{auto_tune={TRef, ?DEFAULT_SYNC_PERIOD}};
task(State, tune, Period) when is_integer(Period), Period > 0 ->
    case State#state.auto_tune of
nil ->
TRef = timer:send_interval(Period * 1000, {tune}),
State#state{auto_tune={TRef, Period * 1000}};
{OldTRef, _} ->
{ok, cancel} = timer:cancel(OldTRef),
TRef = timer:send_interval(Period * 1000, {tune}),
State#state{auto_tune={TRef, Period * 1000}}
    end;
task(State, tune, Period) when is_integer(Period) ->
    task(State, tune, false);

task(State, copy, false) when State#state.auto_copy =:= nil->
    State;
task(State, copy, false) ->
    {TRef, _} = State#state.auto_copy,
    {ok, cancel} = timer:cancel(TRef),
    State#state{auto_copy=nil};
task(State, copy, {Target, Period}) when is_integer(Period), Period > 60 ->
    case State#state.auto_copy of
nil ->
TRef = timer:send_interval(Period * 1000, {copy, Target}),
State#state{auto_tune={TRef, {Target, Period * 1000}}};
{OldTRef, _} ->
{ok, cancel} = timer:cancel(OldTRef),
TRef = timer:send_interval(Period * 1000, {copy, Target}),
State#state{auto_tune={TRef, {Target, Period * 1000}}}
    end;
task(State, tune, Period) when is_integer(Period) ->
    task(State, tune, false).


Something went wrong with that request. Please try again.