105
107
-spec(with_user_and_vhost/3 :: (username(), vhost(), thunk(A)) -> A).
106
108
-spec(execute_mnesia_transaction/1 :: (thunk(A)) -> A).
107
109
-spec(ensure_ok/2 :: (ok_or_error(), atom()) -> 'ok').
108
-spec(localnode/1 :: (atom()) -> erlang_node()).
109
-spec(nodehost/1 :: (erlang_node()) -> string()).
110
-spec(makenode/1 :: ({string(), string()} | string()) -> erlang_node()).
111
-spec(nodeparts/1 :: (erlang_node() | string()) -> {string(), string()}).
110
112
-spec(cookie_hash/0 :: () -> string()).
111
113
-spec(tcp_name/3 :: (atom(), ip_address(), ip_port()) -> atom()).
112
114
-spec(intersperse/2 :: (A, [A]) -> [A]).
308
313
ensure_ok(ok, _) -> ok;
309
314
ensure_ok({error, Reason}, ErrorTag) -> throw({error, {ErrorTag, Reason}}).
312
list_to_atom(lists:append([atom_to_list(Name), "@", nodehost(node())])).
316
makenode({Prefix, Suffix}) ->
317
list_to_atom(lists:append([Prefix, "@", Suffix]));
319
makenode(nodeparts(NodeStr)).
315
%% This is horrible, but there doesn't seem to be a way to split a
316
%% nodename into its constituent parts.
317
tl(lists:dropwhile(fun (E) -> E =/= $@ end, atom_to_list(Node))).
321
nodeparts(Node) when is_atom(Node) ->
322
nodeparts(atom_to_list(Node));
323
nodeparts(NodeStr) ->
324
case lists:splitwith(fun (E) -> E =/= $@ end, NodeStr) of
325
{Prefix, []} -> {_, Suffix} = nodeparts(node()),
327
{Prefix, Suffix} -> {Prefix, tl(Suffix)}
320
ssl_base64:encode(erlang:md5(atom_to_list(erlang:get_cookie()))).
331
base64:encode_to_string(erlang:md5(atom_to_list(erlang:get_cookie()))).
322
333
tcp_name(Prefix, IPAddress, Port)
323
334
when is_atom(Prefix) andalso is_number(Port) ->
501
queue_fold(Fun, Init, Q) ->
504
{{value, V}, Q1} -> queue_fold(Fun, Fun(V, Init), Q1)
507
%% This provides a string representation of a pid that is the same
508
%% regardless of what node we are running on. The representation also
509
%% permits easy identification of the pid's node.
510
pid_to_string(Pid) when is_pid(Pid) ->
511
%% see http://erlang.org/doc/apps/erts/erl_ext_dist.html (8.10 and
513
<<131,103,100,NodeLen:16,NodeBin:NodeLen/binary,Id:32,Ser:32,_Cre:8>>
514
= term_to_binary(Pid),
515
Node = binary_to_term(<<131,100,NodeLen:16,NodeBin:NodeLen/binary>>),
516
lists:flatten(io_lib:format("<~w.~B.~B>", [Node, Id, Ser])).
519
string_to_pid(Str) ->
520
ErrorFun = fun () -> throw({error, {invalid_pid_syntax, Str}}) end,
521
%% TODO: simplify this code by using the 're' module, once we drop
525
%% The \ before the trailing $ is only there to keep emacs
526
%% font-lock from getting confused.
527
case regexp:first_match(Str, "^<.*\\.[0-9]+\\.[0-9]+>\$") of
530
Str1 = string:substr(Str, 2, string:len(Str) - 2),
531
%% 3) extract three constituent parts, taking care to
532
%% handle dots in the node part (hence the reverse and concat)
533
[SerStr, IdStr | Rest] = lists:reverse(string:tokens(Str1, ".")),
534
NodeStr = lists:concat(lists:reverse(Rest)),
535
%% 4) construct a triple term from the three parts
536
TripleStr = lists:flatten(io_lib:format("{~s,~s,~s}.",
537
[NodeStr, IdStr, SerStr])),
538
%% 5) parse the triple
539
Tokens = case erl_scan:string(TripleStr) of
540
{ok, Tokens1, _} -> Tokens1;
541
{error, _, _} -> ErrorFun()
543
Term = case erl_parse:parse_term(Tokens) of
544
{ok, Term1} -> Term1;
545
{error, _} -> ErrorFun()
549
{Node1, Id1, Ser1} when is_atom(Node1) andalso
550
is_integer(Id1) andalso
556
%% 6) turn the triple into a pid - see pid_to_string
557
<<131,NodeEnc/binary>> = term_to_binary(Node),
558
binary_to_term(<<131,103,NodeEnc/binary,Id:32,Ser:32,0:8>>);
562
%% invalid regexp - shouldn't happen
566
version_compare(A, B, lte) ->
567
case version_compare(A, B) of
572
version_compare(A, B, gte) ->
573
case version_compare(A, B) of
578
version_compare(A, B, Result) ->
579
Result =:= version_compare(A, B).
581
version_compare([], []) ->
583
version_compare([], _ ) ->
585
version_compare(_ , []) ->
587
version_compare(A, B) ->
588
{AStr, ATl} = lists:splitwith(fun (X) -> X =/= $. end, A),
589
{BStr, BTl} = lists:splitwith(fun (X) -> X =/= $. end, B),
590
ANum = list_to_integer(AStr),
591
BNum = list_to_integer(BStr),
592
if ANum =:= BNum -> ATl1 = lists:dropwhile(fun (X) -> X =:= $. end, ATl),
593
BTl1 = lists:dropwhile(fun (X) -> X =:= $. end, BTl),
594
version_compare(ATl1, BTl1);