testevt.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235
  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. * test_unlink_channel();
  55. * Test event channel unlink.
  56. */
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <unistd.h>
  61. #include <sys/poll.h>
  62. #include <malloc.h>
  63. #include "saAis.h"
  64. #include "saEvt.h"
  65. extern int get_sa_error(SaAisErrorT, char *, int);
  66. char result_buf[256];
  67. int result_buf_len = sizeof(result_buf);
  68. #define TRY_WAIT 2
  69. void testresult (SaAisErrorT result, SaAisErrorT expected, int test_no)
  70. {
  71. if (result == expected) {
  72. printf ("Test %d passed\n", test_no);
  73. } else {
  74. get_sa_error(result, result_buf, result_buf_len);
  75. printf ("ERROR: Test %d FAILED (expected %d got %s)\n",
  76. test_no, expected, result_buf);
  77. }
  78. }
  79. SaVersionT version1 = { 'B', 0x01, 0x01 };
  80. SaVersionT version2 = { 'A', 0x01, 0x01 };
  81. SaVersionT version3 = { 'B', 0x02, 0x01 };
  82. SaVersionT version4 = { 'B', 0x01, 0xff };
  83. SaVersionT version5 = { 'C', 0xff, 0xff };
  84. struct version_test {
  85. SaVersionT *version;
  86. SaAisErrorT result;
  87. };
  88. struct version_test versions[] = {
  89. { &version1, SA_AIS_OK },
  90. { &version2, SA_AIS_ERR_VERSION },
  91. { &version3, SA_AIS_ERR_VERSION },
  92. { &version4, SA_AIS_OK},
  93. { &version5, SA_AIS_ERR_VERSION},
  94. { 0, SA_AIS_ERR_INVALID_PARAM}
  95. };
  96. int version_size = sizeof(versions) / sizeof(struct version_test);
  97. void open_callback(SaInvocationT invocation,
  98. SaEvtChannelHandleT channelHandle,
  99. SaAisErrorT error);
  100. void event_callback(SaEvtSubscriptionIdT subscriptionId,
  101. const SaEvtEventHandleT eventHandle,
  102. const SaSizeT eventDataSize);
  103. SaEvtCallbacksT callbacks = {
  104. open_callback,
  105. event_callback
  106. };
  107. char channel[256] = "TESTEVT_CHANNEL";
  108. char unlink_channel[256] = "TESTEVT_UNLINK_CHANNEL";
  109. SaEvtSubscriptionIdT subscription_id = 0xabcdef;
  110. SaInvocationT open_invocation = 0xaa55cc33;
  111. unsigned long long test_ret_time = 30000000000ULL; /* 30 seconds */
  112. /*
  113. * event data
  114. */
  115. long *exp_data;
  116. #define DATA_SIZE 2048 /* default data size */
  117. #define LCOUNT DATA_SIZE/sizeof(long)
  118. /*
  119. * Test saEvtInitialize and version checking.
  120. */
  121. void test_initialize (void) {
  122. SaAisErrorT result;
  123. SaEvtHandleT handle;
  124. int i;
  125. /*
  126. * version check tests
  127. */
  128. printf("Test lib version check on initlialize\n");
  129. for (i=0; i < version_size; i++) {
  130. do {
  131. result = saEvtInitialize (&handle, 0, versions[i].version);
  132. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  133. testresult (result, versions[i].result, i);
  134. if (result == SA_AIS_OK) {
  135. saEvtFinalize(handle);
  136. }
  137. }
  138. }
  139. #define _patt1 "Filter pattern 1"
  140. #define patt1 (SaUint8T *) _patt1
  141. #define patt1_size sizeof(_patt1)
  142. #define _patt2 "Filter pattern 2"
  143. #define patt2 (SaUint8T *) _patt2
  144. #define patt2_size sizeof(_patt2)
  145. #define _patt3 "Filter pattern 3"
  146. #define patt3 (SaUint8T *) _patt3
  147. #define patt3_size sizeof(_patt3)
  148. #define _patt4 "Filter pattern 4"
  149. #define patt4 (SaUint8T *) _patt4
  150. #define patt4_size sizeof(_patt4)
  151. SaEvtEventFilterT filters[] = {
  152. {SA_EVT_PREFIX_FILTER, {patt1_size, patt1_size, patt1}},
  153. {SA_EVT_SUFFIX_FILTER, {patt2_size, patt2_size, patt2}},
  154. {SA_EVT_EXACT_FILTER, {patt3_size, patt3_size, patt3}},
  155. {SA_EVT_PASS_ALL_FILTER, {patt4_size, patt4_size, patt4}}
  156. };
  157. SaEvtEventFilterArrayT subscribe_filters = {
  158. sizeof(filters)/sizeof(SaEvtEventFilterT),
  159. filters
  160. };
  161. /*
  162. * Process the open callback
  163. */
  164. void open_callback(SaInvocationT invocation,
  165. SaEvtChannelHandleT channel_handle,
  166. SaAisErrorT error)
  167. {
  168. SaAisErrorT result;
  169. printf(" Received open channel callback\n");
  170. if (error != SA_AIS_OK) {
  171. get_sa_error(error, result_buf, result_buf_len);
  172. printf("ERROR: async channel open result: %s\n", result_buf);
  173. return;
  174. }
  175. if (invocation != open_invocation) {
  176. printf("ERROR: Unexpected invocation value: e 0x%llx, a 0x%llx\n",
  177. open_invocation, invocation);
  178. }
  179. printf(" Close async channel:\n");
  180. do {
  181. result = saEvtChannelClose(channel_handle);
  182. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  183. if (result != SA_AIS_OK) {
  184. get_sa_error(result, result_buf, result_buf_len);
  185. printf("ERROR: channel close result: %s\n", result_buf);
  186. return;
  187. }
  188. }
  189. /*
  190. * Test channel operations.
  191. * 1. Open a channel.
  192. * 2. Close a channel.
  193. * 3. Channel subscription.
  194. * 4. Channel unsubscribe
  195. * 5. unsubscribe/finalize with no channel close.
  196. * 6. Multiple subscriptions.
  197. * 7. Duplicate subscription ID.
  198. * 8. unsubscribe non-exsistent subscription ID.
  199. * 9. Open a channel async.
  200. *
  201. */
  202. void
  203. test_channel()
  204. {
  205. SaEvtHandleT handle;
  206. SaEvtChannelHandleT channel_handle;
  207. SaEvtChannelOpenFlagsT flags;
  208. SaNameT channel_name;
  209. SaAisErrorT result;
  210. struct pollfd pfd;
  211. int nfd;
  212. SaSelectionObjectT fd;
  213. int timeout = 5000;
  214. flags = SA_EVT_CHANNEL_PUBLISHER |
  215. SA_EVT_CHANNEL_SUBSCRIBER |
  216. SA_EVT_CHANNEL_CREATE;
  217. strcpy((char *)channel_name.value, channel);
  218. channel_name.length = strlen(channel);
  219. /*
  220. * Channel open/close test
  221. */
  222. printf("Test Channel operations:\n");
  223. printf(" Channel open:\n");
  224. do {
  225. result = saEvtInitialize (&handle,
  226. &callbacks, versions[0].version);
  227. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  228. if (result != SA_AIS_OK) {
  229. get_sa_error(result, result_buf, result_buf_len);
  230. printf("ERROR: Event Initialize result: %s\n", result_buf);
  231. return;
  232. }
  233. do {
  234. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  235. &channel_handle);
  236. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  237. if (result != SA_AIS_OK) {
  238. get_sa_error(result, result_buf, result_buf_len);
  239. printf("ERROR: channel open result: %s\n", result_buf);
  240. do {
  241. result = saEvtFinalize(handle);
  242. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  243. if (result != SA_AIS_OK) {
  244. get_sa_error(result, result_buf, result_buf_len);
  245. printf("ERROR: Event Finalize result: %s\n", result_buf);
  246. }
  247. return;
  248. }
  249. printf(" Channel close:\n");
  250. do {
  251. result = saEvtChannelClose(channel_handle);
  252. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  253. if (result != SA_AIS_OK) {
  254. get_sa_error(result, result_buf, result_buf_len);
  255. printf("ERROR: channel close result: %s\n", result_buf);
  256. return;
  257. }
  258. do {
  259. result = saEvtFinalize(handle);
  260. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  261. if (result != SA_AIS_OK) {
  262. get_sa_error(result, result_buf, result_buf_len);
  263. printf("ERROR: Event Finalize result: %s\n", result_buf);
  264. return;
  265. }
  266. /*
  267. * Test channel subscribe
  268. */
  269. printf(" Channel subscribe:\n");
  270. do {
  271. result = saEvtInitialize (&handle, &callbacks,
  272. versions[0].version);
  273. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  274. if (result != SA_AIS_OK) {
  275. get_sa_error(result, result_buf, result_buf_len);
  276. printf("ERROR: Event Initialize result: %s\n", result_buf);
  277. return;
  278. }
  279. do {
  280. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  281. &channel_handle);
  282. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  283. if (result != SA_AIS_OK) {
  284. get_sa_error(result, result_buf, result_buf_len);
  285. printf("ERROR: channel open result: %s\n", result_buf);
  286. do {
  287. result = saEvtFinalize(handle);
  288. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  289. if (result != SA_AIS_OK) {
  290. get_sa_error(result, result_buf, result_buf_len);
  291. printf("ERROR: Finalize result: %s\n", result_buf);
  292. }
  293. return;
  294. }
  295. do {
  296. result = saEvtEventSubscribe(channel_handle,
  297. &subscribe_filters,
  298. subscription_id);
  299. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  300. if (result != SA_AIS_OK) {
  301. get_sa_error(result, result_buf, result_buf_len);
  302. printf("ERROR: event subscribe result: %s\n", result_buf);
  303. do {
  304. result = saEvtChannelClose(channel_handle);
  305. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  306. if (result != SA_AIS_OK) {
  307. get_sa_error(result, result_buf, result_buf_len);
  308. printf("ERROR: Channel close result: %s\n", result_buf);
  309. }
  310. do {
  311. result = saEvtFinalize(handle);
  312. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  313. if (result != SA_AIS_OK) {
  314. get_sa_error(result, result_buf, result_buf_len);
  315. printf("ERROR: Finalize result: %s\n", result_buf);
  316. }
  317. return;
  318. }
  319. printf(" Channel unsubscribe:\n");
  320. do {
  321. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  322. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  323. if (result != SA_AIS_OK) {
  324. get_sa_error(result, result_buf, result_buf_len);
  325. printf("ERROR: event unsubscribe result: %s\n", result_buf);
  326. do {
  327. result = saEvtChannelClose(channel_handle);
  328. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  329. if (result != SA_AIS_OK) {
  330. get_sa_error(result, result_buf, result_buf_len);
  331. printf("ERROR: Channel close result: %s\n", result_buf);
  332. }
  333. do {
  334. result = saEvtFinalize(handle);
  335. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  336. if (result != SA_AIS_OK) {
  337. get_sa_error(result, result_buf, result_buf_len);
  338. printf("ERROR: Finalize result: %s\n", result_buf);
  339. }
  340. return;
  341. }
  342. do {
  343. result = saEvtChannelClose(channel_handle);
  344. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  345. if (result != SA_AIS_OK) {
  346. get_sa_error(result, result_buf, result_buf_len);
  347. printf("ERROR: Channel close result: %s\n", result_buf);
  348. }
  349. do {
  350. result = saEvtFinalize(handle);
  351. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  352. if (result != SA_AIS_OK) {
  353. get_sa_error(result, result_buf, result_buf_len);
  354. printf("ERROR: Finalize result: %s\n", result_buf);
  355. }
  356. /*
  357. * Test channel subscribe with no close
  358. */
  359. printf(" Channel subscribe with no close at end:\n");
  360. do {
  361. result = saEvtInitialize (&handle, &callbacks,
  362. versions[0].version);
  363. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  364. if (result != SA_AIS_OK) {
  365. get_sa_error(result, result_buf, result_buf_len);
  366. printf("ERROR: Event Initialize result: %s\n", result_buf);
  367. return;
  368. }
  369. do {
  370. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  371. &channel_handle);
  372. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  373. if (result != SA_AIS_OK) {
  374. get_sa_error(result, result_buf, result_buf_len);
  375. printf("ERROR: channel open result: %s\n", result_buf);
  376. result = saEvtFinalize(handle);
  377. return;
  378. }
  379. do {
  380. result = saEvtEventSubscribe(channel_handle,
  381. &subscribe_filters,
  382. subscription_id);
  383. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  384. if (result != SA_AIS_OK) {
  385. get_sa_error(result, result_buf, result_buf_len);
  386. printf("ERROR: event subscribe result: %s\n", result_buf);
  387. result = saEvtChannelClose(channel_handle);
  388. }
  389. do {
  390. result = saEvtFinalize(handle);
  391. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  392. if (result != SA_AIS_OK) {
  393. get_sa_error(result, result_buf, result_buf_len);
  394. printf("ERROR: Finalize failed\n");
  395. return;
  396. }
  397. /*
  398. * Test multiple subscriptions
  399. */
  400. printf(" Multiple subscriptions\n");
  401. do {
  402. result = saEvtInitialize (&handle, &callbacks,
  403. versions[0].version);
  404. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  405. if (result != SA_AIS_OK) {
  406. get_sa_error(result, result_buf, result_buf_len);
  407. printf("ERROR: Event Initialize result: %s\n", result_buf);
  408. return;
  409. }
  410. do {
  411. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  412. &channel_handle);
  413. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  414. if (result != SA_AIS_OK) {
  415. get_sa_error(result, result_buf, result_buf_len);
  416. printf("ERROR: channel open result: %s\n", result_buf);
  417. do {
  418. result = saEvtFinalize(handle);
  419. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  420. if (result != SA_AIS_OK) {
  421. get_sa_error(result, result_buf, result_buf_len);
  422. printf("ERROR: Finalize result: %s\n", result_buf);
  423. }
  424. return;
  425. }
  426. do {
  427. result = saEvtEventSubscribe(channel_handle,
  428. &subscribe_filters,
  429. subscription_id);
  430. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  431. if (result != SA_AIS_OK) {
  432. get_sa_error(result, result_buf, result_buf_len);
  433. printf("ERROR: First event subscribe result: %s\n", result_buf);
  434. do {
  435. result = saEvtChannelClose(channel_handle);
  436. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  437. if (result != SA_AIS_OK) {
  438. get_sa_error(result, result_buf, result_buf_len);
  439. printf("ERROR: Channel close result: %s\n", result_buf);
  440. }
  441. do {
  442. result = saEvtFinalize(handle);
  443. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  444. if (result != SA_AIS_OK) {
  445. get_sa_error(result, result_buf, result_buf_len);
  446. printf("ERROR: Finalize result: %s\n", result_buf);
  447. }
  448. return;
  449. }
  450. do {
  451. result = saEvtEventSubscribe(channel_handle,
  452. &subscribe_filters,
  453. subscription_id+1);
  454. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  455. if (result != SA_AIS_OK) {
  456. get_sa_error(result, result_buf, result_buf_len);
  457. printf("ERROR: second event subscribe result: %s\n", result_buf);
  458. do {
  459. result = saEvtChannelClose(channel_handle);
  460. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  461. if (result != SA_AIS_OK) {
  462. get_sa_error(result, result_buf, result_buf_len);
  463. printf("ERROR: Channel close result: %s\n", result_buf);
  464. }
  465. do {
  466. result = saEvtFinalize(handle);
  467. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  468. if (result != SA_AIS_OK) {
  469. get_sa_error(result, result_buf, result_buf_len);
  470. printf("ERROR: Finalize result: %s\n", result_buf);
  471. }
  472. return;
  473. }
  474. /*
  475. * Test duplicate subscription
  476. */
  477. printf(" Duplicate subscription\n");
  478. do {
  479. result = saEvtEventSubscribe(channel_handle,
  480. &subscribe_filters,
  481. subscription_id);
  482. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  483. if (result != SA_AIS_ERR_EXIST) {
  484. get_sa_error(result, result_buf, result_buf_len);
  485. printf("ERROR: First event subscribe result: %s\n", result_buf);
  486. result = saEvtChannelClose(channel_handle);
  487. result = saEvtFinalize(handle);
  488. return;
  489. }
  490. /*
  491. * Test unsubscribe non-existent sub_id
  492. */
  493. printf(" Unsubscribe non-existent sub_id\n");
  494. do {
  495. result = saEvtEventUnsubscribe(channel_handle,
  496. subscription_id+2);
  497. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  498. if (result != SA_AIS_ERR_NOT_EXIST) {
  499. get_sa_error(result, result_buf, result_buf_len);
  500. printf("ERROR: event unsubscribe result: %s\n", result_buf);
  501. do {
  502. result = saEvtChannelClose(channel_handle);
  503. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  504. if (result != SA_AIS_OK) {
  505. get_sa_error(result, result_buf, result_buf_len);
  506. printf("ERROR: Channel close result: %s\n", result_buf);
  507. }
  508. do {
  509. result = saEvtFinalize(handle);
  510. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  511. if (result != SA_AIS_OK) {
  512. get_sa_error(result, result_buf, result_buf_len);
  513. printf("ERROR: Finalize result: %s\n", result_buf);
  514. }
  515. return;
  516. }
  517. do {
  518. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  519. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  520. if (result != SA_AIS_OK) {
  521. get_sa_error(result, result_buf, result_buf_len);
  522. printf("ERROR: first event unsubscribe result: %s\n", result_buf);
  523. }
  524. do {
  525. result = saEvtEventUnsubscribe(channel_handle,
  526. subscription_id+1);
  527. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  528. if (result != SA_AIS_OK) {
  529. get_sa_error(result, result_buf, result_buf_len);
  530. printf("ERROR: second event unsubscribe result: %s\n", result_buf);
  531. }
  532. do {
  533. result = saEvtChannelClose(channel_handle);
  534. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  535. if (result != SA_AIS_OK) {
  536. get_sa_error(result, result_buf, result_buf_len);
  537. printf("ERROR: Channel close result: %s\n", result_buf);
  538. }
  539. do {
  540. result = saEvtFinalize(handle);
  541. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  542. if (result != SA_AIS_OK) {
  543. get_sa_error(result, result_buf, result_buf_len);
  544. printf("ERROR: Finalize result: %s\n", result_buf);
  545. }
  546. /*
  547. * Test opening a channel async.
  548. */
  549. printf(" Channel open async:\n");
  550. do {
  551. result = saEvtInitialize (&handle, &callbacks,
  552. versions[0].version);
  553. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  554. if (result != SA_AIS_OK) {
  555. get_sa_error(result, result_buf, result_buf_len);
  556. printf("ERROR: Event Initialize result: %s\n", result_buf);
  557. return;
  558. }
  559. do {
  560. result = saEvtChannelOpenAsync(handle, open_invocation,
  561. &channel_name, flags);
  562. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  563. if (result != SA_AIS_OK) {
  564. get_sa_error(result, result_buf, result_buf_len);
  565. printf("ERROR: channel open async result: %s\n", result_buf);
  566. do {
  567. result = saEvtFinalize(handle);
  568. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  569. if (result != SA_AIS_OK) {
  570. get_sa_error(result, result_buf, result_buf_len);
  571. printf("ERROR: Event Finalize result: %s\n", result_buf);
  572. }
  573. return;
  574. }
  575. /*
  576. * See if we got the open callback
  577. */
  578. do {
  579. result = saEvtSelectionObjectGet(handle, &fd);
  580. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  581. if (result != SA_AIS_OK) {
  582. get_sa_error(result, result_buf, result_buf_len);
  583. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  584. /* error */
  585. return;
  586. }
  587. pfd.fd = fd;
  588. pfd.events = POLLIN;
  589. nfd = poll(&pfd, 1, timeout);
  590. if (nfd <= 0) {
  591. printf("ERROR: poll fds %d\n", nfd);
  592. if (nfd < 0) {
  593. perror("ERROR: poll error");
  594. }
  595. /* Error */
  596. return;
  597. }
  598. do {
  599. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  600. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  601. if (result != SA_AIS_OK) {
  602. get_sa_error(result, result_buf, result_buf_len);
  603. printf("ERROR: saEvtDispatch %s\n", result_buf);
  604. /* error */
  605. return;
  606. }
  607. do {
  608. result = saEvtFinalize(handle);
  609. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  610. if (result != SA_AIS_OK) {
  611. get_sa_error(result, result_buf, result_buf_len);
  612. printf("ERROR: Event Finalize result: %s\n", result_buf);
  613. return;
  614. }
  615. printf("Done\n");
  616. }
  617. SaUint8T pat0[100];
  618. SaUint8T pat1[100];
  619. SaUint8T pat2[100];
  620. SaUint8T pat3[100];
  621. SaUint8T pat4[100];
  622. SaEvtEventPatternT evt_patts[5] = {
  623. {100, 100, pat0},
  624. {100, 100, pat1},
  625. {100, 100, pat2},
  626. {100, 100, pat3},
  627. {100, 100, pat4}};
  628. SaEvtEventPatternArrayT evt_pat_get_array = { 5, 0, evt_patts };
  629. SaEvtEventPatternT patterns[] = {
  630. {patt1_size, patt1_size, patt1},
  631. {patt2_size, patt2_size, patt2},
  632. {patt3_size, patt3_size, patt3},
  633. {patt4_size, patt4_size, patt4}
  634. };
  635. SaNameT test_pub_name = {13, "Test Pub Name"};
  636. #define TEST_PRIORITY 2
  637. SaEvtEventPatternArrayT evt_pat_set_array = {
  638. sizeof(patterns)/sizeof(SaEvtEventPatternT),
  639. sizeof(patterns)/sizeof(SaEvtEventPatternT),
  640. patterns
  641. };
  642. char event_data[1000];
  643. #define EVENT_DATA_SIZE 1000
  644. SaEvtEventIdT event_id = 0;
  645. SaUint8T priority;
  646. SaTimeT retention_time = 0ULL;
  647. SaNameT publisher_name = {0, {0}};
  648. SaSizeT event_data_size = 0;
  649. int expected_pat_count;
  650. /*
  651. * Test event operations.
  652. *
  653. * 1. Event allocation
  654. * 2. Get event attributes (no pointers).
  655. * 3. Get event attributes with pointers.
  656. * 4. Set/Get event attributes.
  657. * 5. Get event user data no pointer or count
  658. * 6. Get event user data with pointer and no count
  659. * 7. Get event user data with no pointer with a count.
  660. * 8. Get event user data with pointer and count.
  661. * 9. Get event user data woth a short count.
  662. * 10. Free event.
  663. * 11. Publish with no set patterns.
  664. * 12. Publish with set patterns and event user data.
  665. *
  666. */
  667. void
  668. event_callback(SaEvtSubscriptionIdT my_subscription_id,
  669. const SaEvtEventHandleT event_handle,
  670. const SaSizeT my_event_data_size)
  671. {
  672. SaAisErrorT result;
  673. SaUint8T my_priority;
  674. SaTimeT my_retention_time;
  675. SaNameT my_publisher_name = {0, {0}};
  676. SaTimeT my_publish_time;
  677. SaEvtEventIdT my_event_id;
  678. int i;
  679. long *act_data;
  680. SaSizeT data_size;
  681. printf(" event_callback called\n");
  682. if (my_subscription_id != subscription_id) {
  683. printf("ERROR: sub ID: e=%x, a=%x\n",
  684. subscription_id, my_subscription_id);
  685. }
  686. if (my_event_data_size != event_data_size) {
  687. printf("ERROR: event data size e=%lld, a=%lld\n",
  688. event_data_size,
  689. my_event_data_size);
  690. }
  691. evt_pat_get_array.patternsNumber = 4;
  692. do {
  693. result = saEvtEventAttributesGet(event_handle,
  694. &evt_pat_get_array, /* patterns */
  695. &my_priority, /* priority */
  696. &my_retention_time, /* retention time */
  697. &my_publisher_name, /* publisher name */
  698. &my_publish_time, /* publish time */
  699. &my_event_id /* event_id */
  700. );
  701. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  702. if (result != SA_AIS_OK) {
  703. get_sa_error(result, result_buf, result_buf_len);
  704. printf("ERROR: event get attr result(2): %s\n", result_buf);
  705. goto evt_free;
  706. }
  707. if (my_event_id != event_id) {
  708. printf("ERROR: Call back event ID error: e=%llx, a=%llx\n",
  709. (unsigned long long)event_id, (unsigned long long)my_event_id);
  710. }
  711. if (evt_pat_get_array.patternsNumber != expected_pat_count) {
  712. printf("ERROR: pattern array count not %d: %lld\n",
  713. expected_pat_count, evt_pat_get_array.patternsNumber);
  714. }
  715. if (expected_pat_count == 0) {
  716. goto evt_free;
  717. }
  718. for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
  719. if (evt_pat_get_array.patterns[i].patternSize !=
  720. evt_pat_set_array.patterns[i].patternSize) {
  721. printf("ERROR: pattern %d count not equal g=%lld, s=%lld\n",
  722. i,
  723. evt_pat_get_array.patterns[i].patternSize,
  724. evt_pat_set_array.patterns[i].patternSize);
  725. printf("ERROR: pattern %d content g=\"%s\", s=\"%s\"\n",
  726. i,
  727. evt_pat_get_array.patterns[i].pattern,
  728. evt_pat_set_array.patterns[i].pattern);
  729. } else {
  730. if (memcmp(evt_pat_get_array.patterns[i].pattern,
  731. evt_pat_set_array.patterns[i].pattern,
  732. evt_pat_get_array.patterns[i].patternSize) != 0){
  733. printf(
  734. "ERROR: pattern %d don't match g=\"%s\", s=\"%s\"\n",
  735. i,
  736. evt_pat_get_array.patterns[i].pattern,
  737. evt_pat_set_array.patterns[i].pattern);
  738. }
  739. }
  740. }
  741. if (priority != my_priority) {
  742. printf("ERROR: priority: e=0x%x a=0x%x\n",
  743. priority, my_priority);
  744. }
  745. if (retention_time != my_retention_time) {
  746. printf("ERROR: retention: e=0x%llx a=0x%llx\n",
  747. (unsigned long long)retention_time,
  748. (unsigned long long)my_retention_time);
  749. }
  750. if (publisher_name.length != my_publisher_name.length) {
  751. printf("ERROR: publisher name length: e=%d, a=%d\n",
  752. publisher_name.length,
  753. my_publisher_name.length);
  754. } else {
  755. if (memcmp(publisher_name.value, my_publisher_name.value,
  756. publisher_name.length) != 0) {
  757. printf("ERROR: publisher name content: e=%s, a=%s\n",
  758. publisher_name.value,
  759. my_publisher_name.value);
  760. }
  761. }
  762. act_data = malloc(my_event_data_size);
  763. memset(act_data, 0, my_event_data_size);
  764. data_size = my_event_data_size;
  765. do {
  766. result = saEvtEventDataGet(event_handle, act_data, &data_size);
  767. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  768. if (result != SA_AIS_OK) {
  769. get_sa_error(result, result_buf, result_buf_len);
  770. printf("ERROR: event data get result: %s\n", result_buf);
  771. goto dat_free;
  772. }
  773. if (data_size != event_data_size) {
  774. printf("ERROR: Data size: e=%lld a=%lld\n",
  775. event_data_size, data_size);
  776. }
  777. for (i = 0; i < (data_size/sizeof(long)); i++) {
  778. if (act_data[i] != exp_data[i]) {
  779. printf("ERROR: Event Data e=%lx a=%lx at index %d\n",
  780. exp_data[i], act_data[i], i);
  781. break;
  782. }
  783. }
  784. dat_free:
  785. free(act_data);
  786. evt_free:
  787. do {
  788. result = saEvtEventFree(event_handle);
  789. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  790. if (result != SA_AIS_OK) {
  791. get_sa_error(result, result_buf, result_buf_len);
  792. printf("ERROR: event free result: %s\n", result_buf);
  793. }
  794. }
  795. void
  796. test_event()
  797. {
  798. SaEvtHandleT handle;
  799. SaEvtChannelHandleT channel_handle;
  800. SaEvtEventHandleT event_handle;
  801. SaEvtChannelOpenFlagsT flags;
  802. SaNameT channel_name;
  803. SaTimeT publish_time;
  804. struct pollfd pfd;
  805. int nfd;
  806. SaSelectionObjectT fd;
  807. int timeout = 5000;
  808. SaAisErrorT result;
  809. int i;
  810. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  811. SA_EVT_CHANNEL_CREATE;
  812. strcpy((char *)channel_name.value, channel);
  813. channel_name.length = strlen(channel);
  814. printf("Test Event operations:\n");
  815. do {
  816. result = saEvtInitialize (&handle, &callbacks, versions[0].version);
  817. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  818. if (result != SA_AIS_OK) {
  819. get_sa_error(result, result_buf, result_buf_len);
  820. printf("ERROR: Event Initialize result: %s\n", result_buf);
  821. return;
  822. }
  823. do {
  824. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  825. &channel_handle);
  826. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  827. if (result != SA_AIS_OK) {
  828. get_sa_error(result, result_buf, result_buf_len);
  829. printf("ERROR: channel open result: %s\n", result_buf);
  830. goto evt_fin;
  831. }
  832. /*
  833. * Allocate an event
  834. */
  835. printf(" Event allocation\n");
  836. do {
  837. result = saEvtEventAllocate(channel_handle, &event_handle);
  838. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  839. if (result != SA_AIS_OK) {
  840. get_sa_error(result, result_buf, result_buf_len);
  841. printf("ERROR: event Allocate result: %s\n", result_buf);
  842. goto evt_close;
  843. }
  844. printf(" Get event attributes(1)\n");
  845. do {
  846. result = saEvtEventAttributesGet(event_handle,
  847. 0, /* patterns */
  848. 0, /* priority */
  849. 0, /* retention time */
  850. 0, /* publisher name */
  851. 0, /* publish time */
  852. 0 /* event_id */
  853. );
  854. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  855. if (result != SA_AIS_OK) {
  856. get_sa_error(result, result_buf, result_buf_len);
  857. printf("ERROR: event get attr result(1): %s\n", result_buf);
  858. goto evt_free;
  859. }
  860. /*
  861. * Get event attributes, this time supply pointers.
  862. * validate the default values.
  863. */
  864. printf(" Get event attributes(2)\n");
  865. evt_pat_get_array.patternsNumber = 4;
  866. do {
  867. result = saEvtEventAttributesGet(event_handle,
  868. &evt_pat_get_array, /* patterns */
  869. &priority, /* priority */
  870. &retention_time, /* retention time */
  871. &publisher_name, /* publisher name */
  872. &publish_time, /* publish time */
  873. &event_id /* event_id */
  874. );
  875. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  876. if (result != SA_AIS_OK) {
  877. get_sa_error(result, result_buf, result_buf_len);
  878. printf("ERROR: event get attr result(2): %s\n", result_buf);
  879. goto evt_free;
  880. }
  881. if (evt_pat_get_array.patternsNumber != 0) {
  882. printf("ERROR: pattern array count not zero: %lld\n",
  883. evt_pat_get_array.patternsNumber);
  884. }
  885. if (priority != SA_EVT_LOWEST_PRIORITY) {
  886. printf("ERROR: priority not lowest: 0x%x\n", priority);
  887. }
  888. if (retention_time != 0) {
  889. printf("ERROR: retention time not zero: %0llx\n",
  890. (unsigned long long)retention_time);
  891. }
  892. if (publisher_name.length != 0) {
  893. printf("ERROR: publisher name not null: %s\n", publisher_name.value);
  894. }
  895. if (event_id != 0) {
  896. printf("ERROR: event id not zero: 0x%llx\n",
  897. (unsigned long long)event_id);
  898. }
  899. /*
  900. * Set some attributes, then read them back
  901. */
  902. printf(" Set/get event attributes(1)\n");
  903. do {
  904. result = saEvtEventAttributesSet(event_handle,
  905. &evt_pat_set_array,
  906. TEST_PRIORITY,
  907. test_ret_time,
  908. &test_pub_name);
  909. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  910. if (result != SA_AIS_OK) {
  911. get_sa_error(result, result_buf, result_buf_len);
  912. printf("ERROR: event set attr result(1): %s\n", result_buf);
  913. goto evt_free;
  914. }
  915. evt_pat_get_array.patternsNumber = 4;
  916. do {
  917. result = saEvtEventAttributesGet(event_handle,
  918. &evt_pat_get_array, /* patterns */
  919. &priority, /* priority */
  920. &retention_time, /* retention time */
  921. &publisher_name, /* publisher name */
  922. &publish_time, /* publish time */
  923. &event_id /* event_id */
  924. );
  925. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  926. if (result != SA_AIS_OK) {
  927. get_sa_error(result, result_buf, result_buf_len);
  928. printf("ERROR: event get attr result(2): %s\n", result_buf);
  929. goto evt_free;
  930. }
  931. if (evt_pat_get_array.patternsNumber != 4) {
  932. printf("ERROR: pattern array count not 4: %lld\n",
  933. evt_pat_get_array.patternsNumber);
  934. }
  935. for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
  936. if (evt_pat_get_array.patterns[i].patternSize !=
  937. evt_pat_set_array.patterns[i].patternSize) {
  938. printf("ERROR: pattern %d count not equal g=%lld, s=%lld\n",
  939. i,
  940. evt_pat_get_array.patterns[i].patternSize,
  941. evt_pat_set_array.patterns[i].patternSize);
  942. printf("ERROR: pattern %d content g=\"%s\", s=\"%s\"\n",
  943. i,
  944. evt_pat_get_array.patterns[i].pattern,
  945. evt_pat_set_array.patterns[i].pattern);
  946. } else {
  947. if (memcmp(evt_pat_get_array.patterns[i].pattern,
  948. evt_pat_set_array.patterns[i].pattern,
  949. evt_pat_get_array.patterns[i].patternSize) != 0){
  950. printf(
  951. "ERROR: pattern %d don't match g=\"%s\", s=\"%s\"\n",
  952. i,
  953. evt_pat_get_array.patterns[i].pattern,
  954. evt_pat_set_array.patterns[i].pattern);
  955. }
  956. }
  957. }
  958. if (priority != TEST_PRIORITY) {
  959. printf("ERROR: priority: e=0x%x a=0x%x\n",
  960. TEST_PRIORITY, priority);
  961. }
  962. if (retention_time != test_ret_time) {
  963. printf("ERROR: retention: e=0x%llx a=0x%llx\n",
  964. (unsigned long long)test_ret_time,
  965. (unsigned long long)retention_time);
  966. }
  967. if (publisher_name.length != test_pub_name.length) {
  968. printf("ERROR: publisher name length: e=%d, a=%d\n",
  969. test_pub_name.length,
  970. publisher_name.length);
  971. } else {
  972. if (memcmp(publisher_name.value, test_pub_name.value,
  973. publisher_name.length) != 0) {
  974. printf("ERROR: publisher name content: e=%s, a=%s\n",
  975. test_pub_name.value,
  976. publisher_name.value);
  977. }
  978. }
  979. if (event_id != 0) {
  980. printf("ERROR: event id not zero: 0x%llx\n",
  981. (unsigned long long)event_id);
  982. }
  983. /*
  984. * event user data
  985. */
  986. event_data_size = 0;
  987. printf(" Get event data(1)\n");
  988. do {
  989. result = saEvtEventDataGet(event_handle, 0, &event_data_size);
  990. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  991. if (result != SA_AIS_ERR_NO_SPACE) {
  992. get_sa_error(result, result_buf, result_buf_len);
  993. printf("ERROR: Get event data(1) result: %s\n", result_buf);
  994. }
  995. printf(" Get event data(2)\n");
  996. do {
  997. result = saEvtEventDataGet(event_handle, event_data, &event_data_size);
  998. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  999. if (result != SA_AIS_ERR_BAD_HANDLE) {
  1000. get_sa_error(result, result_buf, result_buf_len);
  1001. printf("ERROR: Get event data(2) result: %s\n", result_buf);
  1002. }
  1003. printf(" Get event data(3)\n");
  1004. event_data_size = EVENT_DATA_SIZE;
  1005. do {
  1006. result = saEvtEventDataGet(event_handle, 0, &event_data_size);
  1007. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1008. if (result != SA_AIS_ERR_NO_SPACE) {
  1009. get_sa_error(result, result_buf, result_buf_len);
  1010. printf("ERROR: Get event data(3) result: %s\n", result_buf);
  1011. }
  1012. printf(" Get event data(4)\n");
  1013. event_data_size = EVENT_DATA_SIZE;
  1014. do {
  1015. result = saEvtEventDataGet(event_handle, event_data,
  1016. &event_data_size);
  1017. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1018. if (result != SA_AIS_ERR_BAD_HANDLE) {
  1019. get_sa_error(result, result_buf, result_buf_len);
  1020. printf("ERROR: Get event data(4) result: %s\n", result_buf);
  1021. }
  1022. printf(" Get event data(5)\n");
  1023. event_data_size = 1;
  1024. do {
  1025. result = saEvtEventDataGet(event_handle, event_data,
  1026. &event_data_size);
  1027. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1028. if (result != SA_AIS_ERR_BAD_HANDLE) {
  1029. get_sa_error(result, result_buf, result_buf_len);
  1030. printf("ERROR: Get event data(5) result: %s\n", result_buf);
  1031. }
  1032. printf(" Free event(1)\n");
  1033. do {
  1034. result = saEvtEventFree(event_handle);
  1035. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1036. if (result != SA_AIS_OK) {
  1037. get_sa_error(result, result_buf, result_buf_len);
  1038. printf("ERROR: event free result: %s\n", result_buf);
  1039. }
  1040. /*
  1041. * Test publication.
  1042. */
  1043. printf(" Publish with no patterns set\n");
  1044. event_data_size = 0;
  1045. do {
  1046. result = saEvtEventSubscribe(channel_handle,
  1047. &subscribe_filters,
  1048. subscription_id);
  1049. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1050. if (result != SA_AIS_OK) {
  1051. get_sa_error(result, result_buf, result_buf_len);
  1052. printf("ERROR: event subscribe result: %s\n", result_buf);
  1053. do {
  1054. result = saEvtChannelClose(channel_handle);
  1055. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1056. if (result != SA_AIS_OK) {
  1057. get_sa_error(result, result_buf, result_buf_len);
  1058. printf("ERROR: Channel close result: %s\n", result_buf);
  1059. }
  1060. do {
  1061. result = saEvtFinalize(handle);
  1062. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1063. if (result != SA_AIS_OK) {
  1064. get_sa_error(result, result_buf, result_buf_len);
  1065. printf("ERROR: Finalize result: %s\n", result_buf);
  1066. }
  1067. return;
  1068. }
  1069. do {
  1070. result = saEvtEventAllocate(channel_handle, &event_handle);
  1071. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1072. if (result != SA_AIS_OK) {
  1073. get_sa_error(result, result_buf, result_buf_len);
  1074. printf("ERROR: event Allocate result: %s\n", result_buf);
  1075. goto evt_close;
  1076. }
  1077. expected_pat_count = 0;
  1078. do {
  1079. result = saEvtEventPublish(event_handle, 0, 0, &event_id);
  1080. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1081. if (result != SA_AIS_OK) {
  1082. get_sa_error(result, result_buf, result_buf_len);
  1083. printf("ERROR: event Publish result(1): %s\n", result_buf);
  1084. goto evt_close;
  1085. }
  1086. /*
  1087. * See if we got the event
  1088. */
  1089. do {
  1090. result = saEvtSelectionObjectGet(handle, &fd);
  1091. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1092. if (result != SA_AIS_OK) {
  1093. get_sa_error(result, result_buf, result_buf_len);
  1094. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1095. /* error */
  1096. return;
  1097. }
  1098. pfd.fd = fd;
  1099. pfd.events = POLLIN;
  1100. nfd = poll(&pfd, 1, timeout);
  1101. if (nfd <= 0) {
  1102. printf("ERROR: poll fds %d\n", nfd);
  1103. if (nfd < 0) {
  1104. perror("ERROR: poll error");
  1105. }
  1106. /* Error */
  1107. return;
  1108. }
  1109. do {
  1110. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  1111. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1112. if (result != SA_AIS_OK) {
  1113. get_sa_error(result, result_buf, result_buf_len);
  1114. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1115. /* error */
  1116. return;
  1117. }
  1118. /*
  1119. * Publish with pattens
  1120. */
  1121. printf(" Publish with patterns set\n");
  1122. retention_time = 0ULL;
  1123. do {
  1124. result = saEvtEventAttributesSet(event_handle,
  1125. &evt_pat_set_array,
  1126. TEST_PRIORITY,
  1127. retention_time,
  1128. &test_pub_name);
  1129. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1130. if (result != SA_AIS_OK) {
  1131. get_sa_error(result, result_buf, result_buf_len);
  1132. printf("ERROR: event set attr result(2): %s\n", result_buf);
  1133. goto evt_free;
  1134. }
  1135. /*
  1136. * Generate event data
  1137. */
  1138. exp_data = malloc(DATA_SIZE);
  1139. for (i = 0; i < LCOUNT; i++) {
  1140. exp_data[i] = lrand48();
  1141. }
  1142. event_data_size = DATA_SIZE;
  1143. expected_pat_count = 4;
  1144. /*
  1145. * Send it
  1146. */
  1147. do {
  1148. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1149. &event_id);
  1150. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1151. if (result != SA_AIS_OK) {
  1152. get_sa_error(result, result_buf, result_buf_len);
  1153. printf("ERROR: event Publish result(2): %s\n", result_buf);
  1154. goto evt_close;
  1155. }
  1156. /*
  1157. * See if we got the event
  1158. */
  1159. do {
  1160. result = saEvtSelectionObjectGet(handle, &fd);
  1161. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1162. if (result != SA_AIS_OK) {
  1163. get_sa_error(result, result_buf, result_buf_len);
  1164. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1165. /* error */
  1166. return;
  1167. }
  1168. pfd.fd = fd;
  1169. pfd.events = POLLIN;
  1170. nfd = poll(&pfd, 1, timeout);
  1171. if (nfd <= 0) {
  1172. printf("ERROR: poll fds %d\n", nfd);
  1173. if (nfd < 0) {
  1174. perror("ERROR: poll error");
  1175. }
  1176. /* Error */
  1177. return;
  1178. }
  1179. do {
  1180. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  1181. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1182. if (result != SA_AIS_OK) {
  1183. get_sa_error(result, result_buf, result_buf_len);
  1184. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1185. /* error */
  1186. return;
  1187. }
  1188. /*
  1189. * Test cleanup
  1190. */
  1191. evt_free:
  1192. do {
  1193. result = saEvtEventFree(event_handle);
  1194. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1195. if (result != SA_AIS_OK) {
  1196. get_sa_error(result, result_buf, result_buf_len);
  1197. printf("ERROR: event free result: %s\n", result_buf);
  1198. }
  1199. evt_close:
  1200. do {
  1201. result = saEvtChannelClose(channel_handle);
  1202. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1203. if (result != SA_AIS_OK) {
  1204. get_sa_error(result, result_buf, result_buf_len);
  1205. printf("ERROR: channel close result: %s\n", result_buf);
  1206. }
  1207. evt_fin:
  1208. do {
  1209. result = saEvtFinalize(handle);
  1210. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1211. if (result != SA_AIS_OK) {
  1212. get_sa_error(result, result_buf, result_buf_len);
  1213. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1214. }
  1215. printf("Done\n");
  1216. }
  1217. SaEvtEventIdT event_id1;
  1218. SaEvtEventIdT event_id2;
  1219. SaEvtEventIdT event_id3;
  1220. SaEvtSubscriptionIdT sub1 = 0x101010;
  1221. SaEvtSubscriptionIdT sub2 = 0x202020;
  1222. static int call_count = 0;
  1223. /*
  1224. * Handle call back for multi-test1
  1225. * Checks event ID with subscription ID to make sure that we
  1226. * received an event on the correct subscription.
  1227. */
  1228. void
  1229. multi_test_callback1(SaEvtSubscriptionIdT my_subscription_id,
  1230. const SaEvtEventHandleT event_handle,
  1231. const SaSizeT my_event_data_size)
  1232. {
  1233. SaAisErrorT result;
  1234. SaUint8T my_priority;
  1235. SaTimeT my_retention_time;
  1236. SaNameT my_publisher_name = {0, {0}};
  1237. SaTimeT my_publish_time;
  1238. SaEvtEventIdT my_event_id;
  1239. SaEvtSubscriptionIdT exp_sub_id;
  1240. printf(" multi_test_callback1 called(%d)\n", ++call_count);
  1241. evt_pat_get_array.patternsNumber = 4;
  1242. do {
  1243. result = saEvtEventAttributesGet(event_handle,
  1244. &evt_pat_get_array, /* patterns */
  1245. &my_priority, /* priority */
  1246. &my_retention_time, /* retention time */
  1247. &my_publisher_name, /* publisher name */
  1248. &my_publish_time, /* publish time */
  1249. &my_event_id /* event_id */
  1250. );
  1251. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1252. if (result != SA_AIS_OK) {
  1253. get_sa_error(result, result_buf, result_buf_len);
  1254. printf("ERROR: event get attr result: %s\n", result_buf);
  1255. goto evt_free;
  1256. }
  1257. if (my_event_id == event_id1) {
  1258. exp_sub_id = sub1;
  1259. } else if (my_event_id == event_id2) {
  1260. exp_sub_id = sub2;
  1261. } else if (my_event_id == event_id3) {
  1262. printf("ERROR: Received event 3 but not subscribed\n");
  1263. goto evt_free;
  1264. } else {
  1265. printf("ERROR: Received event %llx but not sent\n",
  1266. (unsigned long long)my_event_id);
  1267. goto evt_free;
  1268. }
  1269. if (my_subscription_id != exp_sub_id) {
  1270. printf("ERROR: sub ID: e=%x, a=%x\n",
  1271. exp_sub_id, my_subscription_id);
  1272. goto evt_free;
  1273. }
  1274. if (evt_pat_get_array.patternsNumber != 1) {
  1275. printf("ERROR: pattern array count not 1: %lld\n",
  1276. evt_pat_get_array.patternsNumber);
  1277. }
  1278. evt_free:
  1279. do {
  1280. result = saEvtEventFree(event_handle);
  1281. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1282. if (result != SA_AIS_OK) {
  1283. get_sa_error(result, result_buf, result_buf_len);
  1284. printf("ERROR: event free result: %s\n", result_buf);
  1285. }
  1286. }
  1287. /*
  1288. * Test multiple channel operations
  1289. * 1. Test multiple subscriptions on a single channel and receiving
  1290. * events.
  1291. * 2. Test multiple openings of a single channel and receving events.
  1292. * 3. Test opening of multiple channels and receiving events
  1293. */
  1294. void
  1295. test_multi_channel1()
  1296. {
  1297. SaEvtEventFilterT filt1[1] = {
  1298. {SA_EVT_EXACT_FILTER, {8,8, (SaUint8T *) "ChanPat1"}},
  1299. };
  1300. SaEvtEventFilterT filt2[1] = {
  1301. {SA_EVT_EXACT_FILTER, {8, 8, (SaUint8T *) "ChanPat2"}},
  1302. };
  1303. SaEvtEventFilterArrayT sub_filt = {
  1304. 1, NULL
  1305. };
  1306. SaEvtEventPatternT pat1 = {8, 8, (SaUint8T *) "ChanPat1"};
  1307. SaEvtEventPatternT pat2 = {8, 8, (SaUint8T *) "ChanPat2"};
  1308. SaEvtEventPatternT pat3 = {8, 8, (SaUint8T *) "ChanPat3"};
  1309. SaEvtEventPatternArrayT evt_pat = {
  1310. 1, 1, NULL
  1311. };
  1312. SaEvtHandleT handle;
  1313. SaEvtChannelHandleT channel_handle;
  1314. SaEvtEventHandleT event_handle;
  1315. SaEvtChannelOpenFlagsT flags;
  1316. SaNameT channel_name;
  1317. SaEvtCallbacksT multi_callbacks = {
  1318. 0,
  1319. multi_test_callback1
  1320. };
  1321. struct pollfd pfd;
  1322. int nfd;
  1323. SaSelectionObjectT fd;
  1324. int timeout = 5000;
  1325. SaAisErrorT result;
  1326. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1327. SA_EVT_CHANNEL_CREATE;
  1328. strcpy((char *)channel_name.value, channel);
  1329. channel_name.length = strlen(channel);
  1330. printf("Test multiple operations:\n");
  1331. do {
  1332. result = saEvtInitialize (&handle, &multi_callbacks,
  1333. versions[0].version);
  1334. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1335. if (result != SA_AIS_OK) {
  1336. get_sa_error(result, result_buf, result_buf_len);
  1337. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1338. return;
  1339. }
  1340. do {
  1341. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  1342. &channel_handle);
  1343. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1344. if (result != SA_AIS_OK) {
  1345. get_sa_error(result, result_buf, result_buf_len);
  1346. printf("ERROR: channel open result: %s\n", result_buf);
  1347. goto evt_fin;
  1348. }
  1349. /*
  1350. * Allocate an event
  1351. */
  1352. do {
  1353. result = saEvtEventAllocate(channel_handle, &event_handle);
  1354. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1355. if (result != SA_AIS_OK) {
  1356. get_sa_error(result, result_buf, result_buf_len);
  1357. printf("ERROR: event Allocate result: %s\n", result_buf);
  1358. goto evt_close;
  1359. }
  1360. /*
  1361. * 1. Test multiple subscriptions on a single channel and receiving
  1362. * events.
  1363. *
  1364. * Subscribe twice with two different filters. Then send three events.
  1365. * One will match the first filter, the second will match the second
  1366. * filter, the third will match none. We will validate that we receive
  1367. * two events and that the subscription IDs match what we expect for the
  1368. * given pattern.
  1369. */
  1370. sub_filt.filters = filt1;
  1371. do {
  1372. result = saEvtEventSubscribe(channel_handle,
  1373. &sub_filt,
  1374. sub1);
  1375. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1376. if (result != SA_AIS_OK) {
  1377. get_sa_error(result, result_buf, result_buf_len);
  1378. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1379. goto evt_free;
  1380. }
  1381. sub_filt.filters = filt2;
  1382. do {
  1383. result = saEvtEventSubscribe(channel_handle,
  1384. &sub_filt,
  1385. sub2);
  1386. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1387. if (result != SA_AIS_OK) {
  1388. get_sa_error(result, result_buf, result_buf_len);
  1389. printf("ERROR: event subscribe(2) result: %s\n", result_buf);
  1390. goto evt_free;
  1391. }
  1392. retention_time = 0ULL;
  1393. evt_pat.patterns = &pat1;
  1394. do {
  1395. result = saEvtEventAttributesSet(event_handle,
  1396. &evt_pat,
  1397. TEST_PRIORITY,
  1398. retention_time,
  1399. &test_pub_name);
  1400. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1401. if (result != SA_AIS_OK) {
  1402. get_sa_error(result, result_buf, result_buf_len);
  1403. printf("ERROR: event set attr result(1): %s\n", result_buf);
  1404. goto evt_free;
  1405. }
  1406. do {
  1407. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1408. &event_id1);
  1409. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1410. if (result != SA_AIS_OK) {
  1411. get_sa_error(result, result_buf, result_buf_len);
  1412. printf("ERROR: event Publish result:(1) %s\n", result_buf);
  1413. goto evt_close;
  1414. }
  1415. evt_pat.patterns = &pat2;
  1416. do {
  1417. result = saEvtEventAttributesSet(event_handle,
  1418. &evt_pat,
  1419. TEST_PRIORITY,
  1420. retention_time,
  1421. &test_pub_name);
  1422. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1423. if (result != SA_AIS_OK) {
  1424. get_sa_error(result, result_buf, result_buf_len);
  1425. printf("ERROR: event set attr result(2): %s\n", result_buf);
  1426. goto evt_free;
  1427. }
  1428. do {
  1429. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1430. &event_id2);
  1431. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1432. if (result != SA_AIS_OK) {
  1433. get_sa_error(result, result_buf, result_buf_len);
  1434. printf("ERROR: event Publish result:(2) %s\n", result_buf);
  1435. goto evt_close;
  1436. }
  1437. evt_pat.patterns = &pat3;
  1438. do {
  1439. result = saEvtEventAttributesSet(event_handle,
  1440. &evt_pat,
  1441. TEST_PRIORITY,
  1442. retention_time,
  1443. &test_pub_name);
  1444. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1445. if (result != SA_AIS_OK) {
  1446. get_sa_error(result, result_buf, result_buf_len);
  1447. printf("ERROR: event set attr result(3): %s\n", result_buf);
  1448. goto evt_free;
  1449. }
  1450. do {
  1451. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1452. &event_id3);
  1453. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1454. if (result != SA_AIS_OK) {
  1455. get_sa_error(result, result_buf, result_buf_len);
  1456. printf("ERROR: event Publish result:(3) %s\n", result_buf);
  1457. goto evt_close;
  1458. }
  1459. /*
  1460. * See if we got the event
  1461. */
  1462. do {
  1463. result = saEvtSelectionObjectGet(handle, &fd);
  1464. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1465. if (result != SA_AIS_OK) {
  1466. get_sa_error(result, result_buf, result_buf_len);
  1467. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1468. /* error */
  1469. return;
  1470. }
  1471. while(1) {
  1472. pfd.fd = fd;
  1473. pfd.events = POLLIN;
  1474. nfd = poll(&pfd, 1, timeout);
  1475. if (nfd == 0) {
  1476. break;
  1477. } else if (nfd < 0) {
  1478. perror("ERROR: poll error");
  1479. break;
  1480. }
  1481. do {
  1482. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  1483. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1484. if (result != SA_AIS_OK) {
  1485. get_sa_error(result, result_buf, result_buf_len);
  1486. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1487. /* error */
  1488. goto evt_free;
  1489. }
  1490. }
  1491. if (call_count != 2) {
  1492. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  1493. goto evt_free;
  1494. }
  1495. /*
  1496. * Test cleanup
  1497. */
  1498. evt_free:
  1499. do {
  1500. result = saEvtEventFree(event_handle);
  1501. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1502. if (result != SA_AIS_OK) {
  1503. get_sa_error(result, result_buf, result_buf_len);
  1504. printf("ERROR: event free result: %s\n", result_buf);
  1505. }
  1506. evt_close:
  1507. do {
  1508. result = saEvtChannelClose(channel_handle);
  1509. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1510. if (result != SA_AIS_OK) {
  1511. get_sa_error(result, result_buf, result_buf_len);
  1512. printf("ERROR: channel close result: %s\n", result_buf);
  1513. }
  1514. evt_fin:
  1515. do {
  1516. result = saEvtFinalize(handle);
  1517. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1518. if (result != SA_AIS_OK) {
  1519. get_sa_error(result, result_buf, result_buf_len);
  1520. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1521. }
  1522. printf("Done\n");
  1523. }
  1524. /*
  1525. * Handle call back for multi-test2
  1526. * Counts events received. Makes sure that we get one event from
  1527. * each subscription.
  1528. *
  1529. */
  1530. void
  1531. multi_test_callback2(SaEvtSubscriptionIdT my_subscription_id,
  1532. const SaEvtEventHandleT event_handle,
  1533. const SaSizeT my_event_data_size)
  1534. {
  1535. SaAisErrorT result;
  1536. SaUint8T my_priority;
  1537. SaTimeT my_retention_time;
  1538. SaNameT my_publisher_name = {0, {0}};
  1539. SaTimeT my_publish_time;
  1540. SaEvtEventIdT my_event_id;
  1541. SaEvtSubscriptionIdT last_sub_id = 0;
  1542. printf(" multi_test_callback2 called(%d)\n", ++call_count);
  1543. evt_pat_get_array.patternsNumber = 4;
  1544. do {
  1545. result = saEvtEventAttributesGet(event_handle,
  1546. &evt_pat_get_array, /* patterns */
  1547. &my_priority, /* priority */
  1548. &my_retention_time, /* retention time */
  1549. &my_publisher_name, /* publisher name */
  1550. &my_publish_time, /* publish time */
  1551. &my_event_id /* event_id */
  1552. );
  1553. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1554. if (result != SA_AIS_OK) {
  1555. get_sa_error(result, result_buf, result_buf_len);
  1556. printf("ERROR: event get attr result: %s\n", result_buf);
  1557. goto evt_free;
  1558. }
  1559. if (my_event_id != event_id1) {
  1560. printf("ERROR: Received wrong event\n");
  1561. goto evt_free;
  1562. }
  1563. if (last_sub_id == 0) {
  1564. if (my_subscription_id != sub1 &&
  1565. my_subscription_id != sub2) {
  1566. printf("ERROR: Received bad subscription ID\n");
  1567. goto evt_free;
  1568. }
  1569. last_sub_id = my_subscription_id;
  1570. } else {
  1571. if (my_subscription_id == last_sub_id) {
  1572. printf("ERROR: Received subscription ID twice\n");
  1573. goto evt_free;
  1574. }
  1575. if (my_subscription_id != sub1 &&
  1576. my_subscription_id != sub2) {
  1577. printf("ERROR: Received bad subscription ID\n");
  1578. goto evt_free;
  1579. }
  1580. }
  1581. if (evt_pat_get_array.patternsNumber != 1) {
  1582. printf("ERROR: pattern array count not 1: %lld\n",
  1583. evt_pat_get_array.patternsNumber);
  1584. }
  1585. evt_free:
  1586. do {
  1587. result = saEvtEventFree(event_handle);
  1588. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1589. if (result != SA_AIS_OK) {
  1590. get_sa_error(result, result_buf, result_buf_len);
  1591. printf("ERROR: event free result: %s\n", result_buf);
  1592. }
  1593. }
  1594. void
  1595. test_multi_channel2()
  1596. {
  1597. SaEvtEventFilterT filt1[1] = {
  1598. {SA_EVT_EXACT_FILTER, {8, 8, (SaUint8T *) "ChanPat1"}},
  1599. };
  1600. SaEvtEventFilterArrayT sub_filt = {
  1601. 1, NULL
  1602. };
  1603. SaEvtEventPatternT pat1 = {8, 8, (SaUint8T *) "ChanPat1"};
  1604. SaEvtEventPatternArrayT evt_pat = {
  1605. 1, 1, NULL
  1606. };
  1607. SaEvtHandleT handle;
  1608. SaEvtChannelHandleT channel_handle;
  1609. SaEvtChannelHandleT channel_handle1;
  1610. SaEvtEventHandleT event_handle;
  1611. SaEvtChannelOpenFlagsT flags;
  1612. SaNameT channel_name;
  1613. SaEvtCallbacksT multi_callbacks = {
  1614. 0,
  1615. multi_test_callback2
  1616. };
  1617. struct pollfd pfd;
  1618. int nfd;
  1619. SaSelectionObjectT fd;
  1620. int timeout = 5000;
  1621. SaAisErrorT result;
  1622. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1623. SA_EVT_CHANNEL_CREATE;
  1624. strcpy((char *)channel_name.value, channel);
  1625. channel_name.length = strlen(channel);
  1626. /*
  1627. * 2. Test multiple openings of a single channel and receving events.
  1628. *
  1629. * Open and subscribe to a channel twice. When an event is sent, it
  1630. * should be delivered twice, once for each open channel.
  1631. */
  1632. printf("Test multiple opens/subscribes:\n");
  1633. do {
  1634. result = saEvtInitialize (&handle, &multi_callbacks,
  1635. versions[0].version);
  1636. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1637. if (result != SA_AIS_OK) {
  1638. get_sa_error(result, result_buf, result_buf_len);
  1639. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1640. return;
  1641. }
  1642. do {
  1643. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  1644. &channel_handle);
  1645. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1646. if (result != SA_AIS_OK) {
  1647. get_sa_error(result, result_buf, result_buf_len);
  1648. printf("ERROR: channel open(0) result: %s\n", result_buf);
  1649. goto evt_fin;
  1650. }
  1651. do {
  1652. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  1653. &channel_handle1);
  1654. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1655. if (result != SA_AIS_OK) {
  1656. get_sa_error(result, result_buf, result_buf_len);
  1657. printf("ERROR: channel open(1) result: %s\n", result_buf);
  1658. goto evt_fin;
  1659. }
  1660. do {
  1661. result = saEvtEventAllocate(channel_handle, &event_handle);
  1662. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1663. if (result != SA_AIS_OK) {
  1664. get_sa_error(result, result_buf, result_buf_len);
  1665. printf("ERROR: event Allocate result: %s\n", result_buf);
  1666. goto evt_close;
  1667. }
  1668. sub_filt.filters = filt1;
  1669. do {
  1670. result = saEvtEventSubscribe(channel_handle,
  1671. &sub_filt,
  1672. sub1);
  1673. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1674. if (result != SA_AIS_OK) {
  1675. get_sa_error(result, result_buf, result_buf_len);
  1676. printf("ERROR: event subscribe(0) result: %s\n", result_buf);
  1677. goto evt_free;
  1678. }
  1679. sub_filt.filters = filt1;
  1680. do {
  1681. result = saEvtEventSubscribe(channel_handle1,
  1682. &sub_filt,
  1683. sub2);
  1684. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1685. if (result != SA_AIS_OK) {
  1686. get_sa_error(result, result_buf, result_buf_len);
  1687. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1688. goto evt_free;
  1689. }
  1690. retention_time = 0ULL;
  1691. evt_pat.patterns = &pat1;
  1692. do {
  1693. result = saEvtEventAttributesSet(event_handle,
  1694. &evt_pat,
  1695. TEST_PRIORITY,
  1696. retention_time,
  1697. &test_pub_name);
  1698. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1699. if (result != SA_AIS_OK) {
  1700. get_sa_error(result, result_buf, result_buf_len);
  1701. printf("ERROR: event set attr result: %s\n", result_buf);
  1702. goto evt_free;
  1703. }
  1704. do {
  1705. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1706. &event_id1);
  1707. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1708. if (result != SA_AIS_OK) {
  1709. get_sa_error(result, result_buf, result_buf_len);
  1710. printf("ERROR: event Publish result: %s\n", result_buf);
  1711. goto evt_close;
  1712. }
  1713. /*
  1714. * See if we got the event
  1715. */
  1716. do {
  1717. result = saEvtSelectionObjectGet(handle, &fd);
  1718. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1719. if (result != SA_AIS_OK) {
  1720. get_sa_error(result, result_buf, result_buf_len);
  1721. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1722. /* error */
  1723. return;
  1724. }
  1725. call_count = 0;
  1726. while(1) {
  1727. pfd.fd = fd;
  1728. pfd.events = POLLIN;
  1729. nfd = poll(&pfd, 1, timeout);
  1730. if (nfd == 0) {
  1731. break;
  1732. } else if (nfd < 0) {
  1733. perror("ERROR: poll error");
  1734. break;
  1735. }
  1736. do {
  1737. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  1738. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1739. if (result != SA_AIS_OK) {
  1740. get_sa_error(result, result_buf, result_buf_len);
  1741. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1742. /* error */
  1743. goto evt_free;
  1744. }
  1745. }
  1746. if (call_count != 2) {
  1747. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  1748. goto evt_free;
  1749. }
  1750. /*
  1751. * Test cleanup
  1752. */
  1753. evt_free:
  1754. do {
  1755. result = saEvtEventFree(event_handle);
  1756. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1757. if (result != SA_AIS_OK) {
  1758. get_sa_error(result, result_buf, result_buf_len);
  1759. printf("ERROR: event free result: %s\n", result_buf);
  1760. }
  1761. evt_close:
  1762. do {
  1763. result = saEvtChannelClose(channel_handle);
  1764. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1765. if (result != SA_AIS_OK) {
  1766. get_sa_error(result, result_buf, result_buf_len);
  1767. printf("ERROR: channel close result(0): %s\n", result_buf);
  1768. }
  1769. do {
  1770. result = saEvtChannelClose(channel_handle1);
  1771. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1772. if (result != SA_AIS_OK) {
  1773. get_sa_error(result, result_buf, result_buf_len);
  1774. printf("ERROR: channel close result(1): %s\n", result_buf);
  1775. }
  1776. evt_fin:
  1777. do {
  1778. result = saEvtFinalize(handle);
  1779. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1780. if (result != SA_AIS_OK) {
  1781. get_sa_error(result, result_buf, result_buf_len);
  1782. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1783. }
  1784. printf("Done\n");
  1785. }
  1786. /*
  1787. * Handle call back for multi-test3
  1788. * Verifies that the event recevied is associated with the correct
  1789. * subscription.
  1790. *
  1791. */
  1792. void
  1793. multi_test_callback3(SaEvtSubscriptionIdT my_subscription_id,
  1794. const SaEvtEventHandleT event_handle,
  1795. const SaSizeT my_event_data_size)
  1796. {
  1797. SaAisErrorT result;
  1798. SaUint8T my_priority;
  1799. SaTimeT my_retention_time;
  1800. SaNameT my_publisher_name = {0, {0}};
  1801. SaTimeT my_publish_time;
  1802. SaEvtEventIdT my_event_id;
  1803. printf(" multi_test_callback2 called(%d)\n", ++call_count);
  1804. evt_pat_get_array.patternsNumber = 4;
  1805. do {
  1806. result = saEvtEventAttributesGet(event_handle,
  1807. &evt_pat_get_array, /* patterns */
  1808. &my_priority, /* priority */
  1809. &my_retention_time, /* retention time */
  1810. &my_publisher_name, /* publisher name */
  1811. &my_publish_time, /* publish time */
  1812. &my_event_id /* event_id */
  1813. );
  1814. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1815. if (result != SA_AIS_OK) {
  1816. get_sa_error(result, result_buf, result_buf_len);
  1817. printf("ERROR: event get attr result: %s\n", result_buf);
  1818. goto evt_free;
  1819. }
  1820. if ((my_subscription_id != sub1) && (my_subscription_id != sub2)) {
  1821. printf("ERROR: Received wrong subscription ID %x\n",
  1822. my_subscription_id);
  1823. printf(" sub1 %x, sub2 %x\n", sub1, sub2);
  1824. goto evt_free;
  1825. }
  1826. if ((my_event_id != event_id1) && (my_event_id != event_id2)) {
  1827. printf("ERROR: Received wrong event ID %llx\n",
  1828. (unsigned long long)my_event_id);
  1829. printf(" id1 %llx, id2 %llx\n",
  1830. (unsigned long long)event_id1,
  1831. (unsigned long long)event_id2);
  1832. goto evt_free;
  1833. }
  1834. if ((my_subscription_id == sub1) && (my_event_id != event_id1)) {
  1835. printf("ERROR: Received event on wrong subscription\n");
  1836. goto evt_free;
  1837. }
  1838. if ((my_subscription_id == sub2) && (my_event_id != event_id2)) {
  1839. printf("ERROR: Received event on wrong subscription\n");
  1840. goto evt_free;
  1841. }
  1842. if (evt_pat_get_array.patternsNumber != 1) {
  1843. printf("ERROR: pattern array count not 1: %lld\n",
  1844. evt_pat_get_array.patternsNumber);
  1845. }
  1846. evt_free:
  1847. do {
  1848. result = saEvtEventFree(event_handle);
  1849. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1850. if (result != SA_AIS_OK) {
  1851. get_sa_error(result, result_buf, result_buf_len);
  1852. printf("ERROR: event free result: %s\n", result_buf);
  1853. }
  1854. }
  1855. void
  1856. test_multi_channel3()
  1857. {
  1858. SaEvtEventFilterT filt1[1] = {
  1859. {SA_EVT_PREFIX_FILTER, {7, 7, (SaUint8T *) "ChanPat"}},
  1860. };
  1861. SaEvtEventFilterArrayT sub_filt = {
  1862. 1, NULL
  1863. };
  1864. SaEvtEventPatternT pat1 = {8, 8, (SaUint8T *) "ChanPat1"};
  1865. SaEvtEventPatternT pat2 = {8, 8, (SaUint8T *) "ChanPat2"};
  1866. SaEvtEventPatternArrayT evt_pat = {
  1867. 1, 1, NULL
  1868. };
  1869. SaEvtHandleT handle;
  1870. SaEvtChannelHandleT channel_handle;
  1871. SaEvtChannelHandleT channel_handle1;
  1872. SaEvtEventHandleT event_handle;
  1873. SaEvtEventHandleT event_handle1;
  1874. SaEvtChannelOpenFlagsT flags;
  1875. SaNameT channel_name;
  1876. SaNameT channel_name1;
  1877. SaEvtCallbacksT multi_callbacks = {
  1878. 0,
  1879. multi_test_callback3
  1880. };
  1881. struct pollfd pfd;
  1882. int nfd;
  1883. SaSelectionObjectT fd;
  1884. int timeout = 5000;
  1885. SaAisErrorT result;
  1886. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1887. SA_EVT_CHANNEL_CREATE;
  1888. strcpy((char *)channel_name.value, channel);
  1889. channel_name.length = strlen((char *)channel_name.value);
  1890. strcpy((char *)channel_name1.value, channel);
  1891. strcat((char *)channel_name1.value, "_1");
  1892. channel_name1.length = strlen((char *)channel_name1.value);
  1893. /*
  1894. * 3. Test opening of multiple channels and receiving events.
  1895. * Open and subscribe to two different channels twice.
  1896. * Subscribe to each channel with the same filters.
  1897. * Sending an event on one channel should be received in the
  1898. * call-back with the subscription ID corresponding to the sent
  1899. * channel.
  1900. */
  1901. printf("Test multiple different channels/subscribes:\n");
  1902. do {
  1903. result = saEvtInitialize (&handle, &multi_callbacks,
  1904. versions[0].version);
  1905. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1906. if (result != SA_AIS_OK) {
  1907. get_sa_error(result, result_buf, result_buf_len);
  1908. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1909. return;
  1910. }
  1911. do {
  1912. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  1913. &channel_handle);
  1914. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1915. if (result != SA_AIS_OK) {
  1916. get_sa_error(result, result_buf, result_buf_len);
  1917. printf("ERROR: channel open(0) result: %s\n", result_buf);
  1918. goto evt_fin;
  1919. }
  1920. do {
  1921. result = saEvtChannelOpen(handle, &channel_name1, flags, SA_TIME_MAX,
  1922. &channel_handle1);
  1923. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1924. if (result != SA_AIS_OK) {
  1925. get_sa_error(result, result_buf, result_buf_len);
  1926. printf("ERROR: channel open(1) result: %s\n", result_buf);
  1927. goto evt_fin;
  1928. }
  1929. do {
  1930. result = saEvtEventAllocate(channel_handle, &event_handle);
  1931. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1932. if (result != SA_AIS_OK) {
  1933. get_sa_error(result, result_buf, result_buf_len);
  1934. printf("ERROR: event Allocate(0) result: %s\n", result_buf);
  1935. goto evt_close;
  1936. }
  1937. do {
  1938. result = saEvtEventAllocate(channel_handle1, &event_handle1);
  1939. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1940. if (result != SA_AIS_OK) {
  1941. get_sa_error(result, result_buf, result_buf_len);
  1942. printf("ERROR: event Allocate(1) result: %s\n", result_buf);
  1943. goto evt_close;
  1944. }
  1945. sub_filt.filters = filt1;
  1946. do {
  1947. result = saEvtEventSubscribe(channel_handle,
  1948. &sub_filt,
  1949. sub1);
  1950. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1951. if (result != SA_AIS_OK) {
  1952. get_sa_error(result, result_buf, result_buf_len);
  1953. printf("ERROR: event subscribe(0) result: %s\n", result_buf);
  1954. goto evt_free;
  1955. }
  1956. sub_filt.filters = filt1;
  1957. do {
  1958. result = saEvtEventSubscribe(channel_handle1,
  1959. &sub_filt,
  1960. sub2);
  1961. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1962. if (result != SA_AIS_OK) {
  1963. get_sa_error(result, result_buf, result_buf_len);
  1964. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1965. goto evt_free;
  1966. }
  1967. retention_time = 0ULL;
  1968. evt_pat.patterns = &pat1;
  1969. do {
  1970. result = saEvtEventAttributesSet(event_handle,
  1971. &evt_pat,
  1972. TEST_PRIORITY,
  1973. retention_time,
  1974. &test_pub_name);
  1975. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1976. if (result != SA_AIS_OK) {
  1977. get_sa_error(result, result_buf, result_buf_len);
  1978. printf("ERROR: event set attr(0) result: %s\n", result_buf);
  1979. goto evt_free;
  1980. }
  1981. evt_pat.patterns = &pat2;
  1982. do {
  1983. result = saEvtEventAttributesSet(event_handle1,
  1984. &evt_pat,
  1985. TEST_PRIORITY,
  1986. retention_time,
  1987. &test_pub_name);
  1988. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1989. if (result != SA_AIS_OK) {
  1990. get_sa_error(result, result_buf, result_buf_len);
  1991. printf("ERROR: event set attr(1) result: %s\n", result_buf);
  1992. goto evt_free;
  1993. }
  1994. do {
  1995. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1996. &event_id1);
  1997. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  1998. if (result != SA_AIS_OK) {
  1999. get_sa_error(result, result_buf, result_buf_len);
  2000. printf("ERROR: event Publish result: %s\n", result_buf);
  2001. goto evt_close;
  2002. }
  2003. do {
  2004. result = saEvtEventPublish(event_handle1, exp_data, DATA_SIZE,
  2005. &event_id2);
  2006. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2007. if (result != SA_AIS_OK) {
  2008. get_sa_error(result, result_buf, result_buf_len);
  2009. printf("ERROR: event Publish result: %s\n", result_buf);
  2010. goto evt_close;
  2011. }
  2012. /*
  2013. * See if we got the events
  2014. */
  2015. do {
  2016. result = saEvtSelectionObjectGet(handle, &fd);
  2017. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2018. if (result != SA_AIS_OK) {
  2019. get_sa_error(result, result_buf, result_buf_len);
  2020. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  2021. /* error */
  2022. return;
  2023. }
  2024. call_count = 0;
  2025. while(1) {
  2026. pfd.fd = fd;
  2027. pfd.events = POLLIN;
  2028. nfd = poll(&pfd, 1, timeout);
  2029. if (nfd == 0) {
  2030. break;
  2031. } else if (nfd < 0) {
  2032. perror("ERROR: poll error");
  2033. break;
  2034. }
  2035. do {
  2036. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  2037. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2038. if (result != SA_AIS_OK) {
  2039. get_sa_error(result, result_buf, result_buf_len);
  2040. printf("ERROR: saEvtDispatch %s\n", result_buf);
  2041. /* error */
  2042. goto evt_free;
  2043. }
  2044. }
  2045. if (call_count != 2) {
  2046. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  2047. goto evt_free;
  2048. }
  2049. /*
  2050. * Test cleanup
  2051. */
  2052. evt_free:
  2053. do {
  2054. result = saEvtEventFree(event_handle);
  2055. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2056. if (result != SA_AIS_OK) {
  2057. get_sa_error(result, result_buf, result_buf_len);
  2058. printf("ERROR: event free result: %s\n", result_buf);
  2059. }
  2060. do {
  2061. result = saEvtEventFree(event_handle1);
  2062. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2063. if (result != SA_AIS_OK) {
  2064. get_sa_error(result, result_buf, result_buf_len);
  2065. printf("ERROR: event free result: %s\n", result_buf);
  2066. }
  2067. evt_close:
  2068. do {
  2069. result = saEvtChannelClose(channel_handle);
  2070. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2071. if (result != SA_AIS_OK) {
  2072. get_sa_error(result, result_buf, result_buf_len);
  2073. printf("ERROR: channel close result(0): %s\n", result_buf);
  2074. }
  2075. do {
  2076. result = saEvtChannelClose(channel_handle1);
  2077. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2078. if (result != SA_AIS_OK) {
  2079. get_sa_error(result, result_buf, result_buf_len);
  2080. printf("ERROR: channel close result(1): %s\n", result_buf);
  2081. }
  2082. evt_fin:
  2083. do {
  2084. result = saEvtFinalize(handle);
  2085. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2086. if (result != SA_AIS_OK) {
  2087. get_sa_error(result, result_buf, result_buf_len);
  2088. printf("ERROR: Event Finalize result: %s\n", result_buf);
  2089. }
  2090. printf("Done\n");
  2091. }
  2092. /*
  2093. * Test event retention
  2094. * Test 1: publish the event with a retention time and then
  2095. * subscribe. If the event was retained, we should receive it.
  2096. *
  2097. * Test 2: Publish the event, sleep until it expires, then
  2098. * subscribe. We shouldn't get an event delivered.
  2099. *
  2100. * Test 3: Publish an event with a retention time.
  2101. * subscribe.
  2102. * wait for it.
  2103. * unsubscribe
  2104. * Clear it.
  2105. * Then subscribe and make sure that the messages isn't delivered.
  2106. *
  2107. */
  2108. #define EXPIRE_TIME 10 /* Seconds */
  2109. SaEvtEventIdT retained_id;
  2110. int got_event;
  2111. void
  2112. event_callback_retained(SaEvtSubscriptionIdT my_subscription_id,
  2113. const SaEvtEventHandleT event_handle,
  2114. const SaSizeT my_event_data_size)
  2115. {
  2116. SaAisErrorT result;
  2117. //printf("event_callback_retained called\n");
  2118. do {
  2119. result = saEvtEventAttributesGet(event_handle,
  2120. 0, /* patterns */
  2121. 0, /* priority */
  2122. 0, /* retention time */
  2123. 0, /* publisher name */
  2124. 0, /* publish time */
  2125. &retained_id /* event_id */
  2126. );
  2127. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2128. if (result != SA_AIS_OK) {
  2129. get_sa_error(result, result_buf, result_buf_len);
  2130. printf("ERROR: callback attr get result: %s\n", result_buf);
  2131. return;
  2132. }
  2133. got_event = 1;
  2134. }
  2135. void
  2136. test_retention()
  2137. {
  2138. SaEvtHandleT handle;
  2139. SaEvtChannelHandleT channel_handle;
  2140. SaEvtEventHandleT event_handle;
  2141. SaEvtChannelOpenFlagsT flags;
  2142. SaNameT channel_name;
  2143. SaEvtCallbacksT callbacks_retain = {
  2144. 0,
  2145. event_callback_retained
  2146. };
  2147. struct pollfd pfd;
  2148. int nfd;
  2149. SaSelectionObjectT fd;
  2150. int timeout = (EXPIRE_TIME + 5);
  2151. SaAisErrorT result;
  2152. flags = SA_EVT_CHANNEL_PUBLISHER |
  2153. SA_EVT_CHANNEL_SUBSCRIBER |
  2154. SA_EVT_CHANNEL_CREATE;
  2155. strcpy((char *)channel_name.value, channel);
  2156. channel_name.length = strlen(channel);
  2157. printf("Test Event retention:\n");
  2158. do {
  2159. result = saEvtInitialize (&handle, &callbacks_retain,
  2160. versions[0].version);
  2161. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2162. if (result != SA_AIS_OK) {
  2163. get_sa_error(result, result_buf, result_buf_len);
  2164. printf("ERROR: Event Initialize result: %s\n", result_buf);
  2165. return;
  2166. }
  2167. do {
  2168. result = saEvtSelectionObjectGet(handle, &fd);
  2169. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2170. if (result != SA_AIS_OK) {
  2171. get_sa_error(result, result_buf, result_buf_len);
  2172. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  2173. /* error */
  2174. return;
  2175. }
  2176. do {
  2177. result = saEvtChannelOpen(handle, &channel_name, flags, SA_TIME_MAX,
  2178. &channel_handle);
  2179. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2180. if (result != SA_AIS_OK) {
  2181. get_sa_error(result, result_buf, result_buf_len);
  2182. printf("ERROR: channel open result: %s\n", result_buf);
  2183. goto evt_fin;
  2184. }
  2185. /*
  2186. * Allocate an event
  2187. */
  2188. do {
  2189. result = saEvtEventAllocate(channel_handle, &event_handle);
  2190. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2191. if (result != SA_AIS_OK) {
  2192. get_sa_error(result, result_buf, result_buf_len);
  2193. printf("ERROR: event Allocate result: %s\n", result_buf);
  2194. goto evt_close;
  2195. }
  2196. retention_time = (EXPIRE_TIME)*1000000000ULL;
  2197. do {
  2198. result = saEvtEventAttributesSet(event_handle,
  2199. &evt_pat_set_array,
  2200. TEST_PRIORITY,
  2201. retention_time,
  2202. &test_pub_name);
  2203. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2204. if (result != SA_AIS_OK) {
  2205. get_sa_error(result, result_buf, result_buf_len);
  2206. printf("ERROR: event set attr result: %s\n", result_buf);
  2207. goto evt_free;
  2208. }
  2209. /*
  2210. * Test 1: publish the event with a retention time and then
  2211. * subscribe. If the event was retained, we should receive it.
  2212. */
  2213. printf(" Receive retained event\n");
  2214. got_event=0;
  2215. retained_id=0;
  2216. do {
  2217. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  2218. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2219. if (result != SA_AIS_OK) {
  2220. get_sa_error(result, result_buf, result_buf_len);
  2221. printf("ERROR: event Publish result(1): %s\n", result_buf);
  2222. goto evt_close;
  2223. }
  2224. do {
  2225. result = saEvtEventSubscribe(channel_handle,
  2226. &subscribe_filters,
  2227. subscription_id);
  2228. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2229. if (result != SA_AIS_OK) {
  2230. get_sa_error(result, result_buf, result_buf_len);
  2231. printf("ERROR: event subscribe result: %s\n", result_buf);
  2232. goto evt_free;
  2233. }
  2234. pfd.fd = fd;
  2235. pfd.events = POLLIN;
  2236. nfd = poll(&pfd, 1, 1000);
  2237. if (nfd <= 0) {
  2238. printf("ERROR: poll fds %d\n", nfd);
  2239. if (nfd < 0) {
  2240. perror("ERROR: poll error");
  2241. }
  2242. /* Error */
  2243. goto evt_free;
  2244. }
  2245. do {
  2246. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  2247. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2248. if (result != SA_AIS_OK) {
  2249. get_sa_error(result, result_buf, result_buf_len);
  2250. printf("ERROR: saEvtDispatch %s\n", result_buf);
  2251. /* error */
  2252. goto evt_free;
  2253. }
  2254. if (!got_event) {
  2255. printf("ERROR: retained event not recevied\n");
  2256. goto evt_free;
  2257. }
  2258. if (retained_id != event_id) {
  2259. printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
  2260. (unsigned long long)event_id,
  2261. (unsigned long long)retained_id);
  2262. goto evt_free;
  2263. }
  2264. do {
  2265. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  2266. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2267. if (result != SA_AIS_OK) {
  2268. get_sa_error(result, result_buf, result_buf_len);
  2269. printf("ERROR: unsubscribe result: %s\n", result_buf);
  2270. goto evt_free;
  2271. }
  2272. /*
  2273. * Test 2: Publish the event, sleep until it expires, then
  2274. * subscribe. We shouldn't get an event delivered.
  2275. */
  2276. printf(" Expire retained event\n");
  2277. got_event=0;
  2278. retained_id=0;
  2279. do {
  2280. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  2281. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2282. if (result != SA_AIS_OK) {
  2283. get_sa_error(result, result_buf, result_buf_len);
  2284. printf("ERROR: event Publish result(1): %s\n", result_buf);
  2285. goto evt_close;
  2286. }
  2287. do {
  2288. result = saEvtSelectionObjectGet(handle, &fd);
  2289. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2290. if (result != SA_AIS_OK) {
  2291. get_sa_error(result, result_buf, result_buf_len);
  2292. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  2293. /* error */
  2294. return;
  2295. }
  2296. /*
  2297. * Wait for the event to expire, then subscribe. We shouldn't get
  2298. * an event
  2299. */
  2300. sleep(timeout);
  2301. do {
  2302. result = saEvtEventSubscribe(channel_handle,
  2303. &subscribe_filters,
  2304. subscription_id);
  2305. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2306. if (result != SA_AIS_OK) {
  2307. get_sa_error(result, result_buf, result_buf_len);
  2308. printf("ERROR: event subscribe result: %s\n", result_buf);
  2309. result = saEvtChannelClose(channel_handle);
  2310. goto evt_free;
  2311. }
  2312. pfd.fd = fd;
  2313. pfd.events = POLLIN;
  2314. nfd = poll(&pfd, 1, 1000);
  2315. if (nfd != 0) {
  2316. printf("ERROR: poll fds %d\n", nfd);
  2317. if (nfd < 0) {
  2318. perror("ERROR: poll error");
  2319. }
  2320. /* Error */
  2321. goto evt_free;
  2322. }
  2323. do {
  2324. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  2325. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2326. if (result != SA_AIS_OK) {
  2327. get_sa_error(result, result_buf, result_buf_len);
  2328. printf("ERROR: unsubscribe result: %s\n", result_buf);
  2329. goto evt_free;
  2330. }
  2331. /*
  2332. * Test 3:
  2333. * Publish an event with a retention time.
  2334. * subscribe.
  2335. * wait for it.
  2336. * unsubscribe
  2337. * Clear it.
  2338. * Then subscribe and make sure that the message isn't delivered.
  2339. */
  2340. printf(" Clear event retention time\n");
  2341. got_event=0;
  2342. retained_id=0;
  2343. do {
  2344. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  2345. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2346. if (result != SA_AIS_OK) {
  2347. get_sa_error(result, result_buf, result_buf_len);
  2348. printf("ERROR: event Publish result(2): %s\n", result_buf);
  2349. goto evt_free;
  2350. }
  2351. do {
  2352. result = saEvtEventSubscribe(channel_handle,
  2353. &subscribe_filters,
  2354. subscription_id);
  2355. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2356. if (result != SA_AIS_OK) {
  2357. get_sa_error(result, result_buf, result_buf_len);
  2358. printf("ERROR: event subscribe result: %s\n", result_buf);
  2359. goto evt_free;
  2360. }
  2361. pfd.fd = fd;
  2362. pfd.events = POLLIN;
  2363. nfd = poll(&pfd, 1, 1000);
  2364. if (nfd <= 0) {
  2365. printf("ERROR: poll fds %d\n", nfd);
  2366. if (nfd < 0) {
  2367. perror("ERROR: poll error");
  2368. }
  2369. /* Error */
  2370. goto evt_free;
  2371. }
  2372. do {
  2373. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  2374. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2375. if (result != SA_AIS_OK) {
  2376. get_sa_error(result, result_buf, result_buf_len);
  2377. printf("ERROR: saEvtDispatch %s\n", result_buf);
  2378. /* error */
  2379. goto evt_free;
  2380. }
  2381. if (!got_event) {
  2382. printf("ERROR: retained event not recevied\n");
  2383. goto evt_free;
  2384. }
  2385. if (retained_id != event_id) {
  2386. printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
  2387. (unsigned long long)event_id,
  2388. (unsigned long long)retained_id);
  2389. goto evt_free;
  2390. }
  2391. do {
  2392. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  2393. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2394. if (result != SA_AIS_OK) {
  2395. get_sa_error(result, result_buf, result_buf_len);
  2396. printf("ERROR: unsubscribe result: %s\n", result_buf);
  2397. goto evt_free;
  2398. }
  2399. do {
  2400. result = saEvtEventRetentionTimeClear(channel_handle, event_id);
  2401. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2402. if (result != SA_AIS_OK) {
  2403. get_sa_error(result, result_buf, result_buf_len);
  2404. printf("ERROR: clear retention time result: %s\n", result_buf);
  2405. goto evt_free;
  2406. }
  2407. do {
  2408. result = saEvtEventSubscribe(channel_handle,
  2409. &subscribe_filters,
  2410. subscription_id);
  2411. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2412. if (result != SA_AIS_OK) {
  2413. get_sa_error(result, result_buf, result_buf_len);
  2414. printf("ERROR: event subscribe result: %s\n", result_buf);
  2415. goto evt_free;
  2416. }
  2417. pfd.fd = fd;
  2418. pfd.events = POLLIN;
  2419. nfd = poll(&pfd, 1, 1000);
  2420. if (nfd != 0) {
  2421. printf("ERROR: poll fds %d\n", nfd);
  2422. if (nfd < 0) {
  2423. perror("ERROR: poll error");
  2424. }
  2425. /* Error */
  2426. goto evt_free;
  2427. }
  2428. /*
  2429. * Test cleanup
  2430. */
  2431. evt_free:
  2432. do {
  2433. result = saEvtEventFree(event_handle);
  2434. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2435. if (result != SA_AIS_OK) {
  2436. get_sa_error(result, result_buf, result_buf_len);
  2437. printf("ERROR: event free result: %s\n", result_buf);
  2438. }
  2439. evt_close:
  2440. do {
  2441. result = saEvtChannelClose(channel_handle);
  2442. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2443. if (result != SA_AIS_OK) {
  2444. get_sa_error(result, result_buf, result_buf_len);
  2445. printf("ERROR: channel close result: %s\n", result_buf);
  2446. }
  2447. evt_fin:
  2448. do {
  2449. result = saEvtFinalize(handle);
  2450. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2451. if (result != SA_AIS_OK) {
  2452. get_sa_error(result, result_buf, result_buf_len);
  2453. printf("ERROR: Event Finalize result: %s\n", result_buf);
  2454. }
  2455. printf("Done\n");
  2456. }
  2457. void
  2458. unlink_chan_callback(SaEvtSubscriptionIdT my_subscription_id,
  2459. const SaEvtEventHandleT event_handle,
  2460. const SaSizeT my_event_data_size)
  2461. {
  2462. SaAisErrorT result;
  2463. SaUint8T my_priority;
  2464. SaTimeT my_retention_time;
  2465. SaNameT my_publisher_name = {0, {0}};
  2466. SaTimeT my_publish_time;
  2467. SaEvtEventIdT my_event_id;
  2468. SaEvtSubscriptionIdT exp_sub_id;
  2469. printf(" unlink_chan_callback called(%d)\n", ++call_count);
  2470. evt_pat_get_array.patternsNumber = 4;
  2471. do {
  2472. result = saEvtEventAttributesGet(event_handle,
  2473. &evt_pat_get_array, /* patterns */
  2474. &my_priority, /* priority */
  2475. &my_retention_time, /* retention time */
  2476. &my_publisher_name, /* publisher name */
  2477. &my_publish_time, /* publish time */
  2478. &my_event_id /* event_id */
  2479. );
  2480. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2481. if (result != SA_AIS_OK) {
  2482. get_sa_error(result, result_buf, result_buf_len);
  2483. printf("ERROR: event get attr result: %s\n", result_buf);
  2484. goto evt_free;
  2485. }
  2486. if (my_event_id == event_id1) {
  2487. exp_sub_id = sub1;
  2488. } else if (my_event_id == event_id2) {
  2489. exp_sub_id = sub2;
  2490. } else {
  2491. printf("ERROR: Received event %llx but not sent\n",
  2492. (unsigned long long)my_event_id);
  2493. goto evt_free;
  2494. }
  2495. if (my_subscription_id != exp_sub_id) {
  2496. printf("ERROR: sub ID: e=%x, a=%x\n",
  2497. exp_sub_id, my_subscription_id);
  2498. goto evt_free;
  2499. }
  2500. evt_free:
  2501. do {
  2502. result = saEvtEventFree(event_handle);
  2503. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2504. if (result != SA_AIS_OK) {
  2505. get_sa_error(result, result_buf, result_buf_len);
  2506. printf("ERROR: event free result: %s\n", result_buf);
  2507. }
  2508. }
  2509. /*
  2510. * Test channel unlink operations.
  2511. * 1. Unlink channel.
  2512. * 2. Open/create a channel, close channel, open channel.
  2513. * 3. unlink channel, Open channel.
  2514. * 4. Open/create, unlink channel, close channel, open channel.
  2515. * 5. Open/create a channel, unlink channel, open/create channel, send
  2516. * event on each.
  2517. * 6. unlink all, close all.
  2518. */
  2519. SaEvtCallbacksT unlink_callbacks = {
  2520. open_callback,
  2521. unlink_chan_callback
  2522. };
  2523. void
  2524. test_unlink_channel()
  2525. {
  2526. SaEvtHandleT handle;
  2527. SaEvtChannelHandleT channel_handle1;
  2528. SaEvtChannelHandleT channel_handle2;
  2529. SaEvtEventHandleT event_handle1;
  2530. SaEvtEventHandleT event_handle2;
  2531. SaEvtChannelOpenFlagsT flags1, flags2;
  2532. SaNameT channel_name;
  2533. SaAisErrorT result;
  2534. struct pollfd pfd;
  2535. int nfd;
  2536. SaSelectionObjectT fd;
  2537. int timeout = 5000;
  2538. flags1 = SA_EVT_CHANNEL_PUBLISHER |
  2539. SA_EVT_CHANNEL_SUBSCRIBER |
  2540. SA_EVT_CHANNEL_CREATE;
  2541. flags2 = SA_EVT_CHANNEL_PUBLISHER |
  2542. SA_EVT_CHANNEL_SUBSCRIBER;
  2543. printf("Test Channel Unlink operations:\n");
  2544. do {
  2545. result = saEvtInitialize (&handle, &unlink_callbacks,
  2546. versions[0].version);
  2547. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2548. if (result != SA_AIS_OK) {
  2549. get_sa_error(result, result_buf, result_buf_len);
  2550. printf("ERROR: Event Initialize result: %s\n", result_buf);
  2551. goto unlink_exit;
  2552. }
  2553. /*
  2554. * 1. Unlink channel.
  2555. *
  2556. * Unlink previously opened channel should return OK.
  2557. * Unlink of non-existent channel should return error.
  2558. */
  2559. printf(" 1 Channel unlink:\n");
  2560. strcpy((char *)channel_name.value, channel);
  2561. channel_name.length = strlen(channel);
  2562. do {
  2563. result = saEvtChannelUnlink(handle, &channel_name);
  2564. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2565. if (result != SA_AIS_OK) {
  2566. get_sa_error(result, result_buf, result_buf_len);
  2567. printf("ERROR: channel unlink(1) result: %s\n", result_buf);
  2568. goto unlink_exit;
  2569. }
  2570. strcpy((char *)channel_name.value, unlink_channel);
  2571. channel_name.length = strlen(unlink_channel);
  2572. do {
  2573. result = saEvtChannelUnlink(handle, &channel_name);
  2574. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2575. if (result != SA_AIS_ERR_NOT_EXIST) {
  2576. get_sa_error(result, result_buf, result_buf_len);
  2577. printf("ERROR: channel unlink(2) result: %s\n", result_buf);
  2578. goto unlink_exit;
  2579. }
  2580. /*
  2581. * 2. Open/create a channel, close channel, open channel.
  2582. *
  2583. * Open/create the channel.
  2584. * Close the channel.
  2585. * Open without create. This should succeed in the B spec.
  2586. */
  2587. printf(" 2 Channel open/close/open:\n");
  2588. do {
  2589. result = saEvtChannelOpen(handle, &channel_name, flags1, SA_TIME_MAX,
  2590. &channel_handle1);
  2591. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2592. if (result != SA_AIS_OK) {
  2593. get_sa_error(result, result_buf, result_buf_len);
  2594. printf("ERROR: channel open(1) result: %s\n", result_buf);
  2595. goto unlink_exit;
  2596. }
  2597. do {
  2598. result = saEvtChannelClose(channel_handle1);
  2599. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2600. if (result != SA_AIS_OK) {
  2601. get_sa_error(result, result_buf, result_buf_len);
  2602. printf("ERROR: channel close(1) result: %s\n", result_buf);
  2603. goto unlink_exit;
  2604. }
  2605. do {
  2606. result = saEvtChannelOpen(handle, &channel_name, flags2, SA_TIME_MAX,
  2607. &channel_handle1);
  2608. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2609. if (result != SA_AIS_OK) {
  2610. get_sa_error(result, result_buf, result_buf_len);
  2611. printf("ERROR: channel open(2) result: %s\n", result_buf);
  2612. goto unlink_exit;
  2613. }
  2614. /*
  2615. * 3. unlink channel, Open channel, close channel
  2616. *
  2617. * Unlink the channel. Should mark for deletion but not
  2618. * delete it since it is already open.
  2619. * Open the channel without create. This should fail since
  2620. * the channel is marked for deletion and a new version
  2621. * hasn't been created.
  2622. * Close channel.
  2623. */
  2624. printf(" 3 Channel unlink/open/close:\n");
  2625. do {
  2626. result = saEvtChannelUnlink(handle, &channel_name);
  2627. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2628. if (result != SA_AIS_OK) {
  2629. get_sa_error(result, result_buf, result_buf_len);
  2630. printf("ERROR: channel unlink result: %s\n", result_buf);
  2631. goto unlink_exit;
  2632. }
  2633. do {
  2634. result = saEvtChannelOpen(handle, &channel_name, flags2, SA_TIME_MAX,
  2635. &channel_handle2);
  2636. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2637. if (result != SA_AIS_ERR_NOT_EXIST) {
  2638. get_sa_error(result, result_buf, result_buf_len);
  2639. printf("ERROR: channel open result: %s\n", result_buf);
  2640. goto unlink_exit;
  2641. }
  2642. do {
  2643. result = saEvtChannelClose(channel_handle1);
  2644. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2645. if (result != SA_AIS_OK) {
  2646. get_sa_error(result, result_buf, result_buf_len);
  2647. printf("ERROR: channel close(1) result: %s\n", result_buf);
  2648. goto unlink_exit;
  2649. }
  2650. /*
  2651. *
  2652. * 4. Open/create, unlink channel, close channel, open channel.
  2653. *
  2654. * Open/create the channel.
  2655. * unlink the channel.
  2656. * close the channel. This should delete the channel instance since
  2657. * it was marked for deletion.
  2658. * open the channel without create. This should fail since the
  2659. * channel doesn't exist anymore.
  2660. */
  2661. printf(" 4 Channel open/unlink/close/open:\n");
  2662. do {
  2663. result = saEvtChannelOpen(handle, &channel_name, flags1, SA_TIME_MAX,
  2664. &channel_handle1);
  2665. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2666. if (result != SA_AIS_OK) {
  2667. get_sa_error(result, result_buf, result_buf_len);
  2668. printf("ERROR: channel open(1) result: %s\n", result_buf);
  2669. goto unlink_exit;
  2670. }
  2671. do {
  2672. result = saEvtChannelUnlink(handle, &channel_name);
  2673. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2674. if (result != SA_AIS_OK) {
  2675. get_sa_error(result, result_buf, result_buf_len);
  2676. printf("ERROR: channel unlink result: %s\n", result_buf);
  2677. goto unlink_exit;
  2678. }
  2679. do {
  2680. result = saEvtChannelClose(channel_handle1);
  2681. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2682. if (result != SA_AIS_OK) {
  2683. get_sa_error(result, result_buf, result_buf_len);
  2684. printf("ERROR: channel close(1) result: %s\n", result_buf);
  2685. goto unlink_exit;
  2686. }
  2687. do {
  2688. result = saEvtChannelOpen(handle, &channel_name, flags2, SA_TIME_MAX,
  2689. &channel_handle1);
  2690. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2691. if (result != SA_AIS_ERR_NOT_EXIST) {
  2692. get_sa_error(result, result_buf, result_buf_len);
  2693. printf("ERROR: channel open(2) result: %s\n", result_buf);
  2694. goto unlink_exit;
  2695. }
  2696. /*
  2697. * 5. Open/create a channel, unlink channel, open/create channel, send
  2698. * event on each.
  2699. *
  2700. * Open/create.
  2701. * unlink. Mark for deletion.
  2702. * open/create. Create new channel of same name.
  2703. * send event on each open channel. The events should be received on
  2704. * separate channels.
  2705. */
  2706. printf(" 5 Channel open/unlink/open/send:\n");
  2707. do {
  2708. result = saEvtChannelOpen(handle, &channel_name, flags1, SA_TIME_MAX,
  2709. &channel_handle1);
  2710. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2711. if (result != SA_AIS_OK) {
  2712. get_sa_error(result, result_buf, result_buf_len);
  2713. printf("ERROR: channel open result: %s\n", result_buf);
  2714. goto unlink_exit;
  2715. }
  2716. do {
  2717. result = saEvtChannelUnlink(handle, &channel_name);
  2718. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2719. if (result != SA_AIS_OK) {
  2720. get_sa_error(result, result_buf, result_buf_len);
  2721. printf("ERROR: channel unlink result: %s\n", result_buf);
  2722. goto unlink_exit;
  2723. }
  2724. do {
  2725. result = saEvtChannelOpen(handle, &channel_name, flags1, SA_TIME_MAX,
  2726. &channel_handle2);
  2727. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2728. if (result != SA_AIS_OK) {
  2729. get_sa_error(result, result_buf, result_buf_len);
  2730. printf("ERROR: channel open result: %s\n", result_buf);
  2731. goto unlink_exit;
  2732. }
  2733. do {
  2734. result = saEvtEventSubscribe(channel_handle1,
  2735. &subscribe_filters,
  2736. sub1);
  2737. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2738. if (result != SA_AIS_OK) {
  2739. get_sa_error(result, result_buf, result_buf_len);
  2740. printf("ERROR: channel subscribe(1) result: %s\n", result_buf);
  2741. goto unlink_exit;
  2742. }
  2743. do {
  2744. result = saEvtEventSubscribe(channel_handle2,
  2745. &subscribe_filters,
  2746. sub2);
  2747. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2748. if (result != SA_AIS_OK) {
  2749. get_sa_error(result, result_buf, result_buf_len);
  2750. printf("ERROR: channel subscribe(2) result: %s\n", result_buf);
  2751. goto unlink_exit;
  2752. }
  2753. retention_time = 0ULL;
  2754. do {
  2755. result = saEvtEventAllocate(channel_handle1, &event_handle1);
  2756. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2757. if (result != SA_AIS_OK) {
  2758. get_sa_error(result, result_buf, result_buf_len);
  2759. printf("ERROR: event allocate(1) result: %s\n", result_buf);
  2760. goto unlink_exit;
  2761. }
  2762. do {
  2763. result = saEvtEventAttributesSet(event_handle1,
  2764. &evt_pat_set_array,
  2765. TEST_PRIORITY,
  2766. retention_time,
  2767. &test_pub_name);
  2768. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2769. if (result != SA_AIS_OK) {
  2770. get_sa_error(result, result_buf, result_buf_len);
  2771. printf("ERROR: event set(1) result: %s\n", result_buf);
  2772. goto unlink_exit;
  2773. }
  2774. do {
  2775. result = saEvtEventAllocate(channel_handle2, &event_handle2);
  2776. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2777. if (result != SA_AIS_OK) {
  2778. get_sa_error(result, result_buf, result_buf_len);
  2779. printf("ERROR: event allocate(2) result: %s\n", result_buf);
  2780. goto unlink_exit;
  2781. }
  2782. do {
  2783. result = saEvtEventAttributesSet(event_handle2,
  2784. &evt_pat_set_array,
  2785. TEST_PRIORITY,
  2786. retention_time,
  2787. &test_pub_name);
  2788. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2789. if (result != SA_AIS_OK) {
  2790. get_sa_error(result, result_buf, result_buf_len);
  2791. printf("ERROR: event set(2) result: %s\n", result_buf);
  2792. goto unlink_exit;
  2793. }
  2794. do {
  2795. result = saEvtEventPublish(event_handle1, 0, 0, &event_id1);
  2796. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2797. if (result != SA_AIS_OK) {
  2798. get_sa_error(result, result_buf, result_buf_len);
  2799. printf("ERROR: event publish(1) result: %s\n", result_buf);
  2800. goto unlink_exit;
  2801. }
  2802. do {
  2803. result = saEvtEventPublish(event_handle2, 0, 0, &event_id2);
  2804. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2805. if (result != SA_AIS_OK) {
  2806. get_sa_error(result, result_buf, result_buf_len);
  2807. printf("ERROR: event publish(2) result: %s\n", result_buf);
  2808. goto unlink_exit;
  2809. }
  2810. do {
  2811. result = saEvtSelectionObjectGet(handle, &fd);
  2812. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2813. if (result != SA_AIS_OK) {
  2814. get_sa_error(result, result_buf, result_buf_len);
  2815. printf("ERROR: select object get result: %s\n", result_buf);
  2816. goto unlink_exit;
  2817. }
  2818. /*
  2819. * We should see a total of two events processed, not four
  2820. * as if both events were recevied on both channels.
  2821. */
  2822. call_count = 0;
  2823. do {
  2824. pfd.fd = fd;
  2825. pfd.events = POLLIN;
  2826. nfd = poll(&pfd, 1, timeout);
  2827. if (nfd <= 0) {
  2828. if (nfd < 0) {
  2829. perror("ERROR: poll error");
  2830. goto unlink_exit;
  2831. }
  2832. } else {
  2833. do {
  2834. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  2835. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2836. if (result != SA_AIS_OK) {
  2837. get_sa_error(result, result_buf, result_buf_len);
  2838. printf("ERROR: saEvtDispatch %s\n", result_buf);
  2839. goto unlink_exit;
  2840. }
  2841. }
  2842. } while (nfd > 0);
  2843. if (call_count != 2) {
  2844. printf("ERROR: processed %d events\n", call_count);
  2845. goto unlink_exit;
  2846. }
  2847. /*
  2848. * 6. unlink all, close all.
  2849. *
  2850. * close all open channels.
  2851. * unlink the channel.
  2852. * open without create the channel. Verify that the channel no
  2853. * longer exists.
  2854. */
  2855. printf(" 6 Channel unlink all/close all/open:\n");
  2856. unlink_exit:
  2857. saEvtChannelClose(channel_handle1);
  2858. saEvtChannelClose(channel_handle2);
  2859. saEvtChannelUnlink(handle, &channel_name);
  2860. do {
  2861. result = saEvtFinalize(handle);
  2862. } while ((result == SA_AIS_ERR_TRY_AGAIN) && !sleep(TRY_WAIT));
  2863. if (result != SA_AIS_OK) {
  2864. get_sa_error(result, result_buf, result_buf_len);
  2865. printf("ERROR: Event Finalize result: %s\n", result_buf);
  2866. }
  2867. printf("Done\n");
  2868. }
  2869. int main (void)
  2870. {
  2871. test_initialize ();
  2872. test_channel();
  2873. test_event();
  2874. test_multi_channel1();
  2875. test_multi_channel2();
  2876. test_multi_channel3();
  2877. test_retention();
  2878. test_unlink_channel();
  2879. return (0);
  2880. }
  2881. /*
  2882. * vi: set autoindent tabstop=4 shiftwidth=4 :
  2883. */