Annotation of parser3/src/classes/amqp.C, revision 1.25
1.1 moko 1: /** @file
2: Parser: @b amqp parser class.
3:
1.13 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.1 moko 5: Authors: Konstantin Morshnev <moko@design.ru>
6: */
7:
1.25 ! moko 8:
! 9: #include "classes.h"
1.1 moko 10: #include "pa_vmethod_frame.h"
11:
1.25 ! moko 12: #include "pa_vamqp.h"
1.1 moko 13: #include "pa_request.h"
1.8 moko 14: #include "pa_varray.h"
1.1 moko 15: #include "pa_vbool.h"
1.16 moko 16: #include "pa_os.h"
1.1 moko 17:
18: #ifdef WITH_AMQP
1.22 moko 19:
1.1 moko 20: #include <amqp.h>
21: #include <amqp_tcp_socket.h>
1.22 moko 22: #include <amqp_framing.h>
23:
24: #ifndef WITHOUT_AMQP_SSL
1.5 moko 25: #include <amqp_ssl_socket.h>
1.22 moko 26: #endif
1.1 moko 27:
1.20 moko 28: #endif
29:
1.25 ! moko 30: volatile const char * IDENT_AMQP_C="$Id: amqp.C,v 1.24 2026/09/20 16:00:58 moko Exp $" IDENT_PA_VAMQP_H;
1.1 moko 31:
32: class MAmqp: public Methoded {
33: public: // VStateless_class
34: Value* create_new_value(Pool&) { return new VAmqp(); }
35: public:
36: MAmqp();
37: };
38:
39: DECLARE_CLASS_VAR(amqp, new MAmqp);
40:
1.6 moko 41: #ifdef WITH_AMQP
42:
1.16 moko 43: static void status_check(VAmqp& self, int ret, const char *detail=""){
1.6 moko 44: if(ret == AMQP_STATUS_OK)
45: return;
46:
1.16 moko 47: self.fstate = VAmqp::CONNECTION_DEAD;
48:
1.6 moko 49: const char* error_str = amqp_error_string2(ret);
50: if(error_str) {
51: throw Exception("amqp", 0, "%sfailed: %s", detail, error_str);
52: } else {
53: throw Exception("amqp", 0, "%sfailed: error %d", detail, ret);
54: }
55: }
56:
1.16 moko 57: static void check(VAmqp& self, amqp_rpc_reply_t rr, const char *detail=""){
1.6 moko 58: if(rr.reply_type == AMQP_RESPONSE_NORMAL)
59: return;
60:
1.16 moko 61: self.fstate = (rr.reply_type == AMQP_RESPONSE_SERVER_EXCEPTION && rr.reply.id == AMQP_CHANNEL_CLOSE_METHOD) ? VAmqp::CHANNEL_DEAD : VAmqp::CONNECTION_DEAD;
62:
1.6 moko 63: // Extract error message from reply
64: const char* error_msg = 0;
65: size_t error_len = 0;
66: if(rr.reply_type == AMQP_RESPONSE_SERVER_EXCEPTION) {
67: if(rr.reply.id == AMQP_CHANNEL_CLOSE_METHOD) {
68: amqp_channel_close_t *m = (amqp_channel_close_t *)rr.reply.decoded;
69: if(m->reply_text.len > 0 && m->reply_text.bytes) {
70: error_msg = (const char*)m->reply_text.bytes;
71: error_len = m->reply_text.len;
72: }
73: } else if(rr.reply.id == AMQP_CONNECTION_CLOSE_METHOD) {
74: amqp_connection_close_t *m = (amqp_connection_close_t *)rr.reply.decoded;
75: if(m->reply_text.len > 0 && m->reply_text.bytes) {
76: error_msg = (const char*)m->reply_text.bytes;
77: error_len = m->reply_text.len;
78: }
79: }
80: }
1.16 moko 81:
1.6 moko 82: if(error_msg) {
83: throw Exception("amqp", 0, "%sfailed: %.*s", detail, (int)error_len, error_msg);
84: } else if(rr.reply_type == AMQP_RESPONSE_LIBRARY_EXCEPTION) {
1.16 moko 85: status_check(self, rr.library_error, detail);
1.6 moko 86: }
87:
88: throw Exception("amqp", 0, "%sfailed", detail);
89: }
90:
1.16 moko 91: static void amqp_connect(VAmqp& self, Request& r, HashStringValue* options) {
1.1 moko 92: const char* host_c = "localhost";
93: int port = 5672;
94: const char* user_c = "guest";
95: const char* pass_c = "guest";
96: const char* vhost_c = "/";
97: const char* locale_c = "en_US";
1.22 moko 98: int heartbeat_sec = 30;
99: int reconnect_interval_sec = 0;
100: bool tls_specified = false;
101: #ifndef WITHOUT_AMQP_SSL
1.5 moko 102: const char* tls_ca = 0;
103: const char* tls_cert = 0;
104: const char* tls_key = 0;
105: bool tls_verify = true;
1.22 moko 106: #endif
1.1 moko 107:
1.16 moko 108: if(options){
109: for(HashStringValue::Iterator i(*options); i; i.next()){
110: String::Body key=i.key();
111: Value* value=i.value();
112: if(key=="host"){
113: host_c=value->as_string().cstr();
114: } else if(key=="port"){
115: port=r.process(*value).as_int();
116: } else if(key=="user"){
117: user_c=value->as_string().cstr();
118: } else if(key=="password"){
119: pass_c=value->as_string().cstr();
120: } else if(key=="vhost"){
121: vhost_c=value->as_string().cstr();
122: } else if(key=="locale"){
123: locale_c=value->as_string().cstr();
124: } else if(key=="heartbeat"){
1.22 moko 125: heartbeat_sec=r.process(*value).as_int();
1.16 moko 126: } else if(key=="auto_reconnect"){
127: reconnect_interval_sec=r.process(*value).as_int();
128: } else if(key=="tls"){
1.22 moko 129: #ifndef WITHOUT_AMQP_SSL
1.16 moko 130: tls_specified = true;
131: if(HashStringValue* tls_options=value->get_hash()){
132: for(HashStringValue::Iterator t(*tls_options); t; t.next()){
133: String::Body tkey=t.key();
134: Value* tval=t.value();
135: if(tkey=="ca"){
136: tls_ca=tval->as_string().cstr();
137: } else if(tkey=="cert"){
138: tls_cert=tval->as_string().cstr();
139: } else if(tkey=="key"){
140: tls_key=tval->as_string().cstr();
141: } else if(tkey=="verify"){
142: tls_verify=r.process(*tval).as_bool();
143: } else
144: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.5 moko 145: }
1.16 moko 146: }
1.22 moko 147: #else
148: throw Exception("amqp", 0, "AMQP SSL support is disabled");
149: #endif
1.16 moko 150: } else
151: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.1 moko 152: }
153: }
154:
155: amqp_connection_state_t conn = amqp_new_connection();
1.5 moko 156: amqp_socket_t* socket = 0;
1.16 moko 157:
1.22 moko 158: #ifndef WITHOUT_AMQP_SSL
1.5 moko 159: if(tls_specified) {
160: socket = amqp_ssl_socket_new(conn);
161: if(!socket)
162: throw Exception("amqp", 0, "failed to create SSL socket");
1.16 moko 163:
1.5 moko 164: // Set CA certificate if provided
165: if(tls_ca)
166: if(amqp_ssl_socket_set_cacert(socket, tls_ca))
167: throw Exception("amqp", 0, "failed to set CA certificate");
1.6 moko 168:
1.5 moko 169: // Set client certificate and key if provided
170: if(tls_cert && tls_key) {
171: if(amqp_ssl_socket_set_key(socket, tls_cert, tls_key))
172: throw Exception("amqp", 0, "failed to set client certificate/key");
173: } else if(tls_cert || tls_key) {
174: throw Exception("amqp", 0, "both cert and key must be specified for TLS");
175: }
1.6 moko 176:
1.5 moko 177: // If CA is provided, peer verification will use it
178: amqp_ssl_socket_set_verify_peer(socket, tls_verify && tls_ca);
179: // If verify=true, enable hostname verification
180: amqp_ssl_socket_set_verify_hostname(socket, tls_verify);
1.22 moko 181: } else
182: #endif
183: {
1.5 moko 184: socket = amqp_tcp_socket_new(conn);
185: if(!socket)
186: throw Exception("amqp", 0, "failed to create TCP socket");
187: }
1.16 moko 188:
189: status_check(self, amqp_socket_open(socket, host_c, port), tls_specified ? "open SSL socket " : "open TCP socket ");
1.1 moko 190:
1.22 moko 191: amqp_rpc_reply_t rlogin = amqp_login(conn, vhost_c, 0, 131072, heartbeat_sec, AMQP_SASL_METHOD_PLAIN, user_c, pass_c);
1.1 moko 192: if(rlogin.reply_type != AMQP_RESPONSE_NORMAL){
193: amqp_destroy_connection(conn);
1.16 moko 194: check(self, rlogin, "login ");
1.1 moko 195: }
196:
1.23 moko 197: amqp_channel_t channel = 1;
1.1 moko 198: amqp_channel_open(conn, channel);
199: amqp_rpc_reply_t ropen = amqp_get_rpc_reply(conn);
200: if(ropen.reply_type != AMQP_RESPONSE_NORMAL){
201: amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
202: amqp_destroy_connection(conn);
1.16 moko 203: check(self, ropen, "open channel ");
1.1 moko 204: }
205:
1.16 moko 206: if(self.fconnection)
207: amqp_destroy_connection(self.fconnection);
208:
1.1 moko 209: self.fconnection = conn;
210: self.fchannel = channel;
1.16 moko 211: self.fstate = VAmqp::ALIVE;
212: self.freconnect_interval = reconnect_interval_sec;
213: }
214:
1.19 moko 215: // repairs a channel-level error cheaply: reopen on the same connection. Only valid when the server has
216: // already sent (or is about to send) channel.close - we're completing that handshake with close-ok, not
217: // initiating our own.
1.16 moko 218: static void amqp_repair_channel(VAmqp& self) {
219: amqp_channel_close_ok_t close_ok;
220: amqp_send_method(self.fconnection, self.fchannel, AMQP_CHANNEL_CLOSE_OK_METHOD, &close_ok);
221: amqp_channel_open(self.fconnection, self.fchannel);
222: check(self, amqp_get_rpc_reply(self.fconnection), "channel repair ");
223: self.fstate = VAmqp::ALIVE;
224: }
225:
1.19 moko 226: // voluntarily closes and reopens an otherwise-healthy channel
227: static void amqp_reset_channel(VAmqp& self) {
228: amqp_channel_close(self.fconnection, self.fchannel, AMQP_REPLY_SUCCESS);
229: amqp_channel_open(self.fconnection, self.fchannel);
230: amqp_get_rpc_reply(self.fconnection);
231: }
232:
1.16 moko 233: void VAmqp::ensure_connected() {
234: if(fstate == ALIVE)
235: return;
236:
237: if(fstate == CHANNEL_DEAD){
238: amqp_repair_channel(*this);
239: return;
240: }
241:
242: // CONNECTION_DEAD
243: if(freconnect_interval <= 0)
244: throw Exception("amqp", 0, "connection is dead (auto_reconnect is not enabled)");
245:
246: pa_sleep(freconnect_interval, 0);
247:
248: amqp_connect(*this, pa_thread_request(), fcreate_options);
249: }
250:
251: #endif // WITH_AMQP
252:
253: static void _create(Request& r, MethodParams& params) {
254: VAmqp& self=GET_SELF(r, VAmqp);
255:
256: #ifdef WITH_AMQP
257: self.fcreate_options = params.count()>0 ? params.as_hash(0) : 0;
258: amqp_connect(self, r, self.fcreate_options);
1.1 moko 259: #else
260: (void)params; (void)self;
261: throw Exception("amqp", 0, "compiled without amqp support");
1.6 moko 262: #endif // WITH_AMQP
1.1 moko 263: }
264:
265: #ifdef WITH_AMQP
266:
1.2 moko 267: #define AMQP_STRING(s,l) new String(String::C(pa_strdup((const char*)(s), (l)), (l)))
268: #define AMQP_VSTRING(s,l) new VString(*AMQP_STRING(s,l))
269:
1.17 moko 270: // broker-defined arguments/headers (x-message-ttl, x-dead-letter-exchange, ...)
271: // Type is taken from how the value was assigned: [] -> string, () -> number, (true|false) -> bool
272: static amqp_table_t amqp_build_arguments_table(Request& r, HashStringValue* arguments) {
273: amqp_table_t table = amqp_empty_table;
274: size_t count = arguments ? arguments->count() : 0;
275: if(!count)
276: return table;
277:
278: amqp_table_entry_t* entries = (amqp_table_entry_t*)pa_malloc(count * sizeof(amqp_table_entry_t));
279: size_t i = 0;
280: for(HashStringValue::Iterator it(*arguments); it; it.next()){
281: entries[i].key = amqp_cstring_bytes(it.key().cstr());
282:
283: Value& value = r.process(*it.value());
284: if(value.is_string() || value.is_void()){
285: entries[i].value.kind = AMQP_FIELD_KIND_UTF8;
286: entries[i].value.value.bytes = amqp_cstring_bytes(value.as_string().cstr());
287: } else if(value.is_bool()){
288: entries[i].value.kind = AMQP_FIELD_KIND_BOOLEAN;
289: entries[i].value.value.boolean = value.as_bool();
290: } else {
1.18 moko 291: double d = value.as_double();
292: double rounded = round(d);
293: if(ulp_eq_double(d, rounded)){
294: entries[i].value.kind = AMQP_FIELD_KIND_I64;
295: entries[i].value.value.i64 = (int64_t)rounded;
296: } else {
297: entries[i].value.kind = AMQP_FIELD_KIND_F64;
298: entries[i].value.value.f64 = d;
299: }
1.17 moko 300: }
301: i++;
302: }
303:
304: table.num_entries = (int)count;
305: table.entries = entries;
306: return table;
307: }
308:
309: // reverse of amqp_build_arguments_table - decodes a broker-provided field table (e.g. message headers) back into a hash.
310: static VHash* amqp_table_to_hash(amqp_table_t& table);
311:
312: static Value* amqp_field_to_value(amqp_field_value_t& f) {
313: switch(f.kind){
314: case AMQP_FIELD_KIND_UTF8:
315: case AMQP_FIELD_KIND_BYTES:
316: return AMQP_VSTRING(f.value.bytes.bytes, f.value.bytes.len);
317: case AMQP_FIELD_KIND_BOOLEAN:
318: return &VBool::get(f.value.boolean);
319: case AMQP_FIELD_KIND_I8: return new VInt((pa_wint)f.value.i8);
320: case AMQP_FIELD_KIND_U8: return new VInt((pa_wint)f.value.u8);
321: case AMQP_FIELD_KIND_I16: return new VInt((pa_wint)f.value.i16);
322: case AMQP_FIELD_KIND_U16: return new VInt((pa_wint)f.value.u16);
323: case AMQP_FIELD_KIND_I32: return new VInt((pa_wint)f.value.i32);
324: case AMQP_FIELD_KIND_U32: return new VInt((pa_wint)f.value.u32);
325: case AMQP_FIELD_KIND_I64: return new VInt((pa_wint)f.value.i64);
326: case AMQP_FIELD_KIND_U64: return new VInt((pa_wint)f.value.u64);
327: case AMQP_FIELD_KIND_TIMESTAMP: return new VInt((pa_wint)f.value.u64);
328: case AMQP_FIELD_KIND_F32: return new VDouble((double)f.value.f32);
329: case AMQP_FIELD_KIND_F64: return new VDouble(f.value.f64);
330: case AMQP_FIELD_KIND_DECIMAL: {
331: double divisor=1;
332: for(uint8_t i=0; i<f.value.decimal.decimals; i++) divisor*=10;
333: return new VDouble(f.value.decimal.value/divisor);
334: }
335: case AMQP_FIELD_KIND_TABLE:
336: return amqp_table_to_hash(f.value.table);
337: case AMQP_FIELD_KIND_ARRAY: {
338: VArray* result=new VArray();
339: ArrayValue& av=result->array();
340: for(int i=0; i<f.value.array.num_entries; i++)
341: av+=amqp_field_to_value(f.value.array.entries[i]);
342: return result;
343: }
344: case AMQP_FIELD_KIND_VOID:
345: return VVoid::get();
346: default:
347: return VString::empty();
348: }
349: }
350:
351: static VHash* amqp_table_to_hash(amqp_table_t& table) {
352: VHash* result=new VHash;
353: HashStringValue* h=result->get_hash();
354: for(int i=0; i<table.num_entries; i++){
355: amqp_table_entry_t& e=table.entries[i];
356: h->put(*AMQP_STRING(e.key.bytes, e.key.len), amqp_field_to_value(e.value));
357: }
358: return result;
359: }
360:
1.1 moko 361: static void _publish(Request& r, MethodParams& params) {
362: VAmqp& self=GET_SELF(r, VAmqp);
363: const String &msg=params.as_string(0, "msg must be string");
364: const char* exchange_c = ""; // default exchange
365: const char* routing_key_c = 0;
366: bool mandatory=false;
367:
368: amqp_basic_properties_t props;
369: props._flags = 0;
370:
371: if(params.count()>1){
372: if(HashStringValue* options=params.as_hash(1)){
373: for(HashStringValue::Iterator i(*options); i; i.next()){
374: String::Body key=i.key();
375: Value* value=i.value();
376: if(key=="exchange"){
377: exchange_c=value->as_string().cstr();
378: } else if(key=="routing_key"){
379: routing_key_c=value->as_string().cstr();
380: } else if(key=="queue"){
1.3 moko 381: routing_key_c=value->as_string().cstr();
1.1 moko 382: } else if(key=="mandatory"){
383: mandatory=r.process(*value).as_bool();
1.3 moko 384: } else if(key=="content_type"){
385: const char* v=value->as_string().cstr();
386: props.content_type=amqp_cstring_bytes(v);
387: props._flags|=AMQP_BASIC_CONTENT_TYPE_FLAG;
388: } else if(key=="content_encoding"){
389: const char* v=value->as_string().cstr();
390: props.content_encoding=amqp_cstring_bytes(v);
391: props._flags|=AMQP_BASIC_CONTENT_ENCODING_FLAG;
392: } else if(key=="delivery_mode"){
393: uint8_t dm=(uint8_t)value->as_int();
394: props.delivery_mode=dm;
395: props._flags|=AMQP_BASIC_DELIVERY_MODE_FLAG;
396: } else if(key=="priority"){
397: uint8_t pr=(uint8_t)value->as_int();
398: props.priority=pr;
399: props._flags|=AMQP_BASIC_PRIORITY_FLAG;
400: } else if(key=="correlation_id"){
401: const char* v=value->as_string().cstr();
402: props.correlation_id=amqp_cstring_bytes(v);
403: props._flags|=AMQP_BASIC_CORRELATION_ID_FLAG;
404: } else if(key=="reply_to"){
405: const char* v=value->as_string().cstr();
406: props.reply_to=amqp_cstring_bytes(v);
407: props._flags|=AMQP_BASIC_REPLY_TO_FLAG;
408: } else if(key=="expiration"){
409: const char* v=value->as_string().cstr();
410: props.expiration=amqp_cstring_bytes(v);
411: props._flags|=AMQP_BASIC_EXPIRATION_FLAG;
412: } else if(key=="message_id"){
413: const char* v=value->as_string().cstr();
414: props.message_id=amqp_cstring_bytes(v);
415: props._flags|=AMQP_BASIC_MESSAGE_ID_FLAG;
416: } else if(key=="timestamp"){
417: uint64_t ts=(uint64_t)value->as_double();
418: props.timestamp=ts;
419: props._flags|=AMQP_BASIC_TIMESTAMP_FLAG;
420: } else if(key=="type"){
421: const char* v=value->as_string().cstr();
422: props.type=amqp_cstring_bytes(v);
423: props._flags|=AMQP_BASIC_TYPE_FLAG;
424: } else if(key=="user_id"){
425: const char* v=value->as_string().cstr();
426: props.user_id=amqp_cstring_bytes(v);
427: props._flags|=AMQP_BASIC_USER_ID_FLAG;
428: } else if(key=="app_id"){
429: const char* v=value->as_string().cstr();
430: props.app_id=amqp_cstring_bytes(v);
431: props._flags|=AMQP_BASIC_APP_ID_FLAG;
1.18 moko 432: } else if(key=="cluster_id"){
433: const char* v=value->as_string().cstr();
434: props.cluster_id=amqp_cstring_bytes(v);
435: props._flags|=AMQP_BASIC_CLUSTER_ID_FLAG;
1.3 moko 436: } else if(key=="headers"){
1.17 moko 437: props.headers=amqp_build_arguments_table(r, value->get_hash());
438: props._flags|=AMQP_BASIC_HEADERS_FLAG;
439: } else
1.1 moko 440: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
441: }
442: }
443: }
444:
445: if(!routing_key_c)
446: throw Exception("amqp", 0, "routing_key or queue must be specified");
447:
448: amqp_bytes_t body;
449: body.len = msg.length();
450: body.bytes=(void*)msg.cstr();
451:
1.3 moko 452: int ret = amqp_basic_publish(self.connection(), self.channel(), amqp_cstring_bytes(exchange_c), amqp_cstring_bytes(routing_key_c), mandatory, 0, &props, body);
1.1 moko 453:
1.16 moko 454: status_check(self, ret, "publish ");
1.1 moko 455: }
456:
457: static void _release(Request& r, MethodParams&) {
458: VAmqp& self=GET_SELF(r, VAmqp);
459: if(self.fconnection){
460: amqp_connection_state_t conn=self.fconnection;
461: amqp_channel_close(conn, self.fchannel, AMQP_REPLY_SUCCESS);
462: amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
463: amqp_destroy_connection(conn);
464: self.fconnection=0;
465: self.fchannel=0;
1.16 moko 466: self.fstate=VAmqp::CONNECTION_DEAD;
1.1 moko 467: }
468: }
469:
1.15 moko 470: // to allow both [] and () call syntax
471: static uint64_t as_delivery_tag(MethodParams& params, int index=0) {
472: const String &tag_s=params.as_string(index, "delivery tag must not be code");
473: return pa_atoul(tag_s.cstr());
474: }
475:
1.1 moko 476: static void _ack(Request& r, MethodParams& params) {
477: VAmqp& self=GET_SELF(r, VAmqp);
1.15 moko 478: int ret = amqp_basic_ack(self.connection(), self.channel(), as_delivery_tag(params), 0);
1.16 moko 479: status_check(self, ret, "ack ");
1.1 moko 480: }
481:
482: static void _nack(Request& r, MethodParams& params) {
483: VAmqp& self=GET_SELF(r, VAmqp);
1.15 moko 484: uint64_t tag=as_delivery_tag(params);
1.1 moko 485: bool requeue=false;
486: if(params.count()>1){
487: if(HashStringValue* options=params.as_hash(1)){
488: for(HashStringValue::Iterator i(*options); i; i.next()){
489: if(i.key()=="requeue"){
490: requeue=r.process(*i.value()).as_bool();
491: } else
492: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
493: }
494: }
495: }
1.15 moko 496: int ret = amqp_basic_nack(self.connection(), self.channel(), tag, 0, requeue);
1.16 moko 497: status_check(self, ret, "nack ");
1.1 moko 498: }
499:
500: static void _qos(Request& r, MethodParams& params) {
501: VAmqp& self=GET_SELF(r, VAmqp);
502: uint16_t prefetch_count=0;
503: if(params.count()>0){
504: if(HashStringValue* options=params.as_hash(0)){
505: for(HashStringValue::Iterator i(*options); i; i.next()){
506: if(i.key()=="prefetch_count"){
507: int pc=r.process(*i.value()).as_int();
508: prefetch_count= pc<0 ? 0 : (pc>65535 ? 65535 : (uint16_t)pc);
509: } else
510: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
511: }
512: }
513: }
1.16 moko 514: amqp_basic_qos(self.connection(), self.channel(), 0, prefetch_count, 0);
515: check(self, amqp_get_rpc_reply(self.connection()), "qos ");
1.1 moko 516: }
517:
518: static void _reject(Request& r, MethodParams& params) {
519: VAmqp& self=GET_SELF(r, VAmqp);
1.15 moko 520: uint64_t tag=as_delivery_tag(params);
1.1 moko 521: bool requeue=true; // by default return to queue
522: if(params.count()>1){
523: if(HashStringValue* options = params.as_hash(1)){
524: for(HashStringValue::Iterator i(*options); i; i.next()){
525: if(i.key() == "requeue"){
526: requeue=r.process(*i.value()).as_bool();
527: } else
528: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
529: }
530: }
531: }
1.15 moko 532: int ret = amqp_basic_reject(self.connection(), self.channel(), tag, requeue);
1.16 moko 533: status_check(self, ret, "reject ");
1.1 moko 534: }
535:
1.8 moko 536: static void _declare(Request& r, MethodParams& params) {
1.1 moko 537: VAmqp& self=GET_SELF(r, VAmqp);
1.8 moko 538: const char* exchange_c = 0;
539: const char* queue_c = 0;
1.1 moko 540: const char* type_c = "direct";
1.12 moko 541: bool passive=false, durable=false, auto_delete=false, internal=false, exclusive=false;
1.17 moko 542: amqp_table_t arguments=amqp_empty_table;
1.8 moko 543: if(HashStringValue* options=params.as_hash(0)){
544: for(HashStringValue::Iterator i(*options); i; i.next()){
545: String::Body key=i.key();
546: Value* value=i.value();
547: if(key=="exchange"){
548: exchange_c=value->as_string().cstr();
549: } else if(key=="queue"){
550: queue_c=value->as_string().cstr();
551: } else if(key=="type"){
552: type_c=value->as_string().cstr();
553: } else if(key=="passive"){
554: passive=r.process(*value).as_bool();
555: } else if(key=="durable"){
556: durable=r.process(*value).as_bool();
557: } else if(key=="auto_delete"){
558: auto_delete=r.process(*value).as_bool();
1.11 moko 559: } else if(key=="internal"){
560: internal=r.process(*value).as_bool();
561: } else if(key=="exclusive"){
562: exclusive=r.process(*value).as_bool();
1.17 moko 563: } else if(key=="arguments"){
564: arguments=amqp_build_arguments_table(r, value->get_hash());
1.8 moko 565: } else
566: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.1 moko 567: }
568: }
1.8 moko 569: if(!exchange_c && !queue_c)
570: throw Exception("amqp", 0, "exchange or queue must be specified");
571:
572: if(exchange_c){
1.17 moko 573: amqp_exchange_declare(self.connection(), self.channel(), amqp_cstring_bytes(exchange_c), amqp_cstring_bytes(type_c), passive, durable, auto_delete, internal, arguments);
1.16 moko 574: check(self, amqp_get_rpc_reply(self.connection()));
1.8 moko 575: }
1.1 moko 576:
1.8 moko 577: if(queue_c){
1.17 moko 578: 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, exclusive, auto_delete, arguments);
1.16 moko 579: check(self, amqp_get_rpc_reply(self.connection()));
1.8 moko 580: if(!*queue_c && ok){
581: r.write(*AMQP_STRING(ok->queue.bytes, ok->queue.len));
1.1 moko 582: }
583: }
584: }
585:
1.8 moko 586: static void _delete(Request& r, MethodParams& params) {
1.1 moko 587: VAmqp& self=GET_SELF(r, VAmqp);
1.8 moko 588: const char* exchange_c = 0;
589: const char* queue_c = 0;
1.11 moko 590: bool if_unused=false, if_empty=false;
1.8 moko 591: if(HashStringValue* options=params.as_hash(0)){
592: for(HashStringValue::Iterator i(*options); i; i.next()){
593: String::Body key=i.key();
594: Value* value=i.value();
595: if(key=="exchange"){
596: exchange_c=value->as_string().cstr();
597: } else if(key=="queue"){
598: queue_c=value->as_string().cstr();
599: } else if(key=="if_unused"){
600: if_unused=r.process(*value).as_bool();
601: } else if(key=="if_empty"){
602: if_empty=r.process(*value).as_bool();
603: } else
604: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.1 moko 605: }
606: }
1.8 moko 607: if(!exchange_c && !queue_c)
608: throw Exception("amqp", 0, "exchange or queue must be specified");
609:
610: if(exchange_c){
611: amqp_exchange_delete(self.connection(), self.channel(), amqp_cstring_bytes(exchange_c), if_unused);
1.16 moko 612: check(self, amqp_get_rpc_reply(self.connection()));
1.1 moko 613: }
614:
1.8 moko 615: if(queue_c){
616: amqp_queue_delete(self.connection(), self.channel(), amqp_cstring_bytes(queue_c), if_unused, if_empty);
1.16 moko 617: check(self, amqp_get_rpc_reply(self.connection()));
1.1 moko 618: }
619: }
620:
1.8 moko 621: static void _bind(Request& r, MethodParams& params) {
1.1 moko 622: VAmqp& self=GET_SELF(r, VAmqp);
623: const char* exchange_c=0;
624: const char* queue_c=0;
625: const char* routing_key_c="";
1.8 moko 626: if(HashStringValue* options=params.as_hash(0)){
627: for(HashStringValue::Iterator i(*options); i; i.next()){
628: String::Body key=i.key();
629: Value* value=i.value();
630: if(key=="exchange"){
631: exchange_c=value->as_string().cstr();
632: } else if(key=="queue"){
633: queue_c=value->as_string().cstr();
634: } else if(key=="routing_key"){
635: routing_key_c=value->as_string().cstr();
636: } else
637: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.1 moko 638: }
639: }
640: if(!exchange_c || !queue_c) throw Exception("amqp", 0, "exchange and queue are required");
641: 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);
1.16 moko 642: check(self, amqp_get_rpc_reply(self.connection()));
1.1 moko 643: }
644:
1.8 moko 645: static void _unbind(Request& r, MethodParams& params) {
1.1 moko 646: VAmqp& self=GET_SELF(r, VAmqp);
647: const char* exchange_c=0;
648: const char* queue_c=0;
649: const char* routing_key_c="";
1.8 moko 650: if(HashStringValue* options=params.as_hash(0)){
651: for(HashStringValue::Iterator i(*options); i; i.next()){
652: String::Body key=i.key();
653: Value* value=i.value();
654: if(key=="exchange"){
655: exchange_c=value->as_string().cstr();
656: } else if(key=="queue"){
657: queue_c=value->as_string().cstr();
658: } else if(key=="routing_key"){
659: routing_key_c=value->as_string().cstr();
660: } else
661: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.1 moko 662: }
663: }
664: if(!exchange_c || !queue_c) throw Exception("amqp", 0, "exchange and queue are required");
665: 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);
1.16 moko 666: check(self, amqp_get_rpc_reply(self.connection()));
1.1 moko 667: }
668:
1.8 moko 669: static void _purge(Request& r, MethodParams& params) {
670: VAmqp& self=GET_SELF(r, VAmqp);
671: const char* queue_c = 0;
672: if(HashStringValue* options=params.as_hash(0)){
673: for(HashStringValue::Iterator i(*options); i; i.next()){
674: String::Body key=i.key();
675: Value* value=i.value();
676: if(key=="queue"){
677: queue_c=value->as_string().cstr();
678: } else
679: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
680: }
681: }
682: if(!queue_c)
683: throw Exception("amqp", 0, "queue must be specified");
684:
1.9 moko 685: amqp_queue_purge_ok_t *ok = amqp_queue_purge(self.connection(), self.channel(), amqp_cstring_bytes(queue_c));
1.16 moko 686: check(self, amqp_get_rpc_reply(self.connection()));
1.9 moko 687: r.write(*new VInt(ok ? ok->message_count : 0));
1.8 moko 688: }
689:
690: static void _info(Request& r, MethodParams& params) {
691: VAmqp& self = GET_SELF(r, VAmqp);
692: const char* queue_c = 0;
693:
694: if (HashStringValue* options=params.as_hash(0)) {
695: for (HashStringValue::Iterator i(*options); i; i.next()) {
696: String::Body key=i.key();
697: Value* value=i.value();
698: if(key=="queue"){
699: queue_c=value->as_string().cstr();
700: } else {
701: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
702: }
703: }
704: }
705:
706: if (!queue_c)
707: throw Exception("amqp", 0, "queue must be specified");
708:
709: amqp_queue_declare_ok_t* ok = amqp_queue_declare(self.connection(), self.channel(), amqp_cstring_bytes(queue_c), /*passive*/ 1, 0, 0, 0, amqp_empty_table);
1.16 moko 710: check(self, amqp_get_rpc_reply(self.connection()));
1.8 moko 711:
712: Value& result=*new VHash;
1.9 moko 713: if(ok){
714: result.put_element(*new String("queue"), AMQP_VSTRING(ok->queue.bytes, ok->queue.len));
715: result.put_element(*new String("messages"), new VInt(ok->message_count));
716: result.put_element(*new String("consumers"), new VInt(ok->consumer_count));
717: }
1.8 moko 718: r.write(result);
719: }
720:
721: static VHash *amqp_message_hash(amqp_envelope_t &envelope) {
722: VHash *result=new VHash;
723: HashStringValue* h=result->get_hash();
724: h->put("msg", AMQP_VSTRING(envelope.message.body.bytes, envelope.message.body.len));
725: h->put("delivery_tag", new VInt(envelope.delivery_tag));
726: h->put("consumer_tag", AMQP_VSTRING(envelope.consumer_tag.bytes, envelope.consumer_tag.len));
727: h->put("exchange", AMQP_VSTRING(envelope.exchange.bytes, envelope.exchange.len));
1.17 moko 728: h->put("routing_key", AMQP_VSTRING(envelope.routing_key.bytes, envelope.routing_key.len));
729: h->put("redelivered", &VBool::get(envelope.redelivered));
730:
731: // properties are only put when the publisher actually set them (per the wire's own _flags bitmask) -
732: // same handful of allocations as the fields above, just conditional on what's actually present
733: amqp_basic_properties_t& props=envelope.message.properties;
734: if(props._flags & AMQP_BASIC_CONTENT_TYPE_FLAG)
735: h->put("content_type", AMQP_VSTRING(props.content_type.bytes, props.content_type.len));
736: if(props._flags & AMQP_BASIC_CONTENT_ENCODING_FLAG)
737: h->put("content_encoding", AMQP_VSTRING(props.content_encoding.bytes, props.content_encoding.len));
738: if(props._flags & AMQP_BASIC_HEADERS_FLAG)
739: h->put("headers", amqp_table_to_hash(props.headers));
740: if(props._flags & AMQP_BASIC_DELIVERY_MODE_FLAG)
741: h->put("delivery_mode", new VInt(props.delivery_mode));
742: if(props._flags & AMQP_BASIC_PRIORITY_FLAG)
743: h->put("priority", new VInt(props.priority));
744: if(props._flags & AMQP_BASIC_CORRELATION_ID_FLAG)
745: h->put("correlation_id", AMQP_VSTRING(props.correlation_id.bytes, props.correlation_id.len));
746: if(props._flags & AMQP_BASIC_REPLY_TO_FLAG)
747: h->put("reply_to", AMQP_VSTRING(props.reply_to.bytes, props.reply_to.len));
748: if(props._flags & AMQP_BASIC_EXPIRATION_FLAG)
749: h->put("expiration", AMQP_VSTRING(props.expiration.bytes, props.expiration.len));
750: if(props._flags & AMQP_BASIC_MESSAGE_ID_FLAG)
751: h->put("message_id", AMQP_VSTRING(props.message_id.bytes, props.message_id.len));
752: if(props._flags & AMQP_BASIC_TIMESTAMP_FLAG)
753: h->put("timestamp", new VInt((pa_wint)props.timestamp));
754: if(props._flags & AMQP_BASIC_TYPE_FLAG)
755: h->put("type", AMQP_VSTRING(props.type.bytes, props.type.len));
756: if(props._flags & AMQP_BASIC_USER_ID_FLAG)
757: h->put("user_id", AMQP_VSTRING(props.user_id.bytes, props.user_id.len));
758: if(props._flags & AMQP_BASIC_APP_ID_FLAG)
759: h->put("app_id", AMQP_VSTRING(props.app_id.bytes, props.app_id.len));
760: if(props._flags & AMQP_BASIC_CLUSTER_ID_FLAG)
761: h->put("cluster_id", AMQP_VSTRING(props.cluster_id.bytes, props.cluster_id.len));
762:
1.8 moko 763: return result;
764: }
765:
1.14 moko 766: // timeout<0 - wait forever (NULL), timeout==0 - don't wait (poll), timeout>0 - wait up to timeout seconds
767: static struct timeval* amqp_wait_tv(struct timeval& tv, int timeout){
768: if(timeout<0)
769: return NULL;
770: tv.tv_sec=timeout;
771: tv.tv_usec=0;
772: return &tv;
773: }
774:
1.17 moko 775: // amqp_consume_message() returns AMQP_STATUS_UNEXPECTED_STATE when it read a frame other than a deliver while polling.
776: // Per its own documented contract, read that pending frame to find out what it actually was:
777: // - a basic.return for an earlier mandatory publish that found no route: benign, doesn't close anything;
778: // drain the returned message (header+body, still pending right behind it) and retry - the real
779: // message (if any) is still coming
780: // - anything else: a genuine channel/connection close (typically after a protocol violation) - build a
781: // reply so check() can classify it correctly (cheap channel repair vs full reconnect)
1.23 moko 782: static amqp_rpc_reply_t amqp_consume_message_checked(amqp_connection_state_t conn, amqp_channel_t channel, amqp_envelope_t* envelope, struct timeval* tv) {
1.17 moko 783: for(;;) {
784: amqp_rpc_reply_t res = amqp_consume_message(conn, envelope, tv, 0);
785: if(res.reply_type != AMQP_RESPONSE_LIBRARY_EXCEPTION || res.library_error != AMQP_STATUS_UNEXPECTED_STATE)
786: return res;
787:
788: amqp_frame_t frame;
789: if(amqp_simple_wait_frame(conn, &frame) != AMQP_STATUS_OK || frame.frame_type != AMQP_FRAME_METHOD)
790: return res; // give up, let the original UNEXPECTED_STATE be treated as fatal
791:
792: if(frame.payload.method.id == AMQP_BASIC_RETURN_METHOD) {
793: amqp_message_t message;
794: if(amqp_read_message(conn, channel, &message, 0).reply_type == AMQP_RESPONSE_NORMAL)
795: amqp_destroy_message(&message);
796: continue;
797: }
798:
799: res.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
800: res.reply.id = frame.payload.method.id;
801: res.reply.decoded = frame.payload.method.decoded;
802: return res;
803: }
1.16 moko 804: }
805:
1.18 moko 806: // Auto-object used for cancelling the consumer when the enclosing scope exits. A cancel failure here must
807: // never mask whatever error is already propagating, so its result is discarded rather than check()ed.
808: class Temp_amqp_consumer {
809: public:
1.19 moko 810: Temp_amqp_consumer(VAmqp& aself, const char* aconsumer_tag, bool adrain): self(aself), consumer_tag(aconsumer_tag), drain(adrain) {}
1.18 moko 811: ~Temp_amqp_consumer() {
812: if(!consumer_tag || self.fstate != VAmqp::ALIVE) return;
813: amqp_basic_cancel(self.fconnection, self.fchannel, amqp_cstring_bytes(consumer_tag));
814: amqp_get_rpc_reply(self.fconnection);
1.19 moko 815: if(drain){
816: // The broker may have already sent a deliver frame for this (now cancelled) consumer before it even saw our cancel,
817: // so it can still be sitting in the library's own buffer. Its delivery tag belongs to the pre-reset channel and
818: // won't exist anymore afterward, so drain anything already in flight - otherwise it would surface later, misattributed.
819: amqp_envelope_t envelope;
820: struct timeval no_wait = {0,0};
821: while(amqp_consume_message(self.fconnection, &envelope, &no_wait, 0).reply_type == AMQP_RESPONSE_NORMAL)
822: amqp_destroy_envelope(&envelope);
823: amqp_reset_channel(self);
824: }
1.18 moko 825: }
826: private:
827: VAmqp& self;
828: const char* consumer_tag;
1.19 moko 829: bool drain;
1.18 moko 830: };
831:
1.1 moko 832: static void _consume(Request& r, MethodParams& params) {
833: VAmqp& self=GET_SELF(r, VAmqp);
834: const char* queue_c=0;
835: const char* consumer_tag_c=0;
1.17 moko 836: bool ack=true, exclusive=false;
1.14 moko 837: int count=1; bool count_specified=false;
838: int timeout=0; bool timeout_specified=false;
1.1 moko 839: Junction* callback=0;
840:
1.2 moko 841: if(HashStringValue* options=params.as_hash(0)){
842: for(HashStringValue::Iterator i(*options); i; i.next()){
843: String::Body key=i.key();
844: Value* value=i.value();
1.3 moko 845: if(key=="callback"){
846: callback=value->get_junction();
847: } else if(key=="queue"){
1.2 moko 848: queue_c=value->as_string().cstr();
849: } else if(key=="consumer_tag"){
850: consumer_tag_c=value->as_string().cstr();
1.17 moko 851: } else if(key=="ack"){
852: ack=r.process(*value).as_bool();
1.10 moko 853: } else if(key=="exclusive"){
854: exclusive=r.process(*value).as_bool();
1.8 moko 855: } else if(key=="count"){
856: count=r.process(*value).as_int();
1.14 moko 857: count_specified=true;
858: } else if(key=="timeout"){
859: timeout=r.process(*value).as_int();
860: timeout_specified=true;
1.2 moko 861: } else
862: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.1 moko 863: }
864: }
865:
1.8 moko 866: if(!queue_c) throw Exception("amqp", 0, "queue must be specified");
1.14 moko 867: if(count==0) throw Exception("amqp", 0, "count must not be zero");
868:
869: // listen mode (callback): count/timeout unlimited by default - runs until stop_consume/timeout/error
870: // pull mode (no callback): count(1)/timeout(0) by default - returns whatever is ready right now
871: if(!count_specified)
872: count = callback ? -1 : 1;
873: if(!timeout_specified)
874: timeout = callback ? -1 : 0;
1.1 moko 875:
1.9 moko 876: amqp_basic_consume_ok_t *ok = amqp_basic_consume(self.connection(), self.channel(), amqp_cstring_bytes(queue_c),
1.1 moko 877: consumer_tag_c ? amqp_cstring_bytes(consumer_tag_c) : amqp_empty_bytes,
1.17 moko 878: 0 /*no_local*/, !ack, exclusive, amqp_empty_table);
1.16 moko 879: check(self, amqp_get_rpc_reply(self.connection()));
1.1 moko 880:
1.15 moko 881: // amqp_maybe_release_buffers() recycles ok->consumer_tag
882: const char* consumer_tag_copy = ok ? pa_strdup((const char*)ok->consumer_tag.bytes, ok->consumer_tag.len) : 0;
1.19 moko 883: Temp_amqp_consumer cancel_consumer(self, consumer_tag_copy, ack && callback);
1.15 moko 884:
1.14 moko 885: struct timeval tv;
886:
1.8 moko 887: if(callback){
888: self.fstop=false;
1.14 moko 889: for(int received=0; !self.fstop && (count<0 || received<count); received++){
1.8 moko 890: amqp_envelope_t envelope;
891: memset(&envelope, 0, sizeof(envelope));
892: amqp_maybe_release_buffers(self.connection());
1.14 moko 893: // timeout is the max idle gap between messages, re-armed on every wait
1.17 moko 894: amqp_rpc_reply_t res = amqp_consume_message_checked(self.connection(), self.channel(), &envelope, amqp_wait_tv(tv, timeout));
1.8 moko 895: if(res.reply_type == AMQP_RESPONSE_NORMAL){
896: VHash *vh=amqp_message_hash(envelope);
897: Value *params_cb[]={vh};
898: METHOD_FRAME_ACTION(*callback->method, r.method_frame, callback->self, {
899: frame.store_params(params_cb, 1);
900: r.call(frame);
901: });
902: amqp_destroy_envelope(&envelope);
903: } else if(res.reply_type == AMQP_RESPONSE_LIBRARY_EXCEPTION && res.library_error == AMQP_STATUS_TIMEOUT) {
1.14 moko 904: break; // idle too long - stop cleanly
1.8 moko 905: } else {
1.17 moko 906: check(self, res, "consume ");
1.8 moko 907: }
908: }
909: } else {
910: VArray& result=*new VArray();
911: ArrayValue& result_array=result.array();
912:
1.14 moko 913: for(int received=0; count<0 || received<count; received++){
1.8 moko 914: amqp_envelope_t envelope;
915: memset(&envelope, 0, sizeof(envelope));
916: amqp_maybe_release_buffers(self.connection());
1.14 moko 917: // timeout applies only to the first message of the batch;
918: // the rest is drained with a non-blocking poll until nothing more is ready
1.17 moko 919: amqp_rpc_reply_t res = amqp_consume_message_checked(self.connection(), self.channel(), &envelope, amqp_wait_tv(tv, received==0 ? timeout : 0));
1.8 moko 920: if(res.reply_type == AMQP_RESPONSE_NORMAL){
921: result_array+=amqp_message_hash(envelope);
922: amqp_destroy_envelope(&envelope);
1.14 moko 923: } else if(res.reply_type == AMQP_RESPONSE_LIBRARY_EXCEPTION && res.library_error == AMQP_STATUS_TIMEOUT) {
924: break; // nothing (more) ready - return what we have
1.8 moko 925: } else {
1.17 moko 926: check(self, res, "consume ");
1.8 moko 927: }
1.1 moko 928: }
1.8 moko 929: r.write(result);
1.1 moko 930: }
931: }
932:
933: static void _stop_consume(Request& r, MethodParams&) {
934: VAmqp& self=GET_SELF(r, VAmqp);
935: self.fstop=true;
936: }
937:
1.6 moko 938: #endif // WITH_AMQP
1.1 moko 939:
940: // constructor
941: MAmqp::MAmqp(): Methoded("amqp") {
942: add_native_method("create", Method::CT_DYNAMIC, _create, 0, 1);
943: #ifdef WITH_AMQP
944: add_native_method("publish", Method::CT_DYNAMIC, _publish, 1, 2);
945: add_native_method("release", Method::CT_DYNAMIC, _release, 0, 0);
946: add_native_method("ack", Method::CT_DYNAMIC, _ack, 1, 1);
947: add_native_method("nack", Method::CT_DYNAMIC, _nack, 1, 2);
948: add_native_method("reject", Method::CT_DYNAMIC, _reject, 1, 2);
949: add_native_method("qos", Method::CT_DYNAMIC, _qos, 0, 1);
1.8 moko 950: add_native_method("declare", Method::CT_DYNAMIC, _declare, 1, 1);
951: add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
952: add_native_method("bind", Method::CT_DYNAMIC, _bind, 1, 1);
953: add_native_method("unbind", Method::CT_DYNAMIC, _unbind, 1, 1);
954: add_native_method("purge", Method::CT_DYNAMIC, _purge, 1, 1);
955: add_native_method("info", Method::CT_DYNAMIC, _info, 1, 1);
1.2 moko 956: add_native_method("consume", Method::CT_DYNAMIC, _consume, 1, 1);
1.1 moko 957: add_native_method("stop_consume", Method::CT_DYNAMIC, _stop_consume, 0, 0);
1.6 moko 958: #endif // WITH_AMQP
1.1 moko 959: }
E-mail: