2012-07-13 23:32:13 +08:00
|
|
|
%% The contents of this file are subject to the Mozilla Public License
|
|
|
|
%% Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
%% compliance with the License. You may obtain a copy of the License
|
|
|
|
%% at http://www.mozilla.org/MPL/
|
|
|
|
%%
|
|
|
|
%% Software distributed under the License is distributed on an "AS IS"
|
|
|
|
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
%% the License for the specific language governing rights and
|
|
|
|
%% limitations under the License.
|
|
|
|
%%
|
|
|
|
%% The Original Code is RabbitMQ.
|
|
|
|
%%
|
|
|
|
%% The Initial Developer of the Original Code is VMware, Inc.
|
|
|
|
%% Copyright (c) 2007-2012 VMware, Inc. All rights reserved.
|
|
|
|
%%
|
|
|
|
|
|
|
|
-module(rabbit_mqtt_util).
|
|
|
|
|
|
|
|
-include("include/rabbit_mqtt.hrl").
|
|
|
|
|
|
|
|
-compile(export_all).
|
|
|
|
|
|
|
|
subcription_queue_name(ClientId) ->
|
2012-08-06 06:52:54 +08:00
|
|
|
Base = "mqtt-subscription-" ++ ClientId ++ "qos",
|
|
|
|
{list_to_binary(Base ++ "0"), list_to_binary(Base ++ "1")}.
|
2012-07-13 23:32:13 +08:00
|
|
|
|
|
|
|
%% amqp mqtt descr
|
|
|
|
%% * + match one topic level
|
|
|
|
%% # # match multiple topic levels
|
|
|
|
%% . / topic level separator
|
2012-08-17 01:01:45 +08:00
|
|
|
mqtt2amqp(Topic) ->
|
2012-07-13 23:32:13 +08:00
|
|
|
erlang:iolist_to_binary(
|
|
|
|
re:replace(re:replace(Topic, "/", ".", [global]),
|
|
|
|
"[\+]", "*", [global])).
|
|
|
|
|
2012-08-17 01:01:45 +08:00
|
|
|
amqp2mqtt(Topic) ->
|
2012-07-13 23:32:13 +08:00
|
|
|
erlang:iolist_to_binary(
|
|
|
|
re:replace(re:replace(Topic, "[\*]", "+", [global]),
|
|
|
|
"[\.]", "/", [global])).
|
|
|
|
|
|
|
|
valid_client_id(ClientId) ->
|
|
|
|
ClientIdLen = length(ClientId),
|
|
|
|
1 =< ClientIdLen andalso ClientIdLen =< ?CLIENT_ID_MAXLEN.
|
|
|
|
|
2012-07-16 21:57:31 +08:00
|
|
|
env(Key) ->
|
|
|
|
case application:get_env(rabbitmq_mqtt, Key) of
|
|
|
|
{ok, Val} -> Val;
|
|
|
|
undefined -> undefined
|
|
|
|
end.
|