This sample code provides a simple interactive asynchronous publisher.
/*
* Copyright 2006 by Sun Microsystems, Inc.
* All rights reserved
*
* Syntax:
* apub host port
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "upub.h"
static upub_t *_publisher = NULL;
/* function prototypes */
static void _read_stdin(void);
static void _publish_ack(void *arg);
/**
*
**/
static void _read_stdin()
{
static char input[1024];
while (1) {
printf("apub> ");
fflush(stdout);
if ( !fgets(input, sizeof(input), stdin) ) {
continue;
} else {
char *message;
unsigned int message_len;
input[strlen(input) - 1] = 0; /* Strip off the \n */
if (*input == '.' && input[1] == 0) {
break;
}
message = strdup(input);
message_len = strlen(message);
upub_publish(_publisher, "enp://yoyo.com/xyz",
message, message_len, _publish_ack);
}
}
return;
}
/**
* call back after publish is done
**/
static void
_publish_ack(void *arg)
{
free(arg);
return;
}
int
main(int argc, char **argv)
{
unsigned short port = 7997;
char host[256];
if (argc < 2) {
printf("\nUsage:\n\tapub host port\n");
exit(2);
}
if (*(argv[1]) == '0') {
strcpy(host, "127.0.0.1");
} else {
strcpy(host, argv[1]);
}
if (argc > 2) {
port = (unsigned short)atoi(argv[2]);
}
_publisher = upub_init(NULL, host, port, 1);
if (_publisher == NULL) {
printf("could not create publisher\n");
exit(1);
}
_read_stdin();
upub_shutdown(_publisher);
}