Annotation of parser3/src/classes/amqp.C, revision 1.20

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

E-mail: