/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "apr.h"
#include "stomp.h"

static void check(apr_status_t rc, char *action, char *cmd)
{
    char msgbuf[80];
    if(rc != APR_SUCCESS)
    {
        apr_strerror(rc, msgbuf, sizeof(msgbuf));
        printf("Error %s %s: %s\n", action, cmd, msgbuf);
        exit(EXIT_FAILURE);
    }
}

static void execute(stomp_connection *con, apr_pool_t *pool, char *cmd, char *input, char *output, char *qname, char *un, char *pw)
{
    apr_status_t rc;
    stomp_frame input_frame;
    stomp_frame *output_frame;
    input_frame.command = cmd;
    if(qname != NULL)
    {
        input_frame.headers = apr_hash_make(pool);
        apr_hash_set(input_frame.headers, "destination", APR_HASH_KEY_STRING, qname);
        apr_hash_set(input_frame.headers, "amq-msg-type", APR_HASH_KEY_STRING, "text");
    }
    else if(un != NULL && pw != NULL)
    {
        input_frame.headers = apr_hash_make(pool);
        apr_hash_set(input_frame.headers, "login", APR_HASH_KEY_STRING, un);
        apr_hash_set(input_frame.headers, "passcode", APR_HASH_KEY_STRING, pw);
    }
    else
    {
        input_frame.headers = NULL;
    }
    input_frame.body_length = -1;
    input_frame.body = input;
    rc = stomp_write(con, &input_frame, pool);
    check(rc, "writing", cmd);
    if(output != NULL)
    {
        rc = stomp_read(con, &output_frame, pool);
        check(rc, "reading", cmd);
        strcpy(output, output_frame->body);
    }
}

#define HOST "localhost"
#define PORT 61613
#define UN "arne"
#define PW "topsecret"
#define QNAME "/queue/TestQ"
#define PAYLOAD "<data>\r\n    <id>123</id>\r\n    <name>ABC</name>\r\n</data>"

int main()
{
    char msgbuf[80];
    char res[800];
    int i;
    apr_status_t rc;
    apr_pool_t *pool;
    stomp_connection *con;
    stomp_frame *frame;
    rc = apr_initialize();
    if(rc != APR_SUCCESS)
    {
        apr_strerror(rc, msgbuf, sizeof(msgbuf));
        printf("Error initializing APR: %s\n", msgbuf);
        exit(EXIT_FAILURE);
    }
    rc = apr_pool_create(&pool, NULL);
    if(rc != APR_SUCCESS)
    {
        apr_strerror(rc, msgbuf, sizeof(msgbuf));
        printf("Error creating pool: %s\n", msgbuf);
        exit(EXIT_FAILURE);
    }
    rc = stomp_connect(&con, HOST, PORT, pool);
    if(rc != APR_SUCCESS)
    {
        apr_strerror(rc, msgbuf, sizeof(msgbuf));
        printf("Error connecting: %s\n", msgbuf);
        exit(EXIT_FAILURE);
    }
    /* execute(con, pool, "CONNECT", NULL, res, NULL, UN, PW); */   /* authentication does not work for me */
    execute(con, pool, "CONNECT", NULL, res, NULL, NULL, NULL);
    for(i = 0; i < 3; i++) 
    {
        execute(con, pool, "SEND", PAYLOAD, NULL, QNAME, NULL, NULL);
    }
    execute(con, pool, "DISCONNECT", NULL, NULL, NULL, NULL, NULL);
    rc = stomp_disconnect(&con);
    if(rc != APR_SUCCESS)
    {
        apr_strerror(rc, msgbuf, sizeof(msgbuf));
        printf("Error disconnecting: %s\n", msgbuf);
        exit(EXIT_FAILURE);
    }
    apr_pool_destroy(pool);
    apr_terminate();
    return 0;
}
