testevt.c 89 KB

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