From 51e220983f4360bab24b10ab303538b0733eefe0 Mon Sep 17 00:00:00 2001 From: Tim Watson Date: Mon, 10 Oct 2011 22:32:50 +0100 Subject: [PATCH] Add support for appending additional configuration at runtime --- src/ibrowse.erl | 65 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/src/ibrowse.erl b/src/ibrowse.erl index 5f26d45..2ab3d3a 100644 --- a/src/ibrowse.erl +++ b/src/ibrowse.erl @@ -70,6 +70,7 @@ -export([ rescan_config/0, rescan_config/1, + add_config/1, get_config_value/1, get_config_value/2, spawn_worker_process/1, @@ -664,9 +665,15 @@ rescan_config() -> %% Clear current configuration for ibrowse and load from the specified %% file. Current configuration is cleared only if the specified %% file is readable using file:consult/1 +rescan_config([{_,_}|_]=Terms) -> + gen_server:call(?MODULE, {rescan_config_terms, Terms}); rescan_config(File) when is_list(File) -> gen_server:call(?MODULE, {rescan_config, File}). +%% @doc Add additional configuration elements at runtime. +add_config([{_,_}|_]=Terms) -> + gen_server:call(?MODULE, {add_config_terms, Terms}). + %%==================================================================== %% Server functions %%==================================================================== @@ -702,32 +709,38 @@ import_config() -> import_config(Filename) -> case file:consult(Filename) of {ok, Terms} -> - ets:delete_all_objects(ibrowse_conf), - Fun = fun({dest, Host, Port, MaxSess, MaxPipe, Options}) - when is_list(Host), is_integer(Port), - is_integer(MaxSess), MaxSess > 0, - is_integer(MaxPipe), MaxPipe > 0, is_list(Options) -> - I = [{{max_sessions, Host, Port}, MaxSess}, - {{max_pipeline_size, Host, Port}, MaxPipe}, - {{options, Host, Port}, Options}], - lists:foreach( - fun({X, Y}) -> - ets:insert(ibrowse_conf, - #ibrowse_conf{key = X, - value = Y}) - end, I); - ({K, V}) -> - ets:insert(ibrowse_conf, - #ibrowse_conf{key = K, - value = V}); - (X) -> - io:format("Skipping unrecognised term: ~p~n", [X]) - end, - lists:foreach(Fun, Terms); + apply_config(Terms); _Err -> ok end. +apply_config(Terms) -> + ets:delete_all_objects(ibrowse_conf), + insert_config(Terms). + +insert_config(Terms) -> + Fun = fun({dest, Host, Port, MaxSess, MaxPipe, Options}) + when is_list(Host), is_integer(Port), + is_integer(MaxSess), MaxSess > 0, + is_integer(MaxPipe), MaxPipe > 0, is_list(Options) -> + I = [{{max_sessions, Host, Port}, MaxSess}, + {{max_pipeline_size, Host, Port}, MaxPipe}, + {{options, Host, Port}, Options}], + lists:foreach( + fun({X, Y}) -> + ets:insert(ibrowse_conf, + #ibrowse_conf{key = X, + value = Y}) + end, I); + ({K, V}) -> + ets:insert(ibrowse_conf, + #ibrowse_conf{key = K, + value = V}); + (X) -> + io:format("Skipping unrecognised term: ~p~n", [X]) + end, + lists:foreach(Fun, Terms). + %% @doc Internal export get_config_value(Key) -> [#ibrowse_conf{value = V}] = ets:lookup(ibrowse_conf, Key), @@ -778,6 +791,14 @@ handle_call({rescan_config, File}, _From, State) -> Ret = (catch import_config(File)), {reply, Ret, State}; +handle_call({rescan_config_terms, Terms}, _From, State) -> + Ret = (catch apply_config(Terms)), + {reply, Ret, State}; + +handle_call({add_config_terms, Terms}, _From, State) -> + Ret = (catch insert_config(Terms)), + {reply, Ret, State}; + handle_call(Request, _From, State) -> Reply = {unknown_request, Request}, {reply, Reply, State}.