testevt.c 62 KB

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