From 61f1fc5ae633b9e5509e741e90d3aece37bc8f87 Mon Sep 17 00:00:00 2001 From: David Wragg Date: Fri, 14 Jan 2011 01:32:12 +0000 Subject: Extract common code from generated API functions --- librabbitmq/amqp.h | 38 ++++++++++++++++++++++---------------- librabbitmq/amqp_api.c | 5 ----- librabbitmq/amqp_socket.c | 26 ++++++++++++++++++++++++++ librabbitmq/codegen.py | 16 ++++------------ 4 files changed, 52 insertions(+), 33 deletions(-) diff --git a/librabbitmq/amqp.h b/librabbitmq/amqp.h index 49eaf3f..996372a 100644 --- a/librabbitmq/amqp.h +++ b/librabbitmq/amqp.h @@ -319,6 +319,28 @@ RABBITMQ_EXPORT amqp_rpc_reply_t amqp_simple_rpc(amqp_connection_state_t state, amqp_method_number_t *expected_reply_ids, void *decoded_request_method); +RABBITMQ_EXPORT void *amqp_simple_rpc_decoded(amqp_connection_state_t state, + amqp_channel_t channel, + amqp_method_number_t request_id, + amqp_method_number_t reply_id, + void *decoded_request_method); + +/* + * The API methods corresponding to most synchronous AMQP methods + * return a pointer to the decoded method result. Upon error, they + * return NULL, and we need some way of discovering what, if anything, + * went wrong. amqp_get_rpc_reply() returns the most recent + * amqp_rpc_reply_t instance corresponding to such an API operation + * for the given connection. + * + * Only use it for operations that do not themselves return + * amqp_rpc_reply_t; operations that do return amqp_rpc_reply_t + * generally do NOT update this per-connection-global amqp_rpc_reply_t + * instance. + */ +RABBITMQ_EXPORT amqp_rpc_reply_t amqp_get_rpc_reply( + amqp_connection_state_t state); + RABBITMQ_EXPORT amqp_rpc_reply_t amqp_login(amqp_connection_state_t state, char const *vhost, int channel_max, @@ -369,22 +391,6 @@ RABBITMQ_EXPORT int amqp_basic_reject(amqp_connection_state_t state, RABBITMQ_EXPORT amqp_boolean_t amqp_data_in_buffer( amqp_connection_state_t state); -/* - * For those API operations (such as amqp_basic_ack, - * amqp_queue_declare, and so on) that do not themselves return - * amqp_rpc_reply_t instances, we need some way of discovering what, - * if anything, went wrong. amqp_get_rpc_reply() returns the most - * recent amqp_rpc_reply_t instance corresponding to such an API - * operation for the given connection. - * - * Only use it for operations that do not themselves return - * amqp_rpc_reply_t; operations that do return amqp_rpc_reply_t - * generally do NOT update this per-connection-global amqp_rpc_reply_t - * instance. - */ -RABBITMQ_EXPORT amqp_rpc_reply_t amqp_get_rpc_reply( - amqp_connection_state_t state); - /* * Get the error string for the given error code. * diff --git a/librabbitmq/amqp_api.c b/librabbitmq/amqp_api.c index cde5a93..37f6605 100644 --- a/librabbitmq/amqp_api.c +++ b/librabbitmq/amqp_api.c @@ -261,8 +261,3 @@ int amqp_basic_reject(amqp_connection_state_t state, req.requeue = requeue; return amqp_send_method(state, channel, AMQP_BASIC_REJECT_METHOD, &req); } - -amqp_rpc_reply_t amqp_get_rpc_reply(amqp_connection_state_t state) -{ - return state->most_recent_api_result; -} diff --git a/librabbitmq/amqp_socket.c b/librabbitmq/amqp_socket.c index f23b42b..f9666b4 100644 --- a/librabbitmq/amqp_socket.c +++ b/librabbitmq/amqp_socket.c @@ -351,6 +351,32 @@ amqp_rpc_reply_t amqp_simple_rpc(amqp_connection_state_t state, } } +void *amqp_simple_rpc_decoded(amqp_connection_state_t state, + amqp_channel_t channel, + amqp_method_number_t request_id, + amqp_method_number_t reply_id, + void *decoded_request_method) +{ + amqp_method_number_t replies[2]; + + replies[0] = reply_id; + replies[1] = 0; + + state->most_recent_api_result = amqp_simple_rpc(state, channel, + request_id, replies, + decoded_request_method); + if (state->most_recent_api_result.reply_type == AMQP_RESPONSE_NORMAL) + return state->most_recent_api_result.reply.decoded; + else + return NULL; +} + +amqp_rpc_reply_t amqp_get_rpc_reply(amqp_connection_state_t state) +{ + return state->most_recent_api_result; +} + + static int amqp_login_inner(amqp_connection_state_t state, int channel_max, int frame_max, diff --git a/librabbitmq/codegen.py b/librabbitmq/codegen.py index cded64a..b52cf3b 100644 --- a/librabbitmq/codegen.py +++ b/librabbitmq/codegen.py @@ -493,13 +493,9 @@ int amqp_encode_properties(uint16_t class_id, if info is False: continue - reply = cConstantName(c_ize(m.klass.name) + '_' + c_ize(m.name) - + "_ok_method") - print print m.apiPrototype() print "{" - print " amqp_method_number_t replies[2] = { %s, 0};" % (reply,) print " %s req;" % (m.structName(),) for f in m.arguments: @@ -517,16 +513,12 @@ int amqp_encode_properties(uint16_t class_id, print " req.%s = %s;" % (n, val) + reply = cConstantName(c_ize(m.klass.name) + '_' + c_ize(m.name) + + "_ok_method") print """ - state->most_recent_api_result = amqp_simple_rpc(state, channel, - %s, - replies, &req); - if (state->most_recent_api_result.reply_type == AMQP_RESPONSE_NORMAL) - return state->most_recent_api_result.reply.decoded; - else - return NULL; + return amqp_simple_rpc_decoded(state, channel, %s, %s, &req); } -""" % (m.defName(),) +""" % (m.defName(), reply) def genHrl(spec): def fieldDeclList(fields): -- cgit v1.2.1