2 * Test program for event service
10 #ifndef OPENAIS_SOLARIS
14 #include <sys/types.h>
21 // #define EVENT_SUBSCRIBE
23 #define PUB_RETRIES 100
26 extern int get_sa_error(SaAisErrorT
, char *, int);
28 int result_buf_len
= sizeof(result_buf
);
30 static int pub_count
= 1;
31 static int wait_time
= -1;
33 SaVersionT version
= { 'B', 0x01, 0x01 };
35 void event_callback( SaEvtSubscriptionIdT subscriptionId
,
36 const SaEvtEventHandleT eventHandle
,
37 const SaSizeT eventDataSize
);
39 SaEvtCallbacksT callbacks
= {
45 char channel
[256] = "EVENT_TEST_CHANNEL";
46 unsigned int subscription_id
= 0xfedcba98;
47 unsigned long long ret_time
= 30000000000ULL; /* 30 seconds */
48 char pubname
[256] = "Test Pub Name";
50 #define _patt1 "Filter pattern 1"
51 #define patt1 (SaUint8T *) _patt1
52 #define patt1_size sizeof(_patt1)
54 #define _patt2 "Filter pattern 2"
55 #define patt2 (SaUint8T *) _patt2
56 #define patt2_size sizeof(_patt2)
58 #define _patt3 "Filter pattern 3"
59 #define patt3 (SaUint8T *) _patt3
60 #define patt3_size sizeof(_patt3)
62 #define _patt4 "Filter pattern 4"
63 #define patt4 (SaUint8T *) _patt4
64 #define patt4_size sizeof(_patt4)
67 SaEvtEventFilterT filters
[] = {
68 {SA_EVT_PREFIX_FILTER
, {patt1_size
, patt1_size
, patt1
}},
69 {SA_EVT_SUFFIX_FILTER
, {patt2_size
, patt2_size
, patt2
}},
70 {SA_EVT_EXACT_FILTER
, {patt3_size
, patt3_size
, patt3
}},
71 {SA_EVT_PASS_ALL_FILTER
, {patt4_size
, patt4_size
, patt4
}}
74 SaEvtEventFilterArrayT subscribe_filters
= {
75 sizeof(filters
)/sizeof(SaEvtEventFilterT
),
85 SaEvtEventPatternT evt_patts
[5] = {
91 SaEvtEventPatternArrayT evt_pat_get_array
= { 5, 0, evt_patts
};
93 SaEvtEventPatternT patterns
[] = {
94 {patt1_size
, patt1_size
, patt1
},
95 {patt2_size
, patt2_size
, patt2
},
96 {patt3_size
, patt3_size
, patt3
},
97 {patt4_size
, patt4_size
, patt4
}
99 SaNameT test_pub_name
;
100 #define TEST_PRIORITY 2
102 SaEvtEventPatternArrayT evt_pat_set_array
= {
103 sizeof(patterns
)/sizeof(SaEvtEventPatternT
),
104 sizeof(patterns
)/sizeof(SaEvtEventPatternT
),
108 char user_data_file
[256];
109 char user_data
[65536];
110 int user_data_size
= 0;
112 uint64_t clust_time_now(void)
117 if (gettimeofday(&tv
, 0)) {
121 time_now
= (uint64_t)(tv
.tv_sec
) * 1000000000ULL;
122 time_now
+= (uint64_t)(tv
.tv_usec
) * 1000ULL;
131 SaEvtChannelHandleT channel_handle
;
132 SaEvtEventHandleT event_handle
;
133 SaEvtChannelOpenFlagsT flags
;
134 SaNameT channel_name
;
135 uint64_t test_retention
;
136 SaSelectionObjectT fd
;
139 SaEvtEventIdT event_id
;
140 #ifdef EVENT_SUBSCRIBE
150 flags
= SA_EVT_CHANNEL_PUBLISHER
|
151 #ifdef EVENT_SUBSCRIBE
152 SA_EVT_CHANNEL_SUBSCRIBER
|
154 SA_EVT_CHANNEL_CREATE
;
155 strcpy((char *)channel_name
.value
, channel
);
156 channel_name
.length
= strlen(channel
);
160 result
= saEvtInitialize (&handle
, &callbacks
, &version
);
161 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
162 if (result
!= SA_AIS_OK
) {
163 get_sa_error(result
, result_buf
, result_buf_len
);
164 printf("Event Initialize result: %s\n", result_buf
);
168 result
= saEvtChannelOpen(handle
, &channel_name
, flags
,
169 SA_TIME_MAX
, &channel_handle
);
170 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
171 if (result
!= SA_AIS_OK
) {
172 get_sa_error(result
, result_buf
, result_buf_len
);
173 printf("channel open result: %s\n", result_buf
);
178 * Publish with pattens
182 #ifdef EVENT_SUBSCRIBE
184 result
= saEvtEventSubscribe(channel_handle
,
187 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
189 if (result
!= SA_AIS_OK
) {
190 get_sa_error(result
, result_buf
, result_buf_len
);
191 printf("event subscribe result: %s\n", result_buf
);
196 result
= saEvtEventAllocate(channel_handle
, &event_handle
);
197 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
198 if (result
!= SA_AIS_OK
) {
199 get_sa_error(result
, result_buf
, result_buf_len
);
200 printf("event Allocate result: %s\n", result_buf
);
204 strcpy((char *)test_pub_name
.value
, pubname
);
205 test_pub_name
.length
= strlen(pubname
);
206 test_retention
= ret_time
;
208 result
= saEvtEventAttributesSet(event_handle
,
213 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
214 if (result
!= SA_AIS_OK
) {
215 get_sa_error(result
, result_buf
, result_buf_len
);
216 printf("event set attr result(2): %s\n", result_buf
);
220 for (i
= 0; i
< pub_count
; i
++) {
222 result
= saEvtEventPublish(event_handle
, user_data
,
223 user_data_size
, &event_id
);
224 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
225 if (result
!= SA_AIS_OK
) {
226 get_sa_error(result
, result_buf
, result_buf_len
);
227 printf("event Publish result(2): %s\n", result_buf
);
230 printf("Published event ID: 0x%llx\n",
231 (unsigned long long)event_id
);
235 * See if we got the event
238 result
= saEvtSelectionObjectGet(handle
, &fd
);
239 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
240 if (result
!= SA_AIS_OK
) {
241 get_sa_error(result
, result_buf
, result_buf_len
);
242 printf("saEvtSelectionObject get %s\n", result_buf
);
246 #ifdef EVENT_SUBSCRIBE
248 for (i
= 0; i
< pub_count
; i
++) {
251 nfd
= poll(&pfd
, 1, timeout
);
253 printf("poll fds %d\n", nfd
);
255 perror("poll error");
260 printf("Got poll event\n");
262 result
= saEvtDispatch(handle
, SA_DISPATCH_ONE
);
263 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
264 if (result
!= SA_AIS_OK
) {
265 get_sa_error(result
, result_buf
, result_buf_len
);
266 printf("saEvtDispatch %s\n", result_buf
);
277 result
= saEvtEventFree(event_handle
);
278 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
279 if (result
!= SA_AIS_OK
) {
280 get_sa_error(result
, result_buf
, result_buf_len
);
281 printf("event free result: %s\n", result_buf
);
286 result
= saEvtChannelClose(channel_handle
);
287 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
289 if (result
!= SA_AIS_OK
) {
290 get_sa_error(result
, result_buf
, result_buf_len
);
291 printf("channel close result: %s\n", result_buf
);
295 result
= saEvtFinalize(handle
);
296 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
298 if (result
!= SA_AIS_OK
) {
299 get_sa_error(result
, result_buf
, result_buf_len
);
300 printf("Event Finalize result: %s\n", result_buf
);
309 event_callback( SaEvtSubscriptionIdT subscription_id
,
310 const SaEvtEventHandleT event_handle
,
311 const SaSizeT event_data_size
)
315 SaTimeT retention_time
;
316 SaNameT publisher_name
= {0, {0}};
317 SaTimeT publish_time
;
318 SaEvtEventIdT event_id
;
321 printf("event_callback called\n");
322 printf("sub ID: %x\n", subscription_id
);
323 printf("event_handle %llx\n",
324 (unsigned long long)event_handle
);
325 printf("event data size %llu\n", (unsigned long long)event_data_size
);
327 evt_pat_get_array
.patternsNumber
= 4;
329 result
= saEvtEventAttributesGet(event_handle
,
330 &evt_pat_get_array
, /* patterns */
331 &priority
, /* priority */
332 &retention_time
, /* retention time */
333 &publisher_name
, /* publisher name */
334 &publish_time
, /* publish time */
335 &event_id
/* event_id */
337 } while ((result
== SA_AIS_ERR_TRY_AGAIN
) && !sleep(TRY_WAIT
));
338 if (result
!= SA_AIS_OK
) {
339 get_sa_error(result
, result_buf
, result_buf_len
);
340 printf("event get attr result(2): %s\n", result_buf
);
343 printf("pattern array count: %llu\n",
344 (unsigned long long)evt_pat_get_array
.patternsNumber
);
345 for (i
= 0; i
< evt_pat_get_array
.patternsNumber
; i
++) {
346 printf( "pattern %d =\"%s\"\n", i
,
347 evt_pat_get_array
.patterns
[i
].pattern
);
350 printf("priority: 0x%x\n", priority
);
351 printf("retention: 0x%llx\n",
352 (unsigned long long)retention_time
);
353 printf("publisher name content: \"%s\"\n", publisher_name
.value
);
354 printf("event id: 0x%llx\n",
355 (unsigned long long)event_id
);
357 result
= saEvtEventFree(event_handle
);
358 get_sa_error(result
, result_buf
, result_buf_len
);
359 printf("event free result: %s\n", result_buf
);
363 static int err_wait_time
= -1;
365 int main (int argc
, char **argv
)
367 static const char opts
[] = "c:i:t:n:x:u:w:f:";
373 option
= getopt(argc
, argv
, opts
);
382 strcpy(user_data_file
, optarg
);
383 fd
= open(user_data_file
, O_RDONLY
);
385 printf("Can't open user data file %s\n",
389 sz
= read(fd
, user_data
, 65536);
391 perror("subscription\n");
400 strcpy(channel
, optarg
);
403 strcpy(pubname
, optarg
);
407 (unsigned int)strtoul(optarg
, NULL
, 0);
411 (unsigned int)strtoul(optarg
, NULL
, 0);
415 (unsigned int)strtoul(optarg
, NULL
, 0);
418 ret_time
= strtoull(optarg
, NULL
, 0);
419 ret_time
*= 1000000000;
422 pub_count
= strtoul(optarg
, NULL
, 0);
425 printf("invalid arg: \"%s\"\n", optarg
);
431 if (ret
!= SA_AIS_OK
) {
432 if (err_wait_time
< 0) {
435 sleep(err_wait_time
);
437 } else if (wait_time
< 0) {