Sun Java Communications Suite 5 Event Notification Service Guide

Sample Code

The following two code samples illustrate how to use the ENS API. The sample code is provided with the product in the following directory:

msg-svr-base/examples

ProcedureTo use the sample code

  1. Before running the makefile, set your library search path to include the directory:


    msg-svr-base/lib
  2. Compile the code using the Makefile.sample.

  3. Run apub and asub as follows in separate windows:

    apub localhost 7997

    asub localhost 7997

    Whatever is typed into the apub window should appear on the asub window. If you use the default settings, all iBiff notifications should appear in the asub window.

  4. Remove the msg-svr-base/lib path from your library search path.


    Note –

    If you do not remove this from the library search path, you will not be able to stop and start the directory server.


Sample Publisher

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);
}

Sample Subscriber

This sample code provides a simple subscriber.

/*
 * Copyright 1997, 2006 by Sun Microsystems, Inc.
 * All rights reserved
 *
 * asub : example asynchronous subscriber
 *
 * Syntax:
 *      asub host port
*/
*
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "pasdisp.h"
#include "subscriber.h"

#define DEFAULT_EVENT_REF "enp://yoyo.com/xyz"

static pas_dispatcher_t *disp = NULL;
static subscriber_t *_subscriber = NULL;
static subscription_t *_subscription = NULL;
static renl_t *_renl = NULL;
static char *_event_ref = DEFAULT_EVENT_REF;

static void _exit_usage()
{
    printf("\nUsage:\nasub host port\n");
    exit(5);
}

static void _exit_error(const char *msg)
{
    printf("%s\n", msg);
    exit(1);
}

static void _subscribe_ack(void *arg, int rc, void *subscription)
{
    (void)arg;
    if (!rc) {
        _subscription = subscription;
        printf("Subscription successful\n");
        subscriber_keepalive(_subscriber, 30000);
    }else {
        printf("Subscription failed - status %d\n", rc);
        pas_shutdown(disp);
    }
}

static void _unsubscribe_ack(void *arg, int rc, void *ignored)
{
(void *)ignored;
    (void *)arg;
    
    if (rc != 0) {
        printf("Unsubscribe failed - status %d\n", rc);
    }
    
    subscriber_delete(_subscriber);
    pas_shutdown(disp);
}

static int _handle_notify(void *arg, char *url, char *str, int len)
{
    (void *)arg;
    printf("[%s] %.*s\n", url, len, (str) ? str : "(null)");
    return 0;
}

static void _open_ack(void *arg, int rc, void *enc)
{
    _subscriber = (subscriber_t *)enc;
    
    (void *)arg;
    if (rc) {
        printf("Failed to create subscriber with status %d\n", rc);
        pas_shutdown(disp);
        return;
    }
    
    subscribe(_subscriber, "enp://127.0.0.1/store",
              _handle_notify, NULL,
              _subscribe_ack, NULL);
    return;
}

static void _unsubscribe(int sig)
{
    (int)sig;
    unsubscribe(_subscriber, _subscription, _unsubscribe_ack, NULL);
}

int
main(int argc, char **argv)
{
    unsigned short port = 7997;
    char host[256];
    
    if (argc < 2) _exit_usage();
    if (*(argv[1]) == ’0’) {
        strcpy(host, "127.0.0.1");
    s}else {
        strcpy(host, argv[1]);
    }
    if (argc > 2) {
        port = (unsigned short)atoi(argv[2]);
    }
    if (argc > 3) {
        _event_ref = argv[3];
    }
    
    disp = pas_dispatcher_new(NULL);
    if (disp == NULL) _exit_error("Can’t create publisher");
    
    subscriber_new_a(disp, NULL, host, port, _open_ack, NULL);
    
    pas_dispatch(disp);
    
    pas_dispatcher_delete(disp);
    
exit(0);
}