testevt.c 88 KB

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