testevt.c 87 KB

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