subscription.c 10 KB

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