subscription.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Test program for event service subscriptions
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <time.h>
  8. #include <fcntl.h>
  9. #include <sys/poll.h>
  10. #ifndef OPENAIS_SOLARIS
  11. #include <stdint.h>
  12. #include <getopt.h>
  13. #else
  14. #include <sys/types.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <sched.h>
  18. #include "saAis.h"
  19. #include "saEvt.h"
  20. #ifdef OPENAIS_SOLARIS
  21. char * strsep(char** str, const char* delims)
  22. {
  23. char* token;
  24. if (*str == NULL) {
  25. /* No more tokens */
  26. return NULL;
  27. }
  28. token = *str;
  29. while (**str != '\0') {
  30. if (strchr(delims,**str)!=NULL) {
  31. **str = '\0';
  32. (*str)++;
  33. return token;
  34. }
  35. (*str)++;
  36. }
  37. /* There is no other token */
  38. *str = NULL;
  39. return token;
  40. }
  41. #endif
  42. #define TEST_EVENT_ORDER 1
  43. #define EVT_FREQ 1000
  44. #define TRY_WAIT 2
  45. uint32_t evt_count = 0;
  46. extern int get_sa_error(SaAisErrorT, char *, int);
  47. char result_buf[256];
  48. int result_buf_len = sizeof(result_buf);
  49. int quiet = 0;
  50. SaVersionT version = { 'B', 0x01, 0x01 };
  51. void event_callback( SaEvtSubscriptionIdT subscriptionId,
  52. const SaEvtEventHandleT eventHandle,
  53. const SaSizeT eventDataSize);
  54. SaEvtCallbacksT callbacks = {
  55. 0,
  56. event_callback
  57. };
  58. char channel[256] = "EVENT_TEST_CHANNEL";
  59. #define MAX_NODES 256
  60. SaEvtEventIdT last_event_id[MAX_NODES] = {0,};
  61. #define MAX_SUB 100
  62. uint32_t subscription_id[MAX_SUB] = {0xfedcba98};
  63. int sub_next = 0;
  64. char pubname[256] = "Test Pub Name";
  65. #define patt1 "Filter pattern 1"
  66. #define patt1_size sizeof(patt1)
  67. SaEvtEventFilterT filters[MAX_SUB] = {
  68. {SA_EVT_PASS_ALL_FILTER, {0, 0}}
  69. };
  70. SaEvtEventFilterArrayT subscribe_filters[MAX_SUB] = {
  71. {
  72. 1, &filters[0]
  73. },
  74. };
  75. #define PAT_SIZE 100
  76. SaUint8T pat0[PAT_SIZE];
  77. SaUint8T pat1[PAT_SIZE];
  78. SaUint8T pat2[PAT_SIZE];
  79. SaUint8T pat3[PAT_SIZE];
  80. SaUint8T pat4[PAT_SIZE];
  81. SaEvtEventPatternT evt_patts[5] = {
  82. {PAT_SIZE, PAT_SIZE, pat0},
  83. {PAT_SIZE, PAT_SIZE, pat1},
  84. {PAT_SIZE, PAT_SIZE, pat2},
  85. {PAT_SIZE, PAT_SIZE, pat3},
  86. {PAT_SIZE, PAT_SIZE, pat4}};
  87. SaEvtEventPatternArrayT evt_pat_get_array = { 5, 0, evt_patts };
  88. SaNameT test_pub_name = {13, "Test Pub Name"};
  89. char user_data_file[256];
  90. char user_data[65536];
  91. char event_data[65536];
  92. int user_data_size = 0;
  93. int
  94. test_subscription()
  95. {
  96. SaEvtHandleT handle;
  97. SaEvtChannelHandleT channel_handle;
  98. SaEvtChannelOpenFlagsT flags;
  99. SaNameT channel_name;
  100. struct pollfd pfd;
  101. int nfd;
  102. SaSelectionObjectT fd;
  103. int timeout = 60000;
  104. int i;
  105. SaAisErrorT result;
  106. flags = SA_EVT_CHANNEL_SUBSCRIBER | SA_EVT_CHANNEL_CREATE;
  107. strcpy((char *)channel_name.value, channel);
  108. channel_name.length = strlen(channel);
  109. printf("Test subscription:\n");
  110. do {
  111. result = saEvtInitialize (&handle, &callbacks, &version);
  112. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  113. if (result != SA_AIS_OK) {
  114. get_sa_error(result, result_buf, result_buf_len);
  115. printf("Event Initialize result: %s\n", result_buf);
  116. return result;
  117. }
  118. do {
  119. result = saEvtChannelOpen(handle, &channel_name, flags,
  120. SA_TIME_MAX, &channel_handle);
  121. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  122. if (result != SA_AIS_OK) {
  123. get_sa_error(result, result_buf, result_buf_len);
  124. printf("channel open result: %s\n", result_buf);
  125. goto init_fin;
  126. }
  127. if (sub_next == 0)
  128. sub_next = 1;
  129. for (i = 0; i < sub_next; i++) {
  130. do {
  131. result = saEvtEventSubscribe(channel_handle,
  132. &subscribe_filters[i],
  133. subscription_id[i]);
  134. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  135. if (result != SA_AIS_OK) {
  136. get_sa_error(result, result_buf, result_buf_len);
  137. printf("event subscribe result: %s\n", result_buf);
  138. goto chan_fin;
  139. }
  140. }
  141. /*
  142. * See if we got the event
  143. */
  144. do {
  145. result = saEvtSelectionObjectGet(handle, &fd);
  146. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  147. if (result != SA_AIS_OK) {
  148. get_sa_error(result, result_buf, result_buf_len);
  149. printf("saEvtSelectionObject get %s\n", result_buf);
  150. goto sub_fin;
  151. }
  152. while (1) {
  153. pfd.fd = fd;
  154. pfd.events = POLLIN;
  155. nfd = poll(&pfd, 1, timeout);
  156. if (nfd < 0) {
  157. printf("poll fds %d\n", nfd);
  158. perror("poll error");
  159. goto sub_fin;
  160. } else if (nfd == 0) {
  161. printf("Still waiting\n");
  162. continue;
  163. }
  164. if (pfd.revents & (POLLERR|POLLHUP)) {
  165. printf("Error received on poll fd %llu\n",
  166. (unsigned long long)fd);
  167. result = SA_AIS_ERR_BAD_OPERATION;
  168. goto sub_fin;
  169. }
  170. do {
  171. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  172. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  173. if (result != SA_AIS_OK) {
  174. get_sa_error(result, result_buf, result_buf_len);
  175. printf("saEvtDispatch %s\n", result_buf);
  176. goto sub_fin;
  177. }
  178. if (!quiet)
  179. printf(" - - - - - - - - - - - - - - - -\n\n");
  180. }
  181. sub_fin:
  182. #if 0
  183. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  184. if (result != SA_AIS_OK)
  185. printf("Channel unsubscribe result: %d\n", result);
  186. #endif
  187. chan_fin:
  188. result = saEvtChannelClose(channel_handle);
  189. if (result != SA_AIS_OK)
  190. get_sa_error(result, result_buf, result_buf_len);
  191. printf("Channel close result: %s\n", result_buf);
  192. init_fin:
  193. result = saEvtFinalize(handle);
  194. if (result != SA_AIS_OK) {
  195. get_sa_error(result, result_buf, result_buf_len);
  196. printf("Finalize result: %s\n", result_buf);
  197. }
  198. return result;
  199. }
  200. static char time_buf[1024];
  201. char *ais_time_str(SaTimeT time)
  202. {
  203. time_t t;
  204. if (time == SA_TIME_UNKNOWN) {
  205. return "Unknown Time";
  206. }
  207. t = time / 1000000000ULL;
  208. strcpy(time_buf, ctime(&t));
  209. return time_buf;
  210. }
  211. #define dprintf(format, ...) \
  212. { \
  213. if (did_dot) { \
  214. printf("\n"); \
  215. } \
  216. printf(format, ## __VA_ARGS__); \
  217. did_dot = 0; \
  218. }
  219. void
  220. event_callback( SaEvtSubscriptionIdT subscription_id,
  221. const SaEvtEventHandleT event_handle,
  222. const SaSizeT event_data_size)
  223. {
  224. static int did_dot = 0;
  225. SaAisErrorT result;
  226. SaUint8T priority;
  227. SaTimeT retention_time;
  228. SaNameT publisher_name = {0, {0}};
  229. SaTimeT publish_time;
  230. SaEvtEventIdT event_id;
  231. SaSizeT received_size;
  232. int i;
  233. #ifdef TEST_EVENT_ORDER
  234. int idx;
  235. #endif
  236. if (!quiet)
  237. dprintf("event_callback called\n");
  238. if (!quiet)
  239. dprintf("sub ID: %x\n", subscription_id);
  240. if (!quiet)
  241. dprintf("event_handle %llx\n", (unsigned long long)event_handle);
  242. if (!quiet)
  243. dprintf("event data size %llu\n", (unsigned long long)event_data_size);
  244. evt_pat_get_array.patterns[0].patternSize = PAT_SIZE;
  245. evt_pat_get_array.patterns[1].patternSize = PAT_SIZE;
  246. evt_pat_get_array.patterns[2].patternSize = PAT_SIZE;
  247. evt_pat_get_array.patterns[3].patternSize = PAT_SIZE;
  248. evt_pat_get_array.patternsNumber = 4;
  249. result = saEvtEventAttributesGet(event_handle,
  250. &evt_pat_get_array, /* patterns */
  251. &priority, /* priority */
  252. &retention_time, /* retention time */
  253. &publisher_name, /* publisher name */
  254. &publish_time, /* publish time */
  255. &event_id /* event_id */
  256. );
  257. if (result != SA_AIS_OK) {
  258. get_sa_error(result, result_buf, result_buf_len);
  259. dprintf("event get attr result(2): %s\n", result_buf);
  260. goto evt_free;
  261. }
  262. if (!quiet) {
  263. dprintf("pattern array count: %llu\n",
  264. (unsigned long long)evt_pat_get_array.patternsNumber);
  265. for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
  266. dprintf( "pattern %d =\"%s\"\n", i,
  267. evt_pat_get_array.patterns[i].pattern);
  268. }
  269. dprintf("priority: 0x%x\n", priority);
  270. dprintf("retention: 0x%llx\n", (unsigned long long)retention_time);
  271. dprintf("publisher name content: \"%s\"\n",
  272. publisher_name.value);
  273. }
  274. if (event_id == SA_EVT_EVENTID_LOST) {
  275. dprintf("*** Events have been dropped at %s",
  276. ais_time_str(publish_time));
  277. if ((evt_pat_get_array.patternsNumber == 0)||
  278. (strcmp((char *)evt_pat_get_array.patterns[0].pattern, SA_EVT_LOST_EVENT) != 0)) {
  279. dprintf("*** Received SA_EVT_EVENTID_LOST but pattern is wrong: %s\n",
  280. evt_pat_get_array.patterns[0].pattern);
  281. }
  282. }
  283. if (quiet < 2) {
  284. dprintf("event id: 0x%016llx\n", (unsigned long long)event_id);
  285. }
  286. if (quiet == 2) {
  287. if ((++evt_count % EVT_FREQ) == 0) {
  288. fprintf(stderr, ".");
  289. did_dot = 1;
  290. }
  291. }
  292. if (event_id == SA_EVT_EVENTID_LOST) {
  293. goto evt_free;
  294. }
  295. #ifdef TEST_EVENT_ORDER
  296. for (idx = 0; idx < MAX_NODES; idx++) {
  297. if (last_event_id[idx] == 0) {
  298. last_event_id[idx] = event_id;
  299. break;
  300. } else {
  301. if ((last_event_id[idx] >> 32) == (event_id >> 32)) {
  302. last_event_id[idx]++;
  303. if (last_event_id[idx] != event_id) {
  304. dprintf("*** expected %016llx got %016llx event_id\n",
  305. (unsigned long long)last_event_id[idx],
  306. (unsigned long long)event_id);
  307. last_event_id[idx] = event_id;
  308. }
  309. break;
  310. }
  311. }
  312. }
  313. if (idx == MAX_NODES) {
  314. dprintf("*** Too many nodes in cluster\n");
  315. exit(1);
  316. }
  317. #endif
  318. if (event_data_size != user_data_size) {
  319. dprintf("unexpected data size: e=%d, a=%llu\n",
  320. user_data_size, (unsigned long long)event_data_size);
  321. goto evt_free;
  322. }
  323. received_size = user_data_size;
  324. result = saEvtEventDataGet(event_handle, event_data,
  325. &received_size);
  326. if (result != SA_AIS_OK) {
  327. get_sa_error(result, result_buf, result_buf_len);
  328. dprintf("event get data result: %s\n", result_buf);
  329. goto evt_free;
  330. }
  331. if (received_size != event_data_size) {
  332. dprintf("event data mismatch e=%llu, a=%llu\n",
  333. (unsigned long long)event_data_size,
  334. (unsigned long long)received_size);
  335. goto evt_free;
  336. }
  337. if (memcmp(user_data, event_data, user_data_size) != 0 ) {
  338. dprintf("event data doesn't match specified file data\n");
  339. goto evt_free;
  340. }
  341. if (!quiet) {
  342. dprintf("Received %d bytes of data OK\n",
  343. user_data_size);
  344. }
  345. evt_free:
  346. result = saEvtEventFree(event_handle);
  347. if (!quiet) {
  348. get_sa_error(result, result_buf, result_buf_len);
  349. dprintf("event free result: %s\n", result_buf);
  350. }
  351. }
  352. static int err_wait_time = -1;
  353. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  354. static struct sched_param sched_param = {
  355. sched_priority: 1
  356. };
  357. #endif
  358. int main (int argc, char **argv)
  359. {
  360. static const char opts[] = "c:s:n:qu:f:";
  361. int option;
  362. char *p;
  363. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  364. sched_setscheduler (0, SCHED_RR, &sched_param);
  365. #endif
  366. while (1) {
  367. option = getopt(argc, argv, opts);
  368. if (option == -1)
  369. break;
  370. switch (option) {
  371. case 'u': {
  372. int fd;
  373. int sz;
  374. strcpy(user_data_file, optarg);
  375. fd = open(user_data_file, O_RDONLY);
  376. if (fd < 0) {
  377. printf("Can't open user data file %s\n",
  378. user_data_file);
  379. exit(1);
  380. }
  381. sz = read(fd, user_data, 65536);
  382. if (sz < 0) {
  383. perror("subscription\n");
  384. exit(1);
  385. }
  386. close(fd);
  387. user_data_size = sz;
  388. break;
  389. }
  390. case 'q':
  391. quiet++;
  392. break;
  393. case 'c':
  394. strcpy(channel, optarg);
  395. break;
  396. case 'f':
  397. err_wait_time =
  398. (unsigned int)strtoul(optarg, NULL, 0);
  399. break;
  400. case 'n':
  401. strcpy(pubname, optarg);
  402. break;
  403. case 's':
  404. p = strsep(&optarg, ",");
  405. subscription_id[sub_next] =
  406. (unsigned int)strtoul(p, NULL, 0);
  407. p = strsep(&optarg, ",");
  408. filters[sub_next].filter.pattern = malloc(strlen(p));
  409. strcpy((char *)filters[sub_next].filter.pattern, p);
  410. filters[sub_next].filter.patternSize = strlen(p);
  411. p = strsep(&optarg, ",");
  412. filters[sub_next++].filterType = strtoul(p,0, 0);
  413. break;
  414. default:
  415. printf("invalid arg: \"%s\"\n", optarg);
  416. return 1;
  417. }
  418. }
  419. do {
  420. if (test_subscription() != SA_AIS_OK) {
  421. if (err_wait_time > 0) {
  422. sleep(err_wait_time);
  423. } else {
  424. return 1;
  425. }
  426. }
  427. } while (err_wait_time > 0);
  428. return 0;
  429. }