Skip to content

Commit

Permalink
Merge branch 'WHISTLE-1676'
Browse files Browse the repository at this point in the history
Conflicts:
	whistle_apps/apps/crossbar/src/modules/cb_cdrs.erl
	whistle_apps/apps/crossbar/src/modules/cb_queues.erl
  • Loading branch information
k-anderson committed Nov 1, 2012
2 parents d8c1315 + f0b091c commit 60d60ca
Show file tree
Hide file tree
Showing 67 changed files with 3,045 additions and 4,107 deletions.
153 changes: 88 additions & 65 deletions ecallmgr/src/ecallmgr_config.erl
Expand Up @@ -11,6 +11,7 @@

-export([flush/0, flush/1]).
-export([get/1, get/2, get/3]).
-export([fetch/1, fetch/2, fetch/3]).
-export([set/2, set/3]).

-compile([{no_auto_import, [get/1]}]).
Expand All @@ -19,107 +20,129 @@

-spec flush/0 :: () -> 'ok'.
-spec flush/1 :: (wh_json:json_string()) -> 'ok' | {'error', _}.

flush() ->
wh_cache:flush_local(?ECALLMGR_UTIL_CACHE).

flush(Key) ->
flush(Key, node()).
flush(Key0, Node0) ->
Key = wh_util:to_binary(Key0),
Node = wh_util:to_binary(Node0),

flush(Key, Node) when not is_binary(Key) ->
flush(wh_util:to_binary(Key), Node);
flush(Key, Node) when not is_binary(Node) ->
flush(Key, wh_util:to_binary(Node));
flush(Key, Node) ->
CacheKey = cache_key(Key, Node),
wh_cache:erase_local(?ECALLMGR_UTIL_CACHE, CacheKey),

