subscription.c 10 KB

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