testevt.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  1. /*
  2. * Copyright (c) 2004 Mark Haverkamp
  3. * Copyright (c) 2004 Open Source Development Lab
  4. *
  5. * All rights reserved.
  6. *
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the Open Source Development Lab nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /*
  34. * Test program for event service
  35. *
  36. * test_initialize ();
  37. * Version check in saEvtInitialze()
  38. *
  39. * test_channel();
  40. * Test basic channel operations. Open/close/subscribe/unsubscribe
  41. *
  42. * test_event();
  43. * Event operations: allocation, free, setting and getting
  44. * attributes, publishing.
  45. *
  46. * test_multi_channel1();
  47. * test_multi_channel1();
  48. * test_multi_channel1();
  49. * Test events on multiple channels and multiple subscriptions.
  50. *
  51. * test_retention();
  52. * Test event retention times.
  53. */
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <unistd.h>
  58. #include <sys/poll.h>
  59. #include <malloc.h>
  60. #include "ais_types.h"
  61. #include "saEvt.h"
  62. extern int get_sa_error(SaAisErrorT, char *, int);
  63. char result_buf[256];
  64. int result_buf_len = sizeof(result_buf);
  65. void testresult (SaAisErrorT result, SaAisErrorT expected, int test_no)
  66. {
  67. if (result == expected) {
  68. printf ("Test %d passed\n", test_no);
  69. } else {
  70. get_sa_error(result, result_buf, result_buf_len);
  71. printf ("ERROR: Test %d FAILED (expected %d got %s)\n",
  72. test_no, expected, result_buf);
  73. }
  74. }
  75. SaVersionT version1 = { 'B', 0x01, 0x01 };
  76. SaVersionT version2 = { 'b', 0x01, 0x01 };
  77. SaVersionT version3 = { 'c', 0x01, 0x01 };
  78. SaVersionT version4 = { 'b', 0x02, 0x01 };
  79. SaVersionT version5 = { 'b', 0xff, 0x01 };
  80. SaVersionT version6 = { 'b', 0x01, 0xff };
  81. SaVersionT version7 = { 'B', 0xff, 0xff };
  82. SaVersionT version8 = { 'C', 0xff, 0xff };
  83. struct version_test {
  84. SaVersionT *version;
  85. SaAisErrorT result;
  86. };
  87. struct version_test versions[] = {
  88. { &version1, SA_AIS_OK },
  89. { &version2, SA_AIS_OK },
  90. { &version3, SA_AIS_ERR_VERSION },
  91. { &version4, SA_AIS_ERR_VERSION},
  92. { &version8, SA_AIS_ERR_VERSION},
  93. { 0, SA_AIS_ERR_VERSION}
  94. };
  95. int version_size = sizeof(versions) / sizeof(struct version_test);
  96. void event_callback( SaEvtSubscriptionIdT subscriptionId,
  97. const SaEvtEventHandleT eventHandle,
  98. const SaSizeT eventDataSize);
  99. SaEvtCallbacksT callbacks = {
  100. 0,
  101. event_callback
  102. };
  103. char channel[256] = "TESTEVT_CHANNEL";
  104. SaEvtSubscriptionIdT subscription_id = 0xabcdef;
  105. unsigned long long test_ret_time = 30000000000ULL; /* 30 seconds */
  106. /*
  107. * event data
  108. */
  109. long *exp_data;
  110. #define DATA_SIZE 2048 /* default data size */
  111. #define LCOUNT DATA_SIZE/sizeof(long)
  112. /*
  113. * Test saEvtInitialize and version checking.
  114. */
  115. void test_initialize (void) {
  116. int result;
  117. SaEvtHandleT handle;
  118. int i;
  119. /*
  120. * version check tests
  121. */
  122. printf("Test lib version check on initlialize\n");
  123. for (i=0; i < version_size; i++) {
  124. result = saEvtInitialize (&handle, 0, versions[i].version);
  125. testresult (result, versions[i].result, i);
  126. if (result == SA_AIS_OK) {
  127. saEvtFinalize(handle);
  128. }
  129. }
  130. }
  131. #define patt1 "Filter pattern 1"
  132. #define patt1_size sizeof(patt1)
  133. #define patt2 "Filter pattern 2"
  134. #define patt2_size sizeof(patt2)
  135. #define patt3 "Filter pattern 3"
  136. #define patt3_size sizeof(patt3)
  137. #define patt4 "Filter pattern 4"
  138. #define patt4_size sizeof(patt4)
  139. SaEvtEventFilterT filters[] = {
  140. {SA_EVT_PREFIX_FILTER, {patt1, patt1_size}},
  141. {SA_EVT_SUFFIX_FILTER, {patt2, patt2_size}},
  142. {SA_EVT_EXACT_FILTER, {patt3, patt3_size}},
  143. {SA_EVT_PASS_ALL_FILTER, {patt4, patt4_size}}
  144. };
  145. SaEvtEventFilterArrayT subscribe_filters = {
  146. filters,
  147. sizeof(filters)/sizeof(SaEvtEventFilterT)
  148. };
  149. /*
  150. * Test channel operations.
  151. * 1. Open a channel.
  152. * 2. Close a channel.
  153. * 3. Channel subscription.
  154. * 4. Channel unsubscribe
  155. * 5. unsubscribe/finalize with no channel close.
  156. * 6. Multiple subscriptions.
  157. * 7. Duplicate subscription ID.
  158. * 8. unsubscribe non-exsistent subscription ID.
  159. *
  160. */
  161. void
  162. test_channel()
  163. {
  164. SaEvtHandleT handle;
  165. SaEvtChannelHandleT channel_handle;
  166. SaEvtChannelOpenFlagsT flags;
  167. SaNameT channel_name;
  168. int result;
  169. flags = SA_EVT_CHANNEL_PUBLISHER |
  170. SA_EVT_CHANNEL_SUBSCRIBER |
  171. SA_EVT_CHANNEL_CREATE;
  172. strcpy(channel_name.value, channel);
  173. channel_name.length = strlen(channel);
  174. /*
  175. * Channel open/close test
  176. */
  177. printf("Test Channel operations:\n");
  178. printf(" Channel open:\n");
  179. result = saEvtInitialize (&handle, &callbacks, versions[0].version);
  180. if (result != SA_AIS_OK) {
  181. get_sa_error(result, result_buf, result_buf_len);
  182. printf("ERROR: Event Initialize result: %s\n", result_buf);
  183. return;
  184. }
  185. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  186. &channel_handle);
  187. if (result != SA_AIS_OK) {
  188. get_sa_error(result, result_buf, result_buf_len);
  189. printf("ERROR: channel open result: %s\n", result_buf);
  190. result = saEvtFinalize(handle);
  191. if (result != SA_AIS_OK) {
  192. get_sa_error(result, result_buf, result_buf_len);
  193. printf("ERROR: Event Finalize result: %s\n", result_buf);
  194. }
  195. return;
  196. }
  197. printf(" Channel close:\n");
  198. result = saEvtChannelClose(channel_handle);
  199. if (result != SA_AIS_OK) {
  200. get_sa_error(result, result_buf, result_buf_len);
  201. printf("ERROR: channel close result: %s\n", result_buf);
  202. return;
  203. }
  204. result = saEvtFinalize(handle);
  205. if (result != SA_AIS_OK) {
  206. get_sa_error(result, result_buf, result_buf_len);
  207. printf("ERROR: Event Finalize result: %s\n", result_buf);
  208. return;
  209. }
  210. /*
  211. * Test channel subscribe
  212. */
  213. printf(" Channel subscribe:\n");
  214. result = saEvtInitialize (&handle, &callbacks, versions[0].version);
  215. if (result != SA_AIS_OK) {
  216. get_sa_error(result, result_buf, result_buf_len);
  217. printf("ERROR: Event Initialize result: %s\n", result_buf);
  218. return;
  219. }
  220. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  221. &channel_handle);
  222. if (result != SA_AIS_OK) {
  223. get_sa_error(result, result_buf, result_buf_len);
  224. printf("ERROR: channel open result: %s\n", result_buf);
  225. result = saEvtFinalize(handle);
  226. if (result != SA_AIS_OK) {
  227. get_sa_error(result, result_buf, result_buf_len);
  228. printf("ERROR: Finalize result: %s\n", result_buf);
  229. }
  230. return;
  231. }
  232. result = saEvtEventSubscribe(channel_handle,
  233. &subscribe_filters,
  234. subscription_id);
  235. if (result != SA_AIS_OK) {
  236. get_sa_error(result, result_buf, result_buf_len);
  237. printf("ERROR: event subscribe result: %s\n", result_buf);
  238. result = saEvtChannelClose(channel_handle);
  239. if (result != SA_AIS_OK) {
  240. get_sa_error(result, result_buf, result_buf_len);
  241. printf("ERROR: Channel close result: %s\n", result_buf);
  242. }
  243. result = saEvtFinalize(handle);
  244. if (result != SA_AIS_OK) {
  245. get_sa_error(result, result_buf, result_buf_len);
  246. printf("ERROR: Finalize result: %s\n", result_buf);
  247. }
  248. return;
  249. }
  250. printf(" Channel unsubscribe:\n");
  251. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  252. if (result != SA_AIS_OK) {
  253. get_sa_error(result, result_buf, result_buf_len);
  254. printf("ERROR: event unsubscribe result: %s\n", result_buf);
  255. result = saEvtChannelClose(channel_handle);
  256. if (result != SA_AIS_OK) {
  257. get_sa_error(result, result_buf, result_buf_len);
  258. printf("ERROR: Channel close result: %s\n", result_buf);
  259. }
  260. result = saEvtFinalize(handle);
  261. if (result != SA_AIS_OK) {
  262. get_sa_error(result, result_buf, result_buf_len);
  263. printf("ERROR: Finalize result: %s\n", result_buf);
  264. }
  265. return;
  266. }
  267. result = saEvtChannelClose(channel_handle);
  268. if (result != SA_AIS_OK) {
  269. get_sa_error(result, result_buf, result_buf_len);
  270. printf("ERROR: Channel close result: %s\n", result_buf);
  271. }
  272. result = saEvtFinalize(handle);
  273. if (result != SA_AIS_OK) {
  274. get_sa_error(result, result_buf, result_buf_len);
  275. printf("ERROR: Finalize result: %s\n", result_buf);
  276. }
  277. /*
  278. * Test channel subscribe with no close
  279. */
  280. printf(" Channel subscribe with no close at end:\n");
  281. result = saEvtInitialize (&handle, &callbacks, versions[0].version);
  282. if (result != SA_AIS_OK) {
  283. get_sa_error(result, result_buf, result_buf_len);
  284. printf("ERROR: Event Initialize result: %s\n", result_buf);
  285. return;
  286. }
  287. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  288. &channel_handle);
  289. if (result != SA_AIS_OK) {
  290. get_sa_error(result, result_buf, result_buf_len);
  291. printf("ERROR: channel open result: %s\n", result_buf);
  292. result = saEvtFinalize(handle);
  293. return;
  294. }
  295. result = saEvtEventSubscribe(channel_handle,
  296. &subscribe_filters,
  297. subscription_id);
  298. if (result != SA_AIS_OK) {
  299. get_sa_error(result, result_buf, result_buf_len);
  300. printf("ERROR: event subscribe result: %s\n", result_buf);
  301. result = saEvtChannelClose(channel_handle);
  302. }
  303. result = saEvtFinalize(handle);
  304. if (result != SA_AIS_OK) {
  305. get_sa_error(result, result_buf, result_buf_len);
  306. printf("ERROR: Finalize failed\n");
  307. return;
  308. }
  309. /*
  310. * Test multiple subscriptions
  311. */
  312. printf(" Multiple subscriptions\n");
  313. result = saEvtInitialize (&handle, &callbacks, versions[0].version);
  314. if (result != SA_AIS_OK) {
  315. get_sa_error(result, result_buf, result_buf_len);
  316. printf("ERROR: Event Initialize result: %s\n", result_buf);
  317. return;
  318. }
  319. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  320. &channel_handle);
  321. if (result != SA_AIS_OK) {
  322. get_sa_error(result, result_buf, result_buf_len);
  323. printf("ERROR: channel open result: %s\n", result_buf);
  324. result = saEvtFinalize(handle);
  325. if (result != SA_AIS_OK) {
  326. get_sa_error(result, result_buf, result_buf_len);
  327. printf("ERROR: Finalize result: %s\n", result_buf);
  328. }
  329. return;
  330. }
  331. result = saEvtEventSubscribe(channel_handle,
  332. &subscribe_filters,
  333. subscription_id);
  334. if (result != SA_AIS_OK) {
  335. get_sa_error(result, result_buf, result_buf_len);
  336. printf("ERROR: First event subscribe result: %s\n", result_buf);
  337. result = saEvtChannelClose(channel_handle);
  338. if (result != SA_AIS_OK) {
  339. get_sa_error(result, result_buf, result_buf_len);
  340. printf("ERROR: Channel close result: %s\n", result_buf);
  341. }
  342. result = saEvtFinalize(handle);
  343. if (result != SA_AIS_OK) {
  344. get_sa_error(result, result_buf, result_buf_len);
  345. printf("ERROR: Finalize result: %s\n", result_buf);
  346. }
  347. return;
  348. }
  349. result = saEvtEventSubscribe(channel_handle,
  350. &subscribe_filters,
  351. subscription_id+1);
  352. if (result != SA_AIS_OK) {
  353. get_sa_error(result, result_buf, result_buf_len);
  354. printf("ERROR: second event subscribe result: %s\n", result_buf);
  355. result = saEvtChannelClose(channel_handle);
  356. if (result != SA_AIS_OK) {
  357. get_sa_error(result, result_buf, result_buf_len);
  358. printf("ERROR: Channel close result: %s\n", result_buf);
  359. }
  360. result = saEvtFinalize(handle);
  361. if (result != SA_AIS_OK) {
  362. get_sa_error(result, result_buf, result_buf_len);
  363. printf("ERROR: Finalize result: %s\n", result_buf);
  364. }
  365. return;
  366. }
  367. /*
  368. * Test duplicate subscription
  369. */
  370. printf(" Duplicate subscription\n");
  371. result = saEvtEventSubscribe(channel_handle,
  372. &subscribe_filters,
  373. subscription_id);
  374. if (result != SA_AIS_ERR_EXIST) {
  375. get_sa_error(result, result_buf, result_buf_len);
  376. printf("ERROR: First event subscribe result: %s\n", result_buf);
  377. result = saEvtChannelClose(channel_handle);
  378. result = saEvtFinalize(handle);
  379. return;
  380. }
  381. /*
  382. * Test unsubscribe non-existent sub_id
  383. */
  384. printf(" Unsubscribe non-existent sub_id\n");
  385. result = saEvtEventUnsubscribe(channel_handle, subscription_id+2);
  386. if (result != SA_AIS_ERR_INVALID_PARAM) {
  387. get_sa_error(result, result_buf, result_buf_len);
  388. printf("ERROR: event unsubscribe result: %s\n", result_buf);
  389. result = saEvtChannelClose(channel_handle);
  390. if (result != SA_AIS_OK) {
  391. get_sa_error(result, result_buf, result_buf_len);
  392. printf("ERROR: Channel close result: %s\n", result_buf);
  393. }
  394. result = saEvtFinalize(handle);
  395. if (result != SA_AIS_OK) {
  396. get_sa_error(result, result_buf, result_buf_len);
  397. printf("ERROR: Finalize result: %s\n", result_buf);
  398. }
  399. return;
  400. }
  401. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  402. if (result != SA_AIS_OK) {
  403. get_sa_error(result, result_buf, result_buf_len);
  404. printf("ERROR: first event unsubscribe result: %s\n", result_buf);
  405. }
  406. result = saEvtEventUnsubscribe(channel_handle, subscription_id+1);
  407. if (result != SA_AIS_OK) {
  408. get_sa_error(result, result_buf, result_buf_len);
  409. printf("ERROR: second event unsubscribe result: %s\n", result_buf);
  410. }
  411. result = saEvtChannelClose(channel_handle);
  412. if (result != SA_AIS_OK) {
  413. get_sa_error(result, result_buf, result_buf_len);
  414. printf("ERROR: Channel close result: %s\n", result_buf);
  415. }
  416. result = saEvtFinalize(handle);
  417. if (result != SA_AIS_OK) {
  418. get_sa_error(result, result_buf, result_buf_len);
  419. printf("ERROR: Finalize result: %s\n", result_buf);
  420. }
  421. printf("Done\n");
  422. }
  423. SaUint8T pat0[100];
  424. SaUint8T pat1[100];
  425. SaUint8T pat2[100];
  426. SaUint8T pat3[100];
  427. SaUint8T pat4[100];
  428. SaEvtEventPatternT evt_patts[5] = {
  429. {pat0, 100},
  430. {pat1, 100},
  431. {pat2, 100},
  432. {pat3, 100},
  433. {pat4, 100}};
  434. SaEvtEventPatternArrayT evt_pat_get_array = { evt_patts, 0 };
  435. SaEvtEventPatternT patterns[] = {
  436. {patt1, patt1_size},
  437. {patt2, patt2_size},
  438. {patt3, patt3_size},
  439. {patt4, patt4_size}
  440. };
  441. SaNameT test_pub_name = {13, "Test Pub Name"};
  442. #define TEST_PRIORITY 2
  443. SaEvtEventPatternArrayT evt_pat_set_array = {
  444. patterns,
  445. sizeof(patterns)/sizeof(SaEvtEventPatternT)
  446. };
  447. char event_data[1000];
  448. #define EVENT_DATA_SIZE 1000
  449. SaEvtEventIdT event_id = 0;
  450. SaUint8T priority;
  451. SaTimeT retention_time = 0ULL;
  452. SaNameT publisher_name = {0, {0}};
  453. SaSizeT event_data_size;
  454. /*
  455. * Test event operations.
  456. *
  457. * 1. Event allocation
  458. * 2. Get event attributes (no pointers).
  459. * 3. Get event attributes with pointers.
  460. * 4. Set/Get event attributes.
  461. * 5. Get event user data no pointer or count
  462. * 6. Get event user data with pointer and no count
  463. * 7. Get event user data with no pointer with a count.
  464. * 8. Get event user data with pointer and count.
  465. * 9. Get event user data woth a short count.
  466. * 10. Free event.
  467. * 11. Publish with no set patterns.
  468. * 12. Publish with set patterns and event user data.
  469. *
  470. */
  471. void
  472. event_callback(SaEvtSubscriptionIdT my_subscription_id,
  473. const SaEvtEventHandleT event_handle,
  474. const SaSizeT my_event_data_size)
  475. {
  476. SaAisErrorT result;
  477. SaUint8T my_priority;
  478. SaTimeT my_retention_time;
  479. SaNameT my_publisher_name = {0, {0}};
  480. SaTimeT my_publish_time;
  481. SaEvtEventIdT my_event_id;
  482. int i;
  483. long *act_data;
  484. SaSizeT data_size;
  485. printf(" event_callback called\n");
  486. if (my_subscription_id != subscription_id) {
  487. printf("ERROR: sub ID: e=%x, a=%x\n",
  488. subscription_id, my_subscription_id);
  489. }
  490. if (my_event_data_size != event_data_size) {
  491. printf("ERROR: event data size e=%d, a=%d\n",
  492. event_data_size,
  493. my_event_data_size);
  494. }
  495. evt_pat_get_array.patternsNumber = 4;
  496. result = saEvtEventAttributesGet(event_handle,
  497. &evt_pat_get_array, /* patterns */
  498. &my_priority, /* priority */
  499. &my_retention_time, /* retention time */
  500. &my_publisher_name, /* publisher name */
  501. &my_publish_time, /* publish time */
  502. &my_event_id /* event_id */
  503. );
  504. if (result != SA_AIS_OK) {
  505. get_sa_error(result, result_buf, result_buf_len);
  506. printf("ERROR: event get attr result(2): %s\n", result_buf);
  507. goto evt_free;
  508. }
  509. if (my_event_id != event_id) {
  510. printf("ERROR: Call back event ID error: e=%llx, a=%llx\n",
  511. event_id, my_event_id);
  512. }
  513. if (evt_pat_get_array.patternsNumber != 4) {
  514. printf("ERROR: pattern array count not 4: %d\n",
  515. evt_pat_get_array.patternsNumber);
  516. }
  517. for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
  518. if (evt_pat_get_array.patterns[i].patternSize !=
  519. evt_pat_set_array.patterns[i].patternSize) {
  520. printf("ERROR: pattern %d count not equal g=%d, s=%d\n",
  521. i,
  522. evt_pat_get_array.patterns[i].patternSize,
  523. evt_pat_set_array.patterns[i].patternSize);
  524. printf("ERROR: pattern %d content g=\"%s\", s=\"%s\"\n",
  525. i,
  526. evt_pat_get_array.patterns[i].pattern,
  527. evt_pat_set_array.patterns[i].pattern);
  528. } else {
  529. if (memcmp(evt_pat_get_array.patterns[i].pattern,
  530. evt_pat_set_array.patterns[i].pattern,
  531. evt_pat_get_array.patterns[i].patternSize) != 0){
  532. printf(
  533. "ERROR: pattern %d don't match g=\"%s\", s=\"%s\"\n",
  534. i,
  535. evt_pat_get_array.patterns[i].pattern,
  536. evt_pat_set_array.patterns[i].pattern);
  537. }
  538. }
  539. }
  540. if (priority != my_priority) {
  541. printf("ERROR: priority: e=0x%x a=0x%x\n",
  542. priority, my_priority);
  543. }
  544. if (retention_time != my_retention_time) {
  545. printf("ERROR: retention: e=0x%llx a=0x%llx\n",
  546. retention_time, my_retention_time);
  547. }
  548. if (publisher_name.length != my_publisher_name.length) {
  549. printf("ERROR: publisher name length: e=%d, a=%d\n",
  550. publisher_name.length,
  551. my_publisher_name.length);
  552. } else {
  553. if (memcmp(publisher_name.value, my_publisher_name.value,
  554. publisher_name.length) != 0) {
  555. printf("ERROR: publisher name content: e=%s, a=%s\n",
  556. publisher_name.value,
  557. my_publisher_name.value);
  558. }
  559. }
  560. act_data = malloc(my_event_data_size);
  561. memset(act_data, 0, my_event_data_size);
  562. data_size = my_event_data_size;
  563. result = saEvtEventDataGet(event_handle, act_data, &data_size);
  564. if (result != SA_AIS_OK) {
  565. get_sa_error(result, result_buf, result_buf_len);
  566. printf("ERROR: event data get result: %s\n", result_buf);
  567. goto dat_free;
  568. }
  569. if (data_size != event_data_size) {
  570. printf("ERROR: Data size: e=%d a=%d\n",
  571. event_data_size, data_size);
  572. }
  573. for (i = 0; i < (data_size/sizeof(long)); i++) {
  574. if (act_data[i] != exp_data[i]) {
  575. printf("ERROR: Event Data e=%lx a=%lx at index %d\n",
  576. exp_data[i], act_data[i], i);
  577. break;
  578. }
  579. }
  580. dat_free:
  581. free(act_data);
  582. evt_free:
  583. result = saEvtEventFree(event_handle);
  584. if (result != SA_AIS_OK) {
  585. get_sa_error(result, result_buf, result_buf_len);
  586. printf("ERROR: event free result: %s\n", result_buf);
  587. }
  588. }
  589. void
  590. test_event()
  591. {
  592. SaEvtHandleT handle;
  593. SaEvtChannelHandleT channel_handle;
  594. SaEvtEventHandleT event_handle;
  595. SaEvtChannelOpenFlagsT flags;
  596. SaNameT channel_name;
  597. SaTimeT publish_time;
  598. struct pollfd pfd;
  599. int nfd;
  600. int fd;
  601. int timeout = 5000;
  602. int result;
  603. int i;
  604. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  605. SA_EVT_CHANNEL_CREATE;
  606. strcpy(channel_name.value, channel);
  607. channel_name.length = strlen(channel);
  608. printf("Test Event operations:\n");
  609. result = saEvtInitialize (&handle, &callbacks, versions[0].version);
  610. if (result != SA_AIS_OK) {
  611. get_sa_error(result, result_buf, result_buf_len);
  612. printf("ERROR: Event Initialize result: %s\n", result_buf);
  613. return;
  614. }
  615. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  616. &channel_handle);
  617. if (result != SA_AIS_OK) {
  618. get_sa_error(result, result_buf, result_buf_len);
  619. printf("ERROR: channel open result: %s\n", result_buf);
  620. goto evt_fin;
  621. }
  622. /*
  623. * Allocate an event
  624. */
  625. printf(" Event allocation\n");
  626. result = saEvtEventAllocate(channel_handle, &event_handle);
  627. if (result != SA_AIS_OK) {
  628. get_sa_error(result, result_buf, result_buf_len);
  629. printf("ERROR: event Allocate result: %s\n", result_buf);
  630. goto evt_close;
  631. }
  632. printf(" Get event attributes(1)\n");
  633. result = saEvtEventAttributesGet(event_handle,
  634. 0, /* patterns */
  635. 0, /* priority */
  636. 0, /* retention time */
  637. 0, /* publisher name */
  638. 0, /* publish time */
  639. 0 /* event_id */
  640. );
  641. if (result != SA_AIS_OK) {
  642. get_sa_error(result, result_buf, result_buf_len);
  643. printf("ERROR: event get attr result(1): %s\n", result_buf);
  644. goto evt_free;
  645. }
  646. /*
  647. * Get event attributes, this time supply pointers.
  648. * validate the default values.
  649. */
  650. printf(" Get event attributes(2)\n");
  651. evt_pat_get_array.patternsNumber = 4;
  652. result = saEvtEventAttributesGet(event_handle,
  653. &evt_pat_get_array, /* patterns */
  654. &priority, /* priority */
  655. &retention_time, /* retention time */
  656. &publisher_name, /* publisher name */
  657. &publish_time, /* publish time */
  658. &event_id /* event_id */
  659. );
  660. if (result != SA_AIS_OK) {
  661. get_sa_error(result, result_buf, result_buf_len);
  662. printf("ERROR: event get attr result(2): %s\n", result_buf);
  663. goto evt_free;
  664. }
  665. if (evt_pat_get_array.patternsNumber != 0) {
  666. printf("ERROR: pattern array count not zero: %d\n",
  667. evt_pat_get_array.patternsNumber);
  668. }
  669. if (priority != SA_EVT_LOWEST_PRIORITY) {
  670. printf("ERROR: priority not lowest: 0x%x\n", priority);
  671. }
  672. if (retention_time != 0) {
  673. printf("ERROR: retention time not zero: %0llx\n", retention_time);
  674. }
  675. if (publisher_name.length != 0) {
  676. printf("ERROR: publisher name not null: %s\n", publisher_name.value);
  677. }
  678. if (event_id != 0) {
  679. printf("ERROR: event id not zero: 0x%llx\n", event_id);
  680. }
  681. /*
  682. * Set some attributes, then read them back
  683. */
  684. printf(" Set/get event attributes(1)\n");
  685. result = saEvtEventAttributesSet(event_handle,
  686. &evt_pat_set_array,
  687. TEST_PRIORITY,
  688. test_ret_time,
  689. &test_pub_name);
  690. if (result != SA_AIS_OK) {
  691. get_sa_error(result, result_buf, result_buf_len);
  692. printf("ERROR: event set attr result(1): %s\n", result_buf);
  693. goto evt_free;
  694. }
  695. evt_pat_get_array.patternsNumber = 4;
  696. result = saEvtEventAttributesGet(event_handle,
  697. &evt_pat_get_array, /* patterns */
  698. &priority, /* priority */
  699. &retention_time, /* retention time */
  700. &publisher_name, /* publisher name */
  701. &publish_time, /* publish time */
  702. &event_id /* event_id */
  703. );
  704. if (result != SA_AIS_OK) {
  705. get_sa_error(result, result_buf, result_buf_len);
  706. printf("ERROR: event get attr result(2): %s\n", result_buf);
  707. goto evt_free;
  708. }
  709. if (evt_pat_get_array.patternsNumber != 4) {
  710. printf("ERROR: pattern array count not 4: %d\n",
  711. evt_pat_get_array.patternsNumber);
  712. }
  713. for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
  714. if (evt_pat_get_array.patterns[i].patternSize !=
  715. evt_pat_set_array.patterns[i].patternSize) {
  716. printf("ERROR: pattern %d count not equal g=%d, s=%d\n",
  717. i,
  718. evt_pat_get_array.patterns[i].patternSize,
  719. evt_pat_set_array.patterns[i].patternSize);
  720. printf("ERROR: pattern %d content g=\"%s\", s=\"%s\"\n",
  721. i,
  722. evt_pat_get_array.patterns[i].pattern,
  723. evt_pat_set_array.patterns[i].pattern);
  724. } else {
  725. if (memcmp(evt_pat_get_array.patterns[i].pattern,
  726. evt_pat_set_array.patterns[i].pattern,
  727. evt_pat_get_array.patterns[i].patternSize) != 0){
  728. printf(
  729. "ERROR: pattern %d don't match g=\"%s\", s=\"%s\"\n",
  730. i,
  731. evt_pat_get_array.patterns[i].pattern,
  732. evt_pat_set_array.patterns[i].pattern);
  733. }
  734. }
  735. }
  736. if (priority != TEST_PRIORITY) {
  737. printf("ERROR: priority: e=0x%x a=0x%x\n",
  738. TEST_PRIORITY, priority);
  739. }
  740. if (retention_time != test_ret_time) {
  741. printf("ERROR: retention: e=0x%llx a=0x%llx\n",
  742. test_ret_time, retention_time);
  743. }
  744. if (publisher_name.length != test_pub_name.length) {
  745. printf("ERROR: publisher name length: e=%d, a=%d\n",
  746. test_pub_name.length,
  747. publisher_name.length);
  748. } else {
  749. if (memcmp(publisher_name.value, test_pub_name.value,
  750. publisher_name.length) != 0) {
  751. printf("ERROR: publisher name content: e=%s, a=%s\n",
  752. test_pub_name.value,
  753. publisher_name.value);
  754. }
  755. }
  756. if (event_id != 0) {
  757. printf("ERROR: event id not zero: 0x%llx\n", event_id);
  758. }
  759. /*
  760. * event user data
  761. */
  762. printf(" Get event data(1)\n");
  763. result = saEvtEventDataGet(event_handle, 0, 0);
  764. if (result != SA_AIS_OK) {
  765. get_sa_error(result, result_buf, result_buf_len);
  766. printf("ERROR: Get event data(1) result: %s\n", result_buf);
  767. }
  768. printf(" Get event data(2)\n");
  769. result = saEvtEventDataGet(event_handle, event_data, 0);
  770. if (result != SA_AIS_OK) {
  771. get_sa_error(result, result_buf, result_buf_len);
  772. printf("ERROR: Get event data(2) result: %s\n", result_buf);
  773. }
  774. printf(" Get event data(3)\n");
  775. event_data_size = EVENT_DATA_SIZE;
  776. result = saEvtEventDataGet(event_handle, 0, &event_data_size);
  777. if (result != SA_AIS_OK) {
  778. get_sa_error(result, result_buf, result_buf_len);
  779. printf("ERROR: Get event data(3) result: %s\n", result_buf);
  780. }
  781. printf(" Get event data(4)\n");
  782. event_data_size = EVENT_DATA_SIZE;
  783. result = saEvtEventDataGet(event_handle, event_data, &event_data_size);
  784. if (result != SA_AIS_OK) {
  785. get_sa_error(result, result_buf, result_buf_len);
  786. printf("ERROR: Get event data(4) result: %s\n", result_buf);
  787. }
  788. printf(" Get event data(5)\n");
  789. event_data_size = 1;
  790. result = saEvtEventDataGet(event_handle, event_data, &event_data_size);
  791. if (result != SA_AIS_OK) {
  792. get_sa_error(result, result_buf, result_buf_len);
  793. printf("ERROR: Get event data(5) result: %s\n", result_buf);
  794. }
  795. printf(" Free event(1)\n");
  796. result = saEvtEventFree(event_handle);
  797. if (result != SA_AIS_OK) {
  798. get_sa_error(result, result_buf, result_buf_len);
  799. printf("ERROR: event free result: %s\n", result_buf);
  800. }
  801. /*
  802. * Test publication.
  803. */
  804. printf(" Publish with no patterns set\n");
  805. result = saEvtEventAllocate(channel_handle, &event_handle);
  806. if (result != SA_AIS_OK) {
  807. get_sa_error(result, result_buf, result_buf_len);
  808. printf("ERROR: event Allocate result: %s\n", result_buf);
  809. goto evt_close;
  810. }
  811. result = saEvtEventPublish(event_handle, 0, 0, &event_id);
  812. if (result != SA_AIS_ERR_INVALID_PARAM) {
  813. get_sa_error(result, result_buf, result_buf_len);
  814. printf("ERROR: event Publish result(1): %s\n", result_buf);
  815. goto evt_close;
  816. }
  817. /*
  818. * Publish with pattens
  819. */
  820. printf(" Publish with patterns set\n");
  821. result = saEvtEventSubscribe(channel_handle,
  822. &subscribe_filters,
  823. subscription_id);
  824. if (result != SA_AIS_OK) {
  825. get_sa_error(result, result_buf, result_buf_len);
  826. printf("ERROR: event subscribe result: %s\n", result_buf);
  827. result = saEvtChannelClose(channel_handle);
  828. if (result != SA_AIS_OK) {
  829. get_sa_error(result, result_buf, result_buf_len);
  830. printf("ERROR: Channel close result: %s\n", result_buf);
  831. }
  832. result = saEvtFinalize(handle);
  833. if (result != SA_AIS_OK) {
  834. get_sa_error(result, result_buf, result_buf_len);
  835. printf("ERROR: Finalize result: %s\n", result_buf);
  836. }
  837. return;
  838. }
  839. retention_time = 0ULL;
  840. result = saEvtEventAttributesSet(event_handle,
  841. &evt_pat_set_array,
  842. TEST_PRIORITY,
  843. retention_time,
  844. &test_pub_name);
  845. if (result != SA_AIS_OK) {
  846. get_sa_error(result, result_buf, result_buf_len);
  847. printf("ERROR: event set attr result(2): %s\n", result_buf);
  848. goto evt_free;
  849. }
  850. /*
  851. * Generate event data
  852. */
  853. exp_data = malloc(DATA_SIZE);
  854. for (i = 0; i < LCOUNT; i++) {
  855. exp_data[i] = lrand48();
  856. }
  857. event_data_size = DATA_SIZE;
  858. /*
  859. * Send it
  860. */
  861. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  862. &event_id);
  863. if (result != SA_AIS_OK) {
  864. get_sa_error(result, result_buf, result_buf_len);
  865. printf("ERROR: event Publish result(2): %s\n", result_buf);
  866. goto evt_close;
  867. }
  868. /*
  869. * See if we got the event
  870. */
  871. result = saEvtSelectionObjectGet(handle, &fd);
  872. if (result != SA_AIS_OK) {
  873. get_sa_error(result, result_buf, result_buf_len);
  874. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  875. /* error */
  876. return;
  877. }
  878. pfd.fd = fd;
  879. pfd.events = POLLIN;
  880. nfd = poll(&pfd, 1, timeout);
  881. if (nfd <= 0) {
  882. printf("ERROR: poll fds %d\n", nfd);
  883. if (nfd < 0) {
  884. perror("ERROR: poll error");
  885. }
  886. /* Error */
  887. return;
  888. }
  889. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  890. if (result != SA_AIS_OK) {
  891. get_sa_error(result, result_buf, result_buf_len);
  892. printf("ERROR: saEvtDispatch %s\n", result_buf);
  893. /* error */
  894. return;
  895. }
  896. /*
  897. * Test cleanup
  898. */
  899. evt_free:
  900. result = saEvtEventFree(event_handle);
  901. if (result != SA_AIS_OK) {
  902. get_sa_error(result, result_buf, result_buf_len);
  903. printf("ERROR: event free result: %s\n", result_buf);
  904. }
  905. evt_close:
  906. result = saEvtChannelClose(channel_handle);
  907. if (result != SA_AIS_OK) {
  908. get_sa_error(result, result_buf, result_buf_len);
  909. printf("ERROR: channel close result: %s\n", result_buf);
  910. }
  911. evt_fin:
  912. result = saEvtFinalize(handle);
  913. if (result != SA_AIS_OK) {
  914. get_sa_error(result, result_buf, result_buf_len);
  915. printf("ERROR: Event Finalize result: %s\n", result_buf);
  916. }
  917. printf("Done\n");
  918. }
  919. SaEvtEventIdT event_id1;
  920. SaEvtEventIdT event_id2;
  921. SaEvtEventIdT event_id3;
  922. SaEvtSubscriptionIdT sub1 = 0x101010;
  923. SaEvtSubscriptionIdT sub2 = 0x202020;
  924. static int call_count = 0;
  925. /*
  926. * Handle call back for multi-test1
  927. * Checks event ID with subscription ID to make sure that we
  928. * received an event on the correct subscription.
  929. */
  930. void
  931. multi_test_callback1(SaEvtSubscriptionIdT my_subscription_id,
  932. const SaEvtEventHandleT event_handle,
  933. const SaSizeT my_event_data_size)
  934. {
  935. SaAisErrorT result;
  936. SaUint8T my_priority;
  937. SaTimeT my_retention_time;
  938. SaNameT my_publisher_name = {0, {0}};
  939. SaTimeT my_publish_time;
  940. SaEvtEventIdT my_event_id;
  941. SaEvtSubscriptionIdT exp_sub_id;
  942. printf(" multi_test_callback1 called(%d)\n", ++call_count);
  943. evt_pat_get_array.patternsNumber = 4;
  944. result = saEvtEventAttributesGet(event_handle,
  945. &evt_pat_get_array, /* patterns */
  946. &my_priority, /* priority */
  947. &my_retention_time, /* retention time */
  948. &my_publisher_name, /* publisher name */
  949. &my_publish_time, /* publish time */
  950. &my_event_id /* event_id */
  951. );
  952. if (result != SA_AIS_OK) {
  953. get_sa_error(result, result_buf, result_buf_len);
  954. printf("ERROR: event get attr result: %s\n", result_buf);
  955. goto evt_free;
  956. }
  957. if (my_event_id == event_id1) {
  958. exp_sub_id = sub1;
  959. } else if (my_event_id == event_id2) {
  960. exp_sub_id = sub2;
  961. } else if (my_event_id == event_id3) {
  962. printf("ERROR: Received event 3 but not subscribed\n");
  963. goto evt_free;
  964. } else {
  965. printf("ERROR: Received event %llx but not sent\n", my_event_id);
  966. goto evt_free;
  967. }
  968. if (my_subscription_id != exp_sub_id) {
  969. printf("ERROR: sub ID: e=%x, a=%x\n",
  970. exp_sub_id, my_subscription_id);
  971. goto evt_free;
  972. }
  973. if (evt_pat_get_array.patternsNumber != 1) {
  974. printf("ERROR: pattern array count not 1: %d\n",
  975. evt_pat_get_array.patternsNumber);
  976. }
  977. evt_free:
  978. result = saEvtEventFree(event_handle);
  979. if (result != SA_AIS_OK) {
  980. get_sa_error(result, result_buf, result_buf_len);
  981. printf("ERROR: event free result: %s\n", result_buf);
  982. }
  983. }
  984. /*
  985. * Test multiple channel operations
  986. * 1. Test multiple subscriptions on a single channel and receiving
  987. * events.
  988. * 2. Test multiple openings of a single channel and receving events.
  989. * 3. Test opening of multiple channels and receiving events
  990. */
  991. void
  992. test_multi_channel1()
  993. {
  994. SaEvtEventFilterT filt1[1] = {
  995. {SA_EVT_EXACT_FILTER, {"ChanPat1", 8}},
  996. };
  997. SaEvtEventFilterT filt2[1] = {
  998. {SA_EVT_EXACT_FILTER, {"ChanPat2", 8}},
  999. };
  1000. SaEvtEventFilterArrayT sub_filt = {
  1001. NULL, 1
  1002. };
  1003. SaEvtEventPatternT pat1 = {"ChanPat1", 8};
  1004. SaEvtEventPatternT pat2 = {"ChanPat2", 8};
  1005. SaEvtEventPatternT pat3 = {"ChanPat3", 8};
  1006. SaEvtEventPatternArrayT evt_pat = {
  1007. NULL, 1
  1008. };
  1009. SaEvtHandleT handle;
  1010. SaEvtChannelHandleT channel_handle;
  1011. SaEvtEventHandleT event_handle;
  1012. SaEvtChannelOpenFlagsT flags;
  1013. SaNameT channel_name;
  1014. SaEvtCallbacksT multi_callbacks = {
  1015. 0,
  1016. multi_test_callback1
  1017. };
  1018. struct pollfd pfd;
  1019. int nfd;
  1020. int fd;
  1021. int timeout = 5000;
  1022. int result;
  1023. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1024. SA_EVT_CHANNEL_CREATE;
  1025. strcpy(channel_name.value, channel);
  1026. channel_name.length = strlen(channel);
  1027. printf("Test multiple operations:\n");
  1028. result = saEvtInitialize (&handle, &multi_callbacks, versions[0].version);
  1029. if (result != SA_AIS_OK) {
  1030. get_sa_error(result, result_buf, result_buf_len);
  1031. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1032. return;
  1033. }
  1034. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1035. &channel_handle);
  1036. if (result != SA_AIS_OK) {
  1037. get_sa_error(result, result_buf, result_buf_len);
  1038. printf("ERROR: channel open result: %s\n", result_buf);
  1039. goto evt_fin;
  1040. }
  1041. /*
  1042. * Allocate an event
  1043. */
  1044. result = saEvtEventAllocate(channel_handle, &event_handle);
  1045. if (result != SA_AIS_OK) {
  1046. get_sa_error(result, result_buf, result_buf_len);
  1047. printf("ERROR: event Allocate result: %s\n", result_buf);
  1048. goto evt_close;
  1049. }
  1050. /*
  1051. * 1. Test multiple subscriptions on a single channel and receiving
  1052. * events.
  1053. *
  1054. * Subscribe twice with two different filters. Then send three events.
  1055. * One will match the first filter, the second will match the second
  1056. * filter, the third will match none. We will validate that we receive
  1057. * two events and that the subscription IDs match what we expect for the
  1058. * given pattern.
  1059. */
  1060. sub_filt.filters = filt1;
  1061. result = saEvtEventSubscribe(channel_handle,
  1062. &sub_filt,
  1063. sub1);
  1064. if (result != SA_AIS_OK) {
  1065. get_sa_error(result, result_buf, result_buf_len);
  1066. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1067. goto evt_free;
  1068. }
  1069. sub_filt.filters = filt2;
  1070. result = saEvtEventSubscribe(channel_handle,
  1071. &sub_filt,
  1072. sub2);
  1073. if (result != SA_AIS_OK) {
  1074. get_sa_error(result, result_buf, result_buf_len);
  1075. printf("ERROR: event subscribe(2) result: %s\n", result_buf);
  1076. goto evt_free;
  1077. }
  1078. retention_time = 0ULL;
  1079. evt_pat.patterns = &pat1;
  1080. result = saEvtEventAttributesSet(event_handle,
  1081. &evt_pat,
  1082. TEST_PRIORITY,
  1083. retention_time,
  1084. &test_pub_name);
  1085. if (result != SA_AIS_OK) {
  1086. get_sa_error(result, result_buf, result_buf_len);
  1087. printf("ERROR: event set attr result(1): %s\n", result_buf);
  1088. goto evt_free;
  1089. }
  1090. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1091. &event_id1);
  1092. if (result != SA_AIS_OK) {
  1093. get_sa_error(result, result_buf, result_buf_len);
  1094. printf("ERROR: event Publish result:(1) %s\n", result_buf);
  1095. goto evt_close;
  1096. }
  1097. evt_pat.patterns = &pat2;
  1098. result = saEvtEventAttributesSet(event_handle,
  1099. &evt_pat,
  1100. TEST_PRIORITY,
  1101. retention_time,
  1102. &test_pub_name);
  1103. if (result != SA_AIS_OK) {
  1104. get_sa_error(result, result_buf, result_buf_len);
  1105. printf("ERROR: event set attr result(2): %s\n", result_buf);
  1106. goto evt_free;
  1107. }
  1108. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1109. &event_id2);
  1110. if (result != SA_AIS_OK) {
  1111. get_sa_error(result, result_buf, result_buf_len);
  1112. printf("ERROR: event Publish result:(2) %s\n", result_buf);
  1113. goto evt_close;
  1114. }
  1115. evt_pat.patterns = &pat3;
  1116. result = saEvtEventAttributesSet(event_handle,
  1117. &evt_pat,
  1118. TEST_PRIORITY,
  1119. retention_time,
  1120. &test_pub_name);
  1121. if (result != SA_AIS_OK) {
  1122. get_sa_error(result, result_buf, result_buf_len);
  1123. printf("ERROR: event set attr result(3): %s\n", result_buf);
  1124. goto evt_free;
  1125. }
  1126. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1127. &event_id3);
  1128. if (result != SA_AIS_OK) {
  1129. get_sa_error(result, result_buf, result_buf_len);
  1130. printf("ERROR: event Publish result:(3) %s\n", result_buf);
  1131. goto evt_close;
  1132. }
  1133. /*
  1134. * See if we got the event
  1135. */
  1136. result = saEvtSelectionObjectGet(handle, &fd);
  1137. if (result != SA_AIS_OK) {
  1138. get_sa_error(result, result_buf, result_buf_len);
  1139. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1140. /* error */
  1141. return;
  1142. }
  1143. while(1) {
  1144. pfd.fd = fd;
  1145. pfd.events = POLLIN;
  1146. nfd = poll(&pfd, 1, timeout);
  1147. if (nfd == 0) {
  1148. break;
  1149. } else if (nfd < 0) {
  1150. perror("ERROR: poll error");
  1151. break;
  1152. }
  1153. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  1154. if (result != SA_AIS_OK) {
  1155. get_sa_error(result, result_buf, result_buf_len);
  1156. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1157. /* error */
  1158. goto evt_free;
  1159. }
  1160. }
  1161. if (call_count != 2) {
  1162. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  1163. goto evt_free;
  1164. }
  1165. /*
  1166. * Test cleanup
  1167. */
  1168. evt_free:
  1169. result = saEvtEventFree(event_handle);
  1170. if (result != SA_AIS_OK) {
  1171. get_sa_error(result, result_buf, result_buf_len);
  1172. printf("ERROR: event free result: %s\n", result_buf);
  1173. }
  1174. evt_close:
  1175. result = saEvtChannelClose(channel_handle);
  1176. if (result != SA_AIS_OK) {
  1177. get_sa_error(result, result_buf, result_buf_len);
  1178. printf("ERROR: channel close result: %s\n", result_buf);
  1179. }
  1180. evt_fin:
  1181. result = saEvtFinalize(handle);
  1182. if (result != SA_AIS_OK) {
  1183. get_sa_error(result, result_buf, result_buf_len);
  1184. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1185. }
  1186. printf("Done\n");
  1187. }
  1188. /*
  1189. * Handle call back for multi-test2
  1190. * Counts events received. Makes sure that we get one event from
  1191. * each subscription.
  1192. *
  1193. */
  1194. void
  1195. multi_test_callback2(SaEvtSubscriptionIdT my_subscription_id,
  1196. const SaEvtEventHandleT event_handle,
  1197. const SaSizeT my_event_data_size)
  1198. {
  1199. SaAisErrorT result;
  1200. SaUint8T my_priority;
  1201. SaTimeT my_retention_time;
  1202. SaNameT my_publisher_name = {0, {0}};
  1203. SaTimeT my_publish_time;
  1204. SaEvtEventIdT my_event_id;
  1205. SaEvtSubscriptionIdT last_sub_id = 0;
  1206. printf(" multi_test_callback2 called(%d)\n", ++call_count);
  1207. evt_pat_get_array.patternsNumber = 4;
  1208. result = saEvtEventAttributesGet(event_handle,
  1209. &evt_pat_get_array, /* patterns */
  1210. &my_priority, /* priority */
  1211. &my_retention_time, /* retention time */
  1212. &my_publisher_name, /* publisher name */
  1213. &my_publish_time, /* publish time */
  1214. &my_event_id /* event_id */
  1215. );
  1216. if (result != SA_AIS_OK) {
  1217. get_sa_error(result, result_buf, result_buf_len);
  1218. printf("ERROR: event get attr result: %s\n", result_buf);
  1219. goto evt_free;
  1220. }
  1221. if (my_event_id != event_id1) {
  1222. printf("ERROR: Received wrong event\n");
  1223. goto evt_free;
  1224. }
  1225. if (last_sub_id == 0) {
  1226. if (my_subscription_id != sub1 &&
  1227. my_subscription_id != sub2) {
  1228. printf("ERROR: Received bad subscription ID\n");
  1229. goto evt_free;
  1230. }
  1231. last_sub_id = my_subscription_id;
  1232. } else {
  1233. if (my_subscription_id == last_sub_id) {
  1234. printf("ERROR: Received subscription ID twice\n");
  1235. goto evt_free;
  1236. }
  1237. if (my_subscription_id != sub1 &&
  1238. my_subscription_id != sub2) {
  1239. printf("ERROR: Received bad subscription ID\n");
  1240. goto evt_free;
  1241. }
  1242. }
  1243. if (evt_pat_get_array.patternsNumber != 1) {
  1244. printf("ERROR: pattern array count not 1: %d\n",
  1245. evt_pat_get_array.patternsNumber);
  1246. }
  1247. evt_free:
  1248. result = saEvtEventFree(event_handle);
  1249. if (result != SA_AIS_OK) {
  1250. get_sa_error(result, result_buf, result_buf_len);
  1251. printf("ERROR: event free result: %s\n", result_buf);
  1252. }
  1253. }
  1254. void
  1255. test_multi_channel2()
  1256. {
  1257. SaEvtEventFilterT filt1[1] = {
  1258. {SA_EVT_EXACT_FILTER, {"ChanPat1", 8}},
  1259. };
  1260. SaEvtEventFilterArrayT sub_filt = {
  1261. NULL, 1
  1262. };
  1263. SaEvtEventPatternT pat1 = {"ChanPat1", 8};
  1264. SaEvtEventPatternArrayT evt_pat = {
  1265. NULL, 1
  1266. };
  1267. SaEvtHandleT handle;
  1268. SaEvtChannelHandleT channel_handle;
  1269. SaEvtChannelHandleT channel_handle1;
  1270. SaEvtEventHandleT event_handle;
  1271. SaEvtChannelOpenFlagsT flags;
  1272. SaNameT channel_name;
  1273. SaEvtCallbacksT multi_callbacks = {
  1274. 0,
  1275. multi_test_callback2
  1276. };
  1277. struct pollfd pfd;
  1278. int nfd;
  1279. int fd;
  1280. int timeout = 5000;
  1281. int result;
  1282. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1283. SA_EVT_CHANNEL_CREATE;
  1284. strcpy(channel_name.value, channel);
  1285. channel_name.length = strlen(channel);
  1286. /*
  1287. * 2. Test multiple openings of a single channel and receving events.
  1288. *
  1289. * Open and subscribe to a channel twice. When an event is sent, it
  1290. * should be delivered twice, once for each open channel.
  1291. */
  1292. printf("Test multiple opens/subscribes:\n");
  1293. result = saEvtInitialize (&handle, &multi_callbacks, versions[0].version);
  1294. if (result != SA_AIS_OK) {
  1295. get_sa_error(result, result_buf, result_buf_len);
  1296. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1297. return;
  1298. }
  1299. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1300. &channel_handle);
  1301. if (result != SA_AIS_OK) {
  1302. get_sa_error(result, result_buf, result_buf_len);
  1303. printf("ERROR: channel open(0) result: %s\n", result_buf);
  1304. goto evt_fin;
  1305. }
  1306. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1307. &channel_handle1);
  1308. if (result != SA_AIS_OK) {
  1309. get_sa_error(result, result_buf, result_buf_len);
  1310. printf("ERROR: channel open(1) result: %s\n", result_buf);
  1311. goto evt_fin;
  1312. }
  1313. result = saEvtEventAllocate(channel_handle, &event_handle);
  1314. if (result != SA_AIS_OK) {
  1315. get_sa_error(result, result_buf, result_buf_len);
  1316. printf("ERROR: event Allocate result: %s\n", result_buf);
  1317. goto evt_close;
  1318. }
  1319. sub_filt.filters = filt1;
  1320. result = saEvtEventSubscribe(channel_handle,
  1321. &sub_filt,
  1322. sub1);
  1323. if (result != SA_AIS_OK) {
  1324. get_sa_error(result, result_buf, result_buf_len);
  1325. printf("ERROR: event subscribe(0) result: %s\n", result_buf);
  1326. goto evt_free;
  1327. }
  1328. sub_filt.filters = filt1;
  1329. result = saEvtEventSubscribe(channel_handle1,
  1330. &sub_filt,
  1331. sub2);
  1332. if (result != SA_AIS_OK) {
  1333. get_sa_error(result, result_buf, result_buf_len);
  1334. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1335. goto evt_free;
  1336. }
  1337. retention_time = 0ULL;
  1338. evt_pat.patterns = &pat1;
  1339. result = saEvtEventAttributesSet(event_handle,
  1340. &evt_pat,
  1341. TEST_PRIORITY,
  1342. retention_time,
  1343. &test_pub_name);
  1344. if (result != SA_AIS_OK) {
  1345. get_sa_error(result, result_buf, result_buf_len);
  1346. printf("ERROR: event set attr result: %s\n", result_buf);
  1347. goto evt_free;
  1348. }
  1349. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1350. &event_id1);
  1351. if (result != SA_AIS_OK) {
  1352. get_sa_error(result, result_buf, result_buf_len);
  1353. printf("ERROR: event Publish result: %s\n", result_buf);
  1354. goto evt_close;
  1355. }
  1356. /*
  1357. * See if we got the event
  1358. */
  1359. result = saEvtSelectionObjectGet(handle, &fd);
  1360. if (result != SA_AIS_OK) {
  1361. get_sa_error(result, result_buf, result_buf_len);
  1362. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1363. /* error */
  1364. return;
  1365. }
  1366. call_count = 0;
  1367. while(1) {
  1368. pfd.fd = fd;
  1369. pfd.events = POLLIN;
  1370. nfd = poll(&pfd, 1, timeout);
  1371. if (nfd == 0) {
  1372. break;
  1373. } else if (nfd < 0) {
  1374. perror("ERROR: poll error");
  1375. break;
  1376. }
  1377. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  1378. if (result != SA_AIS_OK) {
  1379. get_sa_error(result, result_buf, result_buf_len);
  1380. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1381. /* error */
  1382. goto evt_free;
  1383. }
  1384. }
  1385. if (call_count != 2) {
  1386. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  1387. goto evt_free;
  1388. }
  1389. /*
  1390. * Test cleanup
  1391. */
  1392. evt_free:
  1393. result = saEvtEventFree(event_handle);
  1394. if (result != SA_AIS_OK) {
  1395. get_sa_error(result, result_buf, result_buf_len);
  1396. printf("ERROR: event free result: %s\n", result_buf);
  1397. }
  1398. evt_close:
  1399. result = saEvtChannelClose(channel_handle);
  1400. if (result != SA_AIS_OK) {
  1401. get_sa_error(result, result_buf, result_buf_len);
  1402. printf("ERROR: channel close result(0): %s\n", result_buf);
  1403. }
  1404. result = saEvtChannelClose(channel_handle1);
  1405. if (result != SA_AIS_OK) {
  1406. get_sa_error(result, result_buf, result_buf_len);
  1407. printf("ERROR: channel close result(1): %s\n", result_buf);
  1408. }
  1409. evt_fin:
  1410. result = saEvtFinalize(handle);
  1411. if (result != SA_AIS_OK) {
  1412. get_sa_error(result, result_buf, result_buf_len);
  1413. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1414. }
  1415. printf("Done\n");
  1416. }
  1417. /*
  1418. * Handle call back for multi-test3
  1419. * Verifies that the event recevied is associated with the correct
  1420. * subscription.
  1421. *
  1422. */
  1423. void
  1424. multi_test_callback3(SaEvtSubscriptionIdT my_subscription_id,
  1425. const SaEvtEventHandleT event_handle,
  1426. const SaSizeT my_event_data_size)
  1427. {
  1428. SaAisErrorT result;
  1429. SaUint8T my_priority;
  1430. SaTimeT my_retention_time;
  1431. SaNameT my_publisher_name = {0, {0}};
  1432. SaTimeT my_publish_time;
  1433. SaEvtEventIdT my_event_id;
  1434. printf(" multi_test_callback2 called(%d)\n", ++call_count);
  1435. evt_pat_get_array.patternsNumber = 4;
  1436. result = saEvtEventAttributesGet(event_handle,
  1437. &evt_pat_get_array, /* patterns */
  1438. &my_priority, /* priority */
  1439. &my_retention_time, /* retention time */
  1440. &my_publisher_name, /* publisher name */
  1441. &my_publish_time, /* publish time */
  1442. &my_event_id /* event_id */
  1443. );
  1444. if (result != SA_AIS_OK) {
  1445. get_sa_error(result, result_buf, result_buf_len);
  1446. printf("ERROR: event get attr result: %s\n", result_buf);
  1447. goto evt_free;
  1448. }
  1449. if ((my_subscription_id != sub1) && (my_subscription_id != sub2)) {
  1450. printf("ERROR: Received wrong subscription ID %x\n",
  1451. my_subscription_id);
  1452. printf(" sub1 %x, sub2 %x\n", sub1, sub2);
  1453. goto evt_free;
  1454. }
  1455. if ((my_event_id != event_id1) && (my_event_id != event_id2)) {
  1456. printf("ERROR: Received wrong event ID %llx\n", my_event_id);
  1457. printf(" id1 %llx, id2 %llx\n", event_id1, event_id2);
  1458. goto evt_free;
  1459. }
  1460. if ((my_subscription_id == sub1) && (my_event_id != event_id1)) {
  1461. printf("ERROR: Received event on wrong subscription\n");
  1462. goto evt_free;
  1463. }
  1464. if ((my_subscription_id == sub2) && (my_event_id != event_id2)) {
  1465. printf("ERROR: Received event on wrong subscription\n");
  1466. goto evt_free;
  1467. }
  1468. if (evt_pat_get_array.patternsNumber != 1) {
  1469. printf("ERROR: pattern array count not 1: %d\n",
  1470. evt_pat_get_array.patternsNumber);
  1471. }
  1472. evt_free:
  1473. result = saEvtEventFree(event_handle);
  1474. if (result != SA_AIS_OK) {
  1475. get_sa_error(result, result_buf, result_buf_len);
  1476. printf("ERROR: event free result: %s\n", result_buf);
  1477. }
  1478. }
  1479. void
  1480. test_multi_channel3()
  1481. {
  1482. SaEvtEventFilterT filt1[1] = {
  1483. {SA_EVT_PREFIX_FILTER, {"ChanPat", 7}},
  1484. };
  1485. SaEvtEventFilterArrayT sub_filt = {
  1486. NULL, 1
  1487. };
  1488. SaEvtEventPatternT pat1 = {"ChanPat1", 8};
  1489. SaEvtEventPatternT pat2 = {"ChanPat2", 8};
  1490. SaEvtEventPatternArrayT evt_pat = {
  1491. NULL, 1
  1492. };
  1493. SaEvtHandleT handle;
  1494. SaEvtChannelHandleT channel_handle;
  1495. SaEvtChannelHandleT channel_handle1;
  1496. SaEvtEventHandleT event_handle;
  1497. SaEvtEventHandleT event_handle1;
  1498. SaEvtChannelOpenFlagsT flags;
  1499. SaNameT channel_name;
  1500. SaNameT channel_name1;
  1501. SaEvtCallbacksT multi_callbacks = {
  1502. 0,
  1503. multi_test_callback3
  1504. };
  1505. struct pollfd pfd;
  1506. int nfd;
  1507. int fd;
  1508. int timeout = 5000;
  1509. int result;
  1510. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1511. SA_EVT_CHANNEL_CREATE;
  1512. strcpy(channel_name.value, channel);
  1513. channel_name.length = strlen(channel_name.value);
  1514. strcpy(channel_name1.value, channel);
  1515. strcat(channel_name1.value, "_1");
  1516. channel_name1.length = strlen(channel_name1.value);
  1517. /*
  1518. * 3. Test opening of multiple channels and receiving events.
  1519. * Open and subscribe to two different channels twice.
  1520. * Subscribe to each channel with the same filters.
  1521. * Sending an event on one channel should be received in the
  1522. * call-back with the subscription ID corresponding to the sent
  1523. * channel.
  1524. */
  1525. printf("Test multiple different channels/subscribes:\n");
  1526. result = saEvtInitialize (&handle, &multi_callbacks, versions[0].version);
  1527. if (result != SA_AIS_OK) {
  1528. get_sa_error(result, result_buf, result_buf_len);
  1529. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1530. return;
  1531. }
  1532. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1533. &channel_handle);
  1534. if (result != SA_AIS_OK) {
  1535. get_sa_error(result, result_buf, result_buf_len);
  1536. printf("ERROR: channel open(0) result: %s\n", result_buf);
  1537. goto evt_fin;
  1538. }
  1539. result = saEvtChannelOpen(handle, &channel_name1, flags, 0,
  1540. &channel_handle1);
  1541. if (result != SA_AIS_OK) {
  1542. get_sa_error(result, result_buf, result_buf_len);
  1543. printf("ERROR: channel open(1) result: %s\n", result_buf);
  1544. goto evt_fin;
  1545. }
  1546. result = saEvtEventAllocate(channel_handle, &event_handle);
  1547. if (result != SA_AIS_OK) {
  1548. get_sa_error(result, result_buf, result_buf_len);
  1549. printf("ERROR: event Allocate(0) result: %s\n", result_buf);
  1550. goto evt_close;
  1551. }
  1552. result = saEvtEventAllocate(channel_handle1, &event_handle1);
  1553. if (result != SA_AIS_OK) {
  1554. get_sa_error(result, result_buf, result_buf_len);
  1555. printf("ERROR: event Allocate(1) result: %s\n", result_buf);
  1556. goto evt_close;
  1557. }
  1558. sub_filt.filters = filt1;
  1559. result = saEvtEventSubscribe(channel_handle,
  1560. &sub_filt,
  1561. sub1);
  1562. if (result != SA_AIS_OK) {
  1563. get_sa_error(result, result_buf, result_buf_len);
  1564. printf("ERROR: event subscribe(0) result: %s\n", result_buf);
  1565. goto evt_free;
  1566. }
  1567. sub_filt.filters = filt1;
  1568. result = saEvtEventSubscribe(channel_handle1,
  1569. &sub_filt,
  1570. sub2);
  1571. if (result != SA_AIS_OK) {
  1572. get_sa_error(result, result_buf, result_buf_len);
  1573. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1574. goto evt_free;
  1575. }
  1576. retention_time = 0ULL;
  1577. evt_pat.patterns = &pat1;
  1578. result = saEvtEventAttributesSet(event_handle,
  1579. &evt_pat,
  1580. TEST_PRIORITY,
  1581. retention_time,
  1582. &test_pub_name);
  1583. if (result != SA_AIS_OK) {
  1584. get_sa_error(result, result_buf, result_buf_len);
  1585. printf("ERROR: event set attr(0) result: %s\n", result_buf);
  1586. goto evt_free;
  1587. }
  1588. evt_pat.patterns = &pat2;
  1589. result = saEvtEventAttributesSet(event_handle1,
  1590. &evt_pat,
  1591. TEST_PRIORITY,
  1592. retention_time,
  1593. &test_pub_name);
  1594. if (result != SA_AIS_OK) {
  1595. get_sa_error(result, result_buf, result_buf_len);
  1596. printf("ERROR: event set attr(1) result: %s\n", result_buf);
  1597. goto evt_free;
  1598. }
  1599. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1600. &event_id1);
  1601. if (result != SA_AIS_OK) {
  1602. get_sa_error(result, result_buf, result_buf_len);
  1603. printf("ERROR: event Publish result: %s\n", result_buf);
  1604. goto evt_close;
  1605. }
  1606. result = saEvtEventPublish(event_handle1, exp_data, DATA_SIZE,
  1607. &event_id2);
  1608. if (result != SA_AIS_OK) {
  1609. get_sa_error(result, result_buf, result_buf_len);
  1610. printf("ERROR: event Publish result: %s\n", result_buf);
  1611. goto evt_close;
  1612. }
  1613. /*
  1614. * See if we got the events
  1615. */
  1616. result = saEvtSelectionObjectGet(handle, &fd);
  1617. if (result != SA_AIS_OK) {
  1618. get_sa_error(result, result_buf, result_buf_len);
  1619. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1620. /* error */
  1621. return;
  1622. }
  1623. call_count = 0;
  1624. while(1) {
  1625. pfd.fd = fd;
  1626. pfd.events = POLLIN;
  1627. nfd = poll(&pfd, 1, timeout);
  1628. if (nfd == 0) {
  1629. break;
  1630. } else if (nfd < 0) {
  1631. perror("ERROR: poll error");
  1632. break;
  1633. }
  1634. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  1635. if (result != SA_AIS_OK) {
  1636. get_sa_error(result, result_buf, result_buf_len);
  1637. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1638. /* error */
  1639. goto evt_free;
  1640. }
  1641. }
  1642. if (call_count != 2) {
  1643. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  1644. goto evt_free;
  1645. }
  1646. /*
  1647. * Test cleanup
  1648. */
  1649. evt_free:
  1650. result = saEvtEventFree(event_handle);
  1651. if (result != SA_AIS_OK) {
  1652. get_sa_error(result, result_buf, result_buf_len);
  1653. printf("ERROR: event free result: %s\n", result_buf);
  1654. }
  1655. result = saEvtEventFree(event_handle1);
  1656. if (result != SA_AIS_OK) {
  1657. get_sa_error(result, result_buf, result_buf_len);
  1658. printf("ERROR: event free result: %s\n", result_buf);
  1659. }
  1660. evt_close:
  1661. result = saEvtChannelClose(channel_handle);
  1662. if (result != SA_AIS_OK) {
  1663. get_sa_error(result, result_buf, result_buf_len);
  1664. printf("ERROR: channel close result(0): %s\n", result_buf);
  1665. }
  1666. result = saEvtChannelClose(channel_handle1);
  1667. if (result != SA_AIS_OK) {
  1668. get_sa_error(result, result_buf, result_buf_len);
  1669. printf("ERROR: channel close result(1): %s\n", result_buf);
  1670. }
  1671. evt_fin:
  1672. result = saEvtFinalize(handle);
  1673. if (result != SA_AIS_OK) {
  1674. get_sa_error(result, result_buf, result_buf_len);
  1675. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1676. }
  1677. printf("Done\n");
  1678. }
  1679. /*
  1680. * Test event retention
  1681. * Test 1: publish the event with a retention time and then
  1682. * subscribe. If the event was retained, we should receive it.
  1683. *
  1684. * Test 2: Publish the event, sleep until it expires, then
  1685. * subscribe. We shouldn't get an event delivered.
  1686. *
  1687. * Test 3: Publish an event with a retention time.
  1688. * subscribe.
  1689. * wait for it.
  1690. * unsubscribe
  1691. * Clear it.
  1692. * Then subscribe and make sure that the messages isn't delivered.
  1693. *
  1694. */
  1695. #define EXPIRE_TIME 10 /* Seconds */
  1696. SaEvtEventIdT retained_id;
  1697. int got_event;
  1698. void
  1699. event_callback_retained(SaEvtSubscriptionIdT my_subscription_id,
  1700. const SaEvtEventHandleT event_handle,
  1701. const SaSizeT my_event_data_size)
  1702. {
  1703. SaAisErrorT result;
  1704. //printf("event_callback_retained called\n");
  1705. result = saEvtEventAttributesGet(event_handle,
  1706. 0, /* patterns */
  1707. 0, /* priority */
  1708. 0, /* retention time */
  1709. 0, /* publisher name */
  1710. 0, /* publish time */
  1711. &retained_id /* event_id */
  1712. );
  1713. if (result != SA_AIS_OK) {
  1714. get_sa_error(result, result_buf, result_buf_len);
  1715. printf("ERROR: callback attr get result: %s\n", result_buf);
  1716. return;
  1717. }
  1718. got_event = 1;
  1719. }
  1720. void
  1721. test_retention()
  1722. {
  1723. SaEvtHandleT handle;
  1724. SaEvtChannelHandleT channel_handle;
  1725. SaEvtEventHandleT event_handle;
  1726. SaEvtChannelOpenFlagsT flags;
  1727. SaNameT channel_name;
  1728. SaEvtCallbacksT callbacks_retain = {
  1729. 0,
  1730. event_callback_retained
  1731. };
  1732. struct pollfd pfd;
  1733. int nfd;
  1734. int fd;
  1735. int timeout = (EXPIRE_TIME + 5);
  1736. SaAisErrorT result;
  1737. flags = SA_EVT_CHANNEL_PUBLISHER |
  1738. SA_EVT_CHANNEL_SUBSCRIBER |
  1739. SA_EVT_CHANNEL_CREATE;
  1740. strcpy(channel_name.value, channel);
  1741. channel_name.length = strlen(channel);
  1742. printf("Test Event retention:\n");
  1743. result = saEvtInitialize (&handle, &callbacks_retain,
  1744. versions[0].version);
  1745. if (result != SA_AIS_OK) {
  1746. get_sa_error(result, result_buf, result_buf_len);
  1747. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1748. return;
  1749. }
  1750. result = saEvtSelectionObjectGet(handle, &fd);
  1751. if (result != SA_AIS_OK) {
  1752. get_sa_error(result, result_buf, result_buf_len);
  1753. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1754. /* error */
  1755. return;
  1756. }
  1757. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1758. &channel_handle);
  1759. if (result != SA_AIS_OK) {
  1760. get_sa_error(result, result_buf, result_buf_len);
  1761. printf("ERROR: channel open result: %s\n", result_buf);
  1762. goto evt_fin;
  1763. }
  1764. /*
  1765. * Allocate an event
  1766. */
  1767. result = saEvtEventAllocate(channel_handle, &event_handle);
  1768. if (result != SA_AIS_OK) {
  1769. get_sa_error(result, result_buf, result_buf_len);
  1770. printf("ERROR: event Allocate result: %s\n", result_buf);
  1771. goto evt_close;
  1772. }
  1773. retention_time = (EXPIRE_TIME)*1000000000ULL;
  1774. result = saEvtEventAttributesSet(event_handle,
  1775. &evt_pat_set_array,
  1776. TEST_PRIORITY,
  1777. retention_time,
  1778. &test_pub_name);
  1779. if (result != SA_AIS_OK) {
  1780. get_sa_error(result, result_buf, result_buf_len);
  1781. printf("ERROR: event set attr result: %s\n", result_buf);
  1782. goto evt_free;
  1783. }
  1784. /*
  1785. * Test 1: publish the event with a retention time and then
  1786. * subscribe. If the event was retained, we should receive it.
  1787. */
  1788. printf(" Receive retained event\n");
  1789. got_event=0;
  1790. retained_id=0;
  1791. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  1792. if (result != SA_AIS_OK) {
  1793. get_sa_error(result, result_buf, result_buf_len);
  1794. printf("ERROR: event Publish result(1): %s\n", result_buf);
  1795. goto evt_close;
  1796. }
  1797. result = saEvtEventSubscribe(channel_handle,
  1798. &subscribe_filters,
  1799. subscription_id);
  1800. if (result != SA_AIS_OK) {
  1801. get_sa_error(result, result_buf, result_buf_len);
  1802. printf("ERROR: event subscribe result: %s\n", result_buf);
  1803. goto evt_free;
  1804. }
  1805. pfd.fd = fd;
  1806. pfd.events = POLLIN;
  1807. nfd = poll(&pfd, 1, 1000);
  1808. if (nfd <= 0) {
  1809. printf("ERROR: poll fds %d\n", nfd);
  1810. if (nfd < 0) {
  1811. perror("ERROR: poll error");
  1812. }
  1813. /* Error */
  1814. goto evt_free;
  1815. }
  1816. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  1817. if (result != SA_AIS_OK) {
  1818. get_sa_error(result, result_buf, result_buf_len);
  1819. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1820. /* error */
  1821. goto evt_free;
  1822. }
  1823. if (!got_event) {
  1824. printf("ERROR: retained event not recevied\n");
  1825. goto evt_free;
  1826. }
  1827. if (retained_id != event_id) {
  1828. printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
  1829. event_id, retained_id);
  1830. goto evt_free;
  1831. }
  1832. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  1833. if (result != SA_AIS_OK) {
  1834. get_sa_error(result, result_buf, result_buf_len);
  1835. printf("ERROR: unsubscribe result: %s\n", result_buf);
  1836. goto evt_free;
  1837. }
  1838. /*
  1839. * Test 2: Publish the event, sleep until it expires, then
  1840. * subscribe. We shouldn't get an event delivered.
  1841. */
  1842. printf(" Expire retained event\n");
  1843. got_event=0;
  1844. retained_id=0;
  1845. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  1846. if (result != SA_AIS_OK) {
  1847. get_sa_error(result, result_buf, result_buf_len);
  1848. printf("ERROR: event Publish result(1): %s\n", result_buf);
  1849. goto evt_close;
  1850. }
  1851. result = saEvtSelectionObjectGet(handle, &fd);
  1852. if (result != SA_AIS_OK) {
  1853. get_sa_error(result, result_buf, result_buf_len);
  1854. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1855. /* error */
  1856. return;
  1857. }
  1858. /*
  1859. * Wait for the event to expire, then subscribe. We shouldn't get
  1860. * an event
  1861. */
  1862. sleep(timeout);
  1863. result = saEvtEventSubscribe(channel_handle,
  1864. &subscribe_filters,
  1865. subscription_id);
  1866. if (result != SA_AIS_OK) {
  1867. get_sa_error(result, result_buf, result_buf_len);
  1868. printf("ERROR: event subscribe result: %s\n", result_buf);
  1869. result = saEvtChannelClose(channel_handle);
  1870. goto evt_free;
  1871. }
  1872. pfd.fd = fd;
  1873. pfd.events = POLLIN;
  1874. nfd = poll(&pfd, 1, 1000);
  1875. if (nfd != 0) {
  1876. printf("ERROR: poll fds %d\n", nfd);
  1877. if (nfd < 0) {
  1878. perror("ERROR: poll error");
  1879. }
  1880. /* Error */
  1881. goto evt_free;
  1882. }
  1883. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  1884. if (result != SA_AIS_OK) {
  1885. get_sa_error(result, result_buf, result_buf_len);
  1886. printf("ERROR: unsubscribe result: %s\n", result_buf);
  1887. goto evt_free;
  1888. }
  1889. /*
  1890. * Test 3:
  1891. * Publish an event with a retention time.
  1892. * subscribe.
  1893. * wait for it.
  1894. * unsubscribe
  1895. * Clear it.
  1896. * Then subscribe and make sure that the messages isn't delivered.
  1897. */
  1898. printf(" Clear event retention time\n");
  1899. got_event=0;
  1900. retained_id=0;
  1901. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  1902. if (result != SA_AIS_OK) {
  1903. get_sa_error(result, result_buf, result_buf_len);
  1904. printf("ERROR: event Publish result(2): %s\n", result_buf);
  1905. goto evt_free;
  1906. }
  1907. result = saEvtEventSubscribe(channel_handle,
  1908. &subscribe_filters,
  1909. subscription_id);
  1910. if (result != SA_AIS_OK) {
  1911. get_sa_error(result, result_buf, result_buf_len);
  1912. printf("ERROR: event subscribe result: %s\n", result_buf);
  1913. goto evt_free;
  1914. }
  1915. pfd.fd = fd;
  1916. pfd.events = POLLIN;
  1917. nfd = poll(&pfd, 1, 1000);
  1918. if (nfd <= 0) {
  1919. printf("ERROR: poll fds %d\n", nfd);
  1920. if (nfd < 0) {
  1921. perror("ERROR: poll error");
  1922. }
  1923. /* Error */
  1924. goto evt_free;
  1925. }
  1926. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  1927. if (result != SA_AIS_OK) {
  1928. get_sa_error(result, result_buf, result_buf_len);
  1929. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1930. /* error */
  1931. goto evt_free;
  1932. }
  1933. if (!got_event) {
  1934. printf("ERROR: retained event not recevied\n");
  1935. goto evt_free;
  1936. }
  1937. if (retained_id != event_id) {
  1938. printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
  1939. event_id, retained_id);
  1940. goto evt_free;
  1941. }
  1942. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  1943. if (result != SA_AIS_OK) {
  1944. get_sa_error(result, result_buf, result_buf_len);
  1945. printf("ERROR: unsubscribe result: %s\n", result_buf);
  1946. goto evt_free;
  1947. }
  1948. result = saEvtEventRetentionTimeClear(channel_handle, event_id);
  1949. if (result != SA_AIS_OK) {
  1950. get_sa_error(result, result_buf, result_buf_len);
  1951. printf("ERROR: clear retention time result: %s\n", result_buf);
  1952. goto evt_free;
  1953. }
  1954. result = saEvtEventSubscribe(channel_handle,
  1955. &subscribe_filters,
  1956. subscription_id);
  1957. if (result != SA_AIS_OK) {
  1958. get_sa_error(result, result_buf, result_buf_len);
  1959. printf("ERROR: event subscribe result: %s\n", result_buf);
  1960. goto evt_free;
  1961. }
  1962. pfd.fd = fd;
  1963. pfd.events = POLLIN;
  1964. nfd = poll(&pfd, 1, 1000);
  1965. if (nfd != 0) {
  1966. printf("ERROR: poll fds %d\n", nfd);
  1967. if (nfd < 0) {
  1968. perror("ERROR: poll error");
  1969. }
  1970. /* Error */
  1971. goto evt_free;
  1972. }
  1973. /*
  1974. * Test cleanup
  1975. */
  1976. evt_free:
  1977. result = saEvtEventFree(event_handle);
  1978. if (result != SA_AIS_OK) {
  1979. get_sa_error(result, result_buf, result_buf_len);
  1980. printf("ERROR: event free result: %s\n", result_buf);
  1981. }
  1982. evt_close:
  1983. result = saEvtChannelClose(channel_handle);
  1984. if (result != SA_AIS_OK) {
  1985. get_sa_error(result, result_buf, result_buf_len);
  1986. printf("ERROR: channel close result: %s\n", result_buf);
  1987. }
  1988. evt_fin:
  1989. result = saEvtFinalize(handle);
  1990. if (result != SA_AIS_OK) {
  1991. get_sa_error(result, result_buf, result_buf_len);
  1992. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1993. }
  1994. printf("Done\n");
  1995. }
  1996. int main (void)
  1997. {
  1998. test_initialize ();
  1999. test_channel();
  2000. test_event();
  2001. test_multi_channel1();
  2002. test_multi_channel2();
  2003. test_multi_channel3();
  2004. test_retention();
  2005. return (0);
  2006. }