litestomp
engine/client.c

A modification of the original example, using the lower level connection engine for message exchange. It is an outdated example.

#include <stdlib.h>
#include "stomp_engine.h"
int die(int exitCode, const char *message, apr_status_t reason)
{
char msgbuf[80];
apr_strerror(reason, msgbuf, sizeof (msgbuf));
fprintf(stderr, "%s: %s (%d)\n", message, msgbuf, reason);
exit(exitCode);
return reason;
}
static void terminate()
{
apr_terminate();
}
static void check_status(apr_status_t rc, const char *message) {
if (rc != APR_SUCCESS) {
die(-2, message, rc);
}
}
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.");
{
frame.command = "CONNECT";
frame.headers = apr_hash_make(pool);
apr_hash_set(frame.headers, "login", APR_HASH_KEY_STRING, "hchirino");
apr_hash_set(frame.headers, "passcode", APR_HASH_KEY_STRING, "letmein");
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.");
{
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 Message.");
{
frame.command = "SEND";
frame.headers = apr_hash_make(pool);
apr_hash_set(frame.headers, "destination", APR_HASH_KEY_STRING, "/queue/test.stomp.queue");
frame.body_length = -1;
frame.body = "This is the message";
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;
}