--- parser3/src/classes/amqp.C 2025/11/07 01:26:13 1.2 +++ parser3/src/classes/amqp.C 2025/11/07 23:00:11 1.4 @@ -21,7 +21,7 @@ #include #endif -volatile const char * IDENT_AMQP_C="$Id: amqp.C,v 1.2 2025/11/07 01:26:13 moko Exp $" IDENT_PA_VAMQP_H; +volatile const char * IDENT_AMQP_C="$Id: amqp.C,v 1.4 2025/11/07 23:00:11 moko Exp $" IDENT_PA_VAMQP_H; class MAmqp: public Methoded { public: // VStateless_class @@ -101,9 +101,36 @@ VAmqp& self=GET_SELF(r, VAmqp); #ifdef WITH_AMQP -static void check(const char* action, amqp_rpc_reply_t rr){ - if(rr.reply_type != AMQP_RESPONSE_NORMAL) - throw Exception("amqp", 0, "%s failed", action); +static void check(amqp_rpc_reply_t rr){ + if(rr.reply_type == AMQP_RESPONSE_NORMAL) + return; + + // Extract error message from reply + const char* error_msg = 0; + size_t error_len = 0; + if(rr.reply_type == AMQP_RESPONSE_SERVER_EXCEPTION) { + if(rr.reply.id == AMQP_CHANNEL_CLOSE_METHOD) { + amqp_channel_close_t *m = (amqp_channel_close_t *)rr.reply.decoded; + if(m->reply_text.len > 0 && m->reply_text.bytes) { + error_msg = (const char*)m->reply_text.bytes; + error_len = m->reply_text.len; + } + } else if(rr.reply.id == AMQP_CONNECTION_CLOSE_METHOD) { + amqp_connection_close_t *m = (amqp_connection_close_t *)rr.reply.decoded; + if(m->reply_text.len > 0 && m->reply_text.bytes) { + error_msg = (const char*)m->reply_text.bytes; + error_len = m->reply_text.len; + } + } + } + + if(error_msg) { + throw Exception("amqp", 0, "failed: %.*s", (int)error_len, error_msg); + } else if(rr.reply_type == AMQP_RESPONSE_LIBRARY_EXCEPTION) { + throw Exception("amqp", 0, "failed: library error %d", rr.library_error); + } else { + throw Exception("amqp", 0, "failed"); + } } #define AMQP_STRING(s,l) new String(String::C(pa_strdup((const char*)(s), (l)), (l))) @@ -114,7 +141,6 @@ static void _publish(Request& r, MethodP const String &msg=params.as_string(0, "msg must be string"); const char* exchange_c = ""; // default exchange const char* routing_key_c = 0; - bool have_queue_sugar=false; bool mandatory=false; amqp_basic_properties_t props; @@ -130,85 +156,75 @@ static void _publish(Request& r, MethodP } else if(key=="routing_key"){ routing_key_c=value->as_string().cstr(); } else if(key=="queue"){ - routing_key_c=value->as_string().cstr(); have_queue_sugar=true; + routing_key_c=value->as_string().cstr(); } else if(key=="mandatory"){ mandatory=r.process(*value).as_bool(); - } else if(key=="properties"){ - // parse message properties - if(HashStringValue* ph=value->get_hash()){ - for(HashStringValue::Iterator p(*ph); p; p.next()){ - String::Body pkey=p.key(); - Value* pval=p.value(); - if(pkey=="content_type"){ - const char* v=pval->as_string().cstr(); - props.content_type=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_CONTENT_TYPE_FLAG; - } else if(pkey=="content_encoding"){ - const char* v=pval->as_string().cstr(); - props.content_encoding=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_CONTENT_ENCODING_FLAG; - } else if(pkey=="delivery_mode"){ - uint8_t dm=(uint8_t)pval->as_int(); - props.delivery_mode=dm; - props._flags|=AMQP_BASIC_DELIVERY_MODE_FLAG; - } else if(pkey=="priority"){ - uint8_t pr=(uint8_t)pval->as_int(); - props.priority=pr; - props._flags|=AMQP_BASIC_PRIORITY_FLAG; - } else if(pkey=="correlation_id"){ - const char* v=pval->as_string().cstr(); - props.correlation_id=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_CORRELATION_ID_FLAG; - } else if(pkey=="reply_to"){ - const char* v=pval->as_string().cstr(); - props.reply_to=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_REPLY_TO_FLAG; - } else if(pkey=="expiration"){ - const char* v=pval->as_string().cstr(); - props.expiration=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_EXPIRATION_FLAG; - } else if(pkey=="message_id"){ - const char* v=pval->as_string().cstr(); - props.message_id=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_MESSAGE_ID_FLAG; - } else if(pkey=="timestamp"){ - uint64_t ts=(uint64_t)pval->as_double(); - props.timestamp=ts; - props._flags|=AMQP_BASIC_TIMESTAMP_FLAG; - } else if(pkey=="type"){ - const char* v=pval->as_string().cstr(); - props.type=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_TYPE_FLAG; - } else if(pkey=="user_id"){ - const char* v=pval->as_string().cstr(); - props.user_id=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_USER_ID_FLAG; - } else if(pkey=="app_id"){ - const char* v=pval->as_string().cstr(); - props.app_id=amqp_cstring_bytes(v); - props._flags|=AMQP_BASIC_APP_ID_FLAG; - } else if(pkey=="headers"){ -/* if(HashStringValue* hh=pval->get_hash()){ - size_t count=hh->count(); - amqp_table_entry_t* entries=count ? new amqp_table_entry_t[count] : 0; - size_t idx=0; - for(HashStringValue::Iterator hi(*hh); hi; hi.next()){ - String::Body hkey=hi.key(); - const char* hv=hi.value()->as_string().cstr(); - entries[idx].key=amqp_cstring_bytes(hkey.cstr()); - entries[idx].value.kind=AMQP_FIELD_KIND_UTF8; - entries[idx].value.value.bytes=amqp_cstring_bytes(hv); - idx++; - } - props.headers.num_entries=(int)count; - props.headers.entries=entries; - props._flags|=AMQP_BASIC_HEADERS_FLAG; - } -*/ } else - throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + } else if(key=="content_type"){ + const char* v=value->as_string().cstr(); + props.content_type=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_CONTENT_TYPE_FLAG; + } else if(key=="content_encoding"){ + const char* v=value->as_string().cstr(); + props.content_encoding=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_CONTENT_ENCODING_FLAG; + } else if(key=="delivery_mode"){ + uint8_t dm=(uint8_t)value->as_int(); + props.delivery_mode=dm; + props._flags|=AMQP_BASIC_DELIVERY_MODE_FLAG; + } else if(key=="priority"){ + uint8_t pr=(uint8_t)value->as_int(); + props.priority=pr; + props._flags|=AMQP_BASIC_PRIORITY_FLAG; + } else if(key=="correlation_id"){ + const char* v=value->as_string().cstr(); + props.correlation_id=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_CORRELATION_ID_FLAG; + } else if(key=="reply_to"){ + const char* v=value->as_string().cstr(); + props.reply_to=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_REPLY_TO_FLAG; + } else if(key=="expiration"){ + const char* v=value->as_string().cstr(); + props.expiration=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_EXPIRATION_FLAG; + } else if(key=="message_id"){ + const char* v=value->as_string().cstr(); + props.message_id=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_MESSAGE_ID_FLAG; + } else if(key=="timestamp"){ + uint64_t ts=(uint64_t)value->as_double(); + props.timestamp=ts; + props._flags|=AMQP_BASIC_TIMESTAMP_FLAG; + } else if(key=="type"){ + const char* v=value->as_string().cstr(); + props.type=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_TYPE_FLAG; + } else if(key=="user_id"){ + const char* v=value->as_string().cstr(); + props.user_id=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_USER_ID_FLAG; + } else if(key=="app_id"){ + const char* v=value->as_string().cstr(); + props.app_id=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_APP_ID_FLAG; + } else if(key=="headers"){ +/* if(HashStringValue* hh=pval->get_hash()){ + size_t count=hh->count(); + amqp_table_entry_t* entries=count ? new amqp_table_entry_t[count] : 0; + size_t idx=0; + for(HashStringValue::Iterator hi(*hh); hi; hi.next()){ + String::Body hkey=hi.key(); + const char* hv=hi.value()->as_string().cstr(); + entries[idx].key=amqp_cstring_bytes(hkey.cstr()); + entries[idx].value.kind=AMQP_FIELD_KIND_UTF8; + entries[idx].value.value.bytes=amqp_cstring_bytes(hv); + idx++; } + props.headers.num_entries=(int)count; + props.headers.entries=entries; + props._flags|=AMQP_BASIC_HEADERS_FLAG; } - } else +*/ } else throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } } @@ -221,7 +237,7 @@ static void _publish(Request& r, MethodP body.len = msg.length(); body.bytes=(void*)msg.cstr(); - int ret = amqp_basic_publish(self.connection(), self.channel(), amqp_cstring_bytes(exchange_c), amqp_cstring_bytes(routing_key_c), mandatory , 0, &props, body); + int ret = amqp_basic_publish(self.connection(), self.channel(), amqp_cstring_bytes(exchange_c), amqp_cstring_bytes(routing_key_c), mandatory, 0, &props, body); if(ret!=AMQP_STATUS_OK) throw Exception("amqp", 0, "publish failed"); @@ -230,7 +246,6 @@ static void _publish(Request& r, MethodP if(props._flags & AMQP_BASIC_HEADERS_FLAG){ // delete [] props.headers.entries; } - (void)have_queue_sugar; } static void _release(Request& r, MethodParams&) { @@ -340,7 +355,7 @@ static void _declare_exchange(Request& r if(!name_c) throw Exception("amqp", 0, "name is required"); amqp_exchange_declare(self.connection(), self.channel(), amqp_cstring_bytes(name_c), amqp_cstring_bytes(type_c), passive, durable, auto_delete, nowait, amqp_empty_table); - check("declare exchange", amqp_get_rpc_reply(self.connection())); + check(amqp_get_rpc_reply(self.connection())); } static void _delete_exchange(Request& r, MethodParams& params) { @@ -365,7 +380,7 @@ static void _delete_exchange(Request& r, } if(!name_c) throw Exception("amqp", 0, "exchange is required"); amqp_exchange_delete(self.connection(), self.channel(), amqp_cstring_bytes(name_c), if_unused); - check("delete exchange", amqp_get_rpc_reply(self.connection())); + check(amqp_get_rpc_reply(self.connection())); } static void _declare_queue(Request& r, MethodParams& params) { @@ -392,7 +407,7 @@ static void _declare_queue(Request& r, M } } amqp_queue_declare_ok_t *ok = amqp_queue_declare(self.connection(), self.channel(), queue_c ? amqp_cstring_bytes(queue_c) : amqp_empty_bytes, passive, durable, auto_delete, nowait, amqp_empty_table); - check("declare queue", amqp_get_rpc_reply(self.connection())); + check(amqp_get_rpc_reply(self.connection())); if(!queue_c && ok){ r.write(*AMQP_STRING(ok->queue.bytes, ok->queue.len)); } @@ -421,7 +436,7 @@ static void _delete_queue(Request& r, Me } if(!queue_c) throw Exception("amqp", 0, "queue is required"); amqp_queue_delete(self.connection(), self.channel(), amqp_cstring_bytes(queue_c), if_unused, if_empty); - check("delete queue", amqp_get_rpc_reply(self.connection())); + check(amqp_get_rpc_reply(self.connection())); } static void _bind_queue(Request& r, MethodParams& params) { @@ -447,7 +462,7 @@ static void _bind_queue(Request& r, Meth } if(!exchange_c || !queue_c) throw Exception("amqp", 0, "exchange and queue are required"); amqp_queue_bind(self.connection(), self.channel(), amqp_cstring_bytes(queue_c), amqp_cstring_bytes(exchange_c), amqp_cstring_bytes(routing_key_c), amqp_empty_table); - check("bind queue", amqp_get_rpc_reply(self.connection())); + check(amqp_get_rpc_reply(self.connection())); } static void _unbind_queue(Request& r, MethodParams& params) { @@ -473,7 +488,7 @@ static void _unbind_queue(Request& r, Me } if(!exchange_c || !queue_c) throw Exception("amqp", 0, "exchange and queue are required"); amqp_queue_unbind(self.connection(), self.channel(), amqp_cstring_bytes(queue_c), amqp_cstring_bytes(exchange_c), amqp_cstring_bytes(routing_key_c), amqp_empty_table); - check("unbind queue", amqp_get_rpc_reply(self.connection())); + check(amqp_get_rpc_reply(self.connection())); } static void _consume(Request& r, MethodParams& params) { @@ -487,7 +502,9 @@ static void _consume(Request& r, MethodP for(HashStringValue::Iterator i(*options); i; i.next()){ String::Body key=i.key(); Value* value=i.value(); - if(key=="queue"){ + if(key=="callback"){ + callback=value->get_junction(); + } else if(key=="queue"){ queue_c=value->as_string().cstr(); } else if(key=="consumer_tag"){ consumer_tag_c=value->as_string().cstr(); @@ -495,8 +512,6 @@ static void _consume(Request& r, MethodP no_ack=r.process(*value).as_bool(); } else if(key=="nowait"){ nowait=r.process(*value).as_bool(); - } else if(key=="callback"){ - callback=value->get_junction(); } else throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } @@ -508,9 +523,7 @@ static void _consume(Request& r, MethodP amqp_basic_consume(self.connection(), self.channel(), amqp_cstring_bytes(queue_c), consumer_tag_c ? amqp_cstring_bytes(consumer_tag_c) : amqp_empty_bytes, 0 /*no_local*/, no_ack, nowait, amqp_empty_table); - amqp_rpc_reply_t rr = amqp_get_rpc_reply(self.connection()); - if(rr.reply_type != AMQP_RESPONSE_NORMAL) - throw Exception("amqp", 0, "consume failed"); + check(amqp_get_rpc_reply(self.connection())); self.fstop=false; while(!self.fstop){ @@ -567,5 +580,3 @@ MAmqp::MAmqp(): Methoded("amqp") { add_native_method("stop_consume", Method::CT_DYNAMIC, _stop_consume, 0, 0); #endif } - -