summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Radestock <matthias@lshift.net>2010-02-04 22:17:36 +0000
committerMatthias Radestock <matthias@lshift.net>2010-02-04 22:17:36 +0000
commitacfc23a695b2adbdb3f5be205b3b64b250d6afb7 (patch)
treea508dc47efb07f4b115b602be921b04f638a6e8d
parent17d99556a4857032711b78499d553978a6440f0b (diff)
downloadrabbitmq-server-acfc23a695b2adbdb3f5be205b3b64b250d6afb7.tar.gz
refactor: move pid<->string conversion funs into misc
since they are generally useful
-rw-r--r--src/rabbit_control.erl58
-rw-r--r--src/rabbit_misc.erl62
2 files changed, 64 insertions, 56 deletions
diff --git a/src/rabbit_control.erl b/src/rabbit_control.erl
index 2c2c67a6..f7518399 100644
--- a/src/rabbit_control.erl
+++ b/src/rabbit_control.erl
@@ -309,7 +309,7 @@ action(list_connections, Node, Args, Inform) ->
action(close_connection, Node, [PidStr, Explanation], Inform) ->
Inform("Closing connection ~s", [PidStr]),
rpc_call(Node, rabbit_reader, shutdown,
- [string_to_pid(PidStr), Explanation]);
+ [rabbit_misc:string_to_pid(PidStr), Explanation]);
action(Command, Node, Args, Inform) ->
{VHost, RemainingArgs} = parse_vhost_flag(Args),
@@ -368,7 +368,7 @@ format_info_item(Key, Items) ->
is_tuple(Value) ->
inet_parse:ntoa(Value);
Value when is_pid(Value) ->
- pid_to_string(Value);
+ rabbit_misc:pid_to_string(Value);
Value when is_binary(Value) ->
escape(Value);
Value when is_atom(Value) ->
@@ -426,57 +426,3 @@ prettify_typed_amqp_value(Type, Value) ->
array -> [prettify_typed_amqp_value(T, V) || {T, V} <- Value];
_ -> Value
end.
-
-%% see http://erlang.org/doc/apps/erts/erl_ext_dist.html (8.10 and 8.7)
-pid_to_string(Pid) ->
- <<131,103,100,NodeLen:16,NodeBin:NodeLen/binary,Id:32,Ser:32,_Cre:8>>
- = term_to_binary(Pid),
- Node = binary_to_term(<<131,100,NodeLen:16,NodeBin:NodeLen/binary>>),
- lists:flatten(io_lib:format("<~w.~B.~B>", [Node, Id, Ser])).
-
-string_to_pid(Str) ->
- ErrorFun = fun () -> throw({error, {invalid_pid_syntax, Str}}) end,
- %% TODO: simplify this code by using the 're' module, once we drop
- %% support for R11
- %%
- %% 1) sanity check
- %% The \ before the trailing $ is only there to keep emacs
- %% font-lock from getting confused.
- case regexp:first_match(Str, "^<.*\.[0-9]+\.[0-9]+>\$") of
- {match, _, _} ->
- %% 2) strip <>
- Str1 = string:substr(Str, 2, string:len(Str) - 2),
- %% 3) extract three constituent parts, taking care to
- %% handle dots in the node part (hence the reverse and concat)
- [SerStr, IdStr | Rest] = lists:reverse(string:tokens(Str1, ".")),
- NodeStr = lists:concat(lists:reverse(Rest)),
- %% 4) construct a triple term from the three parts
- TripleStr = lists:flatten(io_lib:format("{~s,~s,~s}.",
- [NodeStr, IdStr, SerStr])),
- %% 5) parse the triple
- Tokens = case erl_scan:string(TripleStr) of
- {ok, Tokens1, _} -> Tokens1;
- {error, _, _} -> ErrorFun()
- end,
- Term = case erl_parse:parse_term(Tokens) of
- {ok, Term1} -> Term1;
- {error, _} -> ErrorFun()
- end,
- {Node, Id, Ser} =
- case Term of
- {Node1, Id1, Ser1} when is_atom(Node1) andalso
- is_integer(Id1) andalso
- is_integer(Ser1) ->
- Term;
- _ ->
- ErrorFun()
- end,
- %% 6) turn the triple into a pid - see pid_to_string
- <<131,NodeEnc/binary>> = term_to_binary(Node),
- binary_to_term(<<131,103,NodeEnc/binary,Id:32,Ser:32,0:8>>);
- nomatch ->
- ErrorFun();
- Error ->
- %% invalid regexp - shouldn't happen
- throw(Error)
- end.
diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl
index 172e27f4..55520510 100644
--- a/src/rabbit_misc.erl
+++ b/src/rabbit_misc.erl
@@ -56,6 +56,7 @@
-export([format_stderr/2]).
-export([start_applications/1, stop_applications/1]).
-export([unfold/2, ceil/1, queue_fold/3]).
+-export([pid_to_string/1, string_to_pid/1]).
-import(mnesia).
-import(lists).
@@ -127,6 +128,8 @@
-spec(unfold/2 :: (fun ((A) -> ({'true', B, A} | 'false')), A) -> {[B], A}).
-spec(ceil/1 :: (number()) -> number()).
-spec(queue_fold/3 :: (fun ((any(), B) -> B), B, queue()) -> B).
+-spec(pid_to_string/1 :: (pid()) -> string()).
+-spec(string_to_pid/1 :: (string()) -> pid()).
-endif.
@@ -499,3 +502,62 @@ queue_fold(Fun, Init, Q) ->
{empty, _Q} -> Init;
{{value, V}, Q1} -> queue_fold(Fun, Fun(V, Init), Q1)
end.
+
+%% This provides a string representation of a pid that is the same
+%% regardless of what node we are running on. The representation also
+%% permits easy identification of the pid's node.
+pid_to_string(Pid) when is_pid(Pid) ->
+ %% see http://erlang.org/doc/apps/erts/erl_ext_dist.html (8.10 and
+ %% 8.7)
+ <<131,103,100,NodeLen:16,NodeBin:NodeLen/binary,Id:32,Ser:32,_Cre:8>>
+ = term_to_binary(Pid),
+ Node = binary_to_term(<<131,100,NodeLen:16,NodeBin:NodeLen/binary>>),
+ lists:flatten(io_lib:format("<~w.~B.~B>", [Node, Id, Ser])).
+
+%% inverse of above
+string_to_pid(Str) ->
+ ErrorFun = fun () -> throw({error, {invalid_pid_syntax, Str}}) end,
+ %% TODO: simplify this code by using the 're' module, once we drop
+ %% support for R11
+ %%
+ %% 1) sanity check
+ %% The \ before the trailing $ is only there to keep emacs
+ %% font-lock from getting confused.
+ case regexp:first_match(Str, "^<.*\.[0-9]+\.[0-9]+>\$") of
+ {match, _, _} ->
+ %% 2) strip <>
+ Str1 = string:substr(Str, 2, string:len(Str) - 2),
+ %% 3) extract three constituent parts, taking care to
+ %% handle dots in the node part (hence the reverse and concat)
+ [SerStr, IdStr | Rest] = lists:reverse(string:tokens(Str1, ".")),
+ NodeStr = lists:concat(lists:reverse(Rest)),
+ %% 4) construct a triple term from the three parts
+ TripleStr = lists:flatten(io_lib:format("{~s,~s,~s}.",
+ [NodeStr, IdStr, SerStr])),
+ %% 5) parse the triple
+ Tokens = case erl_scan:string(TripleStr) of
+ {ok, Tokens1, _} -> Tokens1;
+ {error, _, _} -> ErrorFun()
+ end,
+ Term = case erl_parse:parse_term(Tokens) of
+ {ok, Term1} -> Term1;
+ {error, _} -> ErrorFun()
+ end,
+ {Node, Id, Ser} =
+ case Term of
+ {Node1, Id1, Ser1} when is_atom(Node1) andalso
+ is_integer(Id1) andalso
+ is_integer(Ser1) ->
+ Term;
+ _ ->
+ ErrorFun()
+ end,
+ %% 6) turn the triple into a pid - see pid_to_string
+ <<131,NodeEnc/binary>> = term_to_binary(Node),
+ binary_to_term(<<131,103,NodeEnc/binary,Id:32,Ser:32,0:8>>);
+ nomatch ->
+ ErrorFun();
+ Error ->
+ %% invalid regexp - shouldn't happen
+ throw(Error)
+ end.