litestomp
messenger/client.c

A simple client that connects to local host and exchange 1 message with the server

#include "stomp_message.h"
#include "stomp_status.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/*
* Initializes the messenger
*/
if (!messenger) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_without_message;
}
/*
* Sets the endpoint address
*/
stat = stomp_set_endpoint(messenger, "stomp://localhost:61613/queue/test.stomp.queue");
if (stat != STOMP_SUCCESS) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_without_message;
}
/*
* Connects to the endpoint
*/
stat = stomp_connect(messenger, NULL, 1000);
if (stat != STOMP_SUCCESS) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_without_message;
}
/*
* Creates a message to be sent
*/
stomp_message_t *message = stomp_message_create(&messenger->status);
if (!message) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_with_message;
}
/*
* Formats the message
*/
char *text = "HIGH LEVEL API TEST";
stomp_message_format(message, text, strlen(text));
stomp_send_header_t send_header = {0};
send_header.transaction_id = -1;
send_header.receipt = 124;
// Sets an arbitrary property to the exchange
stomp_exchange_add(messenger->exchange_properties, "test", "123");
&messenger->status);
if (stat != STOMP_SUCCESS) {
fprintf(stderr, messenger->status.message);
goto failure_with_message;
}
/*
* Sends the message
*/
stat = stomp_send(messenger, &send_header, message);
if (stat != STOMP_SUCCESS) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_with_message;
}
/*
* Disconnects from the broker after receiving the receipt response
*/
disconn.receipt = 124;
stat = stomp_disconnect(messenger, &disconn);
if (stat != STOMP_SUCCESS) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_with_message;
}
/*
* Cleanup the objects
*/
return EXIT_SUCCESS;
failure_with_message:
failure_without_message:
return EXIT_FAILURE;
}