subscription.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. t = time / 1000000000ULL;
  205. strcpy(time_buf, ctime(&t));
  206. return time_buf;
  207. }
  208. #define dprintf(format, ...) \
  209. { \
  210. if (did_dot) { \
  211. printf("\n"); \
  212. } \
  213. printf(format, ## __VA_ARGS__); \
  214. did_dot = 0; \
  215. }
  216. void
  217. event_callback( SaEvtSubscriptionIdT subscription_id,
  218. const SaEvtEventHandleT event_handle,
  219. const SaSizeT event_data_size)
  220. {
  221. static int did_dot = 0;
  222. SaAisErrorT result;
  223. SaUint8T priority;
  224. SaTimeT retention_time;
  225. SaNameT publisher_name = {0, {0}};
  226. SaTimeT publish_time;
  227. SaEvtEventIdT event_id;
  228. SaSizeT received_size;
  229. int i;
  230. #ifdef TEST_EVENT_ORDER
  231. int idx;
  232. #endif
  233. if (!quiet)
  234. dprintf("event_callback called\n");
  235. if (!quiet)
  236. dprintf("sub ID: %x\n", subscription_id);
  237. if (!quiet)
  238. dprintf("event_handle %llx\n", (unsigned long long)event_handle);
  239. if (!quiet)
  240. dprintf("event data size %llu\n", (unsigned long long)event_data_size);
  241. evt_pat_get_array.patterns[0].patternSize = PAT_SIZE;
  242. evt_pat_get_array.patterns[1].patternSize = PAT_SIZE;
  243. evt_pat_get_array.patterns[2].patternSize = PAT_SIZE;
  244. evt_pat_get_array.patterns[3].patternSize = PAT_SIZE;
  245. evt_pat_get_array.patternsNumber = 4;
  246. result = saEvtEventAttributesGet(event_handle,
  247. &evt_pat_get_array, /* patterns */
  248. &priority, /* priority */
  249. &retention_time, /* retention time */
  250. &publisher_name, /* publisher name */
  251. &publish_time, /* publish time */
  252. &event_id /* event_id */
  253. );
  254. if (result != SA_AIS_OK) {
  255. get_sa_error(result, result_buf, result_buf_len);
  256. dprintf("event get attr result(2): %s\n", result_buf);
  257. goto evt_free;
  258. }
  259. if (!quiet) {
  260. dprintf("pattern array count: %llu\n",
  261. (unsigned long long)evt_pat_get_array.patternsNumber);
  262. for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
  263. dprintf( "pattern %d =\"%s\"\n", i,
  264. evt_pat_get_array.patterns[i].pattern);
  265. }
  266. dprintf("priority: 0x%x\n", priority);
  267. dprintf("retention: 0x%llx\n", (unsigned long long)retention_time);
  268. dprintf("publisher name content: \"%s\"\n",
  269. publisher_name.value);
  270. }
  271. if (event_id == SA_EVT_EVENTID_LOST) {
  272. dprintf("*** Events have been dropped at %s",
  273. ais_time_str(publish_time));
  274. if ((evt_pat_get_array.patternsNumber == 0)||
  275. (strcmp((char *)evt_pat_get_array.patterns[0].pattern, SA_EVT_LOST_EVENT) != 0)) {
  276. dprintf("*** Received SA_EVT_EVENTID_LOST but pattern is wrong: %s\n",
  277. evt_pat_get_array.patterns[0].pattern);
  278. }
  279. }
  280. if (quiet < 2) {
  281. dprintf("event id: 0x%016llx\n", (unsigned long long)event_id);
  282. }
  283. if (quiet == 2) {
  284. if ((++evt_count % EVT_FREQ) == 0) {
  285. fprintf(stderr, ".");
  286. did_dot = 1;
  287. }
  288. }
  289. if (event_id == SA_EVT_EVENTID_LOST) {
  290. goto evt_free;
  291. }
  292. #ifdef TEST_EVENT_ORDER
  293. for (idx = 0; idx < MAX_NODES; idx++) {
  294. if (last_event_id[idx] == 0) {
  295. last_event_id[idx] = event_id;
  296. break;
  297. } else {
  298. if ((last_event_id[idx] >> 32) == (event_id >> 32)) {
  299. last_event_id[idx]++;
  300. if (last_event_id[idx] != event_id) {
  301. dprintf("*** expected %016llx got %016llx event_id\n",
  302. (unsigned long long)last_event_id[idx],
  303. (unsigned long long)event_id);
  304. last_event_id[idx] = event_id;
  305. }
  306. break;
  307. }
  308. }
  309. }
  310. if (idx == MAX_NODES) {
  311. dprintf("*** Too many nodes in cluster\n");
  312. exit(1);
  313. }
  314. #endif
  315. if (event_data_size != user_data_size) {
  316. dprintf("unexpected data size: e=%d, a=%llu\n",
  317. user_data_size, (unsigned long long)event_data_size);
  318. goto evt_free;
  319. }
  320. received_size = user_data_size;
  321. result = saEvtEventDataGet(event_handle, event_data,
  322. &received_size);
  323. if (result != SA_AIS_OK) {
  324. get_sa_error(result, result_buf, result_buf_len);
  325. dprintf("event get data result: %s\n", result_buf);
  326. goto evt_free;
  327. }
  328. if (received_size != event_data_size) {
  329. dprintf("event data mismatch e=%llu, a=%llu\n",
  330. (unsigned long long)event_data_size,
  331. (unsigned long long)received_size);
  332. goto evt_free;
  333. }
  334. if (memcmp(user_data, event_data, user_data_size) != 0 ) {
  335. dprintf("event data doesn't match specified file data\n");
  336. goto evt_free;
  337. }
  338. if (!quiet) {
  339. dprintf("Received %d bytes of data OK\n",
  340. user_data_size);
  341. }
  342. evt_free:
  343. result = saEvtEventFree(event_handle);
  344. if (!quiet) {
  345. get_sa_error(result, result_buf, result_buf_len);
  346. dprintf("event free result: %s\n", result_buf);
  347. }
  348. }
  349. static int err_wait_time = -1;
  350. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  351. static struct sched_param sched_param = {
  352. sched_priority: 1
  353. };
  354. #endif
  355. int main (int argc, char **argv)
  356. {
  357. static const char opts[] = "c:s:n:qu:f:";
  358. int option;
  359. char *p;
  360. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  361. sched_setscheduler (0, SCHED_RR, &sched_param);
  362. #endif
  363. while (1) {
  364. option = getopt(argc, argv, opts);
  365. if (option == -1)
  366. break;
  367. switch (option) {
  368. case 'u': {
  369. int fd;
  370. int sz;
  371. strcpy(user_data_file, optarg);
  372. fd = open(user_data_file, O_RDONLY);
  373. if (fd < 0) {
  374. printf("Can't open user data file %s\n",
  375. user_data_file);
  376. exit(1);
  377. }
  378. sz = read(fd, user_data, 65536);
  379. if (sz < 0) {
  380. perror("subscription\n");
  381. exit(1);
  382. }
  383. close(fd);
  384. user_data_size = sz;
  385. break;
  386. }
  387. case 'q':
  388. quiet++;
  389. break;
  390. case 'c':
  391. strcpy(channel, optarg);
  392. break;
  393. case 'f':
  394. err_wait_time =
  395. (unsigned int)strtoul(optarg, NULL, 0);
  396. break;
  397. case 'n':
  398. strcpy(pubname, optarg);
  399. break;
  400. case 's':
  401. p = strsep(&optarg, ",");
  402. subscription_id[sub_next] =
  403. (unsigned int)strtoul(p, NULL, 0);
  404. p = strsep(&optarg, ",");
  405. filters[sub_next].filter.pattern = malloc(strlen(p));
  406. strcpy((char *)filters[sub_next].filter.pattern, p);
  407. filters[sub_next].filter.patternSize = strlen(p);
  408. p = strsep(&optarg, ",");
  409. filters[sub_next++].filterType = strtoul(p,0, 0);
  410. break;
  411. default:
  412. printf("invalid arg: \"%s\"\n", optarg);
  413. return 1;
  414. }
  415. }
  416. do {
  417. if (test_subscription() != SA_AIS_OK) {
  418. if (err_wait_time > 0) {
  419. sleep(err_wait_time);
  420. } else {
  421. return 1;
  422. }
  423. }
  424. } while (err_wait_time > 0);
  425. return 0;
  426. }