Req = [KV ||
{_, V} = KV <- [{<<"Category">>, <<"ecallmgr">>}
,{<<"Key">>, Key}
,{<<"Node">>, Node}
,{<<"Msg-ID">>, wh_util:rand_hex_binary(16)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
V =/= undefined],

Req = [{<<"Category">>, <<"ecallmgr">>}
,{<<"Key">>, Key}
,{<<"Node">>, Node}
,{<<"Msg-ID">>, wh_util:rand_hex_binary(16)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
lager:debug("flushing ~s from sysconf", [Key]),
wh_amqp_worker:cast(?ECALLMGR_AMQP_POOL
,Req
,props:filter_undefined(Req)
,fun wapi_sysconf:publish_flush_req/1
).


-spec get/1 :: (wh_json:json_string()) -> wh_json:json_term() | 'undefined'.
-spec get/2 :: (wh_json:json_string(), Default) -> wh_json:json_term() | Default.
-spec get/3 :: (wh_json:json_string(), Default, wh_json:json_string() | atom()) -> wh_json:json_term() | Default.

get(Key) ->
get(Key, undefined).

get(Key, Default) ->
get(Key, Default, wh_util:to_binary(node())).
get(Key0, Default, Node0) ->
Key = wh_util:to_binary(Key0),
Node = wh_util:to_binary(Node0),

get(Key, Default, Node) when not is_binary(Key) ->
get(wh_util:to_binary(Key), Default, Node);
get(Key, Default, Node) when not is_binary(Node) ->
get(Key, Default, wh_util:to_binary(Node));
get(Key, Default, Node) ->
case wh_cache:fetch_local(?ECALLMGR_UTIL_CACHE, cache_key(Key, Node)) of
{ok, V} -> V;
{error, E} when E =:= not_found orelse E =:= undefined ->
Req = [KV ||
{_, V} = KV <- [{<<"Category">>, <<"ecallmgr">>}
,{<<"Key">>, Key}
,{<<"Default">>, Default}
,{<<"Node">>, Node}
,{<<"Msg-ID">>, wh_util:rand_hex_binary(16)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
V =/= undefined],

lager:debug("looking up ~s from sysconf", [Key]),
ReqResp = wh_amqp_worker:call(?ECALLMGR_AMQP_POOL
,Req
,fun wapi_sysconf:publish_get_req/1
,fun wapi_sysconf:get_resp_v/1
),
case ReqResp of
{error, _R} ->
lager:debug("unable to get config for key '~s' failed: ~p", [Key0, _R]),
Default;
{ok, RespJObj} ->
V = case wh_json:get_value(<<"Value">>, RespJObj) of
undefined -> Default;
null -> Default;
<<"undefined">> -> Default;
<<"null">> -> Default;
Value ->
wh_cache:store_local(?ECALLMGR_UTIL_CACHE, cache_key(Key, Node), Value),
Value
end,
V
end
Value = fetch(Key, Default, Node),
wh_cache:store_local(?ECALLMGR_UTIL_CACHE, cache_key(Key, Node), Value),
Value
end.

-spec fetch/1 :: (wh_json:json_string()) -> wh_json:json_term() | 'undefined'.
-spec fetch/2 :: (wh_json:json_string(), Default) -> wh_json:json_term() | Default.
-spec fetch/3 :: (wh_json:json_string(), Default, wh_json:json_string() | atom()) -> wh_json:json_term() | Default.

fetch(Key) ->
fetch(Key, undefined).

fetch(Key, Default) ->
fetch(Key, Default, wh_util:to_binary(node())).

fetch(Key, Default, Node) when not is_binary(Key) ->
fetch(wh_util:to_binary(Key), Default, Node);
fetch(Key, Default, Node) when not is_binary(Node) ->
fetch(Key, Default, wh_util:to_binary(Node));
fetch(Key, Default, Node) ->
Req = [{<<"Category">>, <<"ecallmgr">>}
,{<<"Key">>, Key}
,{<<"Default">>, Default}
,{<<"Node">>, Node}
,{<<"Msg-ID">>, wh_util:rand_hex_binary(16)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
lager:debug("looking up ~s from sysconf", [Key]),
ReqResp = wh_amqp_worker:call(?ECALLMGR_AMQP_POOL
,props:filter_undefined(Req)
,fun wapi_sysconf:publish_get_req/1
,fun wapi_sysconf:get_resp_v/1
),
case ReqResp of
{error, _R} ->
lager:debug("unable to get config for key '~s' failed: ~p", [Key, _R]),
Default;
{ok, JObj} ->
get_response_value(JObj, Default)
end.

-spec set/2 :: (wh_json:json_string(), wh_json:json_term()) -> 'ok'.
-spec set/3 :: (wh_json:json_string(), wh_json:json_term(), wh_json:json_string() | atom()) -> 'ok'.

set(Key, Value) ->
set(Key, Value, wh_util:to_binary(node())).
set(Key0, Value, Node0) ->
Key = wh_util:to_binary(Key0),
Node = wh_util:to_binary(Node0),

set(Key, Value, Node) when not is_binary(Key) ->
set(wh_util:to_binary(Key), Value, Node);
set(Key, Value, Node) when not is_binary(Node) ->
set(Key, Value, wh_util:to_binary(Node));
set(Key, Value, Node) ->
wh_cache:store_local(?ECALLMGR_UTIL_CACHE, cache_key(Key, Node), Value),
Req = [KV ||
{_, V} = KV <- [{<<"Category">>, <<"ecallmgr">>}
,{<<"Key">>, Key}
,{<<"Value">>, Value}
,{<<"Node">>, Node}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
V =/= undefined],
Req = [{<<"Category">>, <<"ecallmgr">>}
,{<<"Key">>, Key}
,{<<"Value">>, Value}
,{<<"Node">>, Node}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
],
ReqResp = wh_amqp_worker:call(?ECALLMGR_AMQP_POOL
,Req
,props:filter_undefined(Req)
,fun wapi_sysconf:publish_set_req/1
,fun wh_amqp_worker:any_resp/1),
case ReqResp of
{error, _R} ->
lager:debug("set config for key '~s' failed: ~p", [Key0, _R]);
lager:debug("set config for key '~s' failed: ~p", [Key, _R]);
{ok, _} ->
lager:debug("set config for key '~s' to new value: ~p", [Key0, Value])
lager:debug("set config for key '~s' to new value: ~p", [Key, Value])
end.


-spec get_response_value/2 :: (wh_json:json_object(), term()) -> term().
get_response_value(JObj, Default) ->
case wh_json:get_value(<<"Value">>, JObj) of
undefined -> Default;
null -> Default;
<<"undefined">> -> Default;
<<"null">> -> Default;
Value -> Value
end.

cache_key(K, Node) ->
{?MODULE, K, Node}.
4 changes: 2 additions & 2 deletions ecallmgr/src/ecallmgr_fs_config.erl
Expand Up @@ -66,7 +66,7 @@ init([Node, Options]) ->
put(callid, Node),
process_flag(trap_exit, true),
lager:debug("starting new fs config listener for ~s", [Node]),
case freeswitch:bind(Node, config) of
case freeswitch:bind(Node, configuration) of
ok ->
lager:debug("bound to config request on ~s", [Node]),
{ok, #state{node=Node, options=Options}};
Expand Down Expand Up @@ -165,7 +165,7 @@ handle_config_req(Node, ID, FsConf) ->
put(callid, ID),
try fsconf_key(FsConf) of
ConfKey ->
SysconfResp = ecallmgr_config:get(ConfKey, wh_json:new()),
SysconfResp = ecallmgr_config:fetch(ConfKey, wh_json:new()),
ConfigXml = generate_resp_xml(ConfKey, SysconfResp),

lager:debug("sending XML to ~s: ~s", [Node, ConfigXml]),
Expand Down
16 changes: 10 additions & 6 deletions ecallmgr/src/ecallmgr_fs_node.erl
Expand Up @@ -416,6 +416,7 @@ process_broadcast_event(<<"channel_update">>, Data) ->
-spec run_start_cmds/1 :: (atom()) -> pid().
run_start_cmds(Node) ->
spawn_link(fun() ->
timer:sleep(5000),
Cmds = ecallmgr_config:get(<<"fs_cmds">>, [], Node),
Res = process_cmds(Node, Cmds),
case lists:filter(fun was_not_successful_cmd/1, Res) of
Expand Down Expand Up @@ -451,13 +452,15 @@ process_cmd(Node, ApiCmd0, ApiArg, Acc) ->
process_cmd(Node, ApiCmd0, ApiArg, Acc, list).
process_cmd(Node, ApiCmd0, ApiArg, Acc, ArgFormat) ->
ApiCmd = wh_util:to_atom(wh_util:to_binary(ApiCmd0), ?FS_CMD_SAFELIST),
case freeswitch:api(Node, ApiCmd, format_args(ArgFormat, ApiArg)) of
{ok, FSResp} ->
{ok, BGApiID} = freeswitch:bgapi(Node, ApiCmd, format_args(ArgFormat, ApiArg)),
receive
{bgok, BGApiID, FSResp} ->
process_resp(ApiCmd, ApiArg, binary:split(FSResp, <<"\n">>, [global]), Acc);
{error, badarg} when ArgFormat =:= list ->
{bgerror, BGApiID, _} when ArgFormat =:= list ->
process_cmd(Node, ApiCmd0, ApiArg, Acc, binary);
{error, _}=E -> [E|Acc];
timeout -> [{timeout, {ApiCmd, ApiArg}} | Acc]
{bgerror, BGApiID, Error} -> [Error | Acc]
after 120000 ->
[{timeout, {ApiCmd, ApiArg}} | Acc]
end.

format_args(list, Args) -> wh_util:to_list(Args);
Expand Down Expand Up @@ -521,7 +524,8 @@ show_channels_as_json(Node) ->
,((Values = binary:split(Line, <<"|||">>, [global])) =/= [Line])
]
end;
{error, _} -> []
{error, _} -> [];
timeout -> []
end.

-spec maybe_start_event_listener/2 :: (atom(), ne_binary()) -> 'ok' | sup_startchild_ret().
Expand Down
1 change: 1 addition & 0 deletions lib/whistle-1.0.0/include/wh_databases.hrl
Expand Up @@ -9,6 +9,7 @@
-define(WH_PROVISIONER_DB, <<"global_provisioner">>).
-define(WH_FAXES, <<"faxes">>).
-define(WH_SERVICES_DB, <<"services">>).
-define(WH_OFFNET_DB, <<"offnet">>).

-define(WH_ACCOUNT_CONFIGS, <<"configs_">>).

Expand Down
22 changes: 2 additions & 20 deletions lib/whistle-1.0.0/src/wh_json_validator.erl
Expand Up @@ -71,28 +71,10 @@ is_valid(JObj, Schema) ->
lager:debug("json validated against ~s schema", [wh_json:get_value(<<"_id">>, Schema)]),
Ok;
{fail, Errors} ->
E = format_errors(Errors),
lager:debug("json failed validation against ~s schema: ~s", [wh_json:get_value(<<"_id">>, Schema), wh_json:encode(E)]),
{fail, E}
lager:debug("json failed validation against ~s schema", [wh_json:get_value(<<"_id">>, Schema)]),
{fail, Errors}
end.


-spec format_errors/1 :: (error_acc()) -> wh_json:json_object().
-spec format_errors/2 :: (error_acc(), wh_json:json_object()) -> wh_json:json_object().

format_errors(Errors) ->
format_errors(Errors, wh_json:new()).

format_errors([], JObj) ->
JObj;
format_errors([{K, V}|T], JObj) when is_list(K) ->
Property = wh_util:join_binary(K, <<".">>),
[Attr, Msg] = binary:split(V, <<":">>),
format_errors(T, wh_json:set_value([Property, Attr], Msg, JObj));
format_errors([{Property, V}|T], JObj) ->
[Attr, Msg] = binary:split(V, <<":">>),
format_errors(T, wh_json:set_value([Property, Attr], Msg, JObj)).

%% JObj = { ...,"name":"Mal Reynolds",...}
%%
%% Schema = {
Expand Down
56 changes: 56 additions & 0 deletions lib/whistle-1.0.0/src/wh_util.erl
Expand Up @@ -58,6 +58,10 @@

-export([whistle_version/0, write_pid/1]).
-export([is_ipv4/1, is_ipv6/1]).
-export([expand_cidr/1]).
-export([is_rfc1918_ip/1]).
-export([iptuple_to_binary/1]).
-export([verify_cidr/2]).
-export([get_hostname/0]).

-include_lib("kernel/include/inet.hrl").
Expand Down Expand Up @@ -353,6 +357,7 @@ join_binary([_|Bins], Sep, Acc) ->
%% @end
%%--------------------------------------------------------------------
-spec shuffle_list/1 :: (list()) -> list().
shuffle_list([]) -> [];
shuffle_list(List) when is_list(List) ->
Len = length(List),
randomize_list(round(math:log(Len) + 0.5), List).
Expand Down Expand Up @@ -594,10 +599,12 @@ is_empty([]) -> true;
is_empty("0") -> true;
is_empty("false") -> true;
is_empty("NULL") -> true;
is_empty("undefined") -> true;
is_empty(<<>>) -> true;
is_empty(<<"0">>) -> true;
is_empty(<<"false">>) -> true;
is_empty(<<"NULL">>) -> true;
is_empty(<<"undefined">>) -> true;
is_empty(null) -> true;
is_empty(false) -> true;
is_empty(undefined) -> true;
Expand Down Expand Up @@ -801,6 +808,55 @@ is_ipv6(Address) when is_list(Address) ->
{error, _} -> false
end.

-spec verify_cidr/2 :: (string() | ne_binary(), string() | ne_binary()) -> boolean().
verify_cidr(IP, CIDR) when is_binary(IP) ->
verify_cidr(to_list(IP), CIDR);
verify_cidr(IP, CIDR) when is_binary(CIDR) ->
verify_cidr(IP, to_list(CIDR));
verify_cidr(IP, CIDR) ->
%% As per the docs... "This operation should only be used for test purposes"
%% so, ummm ya, but probably cheaper then my expand bellow followed by a list
%% test. Just be aware this should only be used where performance is not
%% critical
case orber_acl:verify(IP, CIDR, inet) of
true -> true;
{false, _, _} -> false;
{error, _} -> false
end.

-spec expand_cidr/1 :: (string() | ne_binary()) -> [ne_binary(),...] | [].
expand_cidr(CIDR) when is_binary(CIDR) ->
expand_cidr(to_list(CIDR));
expand_cidr(CIDR) ->
%% EXTREMELY wastefull/naive approach, should never be used, but if you
%% must we keep it in a class C
case orber_acl:range(CIDR, inet) of
{error, _} -> [];
{ok, Start, End} ->
[A1, B1, C1, D1] = lists:map(fun wh_util:to_integer/1, string:tokens(Start, ".")),
[A2, B2, C2, D2] = lists:map(fun wh_util:to_integer/1, string:tokens(End, ".")),
true = ((A2 + B2 + C2 + D2) - (A1 + B1 + C1 + D1)) =< 510,
[iptuple_to_binary({A,B,C,D})
|| A <- lists:seq(A1, A2)
,B <- lists:seq(B1, B2)
,C <- lists:seq(C1, C2)
,D <- lists:seq(D1, D2)
]
end.

-spec is_rfc1918_ip/1 :: (string() | ne_binary()) -> boolean().
is_rfc1918_ip(IP) ->
verify_cidr(IP, "192.168.0.0/16")
orelse verify_cidr(IP, "10.0.0.0/8")
orelse verify_cidr(IP, "172.16.0.0/12").

-spec iptuple_to_binary/1 :: ({integer(), integer(), integer(), integer()}) -> ne_binary().
iptuple_to_binary({A,B,C,D}) ->
<<(to_binary(A))/binary, "."
,(to_binary(B))/binary, "."
,(to_binary(C))/binary, "."
,(to_binary(D))/binary>>.

-spec elapsed_s/1 :: (wh_now() | pos_integer()) -> pos_integer().
-spec elapsed_ms/1 :: (wh_now() | pos_integer()) -> pos_integer().
-spec elapsed_us/1 :: (wh_now() | pos_integer()) -> pos_integer().
Expand Down
2 changes: 1 addition & 1 deletion lib/whistle_couch-1.0.0/include/wh_couch.hrl
Expand Up @@ -43,7 +43,7 @@
-define(MIN_DISK_SIZE, 131072).
-define(DEFAULT_PORT, 5984).
-define(DEFAULT_ADMIN_PORT, 5986).
-define(IBROWSE_OPTS, [{max_sessions, 1024}, {max_pipeline_size, 10}]).
-define(IBROWSE_OPTS, [{max_sessions, 512}, {max_pipeline_size, 10}, {connect_timeout, 100}]).

-define(CONFIG_FILE_PATH, [code:priv_dir(whistle_couch), "/startup.config"]).

Expand Down

0 comments on commit 60d60ca

Please sign in to comment.