diff options
author | Tony Garnock-Jones <tonyg@lshift.net> | 2009-06-22 10:34:03 +0100 |
---|---|---|
committer | Tony Garnock-Jones <tonyg@lshift.net> | 2009-06-22 10:34:03 +0100 |
commit | d9a3dc8f43bc3b298e1a6e6fc4d2c7a84c81c8d6 (patch) | |
tree | 6d64c51456461229d99387497f4eb0258aa74b71 | |
parent | ccf76ca9df7a7030e663874697d146ad1419a9fa (diff) | |
download | rabbitmq-server-d9a3dc8f43bc3b298e1a6e6fc4d2c7a84c81c8d6.tar.gz |
Convenience rabbit_basic functions.
-rw-r--r-- | src/rabbit_basic.erl | 60 | ||||
-rw-r--r-- | src/rabbit_error_logger.erl | 10 |
2 files changed, 59 insertions, 11 deletions
diff --git a/src/rabbit_basic.erl b/src/rabbit_basic.erl index 761b3863..3f298b0c 100644 --- a/src/rabbit_basic.erl +++ b/src/rabbit_basic.erl @@ -33,17 +33,27 @@ -include("rabbit.hrl"). -include("rabbit_framing.hrl"). --export([publish/1, message/4, delivery/4]). +-export([publish/1, message/4, properties/1, delivery/4]). +-export([publish/4, publish/7]). %%---------------------------------------------------------------------------- -ifdef(use_specs). +-type(properties_input() :: (amqp_properties() | [{atom(), any()}])). +-type(publish_result() :: ({ok, routing_result(), [pid()]} | not_found())). + -spec(publish/1 :: (delivery()) -> - {ok, routing_result(), [pid()]} | not_found()). + publish_result()). -spec(delivery/4 :: (bool(), bool(), maybe(txn()), message()) -> delivery()). --spec(message/4 :: (exchange_name(), routing_key(), binary(), binary()) -> +-spec(message/4 :: (exchange_name(), routing_key(), properties_input(), binary()) -> message()). +-spec(properties/1 :: (properties_input()) -> amqp_properties()). +-spec(publish/4 :: (exchange_name(), routing_key(), properties_input(), binary()) -> + publish_result()). +-spec(publish/7 :: (exchange_name(), routing_key(), bool(), bool(), maybe(txn()), + properties_input(), binary()) -> + publish_result()). -endif. @@ -63,13 +73,53 @@ delivery(Mandatory, Immediate, Txn, Message) -> #delivery{mandatory = Mandatory, immediate = Immediate, txn = Txn, sender = self(), message = Message}. -message(ExchangeName, RoutingKeyBin, ContentTypeBin, BodyBin) -> +message(ExchangeName, RoutingKeyBin, RawProperties, BodyBin) -> + Properties = properties(RawProperties), {ClassId, _MethodId} = rabbit_framing:method_id('basic.publish'), Content = #content{class_id = ClassId, - properties = #'P_basic'{content_type = ContentTypeBin}, + properties = Properties, properties_bin = none, payload_fragments_rev = [BodyBin]}, #basic_message{exchange_name = ExchangeName, routing_key = RoutingKeyBin, content = Content, persistent_key = none}. + +properties(P = #'P_basic'{}) -> + P; +properties(P) when is_list(P) -> + %% Yes, this is O(length(P) * record_info(size, 'P_basic') / 2), + %% i.e. slow. Use the definition of 'P_basic' directly if + %% possible! + alist_to_properties(P). + +alist_to_properties([]) -> + #'P_basic'{}; +alist_to_properties([{Key, Value} | Rest]) -> + case indexof(record_info(fields, 'P_basic'), Key) of + 0 -> + throw({unknown_P_basic_property_name, Key}); + N -> + setelement(N, alist_to_properties(Rest), Value) + end. + +indexof(L, Element) -> + indexof(L, Element, 0). + +indexof([], _Element, _N) -> + 0; +indexof([Element | _Rest], Element, N) -> + N; +indexof([_ | Rest], Element, N) -> + indexof(Rest, Element, N + 1). + +%% Convenience function, for avoiding round-trips in calls across the +%% erlang distributed network. +publish(ExchangeName, RoutingKeyBin, Properties, BodyBin) -> + publish(ExchangeName, RoutingKeyBin, false, false, none, Properties, BodyBin). + +%% Convenience function, for avoiding round-trips in calls across the +%% erlang distributed network. +publish(ExchangeName, RoutingKeyBin, Mandatory, Immediate, Txn, Properties, BodyBin) -> + publish(delivery(Mandatory, Immediate, Txn, + message(ExchangeName, RoutingKeyBin, properties(Properties), BodyBin))). diff --git a/src/rabbit_error_logger.erl b/src/rabbit_error_logger.erl index 76016a8c..b28574b7 100644 --- a/src/rabbit_error_logger.erl +++ b/src/rabbit_error_logger.erl @@ -31,6 +31,7 @@ -module(rabbit_error_logger). -include("rabbit.hrl"). +-include("rabbit_framing.hrl"). -define(LOG_EXCH_NAME, <<"amq.rabbitmq.log">>). @@ -75,10 +76,7 @@ publish(_Other, _Format, _Data, _State) -> publish1(RoutingKey, Format, Data, LogExch) -> {ok, _RoutingRes, _DeliveredQPids} = - rabbit_basic:publish( - rabbit_basic:delivery( - false, false, none, - rabbit_basic:message( - LogExch, RoutingKey, <<"text/plain">>, - list_to_binary(io_lib:format(Format, Data))))), + rabbit_basic:publish(LogExch, RoutingKey, false, false, none, + #'P_basic'{content_type = <<"text/plain">>}, + list_to_binary(io_lib:format(Format, Data))), ok. |