4
0

subscription.c 11 KB

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