2 * Test program for event service subscriptions
11 #ifndef OPENAIS_SOLARIS
15 #include <sys/types.h>
22 #ifdef OPENAIS_SOLARIS
23 char * strsep(char** str
, const char* delims
)
33 while (**str
!= '\0') {
34 if (strchr(delims
,**str
)!=NULL
) {
41 /* There is no other token */
47 #define TEST_EVENT_ORDER 1
50 uint32_t evt_count
= 0;
52 extern int get_sa_error(SaAisErrorT
, char *, int);
54 int result_buf_len
= sizeof(result_buf
);
58 SaVersionT version
= { 'B', 0x01, 0x01 };
60 void event_callback( SaEvtSubscriptionIdT subscriptionId
,
61 const SaEvtEventHandleT eventHandle
,
62 const SaSizeT eventDataSize
);
64 SaEvtCallbacksT callbacks
= {
69 char channel
[256] = "EVENT_TEST_CHANNEL";
72 SaEvtEventIdT last_event_id
[MAX_NODES
] = {0,};
76 uint32_t subscription_id
[MAX_SUB
] = {0xfedcba98};
80 char pubname
[256] = "Test Pub Name";
82 #define patt1 "Filter pattern 1"
83 #define patt1_size sizeof(patt1)
85 SaEvtEventFilterT filters
[MAX_SUB
] = {
86 {SA_EVT_PASS_ALL_FILTER
, {0, 0}}
89 SaEvtEventFilterArrayT subscribe_filters
[MAX_SUB
] = {
97 SaUint8T pat0
[PAT_SIZE
];
98 SaUint8T pat1
[PAT_SIZE
];
99 SaUint8T pat2
[PAT_SIZE
];
100 SaUint8T pat3
[PAT_SIZE
];
101 SaUint8T pat4
[PAT_SIZE
];
102 SaEvtEventPatternT evt_patts
[5] = {
103 {PAT_SIZE
, PAT_SIZE
, pat0
},
104 {PAT_SIZE
, PAT_SIZE
, pat1
},
105 {PAT_SIZE
, PAT_SIZE
, pat2
},
106 {PAT_SIZE
, PAT_SIZE
, pat3
},
107 {PAT_SIZE
, PAT_SIZE
, pat4
}};
108 SaEvtEventPatternArrayT evt_pat_get_array
= { 5, 0, evt_patts
};
110 SaNameT test_pub_name
= {13, "Test Pub Name"};
113 char user_data_file
[256];
114 char user_data
[65536];
115 char event_data
[65536];
116 int user_data_size
= 0;
122 SaEvtChannelHandleT channel_handle
;
123 SaEvtChannelOpenFlagsT flags
;
124 SaNameT channel_name
;
128 SaSelectionObjectT fd
;
136 flags
= SA_EVT_CHANNEL_SUBSCRIBER
| SA_EVT_CHANNEL_CREATE
;
137 strcpy((char *)channel_name
.value
, channel
);
138 channel_name
.length
= strlen(channel
);
140 printf("Test subscription:\n");
143 result
= saEvtInitialize (&handle
, &callbacks
, &version
);
144 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
145 if (result
!= SA_AIS_OK
) {
146 get_sa_error(result
, result_buf
, result_buf_len
);
147 printf("Event Initialize result: %s\n", result_buf
);
151 result
= saEvtChannelOpen(handle
, &channel_name
, flags
,
152 SA_TIME_MAX
, &channel_handle
);
153 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
154 if (result
!= SA_AIS_OK
) {
155 get_sa_error(result
, result_buf
, result_buf_len
);
156 printf("channel open result: %s\n", result_buf
);
163 for (i
= 0; i
< sub_next
; i
++) {
165 result
= saEvtEventSubscribe(channel_handle
,
166 &subscribe_filters
[i
],
168 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
170 if (result
!= SA_AIS_OK
) {
171 get_sa_error(result
, result_buf
, result_buf_len
);
172 printf("event subscribe result: %s\n", result_buf
);
178 * See if we got the event
181 result
= saEvtSelectionObjectGet(handle
, &fd
);
182 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
183 if (result
!= SA_AIS_OK
) {
184 get_sa_error(result
, result_buf
, result_buf_len
);
185 printf("saEvtSelectionObject get %s\n", result_buf
);
192 nfd
= poll(&pfd
, 1, timeout
);
194 printf("poll fds %d\n", nfd
);
195 perror("poll error");
197 } else if (nfd
== 0) {
198 printf("Still waiting\n");
202 if (pfd
.revents
& (POLLERR
|POLLHUP
)) {
203 printf("Error received on poll fd %llu\n",
204 (unsigned long long)fd
);
205 result
= SA_AIS_ERR_BAD_OPERATION
;
209 result
= saEvtDispatch(handle
, SA_DISPATCH_ONE
);
210 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
211 if (result
!= SA_AIS_OK
) {
212 get_sa_error(result
, result_buf
, result_buf_len
);
213 printf("saEvtDispatch %s\n", result_buf
);
217 printf(" - - - - - - - - - - - - - - - -\n\n");
222 result
= saEvtEventUnsubscribe(channel_handle
, subscription_id
);
223 if (result
!= SA_AIS_OK
)
224 printf("Channel unsubscribe result: %d\n", result
);
227 result
= saEvtChannelClose(channel_handle
);
228 if (result
!= SA_AIS_OK
)
229 get_sa_error(result
, result_buf
, result_buf_len
);
230 printf("Channel close result: %s\n", result_buf
);
232 result
= saEvtFinalize(handle
);
233 if (result
!= SA_AIS_OK
) {
234 get_sa_error(result
, result_buf
, result_buf_len
);
235 printf("Finalize result: %s\n", result_buf
);
241 static char time_buf
[1024];
243 char *ais_time_str(SaTimeT time
)
246 if (time
== SA_TIME_UNKNOWN
) {
247 return "Unknown Time";
249 t
= time
/ 1000000000ULL;
250 strcpy(time_buf
, ctime(&t
));
254 #define dprintf(format, ...) \
259 printf(format, ## __VA_ARGS__); \
264 event_callback( SaEvtSubscriptionIdT subscription_id
,
265 const SaEvtEventHandleT event_handle
,
266 const SaSizeT event_data_size
)
268 static int did_dot
= 0;
271 SaTimeT retention_time
;
272 SaNameT publisher_name
= {0, {0}};
273 SaTimeT publish_time
;
274 SaEvtEventIdT event_id
;
275 SaSizeT received_size
;
277 #ifdef TEST_EVENT_ORDER
282 dprintf("event_callback called\n");
284 dprintf("sub ID: %x\n", subscription_id
);
286 dprintf("event_handle %llx\n", (unsigned long long)event_handle
);
288 dprintf("event data size %llu\n", (unsigned long long)event_data_size
);
290 evt_pat_get_array
.patterns
[0].patternSize
= PAT_SIZE
;
291 evt_pat_get_array
.patterns
[1].patternSize
= PAT_SIZE
;
292 evt_pat_get_array
.patterns
[2].patternSize
= PAT_SIZE
;
293 evt_pat_get_array
.patterns
[3].patternSize
= PAT_SIZE
;
294 evt_pat_get_array
.patternsNumber
= 4;
295 result
= saEvtEventAttributesGet(event_handle
,
296 &evt_pat_get_array
, /* patterns */
297 &priority
, /* priority */
298 &retention_time
, /* retention time */
299 &publisher_name
, /* publisher name */
300 &publish_time
, /* publish time */
301 &event_id
/* event_id */
303 if (result
!= SA_AIS_OK
) {
304 get_sa_error(result
, result_buf
, result_buf_len
);
305 dprintf("event get attr result(2): %s\n", result_buf
);
309 dprintf("pattern array count: %llu\n",
310 (unsigned long long)evt_pat_get_array
.patternsNumber
);
311 for (i
= 0; i
< evt_pat_get_array
.patternsNumber
; i
++) {
312 dprintf( "pattern %d =\"%s\"\n", i
,
313 evt_pat_get_array
.patterns
[i
].pattern
);
316 dprintf("priority: 0x%x\n", priority
);
317 dprintf("retention: 0x%llx\n", (unsigned long long)retention_time
);
318 dprintf("publisher name content: \"%s\"\n",
319 publisher_name
.value
);
322 if (event_id
== SA_EVT_EVENTID_LOST
) {
323 dprintf("*** Events have been dropped at %s",
324 ais_time_str(publish_time
));
326 if ((evt_pat_get_array
.patternsNumber
== 0)||
327 (strcmp((char *)evt_pat_get_array
.patterns
[0].pattern
, SA_EVT_LOST_EVENT
) != 0)) {
328 dprintf("*** Received SA_EVT_EVENTID_LOST but pattern is wrong: %s\n",
329 evt_pat_get_array
.patterns
[0].pattern
);
333 dprintf("event id: 0x%016llx\n", (unsigned long long)event_id
);
336 if ((++evt_count
% EVT_FREQ
) == 0) {
337 fprintf(stderr
, ".");
342 if (event_id
== SA_EVT_EVENTID_LOST
) {
346 #ifdef TEST_EVENT_ORDER
347 for (idx
= 0; idx
< MAX_NODES
; idx
++) {
348 if (last_event_id
[idx
] == 0) {
349 last_event_id
[idx
] = event_id
;
352 if ((last_event_id
[idx
] >> 32) == (event_id
>> 32)) {
353 last_event_id
[idx
]++;
354 if (last_event_id
[idx
] != event_id
) {
355 dprintf("*** expected %016llx got %016llx event_id\n",
356 (unsigned long long)last_event_id
[idx
],
357 (unsigned long long)event_id
);
358 last_event_id
[idx
] = event_id
;
364 if (idx
== MAX_NODES
) {
365 dprintf("*** Too many nodes in cluster\n");
370 if (event_data_size
!= user_data_size
) {
371 dprintf("unexpected data size: e=%d, a=%llu\n",
372 user_data_size
, (unsigned long long)event_data_size
);
376 received_size
= user_data_size
;
377 result
= saEvtEventDataGet(event_handle
, event_data
,
379 if (result
!= SA_AIS_OK
) {
380 get_sa_error(result
, result_buf
, result_buf_len
);
381 dprintf("event get data result: %s\n", result_buf
);
384 if (received_size
!= event_data_size
) {
385 dprintf("event data mismatch e=%llu, a=%llu\n",
386 (unsigned long long)event_data_size
,
387 (unsigned long long)received_size
);
390 if (memcmp(user_data
, event_data
, user_data_size
) != 0 ) {
391 dprintf("event data doesn't match specified file data\n");
395 dprintf("Received %d bytes of data OK\n",
400 result
= saEvtEventFree(event_handle
);
402 get_sa_error(result
, result_buf
, result_buf_len
);
403 dprintf("event free result: %s\n", result_buf
);
407 static int err_wait_time
= -1;
409 #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
410 static struct sched_param sched_param
= {
415 int main (int argc
, char **argv
)
417 static const char opts
[] = "c:s:n:qu:f:";
422 #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
423 sched_setscheduler (0, SCHED_RR
, &sched_param
);
427 option
= getopt(argc
, argv
, opts
);
436 strcpy(user_data_file
, optarg
);
437 fd
= open(user_data_file
, O_RDONLY
);
439 printf("Can't open user data file %s\n",
443 sz
= read(fd
, user_data
, 65536);
445 perror("subscription\n");
456 strcpy(channel
, optarg
);
460 (unsigned int)strtoul(optarg
, NULL
, 0);
463 strcpy(pubname
, optarg
);
466 p
= strsep(&optarg
, ",");
467 subscription_id
[sub_next
] =
468 (unsigned int)strtoul(p
, NULL
, 0);
469 p
= strsep(&optarg
, ",");
470 filters
[sub_next
].filter
.pattern
= malloc(strlen(p
));
471 strcpy((char *)filters
[sub_next
].filter
.pattern
, p
);
472 filters
[sub_next
].filter
.patternSize
= strlen(p
);
473 p
= strsep(&optarg
, ",");
474 filters
[sub_next
++].filterType
= strtoul(p
,0, 0);
477 printf("invalid arg: \"%s\"\n", optarg
);
482 if (test_subscription() != SA_AIS_OK
) {
483 if (err_wait_time
> 0) {
484 sleep(err_wait_time
);
489 } while (err_wait_time
> 0);