void AMQP::login() { amqp_rpc_reply_t res = amqp_login(cnn, vhost.c_str(), 0, FRAME_MAX, 0, AMQP_SASL_METHOD_PLAIN, user.c_str(), password.c_str()); if ( res.reply_type != AMQP_RESPONSE_NORMAL) { amqp_destroy_connection(cnn); throw AMQPException(&res); } }
void AMQP::sockConnect() { cnn = amqp_new_connection(); sockfd = amqp_tcp_socket_new(cnn); if (!sockfd) { amqp_destroy_connection(cnn); throw AMQPException("AMQP cannot create socket descriptor"); } //cout << "sockfd="<< sockfd << " pid=" << getpid() <<endl; int status = amqp_socket_open(sockfd, host.c_str(), port); if (status != AMQP_STATUS_OK) { amqp_connection_close(cnn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(cnn); throw AMQPException("AMQP cannot open socket"); } }
void AMQP::sockConnect() { cnn = amqp_new_connection(); sockfd = amqp_open_socket(host.c_str(), port); if (sockfd<0){ amqp_destroy_connection(cnn); throw AMQPException("AMQP cannot create socket descriptor", sockfd); } //cout << "sockfd="<< sockfd << " pid=" << getpid() <<endl; amqp_set_sockfd(cnn, sockfd); }
void AMQPExchange::checkType() { short isErr = 1; if ( type == "direct" ) isErr = 0; if ( type == "fanout" ) isErr = 0; if ( type == "topic" ) isErr = 0; if (isErr) throw AMQPException("the type of AMQPExchange must be direct | fanout | topic" ); }
void AMQP::sockConnect() { int status; cnn = amqp_new_connection(); switch(proto) { case AMQPS_proto: { sockfd = amqp_ssl_socket_new(cnn); status = amqp_ssl_socket_set_cacert(sockfd, cacert_path.c_str()); if (status) { throw AMQPException("AMQP cannot set CA certificate"); } status = amqp_ssl_socket_set_key(sockfd, client_cert_path.c_str(), client_key_path.c_str()); if (status) { throw AMQPException("AMQP cannot set client certificate or key"); } amqp_ssl_socket_set_verify_peer(sockfd, verify_peer ? 1 : 0); amqp_ssl_socket_set_verify_hostname(sockfd, verify_hostname ? 1 : 0); } break; case AMQP_proto: default: sockfd = amqp_tcp_socket_new(cnn); break; } status = amqp_socket_open(sockfd, host.c_str(), port); if (status){ amqp_destroy_connection(cnn); throw AMQPException("AMQP cannot create socket"); } }
void AMQP::login() { amqp_rpc_reply_t res = amqp_login(cnn, vhost.c_str(), AMQP_DEFAULT_MAX_CHANNELS, AMQP_DEFAULT_FRAME_SIZE, AMQP_DEFAULT_HEARTBEAT, AMQP_SASL_METHOD_PLAIN, user.c_str(), password.c_str()); if ( res.reply_type == AMQP_RESPONSE_NORMAL) return; amqp_connection_close(cnn, AMQP_REPLY_SUCCESS); amqp_destroy_connection(cnn); throw AMQPException(&res); }
void AMQPExchange::Delete() { if (!name.size()) throw AMQPException("the name of exchange not set"); sendDeleteCommand(); }
void AMQPExchange::sendPublishCommand(const char * message, const char * key) { amqp_bytes_t exchangeByte = amqp_cstring_bytes(name.c_str()); amqp_bytes_t keyrouteByte = amqp_cstring_bytes(key); amqp_bytes_t messageByte = amqp_cstring_bytes(message); amqp_basic_properties_t props; if (sHeaders.find("Content-type")!= sHeaders.end()) props.content_type = amqp_cstring_bytes(sHeaders["Content-type"].c_str()); else props.content_type = amqp_cstring_bytes("text/plain"); if (iHeaders.find("Delivery-mode")!= iHeaders.end()) props.delivery_mode = (uint8_t)iHeaders["Delivery-mode"]; else props.delivery_mode = 2; // persistent delivery mode props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG; if (sHeaders.find("Content-encoding")!= sHeaders.end()) { props.content_encoding = amqp_cstring_bytes(sHeaders["Content-encoding"].c_str()); props._flags += AMQP_BASIC_CONTENT_ENCODING_FLAG; } if (sHeaders.find("message_id")!= sHeaders.end()) { props.message_id = amqp_cstring_bytes(sHeaders["message_id"].c_str()); props._flags += AMQP_BASIC_MESSAGE_ID_FLAG; } if (sHeaders.find("user_id")!= sHeaders.end()) { props.user_id = amqp_cstring_bytes(sHeaders["user_id"].c_str()); props._flags += AMQP_BASIC_USER_ID_FLAG; } if (sHeaders.find("app_id")!= sHeaders.end()) { props.app_id = amqp_cstring_bytes(sHeaders["app_id"].c_str()); props._flags += AMQP_BASIC_APP_ID_FLAG; } if (sHeaders.find("cluster_id")!= sHeaders.end()) { props.cluster_id = amqp_cstring_bytes(sHeaders["cluster_id"].c_str()); props._flags += AMQP_BASIC_CLUSTER_ID_FLAG; } if (sHeaders.find("correlation_id")!= sHeaders.end()) { props.correlation_id = amqp_cstring_bytes(sHeaders["correlation_id"].c_str()); props._flags += AMQP_BASIC_CORRELATION_ID_FLAG; } if (iHeaders.find("priority")!= iHeaders.end()) { props.priority = (uint8_t) iHeaders["priority"]; props._flags += AMQP_BASIC_PRIORITY_FLAG; } if (iHeaders.find("timestamp")!= iHeaders.end()) { props.timestamp = (uint64_t) iHeaders["timestamp"]; props._flags += AMQP_BASIC_TIMESTAMP_FLAG; } if (sHeaders.find("Expiration")!= sHeaders.end()) { props.expiration = amqp_cstring_bytes(sHeaders["Expiration"].c_str()); props._flags += AMQP_BASIC_EXPIRATION_FLAG; } if (sHeaders.find("type")!= sHeaders.end()) { props.type = amqp_cstring_bytes(sHeaders["type"].c_str()); props._flags += AMQP_BASIC_TYPE_FLAG; } if (sHeaders.find("Reply-to")!= sHeaders.end()) { props.reply_to = amqp_cstring_bytes(sHeaders["Reply-to"].c_str()); props._flags += AMQP_BASIC_REPLY_TO_FLAG; } props.headers.num_entries = sHeadersSpecial.size(); amqp_table_entry_t_ entries[props.headers.num_entries]; int i = 0; map<string, string>::iterator it; for (it = sHeadersSpecial.begin(); it != sHeadersSpecial.end(); it++) { entries[i].key = amqp_cstring_bytes((*it).first.c_str()); entries[i].value.kind = AMQP_FIELD_KIND_UTF8; entries[i].value.value.bytes = amqp_cstring_bytes((*it).second.c_str()); i++; } props.headers.entries = entries; props._flags += AMQP_BASIC_HEADERS_FLAG; short mandatory = (parms & AMQP_MANDATORY) ? 1:0; short immediate = (parms & AMQP_IMMIDIATE) ? 1:0; int res = amqp_basic_publish( *cnn, 1, exchangeByte, keyrouteByte, mandatory, immediate, &props, messageByte ); if ( 0 > res ) { throw AMQPException("AMQP Publish Fail." ); } }
void AMQPExchange::Bind(string name) { if (type != "fanout") throw AMQPException("key is NULL, this using only for the type fanout" ); sendBindCommand(name.c_str(), NULL ); }