litestomp
messenger/server.c

A simple server 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, "Unable to initialize stomp messenger\n");
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, 10000);
if (stat != STOMP_SUCCESS) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_without_message;
}
/*
* Subscribes to the endpoint. Uses a fake ID and receipt just for the sake
* of the example
*/
stomp_subscription_header_t sub_header = {0};
sub_header.id = 1;
sub_header.receipt = 123;
stat = stomp_subscribe(messenger, &sub_header);
if (stat != STOMP_SUCCESS) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_without_message;
}
/*
* Creates a message to be sent
*/
stomp_receive_header_t receive_header = {0};
stat = stomp_receive(messenger, &receive_header, message);
if (stat != STOMP_SUCCESS) {
fprintf(stderr, "%s\n", messenger->status.message);
goto failure_with_message;
}
fprintf(stdout, "%s\n", message->body);
const char *test_data = stomp_exchange_get(messenger->exchange_properties,
"test");
fprintf(stdout, "test: %s\n", test_data);
const char *ctime = stomp_exchange_get(messenger->exchange_properties,
fprintf(stdout, "Creation time: %s\n", ctime);
/*
* Disconnects from the broker disregarding any request for confirmation
*/
stat = stomp_disconnect(messenger, NULL);
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;
}