以 PDF 格式下载本书 (923 KB)
Chapter 4 Messaging Server Specific InformationThis chapter describes the Messaging Server specific items you need to use the ENS APIs. This chapter contains these sections: Event Notification Types and ParametersFor Messaging Server, there is only one event reference, which can be composed of several parameters. There are various types of event notifications. Table 4–1 lists the event types supported by Messaging Server and gives a description of each: Table 4–1 Event Types
The following applies to the above supported event types:
Note – There is no mechanism to select folders; all folders are included when the variable is enabled (value = 1).
ParametersiBiff uses the following format for the ENS event reference: enp://127.0.0.1/store?param=value¶m1=value1¶m2=value2 The event key enp://127.0.0.1/store has no significance other than its uniqueness as a string. For example, the hostname portion of the event key has no significance as a hostname. It is simply a string that is part of the URI. However, the event key is user configurable. The list of iBiff event reference parameters is listed in Table 4–2 and Table 4–3 that follow. The second part of the event reference consists of parameter-value pairs. This part of the event reference is separated from the event key by a question mark (?). The parameter and value are separated by an equals sign (=). The parameter-value pairs are separated by an ampersand (&). Note that there can be empty values, for which the value simply does not exist. Table 4–2 describes the mandatory event reference parameters that need to be included in every notification. Table 4–2 Mandatory Event Reference Parameters
Table 4–3 describes optional event reference parameters, which might be seen in the event depending on the event type (see Table 4–4). Table 4–3 Optional Event Reference Parameters
Note – Subscribers should allow for undocumented parameters when parsing the event reference. This allows for future compatibility when new parameters are added. Table 4–4 shows the parameters that are available for each event type. For example, to see which parameters apply to a TrashMsg event, look in the column header for “ReadMsg, TrashMsg” and then note that these events can use numDel, numMsgs, numSeen, and userValidity. Table 4–4 Available Parameters for Each Event Type
PayloadENS allows a payload for two event types: NewMsg, and UpdateMsg; the other event types do not carry a payload. The payload portion of these two notifications can contain any of the following data:
The amount and type of data sent as the payload of the ENS event is determined by the configuration parameters found in Table 4–5. Table 4–5 Payload Configuration Parameters
Note that both parameters are set to zero as the default so that no header or body data is sent with ENS notifications. ExamplesThe following example shows a NewMsg event reference (it is actually a single line that is broken up to several lines for readability):
In this example, for the DeleteMsg event. Messages marked as deleted by IMAP or HTTP were expunged. The user would not see the message in the folder any more.
And a third example shows a ReadMsg event. Message was marked as Seen by IMAP or HTTP.
Sample CodeThe 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
|
msg-svr-base/lib |
Compile the code using the Makefile.sample.
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.
Remove the msg-svr-base/lib path from your library search path.
If you do not remove this from the library search path, you will not be able to stop and start the directory server.
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);
}
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);
}
The current implementation does not provide security on events that can be subscribed to. Thus, a user could register for all events, and portions of all other users’ mail. Because of this it is strongly recommended that the ENS subscriber be on the “safe” side of the firewall at the very least.