static int stomp_hello(void) { int len; if (fd < 0) return -1; len = snprintf(buffer, sizeof(buffer), "SEND\ndestination:/topic/%s\n\n" "INFO: PMIE: Established initial connection", topic); if (stomp_write(buffer, len) < 0) return -1; if (stomp_write("\0\n", 2) < 0) return -1; return 0; }
static int stomp_destination(void) { int len; if (fd < 0) return -1; len = snprintf(buffer, sizeof(buffer), "SUB\ndestination:/topic/%s\n\n", topic); if (stomp_write(buffer, len) < 0) return -1; if (stomp_write("\0\n", 2) < 0) return -1; return 0; }
static int stomp_authenticate(void) { int len; if (fd < 0) return -1; len = snprintf(buffer, sizeof(buffer), "CONNECT\nlogin:%s\npasscode:%s\n\n", username, passcode); if (stomp_write(buffer, len) < 0) return -1; if (stomp_write("\0\n", 2) < 0) return -1; return 0; }
static int noit_stomp_submit(iep_thread_driver_t *dr, const char *payload, size_t payloadlen) { struct stomp_driver *driver = (struct stomp_driver *)dr; apr_pool_t *dummy; apr_status_t rc; stomp_frame out; if(apr_pool_create(&dummy, NULL) != APR_SUCCESS) return -1; out.command = "SEND"; out.headers = apr_hash_make(dummy); if (driver->exchange) apr_hash_set(out.headers, "exchange", APR_HASH_KEY_STRING, driver->exchange); apr_hash_set(out.headers, "destination", APR_HASH_KEY_STRING, driver->destination); apr_hash_set(out.headers, "ack", APR_HASH_KEY_STRING, "auto"); out.body_length = -1; out.body = (char *)payload; rc = stomp_write(driver->connection, &out, dummy); if(rc != APR_SUCCESS) { noitL(noit_error, "STOMP send failed, disconnecting\n"); if(driver->connection) stomp_disconnect(&driver->connection); driver->connection = NULL; } else noitL(noit_debug, "STOMP send succeeded\n"); apr_pool_destroy(dummy); return (rc == APR_SUCCESS) ? 0 : -1; }
/* * Send a message to the stomp server. */ int stompSend(const char *msg) { int len; if (fd < 0) stompInit(); /* reconnect */ if (fd < -1) return -1; len = snprintf(buffer, sizeof(buffer), "SEND\ndestination:/topic/%s\n\n", topic); if (stomp_write(buffer, len) < 0) return -1; if (stomp_write(msg, strlen(msg)) < 0) return -1; if (stomp_write("\0\n", 2) < 0) return -1; return 0; }
static int noit_stomp_connect(iep_thread_driver_t *dr) { struct stomp_driver *driver = (struct stomp_driver *)dr; apr_status_t rc; stomp_frame frame; if(!driver->connection) { if(stomp_connect(&driver->connection, driver->hostname, driver->port, driver->pool)!= APR_SUCCESS) { noitL(noit_error, "MQ connection failed\n"); stomp_disconnect(&driver->connection); return -1; } frame.command = "CONNECT"; frame.headers = apr_hash_make(driver->pool); /* This is for RabbitMQ Support */ if(driver->user && driver->pass) { apr_hash_set(frame.headers, "login", APR_HASH_KEY_STRING, driver->user); apr_hash_set(frame.headers, "passcode", APR_HASH_KEY_STRING, driver->pass); } if(driver->exchange) apr_hash_set(frame.headers, "exchange", APR_HASH_KEY_STRING, driver->exchange); frame.body = NULL; frame.body_length = -1; rc = stomp_write(driver->connection, &frame, driver->pool); if(rc != APR_SUCCESS) { noitL(noit_error, "MQ STOMP CONNECT failed, %d\n", rc); stomp_disconnect(&driver->connection); return -1; } noitL(noit_debug, "MQ STOMP connection established.\n"); return 0; } /* 1 means already connected */ return 1; }
static gboolean afstomp_send_frame(STOMPDestDriver *self, stomp_frame* frame) { return stomp_write(self->conn, frame); }
int main(int argc, char *argv[]) { apr_status_t rc; apr_pool_t *pool; stomp_connection *connection; setbuf(stdout, NULL); rc = apr_initialize(); check_status(rc, "Could not initialize"); atexit(terminate); rc = apr_pool_create(&pool, NULL); check_status(rc, "Could not allocate pool"); fprintf(stdout, "Connecting......"); rc = stomp_engine_connect(&connection, "localhost", 61613, pool); check_status(rc, "Could not connect"); fprintf(stdout, "OK\n"); fprintf(stdout, "Sending connect message."); { stomp_frame frame; frame.command = "CONNECT"; frame.headers = apr_hash_make(pool); // apr_hash_set(frame.headers, "login", APR_HASH_KEY_STRING, "<username>"); // apr_hash_set(frame.headers, "passcode", APR_HASH_KEY_STRING, "<password>"); frame.body = NULL; frame.body_length = -1; rc = stomp_write(connection, &frame, pool); check_status(rc, "Could not send frame"); } fprintf(stdout, "OK\n"); fprintf(stdout, "Reading Response."); { stomp_frame *frame; rc = stomp_read(connection, &frame, pool); check_status(rc, "Could not read frame"); fprintf(stdout, "Response: %s, %s\n", frame->command, frame->body); } fprintf(stdout, "OK\n"); fprintf(stdout, "Sending Subscribe."); { stomp_frame frame; frame.command = "SUB"; frame.headers = apr_hash_make(pool); apr_hash_set(frame.headers, "destination", APR_HASH_KEY_STRING, "/queue/FOO.BAR"); frame.body_length = -1; frame.body = NULL; rc = stomp_write(connection, &frame, pool); check_status(rc, "Could not send frame"); } fprintf(stdout, "OK\n"); fprintf(stdout, "Reading Response."); { stomp_frame *frame; rc = stomp_read(connection, &frame, pool); check_status(rc, "Could not read frame"); fprintf(stdout, "Response: %s, %s\n", frame->command, frame->body); fprintf(stdout, "Body: %s", frame->body); } fprintf(stdout, "OK\n"); fprintf(stdout, "Sending Disconnect."); { stomp_frame frame; frame.command = "DISCONNECT"; frame.headers = NULL; frame.body_length = -1; frame.body = NULL; rc = stomp_write(connection, &frame, pool); check_status(rc, "Could not send frame"); } fprintf(stdout, "OK\n"); fprintf(stdout, "Disconnecting..."); rc = stomp_engine_disconnect(&connection); check_status(rc, "Could not disconnect"); fprintf(stdout, "OK\n"); apr_pool_destroy(pool); return 0; }