testevt.c 74 KB

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