testevt.c 88 KB

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