testevt.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  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 "ais_evt.h"
  62. extern int get_sa_error(SaErrorT, char *, int);
  63. char result_buf[256];
  64. int result_buf_len = sizeof(result_buf);
  65. void testresult (SaErrorT result, SaErrorT 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 = { 'A', 0x01, 0x01 };
  76. SaVersionT version2 = { 'a', 0x01, 0x01 };
  77. SaVersionT version3 = { 'b', 0x01, 0x01 };
  78. SaVersionT version4 = { 'a', 0x02, 0x01 };
  79. SaVersionT version5 = { 'a', 0xff, 0x01 };
  80. SaVersionT version6 = { 'a', 0x01, 0xff };
  81. SaVersionT version7 = { 'A', 0xff, 0xff };
  82. SaVersionT version8 = { 'B', 0xff, 0xff };
  83. struct version_test {
  84. SaVersionT *version;
  85. SaErrorT result;
  86. };
  87. struct version_test versions[] = {
  88. { &version1, SA_OK },
  89. { &version2, SA_OK },
  90. { &version3, SA_ERR_VERSION },
  91. { &version4, SA_ERR_VERSION},
  92. { &version8, SA_ERR_VERSION},
  93. { 0, SA_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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. SaErrorT 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=%lx, a=%lx\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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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. SaErrorT 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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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. SaErrorT 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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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. SaErrorT 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_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 %lx\n",
  1451. my_subscription_id);
  1452. printf(" sub1 %lx, sub2 %lx\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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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. SaErrorT 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_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. SaErrorT result;
  1737. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER;
  1738. strcpy(channel_name.value, channel);
  1739. channel_name.length = strlen(channel);
  1740. printf("Test Event retention:\n");
  1741. result = saEvtInitialize (&handle, &callbacks_retain,
  1742. versions[0].version);
  1743. if (result != SA_OK) {
  1744. get_sa_error(result, result_buf, result_buf_len);
  1745. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1746. return;
  1747. }
  1748. result = saEvtSelectionObjectGet(handle, &fd);
  1749. if (result != SA_OK) {
  1750. get_sa_error(result, result_buf, result_buf_len);
  1751. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1752. /* error */
  1753. return;
  1754. }
  1755. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1756. &channel_handle);
  1757. if (result != SA_OK) {
  1758. get_sa_error(result, result_buf, result_buf_len);
  1759. printf("ERROR: channel open result: %s\n", result_buf);
  1760. goto evt_fin;
  1761. }
  1762. /*
  1763. * Allocate an event
  1764. */
  1765. result = saEvtEventAllocate(channel_handle, &event_handle);
  1766. if (result != SA_OK) {
  1767. get_sa_error(result, result_buf, result_buf_len);
  1768. printf("ERROR: event Allocate result: %s\n", result_buf);
  1769. goto evt_close;
  1770. }
  1771. retention_time = (EXPIRE_TIME)*1000000000ULL;
  1772. result = saEvtEventAttributesSet(event_handle,
  1773. &evt_pat_set_array,
  1774. TEST_PRIORITY,
  1775. retention_time,
  1776. &test_pub_name);
  1777. if (result != SA_OK) {
  1778. get_sa_error(result, result_buf, result_buf_len);
  1779. printf("ERROR: event set attr result: %s\n", result_buf);
  1780. goto evt_free;
  1781. }
  1782. /*
  1783. * Test 1: publish the event with a retention time and then
  1784. * subscribe. If the event was retained, we should receive it.
  1785. */
  1786. printf(" Receive retained event\n");
  1787. got_event=0;
  1788. retained_id=0;
  1789. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  1790. if (result != SA_OK) {
  1791. get_sa_error(result, result_buf, result_buf_len);
  1792. printf("ERROR: event Publish result(1): %s\n", result_buf);
  1793. goto evt_close;
  1794. }
  1795. result = saEvtEventSubscribe(channel_handle,
  1796. &subscribe_filters,
  1797. subscription_id);
  1798. if (result != SA_OK) {
  1799. get_sa_error(result, result_buf, result_buf_len);
  1800. printf("ERROR: event subscribe result: %s\n", result_buf);
  1801. goto evt_free;
  1802. }
  1803. pfd.fd = fd;
  1804. pfd.events = POLLIN;
  1805. nfd = poll(&pfd, 1, 1000);
  1806. if (nfd <= 0) {
  1807. printf("ERROR: poll fds %d\n", nfd);
  1808. if (nfd < 0) {
  1809. perror("ERROR: poll error");
  1810. }
  1811. /* Error */
  1812. goto evt_free;
  1813. }
  1814. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  1815. if (result != SA_OK) {
  1816. get_sa_error(result, result_buf, result_buf_len);
  1817. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1818. /* error */
  1819. goto evt_free;
  1820. }
  1821. if (!got_event) {
  1822. printf("ERROR: retained event not recevied\n");
  1823. goto evt_free;
  1824. }
  1825. if (retained_id != event_id) {
  1826. printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
  1827. event_id, retained_id);
  1828. goto evt_free;
  1829. }
  1830. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  1831. if (result != SA_OK) {
  1832. get_sa_error(result, result_buf, result_buf_len);
  1833. printf("ERROR: unsubscribe result: %s\n", result_buf);
  1834. goto evt_free;
  1835. }
  1836. /*
  1837. * Test 2: Publish the event, sleep until it expires, then
  1838. * subscribe. We shouldn't get an event delivered.
  1839. */
  1840. printf(" Expire retained event\n");
  1841. got_event=0;
  1842. retained_id=0;
  1843. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  1844. if (result != SA_OK) {
  1845. get_sa_error(result, result_buf, result_buf_len);
  1846. printf("ERROR: event Publish result(1): %s\n", result_buf);
  1847. goto evt_close;
  1848. }
  1849. result = saEvtSelectionObjectGet(handle, &fd);
  1850. if (result != SA_OK) {
  1851. get_sa_error(result, result_buf, result_buf_len);
  1852. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1853. /* error */
  1854. return;
  1855. }
  1856. /*
  1857. * Wait for the event to expire, then subscribe. We shouldn't get
  1858. * an event
  1859. */
  1860. sleep(timeout);
  1861. result = saEvtEventSubscribe(channel_handle,
  1862. &subscribe_filters,
  1863. subscription_id);
  1864. if (result != SA_OK) {
  1865. get_sa_error(result, result_buf, result_buf_len);
  1866. printf("ERROR: event subscribe result: %s\n", result_buf);
  1867. result = saEvtChannelClose(channel_handle);
  1868. goto evt_free;
  1869. }
  1870. pfd.fd = fd;
  1871. pfd.events = POLLIN;
  1872. nfd = poll(&pfd, 1, 1000);
  1873. if (nfd != 0) {
  1874. printf("ERROR: poll fds %d\n", nfd);
  1875. if (nfd < 0) {
  1876. perror("ERROR: poll error");
  1877. }
  1878. /* Error */
  1879. goto evt_free;
  1880. }
  1881. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  1882. if (result != SA_OK) {
  1883. get_sa_error(result, result_buf, result_buf_len);
  1884. printf("ERROR: unsubscribe result: %s\n", result_buf);
  1885. goto evt_free;
  1886. }
  1887. /*
  1888. * Test 3:
  1889. * Publish an event with a retention time.
  1890. * subscribe.
  1891. * wait for it.
  1892. * unsubscribe
  1893. * Clear it.
  1894. * Then subscribe and make sure that the messages isn't delivered.
  1895. */
  1896. printf(" Clear event retention time\n");
  1897. got_event=0;
  1898. retained_id=0;
  1899. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  1900. if (result != SA_OK) {
  1901. get_sa_error(result, result_buf, result_buf_len);
  1902. printf("ERROR: event Publish result(2): %s\n", result_buf);
  1903. goto evt_free;
  1904. }
  1905. result = saEvtEventSubscribe(channel_handle,
  1906. &subscribe_filters,
  1907. subscription_id);
  1908. if (result != SA_OK) {
  1909. get_sa_error(result, result_buf, result_buf_len);
  1910. printf("ERROR: event subscribe result: %s\n", result_buf);
  1911. goto evt_free;
  1912. }
  1913. pfd.fd = fd;
  1914. pfd.events = POLLIN;
  1915. nfd = poll(&pfd, 1, 1000);
  1916. if (nfd <= 0) {
  1917. printf("ERROR: poll fds %d\n", nfd);
  1918. if (nfd < 0) {
  1919. perror("ERROR: poll error");
  1920. }
  1921. /* Error */
  1922. goto evt_free;
  1923. }
  1924. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  1925. if (result != SA_OK) {
  1926. get_sa_error(result, result_buf, result_buf_len);
  1927. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1928. /* error */
  1929. goto evt_free;
  1930. }
  1931. if (!got_event) {
  1932. printf("ERROR: retained event not recevied\n");
  1933. goto evt_free;
  1934. }
  1935. if (retained_id != event_id) {
  1936. printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
  1937. event_id, retained_id);
  1938. goto evt_free;
  1939. }
  1940. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  1941. if (result != SA_OK) {
  1942. get_sa_error(result, result_buf, result_buf_len);
  1943. printf("ERROR: unsubscribe result: %s\n", result_buf);
  1944. goto evt_free;
  1945. }
  1946. result = saEvtEventRetentionTimeClear(channel_handle, event_id);
  1947. if (result != SA_OK) {
  1948. get_sa_error(result, result_buf, result_buf_len);
  1949. printf("ERROR: clear retention time result: %s\n", result_buf);
  1950. goto evt_free;
  1951. }
  1952. result = saEvtEventSubscribe(channel_handle,
  1953. &subscribe_filters,
  1954. subscription_id);
  1955. if (result != SA_OK) {
  1956. get_sa_error(result, result_buf, result_buf_len);
  1957. printf("ERROR: event subscribe result: %s\n", result_buf);
  1958. goto evt_free;
  1959. }
  1960. pfd.fd = fd;
  1961. pfd.events = POLLIN;
  1962. nfd = poll(&pfd, 1, 1000);
  1963. if (nfd != 0) {
  1964. printf("ERROR: poll fds %d\n", nfd);
  1965. if (nfd < 0) {
  1966. perror("ERROR: poll error");
  1967. }
  1968. /* Error */
  1969. goto evt_free;
  1970. }
  1971. /*
  1972. * Test cleanup
  1973. */
  1974. evt_free:
  1975. result = saEvtEventFree(event_handle);
  1976. if (result != SA_OK) {
  1977. get_sa_error(result, result_buf, result_buf_len);
  1978. printf("ERROR: event free result: %s\n", result_buf);
  1979. }
  1980. evt_close:
  1981. result = saEvtChannelClose(channel_handle);
  1982. if (result != SA_OK) {
  1983. get_sa_error(result, result_buf, result_buf_len);
  1984. printf("ERROR: channel close result: %s\n", result_buf);
  1985. }
  1986. evt_fin:
  1987. result = saEvtFinalize(handle);
  1988. if (result != SA_OK) {
  1989. get_sa_error(result, result_buf, result_buf_len);
  1990. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1991. }
  1992. printf("Done\n");
  1993. }
  1994. int main (void)
  1995. {
  1996. test_initialize ();
  1997. test_channel();
  1998. test_event();
  1999. test_multi_channel1();
  2000. test_multi_channel2();
  2001. test_multi_channel3();
  2002. test_retention();
  2003. return (0);
  2004. }