testevt.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811
  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. (unsigned long long)event_id, (unsigned long long)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. (unsigned long long)retention_time,
  642. (unsigned long long)my_retention_time);
  643. }
  644. if (publisher_name.length != my_publisher_name.length) {
  645. printf("ERROR: publisher name length: e=%d, a=%d\n",
  646. publisher_name.length,
  647. my_publisher_name.length);
  648. } else {
  649. if (memcmp(publisher_name.value, my_publisher_name.value,
  650. publisher_name.length) != 0) {
  651. printf("ERROR: publisher name content: e=%s, a=%s\n",
  652. publisher_name.value,
  653. my_publisher_name.value);
  654. }
  655. }
  656. act_data = malloc(my_event_data_size);
  657. memset(act_data, 0, my_event_data_size);
  658. data_size = my_event_data_size;
  659. result = saEvtEventDataGet(event_handle, act_data, &data_size);
  660. if (result != SA_AIS_OK) {
  661. get_sa_error(result, result_buf, result_buf_len);
  662. printf("ERROR: event data get result: %s\n", result_buf);
  663. goto dat_free;
  664. }
  665. if (data_size != event_data_size) {
  666. printf("ERROR: Data size: e=%d a=%d\n",
  667. event_data_size, data_size);
  668. }
  669. for (i = 0; i < (data_size/sizeof(long)); i++) {
  670. if (act_data[i] != exp_data[i]) {
  671. printf("ERROR: Event Data e=%lx a=%lx at index %d\n",
  672. exp_data[i], act_data[i], i);
  673. break;
  674. }
  675. }
  676. dat_free:
  677. free(act_data);
  678. evt_free:
  679. result = saEvtEventFree(event_handle);
  680. if (result != SA_AIS_OK) {
  681. get_sa_error(result, result_buf, result_buf_len);
  682. printf("ERROR: event free result: %s\n", result_buf);
  683. }
  684. }
  685. void
  686. test_event()
  687. {
  688. SaEvtHandleT handle;
  689. SaEvtChannelHandleT channel_handle;
  690. SaEvtEventHandleT event_handle;
  691. SaEvtChannelOpenFlagsT flags;
  692. SaNameT channel_name;
  693. SaTimeT publish_time;
  694. struct pollfd pfd;
  695. int nfd;
  696. int fd;
  697. int timeout = 5000;
  698. int result;
  699. int i;
  700. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  701. SA_EVT_CHANNEL_CREATE;
  702. strcpy(channel_name.value, channel);
  703. channel_name.length = strlen(channel);
  704. printf("Test Event operations:\n");
  705. result = saEvtInitialize (&handle, &callbacks, versions[0].version);
  706. if (result != SA_AIS_OK) {
  707. get_sa_error(result, result_buf, result_buf_len);
  708. printf("ERROR: Event Initialize result: %s\n", result_buf);
  709. return;
  710. }
  711. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  712. &channel_handle);
  713. if (result != SA_AIS_OK) {
  714. get_sa_error(result, result_buf, result_buf_len);
  715. printf("ERROR: channel open result: %s\n", result_buf);
  716. goto evt_fin;
  717. }
  718. /*
  719. * Allocate an event
  720. */
  721. printf(" Event allocation\n");
  722. result = saEvtEventAllocate(channel_handle, &event_handle);
  723. if (result != SA_AIS_OK) {
  724. get_sa_error(result, result_buf, result_buf_len);
  725. printf("ERROR: event Allocate result: %s\n", result_buf);
  726. goto evt_close;
  727. }
  728. printf(" Get event attributes(1)\n");
  729. result = saEvtEventAttributesGet(event_handle,
  730. 0, /* patterns */
  731. 0, /* priority */
  732. 0, /* retention time */
  733. 0, /* publisher name */
  734. 0, /* publish time */
  735. 0 /* event_id */
  736. );
  737. if (result != SA_AIS_OK) {
  738. get_sa_error(result, result_buf, result_buf_len);
  739. printf("ERROR: event get attr result(1): %s\n", result_buf);
  740. goto evt_free;
  741. }
  742. /*
  743. * Get event attributes, this time supply pointers.
  744. * validate the default values.
  745. */
  746. printf(" Get event attributes(2)\n");
  747. evt_pat_get_array.patternsNumber = 4;
  748. result = saEvtEventAttributesGet(event_handle,
  749. &evt_pat_get_array, /* patterns */
  750. &priority, /* priority */
  751. &retention_time, /* retention time */
  752. &publisher_name, /* publisher name */
  753. &publish_time, /* publish time */
  754. &event_id /* event_id */
  755. );
  756. if (result != SA_AIS_OK) {
  757. get_sa_error(result, result_buf, result_buf_len);
  758. printf("ERROR: event get attr result(2): %s\n", result_buf);
  759. goto evt_free;
  760. }
  761. if (evt_pat_get_array.patternsNumber != 0) {
  762. printf("ERROR: pattern array count not zero: %d\n",
  763. evt_pat_get_array.patternsNumber);
  764. }
  765. if (priority != SA_EVT_LOWEST_PRIORITY) {
  766. printf("ERROR: priority not lowest: 0x%x\n", priority);
  767. }
  768. if (retention_time != 0) {
  769. printf("ERROR: retention time not zero: %0llx\n",
  770. (unsigned long long)retention_time);
  771. }
  772. if (publisher_name.length != 0) {
  773. printf("ERROR: publisher name not null: %s\n", publisher_name.value);
  774. }
  775. if (event_id != 0) {
  776. printf("ERROR: event id not zero: 0x%llx\n",
  777. (unsigned long long)event_id);
  778. }
  779. /*
  780. * Set some attributes, then read them back
  781. */
  782. printf(" Set/get event attributes(1)\n");
  783. result = saEvtEventAttributesSet(event_handle,
  784. &evt_pat_set_array,
  785. TEST_PRIORITY,
  786. test_ret_time,
  787. &test_pub_name);
  788. if (result != SA_AIS_OK) {
  789. get_sa_error(result, result_buf, result_buf_len);
  790. printf("ERROR: event set attr result(1): %s\n", result_buf);
  791. goto evt_free;
  792. }
  793. evt_pat_get_array.patternsNumber = 4;
  794. result = saEvtEventAttributesGet(event_handle,
  795. &evt_pat_get_array, /* patterns */
  796. &priority, /* priority */
  797. &retention_time, /* retention time */
  798. &publisher_name, /* publisher name */
  799. &publish_time, /* publish time */
  800. &event_id /* event_id */
  801. );
  802. if (result != SA_AIS_OK) {
  803. get_sa_error(result, result_buf, result_buf_len);
  804. printf("ERROR: event get attr result(2): %s\n", result_buf);
  805. goto evt_free;
  806. }
  807. if (evt_pat_get_array.patternsNumber != 4) {
  808. printf("ERROR: pattern array count not 4: %d\n",
  809. evt_pat_get_array.patternsNumber);
  810. }
  811. for (i = 0; i < evt_pat_get_array.patternsNumber; i++) {
  812. if (evt_pat_get_array.patterns[i].patternSize !=
  813. evt_pat_set_array.patterns[i].patternSize) {
  814. printf("ERROR: pattern %d count not equal g=%d, s=%d\n",
  815. i,
  816. evt_pat_get_array.patterns[i].patternSize,
  817. evt_pat_set_array.patterns[i].patternSize);
  818. printf("ERROR: pattern %d content g=\"%s\", s=\"%s\"\n",
  819. i,
  820. evt_pat_get_array.patterns[i].pattern,
  821. evt_pat_set_array.patterns[i].pattern);
  822. } else {
  823. if (memcmp(evt_pat_get_array.patterns[i].pattern,
  824. evt_pat_set_array.patterns[i].pattern,
  825. evt_pat_get_array.patterns[i].patternSize) != 0){
  826. printf(
  827. "ERROR: pattern %d don't match g=\"%s\", s=\"%s\"\n",
  828. i,
  829. evt_pat_get_array.patterns[i].pattern,
  830. evt_pat_set_array.patterns[i].pattern);
  831. }
  832. }
  833. }
  834. if (priority != TEST_PRIORITY) {
  835. printf("ERROR: priority: e=0x%x a=0x%x\n",
  836. TEST_PRIORITY, priority);
  837. }
  838. if (retention_time != test_ret_time) {
  839. printf("ERROR: retention: e=0x%llx a=0x%llx\n",
  840. (unsigned long long)test_ret_time,
  841. (unsigned long long)retention_time);
  842. }
  843. if (publisher_name.length != test_pub_name.length) {
  844. printf("ERROR: publisher name length: e=%d, a=%d\n",
  845. test_pub_name.length,
  846. publisher_name.length);
  847. } else {
  848. if (memcmp(publisher_name.value, test_pub_name.value,
  849. publisher_name.length) != 0) {
  850. printf("ERROR: publisher name content: e=%s, a=%s\n",
  851. test_pub_name.value,
  852. publisher_name.value);
  853. }
  854. }
  855. if (event_id != 0) {
  856. printf("ERROR: event id not zero: 0x%llx\n",
  857. (unsigned long long)event_id);
  858. }
  859. /*
  860. * event user data
  861. */
  862. printf(" Get event data(1)\n");
  863. result = saEvtEventDataGet(event_handle, 0, 0);
  864. if (result != SA_AIS_OK) {
  865. get_sa_error(result, result_buf, result_buf_len);
  866. printf("ERROR: Get event data(1) result: %s\n", result_buf);
  867. }
  868. printf(" Get event data(2)\n");
  869. result = saEvtEventDataGet(event_handle, event_data, 0);
  870. if (result != SA_AIS_OK) {
  871. get_sa_error(result, result_buf, result_buf_len);
  872. printf("ERROR: Get event data(2) result: %s\n", result_buf);
  873. }
  874. printf(" Get event data(3)\n");
  875. event_data_size = EVENT_DATA_SIZE;
  876. result = saEvtEventDataGet(event_handle, 0, &event_data_size);
  877. if (result != SA_AIS_OK) {
  878. get_sa_error(result, result_buf, result_buf_len);
  879. printf("ERROR: Get event data(3) result: %s\n", result_buf);
  880. }
  881. printf(" Get event data(4)\n");
  882. event_data_size = EVENT_DATA_SIZE;
  883. result = saEvtEventDataGet(event_handle, event_data, &event_data_size);
  884. if (result != SA_AIS_OK) {
  885. get_sa_error(result, result_buf, result_buf_len);
  886. printf("ERROR: Get event data(4) result: %s\n", result_buf);
  887. }
  888. printf(" Get event data(5)\n");
  889. event_data_size = 1;
  890. result = saEvtEventDataGet(event_handle, event_data, &event_data_size);
  891. if (result != SA_AIS_OK) {
  892. get_sa_error(result, result_buf, result_buf_len);
  893. printf("ERROR: Get event data(5) result: %s\n", result_buf);
  894. }
  895. printf(" Free event(1)\n");
  896. result = saEvtEventFree(event_handle);
  897. if (result != SA_AIS_OK) {
  898. get_sa_error(result, result_buf, result_buf_len);
  899. printf("ERROR: event free result: %s\n", result_buf);
  900. }
  901. /*
  902. * Test publication.
  903. */
  904. printf(" Publish with no patterns set\n");
  905. result = saEvtEventAllocate(channel_handle, &event_handle);
  906. if (result != SA_AIS_OK) {
  907. get_sa_error(result, result_buf, result_buf_len);
  908. printf("ERROR: event Allocate result: %s\n", result_buf);
  909. goto evt_close;
  910. }
  911. result = saEvtEventPublish(event_handle, 0, 0, &event_id);
  912. if (result != SA_AIS_ERR_INVALID_PARAM) {
  913. get_sa_error(result, result_buf, result_buf_len);
  914. printf("ERROR: event Publish result(1): %s\n", result_buf);
  915. goto evt_close;
  916. }
  917. /*
  918. * Publish with pattens
  919. */
  920. printf(" Publish with patterns set\n");
  921. result = saEvtEventSubscribe(channel_handle,
  922. &subscribe_filters,
  923. subscription_id);
  924. if (result != SA_AIS_OK) {
  925. get_sa_error(result, result_buf, result_buf_len);
  926. printf("ERROR: event subscribe result: %s\n", result_buf);
  927. result = saEvtChannelClose(channel_handle);
  928. if (result != SA_AIS_OK) {
  929. get_sa_error(result, result_buf, result_buf_len);
  930. printf("ERROR: Channel close result: %s\n", result_buf);
  931. }
  932. result = saEvtFinalize(handle);
  933. if (result != SA_AIS_OK) {
  934. get_sa_error(result, result_buf, result_buf_len);
  935. printf("ERROR: Finalize result: %s\n", result_buf);
  936. }
  937. return;
  938. }
  939. retention_time = 0ULL;
  940. result = saEvtEventAttributesSet(event_handle,
  941. &evt_pat_set_array,
  942. TEST_PRIORITY,
  943. retention_time,
  944. &test_pub_name);
  945. if (result != SA_AIS_OK) {
  946. get_sa_error(result, result_buf, result_buf_len);
  947. printf("ERROR: event set attr result(2): %s\n", result_buf);
  948. goto evt_free;
  949. }
  950. /*
  951. * Generate event data
  952. */
  953. exp_data = malloc(DATA_SIZE);
  954. for (i = 0; i < LCOUNT; i++) {
  955. exp_data[i] = lrand48();
  956. }
  957. event_data_size = DATA_SIZE;
  958. /*
  959. * Send it
  960. */
  961. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  962. &event_id);
  963. if (result != SA_AIS_OK) {
  964. get_sa_error(result, result_buf, result_buf_len);
  965. printf("ERROR: event Publish result(2): %s\n", result_buf);
  966. goto evt_close;
  967. }
  968. /*
  969. * See if we got the event
  970. */
  971. result = saEvtSelectionObjectGet(handle, &fd);
  972. if (result != SA_AIS_OK) {
  973. get_sa_error(result, result_buf, result_buf_len);
  974. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  975. /* error */
  976. return;
  977. }
  978. pfd.fd = fd;
  979. pfd.events = POLLIN;
  980. nfd = poll(&pfd, 1, timeout);
  981. if (nfd <= 0) {
  982. printf("ERROR: poll fds %d\n", nfd);
  983. if (nfd < 0) {
  984. perror("ERROR: poll error");
  985. }
  986. /* Error */
  987. return;
  988. }
  989. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  990. if (result != SA_AIS_OK) {
  991. get_sa_error(result, result_buf, result_buf_len);
  992. printf("ERROR: saEvtDispatch %s\n", result_buf);
  993. /* error */
  994. return;
  995. }
  996. /*
  997. * Test cleanup
  998. */
  999. evt_free:
  1000. result = saEvtEventFree(event_handle);
  1001. if (result != SA_AIS_OK) {
  1002. get_sa_error(result, result_buf, result_buf_len);
  1003. printf("ERROR: event free result: %s\n", result_buf);
  1004. }
  1005. evt_close:
  1006. result = saEvtChannelClose(channel_handle);
  1007. if (result != SA_AIS_OK) {
  1008. get_sa_error(result, result_buf, result_buf_len);
  1009. printf("ERROR: channel close result: %s\n", result_buf);
  1010. }
  1011. evt_fin:
  1012. result = saEvtFinalize(handle);
  1013. if (result != SA_AIS_OK) {
  1014. get_sa_error(result, result_buf, result_buf_len);
  1015. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1016. }
  1017. printf("Done\n");
  1018. }
  1019. SaEvtEventIdT event_id1;
  1020. SaEvtEventIdT event_id2;
  1021. SaEvtEventIdT event_id3;
  1022. SaEvtSubscriptionIdT sub1 = 0x101010;
  1023. SaEvtSubscriptionIdT sub2 = 0x202020;
  1024. static int call_count = 0;
  1025. /*
  1026. * Handle call back for multi-test1
  1027. * Checks event ID with subscription ID to make sure that we
  1028. * received an event on the correct subscription.
  1029. */
  1030. void
  1031. multi_test_callback1(SaEvtSubscriptionIdT my_subscription_id,
  1032. const SaEvtEventHandleT event_handle,
  1033. const SaSizeT my_event_data_size)
  1034. {
  1035. SaAisErrorT result;
  1036. SaUint8T my_priority;
  1037. SaTimeT my_retention_time;
  1038. SaNameT my_publisher_name = {0, {0}};
  1039. SaTimeT my_publish_time;
  1040. SaEvtEventIdT my_event_id;
  1041. SaEvtSubscriptionIdT exp_sub_id;
  1042. printf(" multi_test_callback1 called(%d)\n", ++call_count);
  1043. evt_pat_get_array.patternsNumber = 4;
  1044. result = saEvtEventAttributesGet(event_handle,
  1045. &evt_pat_get_array, /* patterns */
  1046. &my_priority, /* priority */
  1047. &my_retention_time, /* retention time */
  1048. &my_publisher_name, /* publisher name */
  1049. &my_publish_time, /* publish time */
  1050. &my_event_id /* event_id */
  1051. );
  1052. if (result != SA_AIS_OK) {
  1053. get_sa_error(result, result_buf, result_buf_len);
  1054. printf("ERROR: event get attr result: %s\n", result_buf);
  1055. goto evt_free;
  1056. }
  1057. if (my_event_id == event_id1) {
  1058. exp_sub_id = sub1;
  1059. } else if (my_event_id == event_id2) {
  1060. exp_sub_id = sub2;
  1061. } else if (my_event_id == event_id3) {
  1062. printf("ERROR: Received event 3 but not subscribed\n");
  1063. goto evt_free;
  1064. } else {
  1065. printf("ERROR: Received event %llx but not sent\n",
  1066. (unsigned long long)my_event_id);
  1067. goto evt_free;
  1068. }
  1069. if (my_subscription_id != exp_sub_id) {
  1070. printf("ERROR: sub ID: e=%x, a=%x\n",
  1071. exp_sub_id, my_subscription_id);
  1072. goto evt_free;
  1073. }
  1074. if (evt_pat_get_array.patternsNumber != 1) {
  1075. printf("ERROR: pattern array count not 1: %d\n",
  1076. evt_pat_get_array.patternsNumber);
  1077. }
  1078. evt_free:
  1079. result = saEvtEventFree(event_handle);
  1080. if (result != SA_AIS_OK) {
  1081. get_sa_error(result, result_buf, result_buf_len);
  1082. printf("ERROR: event free result: %s\n", result_buf);
  1083. }
  1084. }
  1085. /*
  1086. * Test multiple channel operations
  1087. * 1. Test multiple subscriptions on a single channel and receiving
  1088. * events.
  1089. * 2. Test multiple openings of a single channel and receving events.
  1090. * 3. Test opening of multiple channels and receiving events
  1091. */
  1092. void
  1093. test_multi_channel1()
  1094. {
  1095. SaEvtEventFilterT filt1[1] = {
  1096. {SA_EVT_EXACT_FILTER, {"ChanPat1", 8}},
  1097. };
  1098. SaEvtEventFilterT filt2[1] = {
  1099. {SA_EVT_EXACT_FILTER, {"ChanPat2", 8}},
  1100. };
  1101. SaEvtEventFilterArrayT sub_filt = {
  1102. NULL, 1
  1103. };
  1104. SaEvtEventPatternT pat1 = {"ChanPat1", 8};
  1105. SaEvtEventPatternT pat2 = {"ChanPat2", 8};
  1106. SaEvtEventPatternT pat3 = {"ChanPat3", 8};
  1107. SaEvtEventPatternArrayT evt_pat = {
  1108. NULL, 1
  1109. };
  1110. SaEvtHandleT handle;
  1111. SaEvtChannelHandleT channel_handle;
  1112. SaEvtEventHandleT event_handle;
  1113. SaEvtChannelOpenFlagsT flags;
  1114. SaNameT channel_name;
  1115. SaEvtCallbacksT multi_callbacks = {
  1116. 0,
  1117. multi_test_callback1
  1118. };
  1119. struct pollfd pfd;
  1120. int nfd;
  1121. int fd;
  1122. int timeout = 5000;
  1123. int result;
  1124. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1125. SA_EVT_CHANNEL_CREATE;
  1126. strcpy(channel_name.value, channel);
  1127. channel_name.length = strlen(channel);
  1128. printf("Test multiple operations:\n");
  1129. result = saEvtInitialize (&handle, &multi_callbacks, versions[0].version);
  1130. if (result != SA_AIS_OK) {
  1131. get_sa_error(result, result_buf, result_buf_len);
  1132. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1133. return;
  1134. }
  1135. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1136. &channel_handle);
  1137. if (result != SA_AIS_OK) {
  1138. get_sa_error(result, result_buf, result_buf_len);
  1139. printf("ERROR: channel open result: %s\n", result_buf);
  1140. goto evt_fin;
  1141. }
  1142. /*
  1143. * Allocate an event
  1144. */
  1145. result = saEvtEventAllocate(channel_handle, &event_handle);
  1146. if (result != SA_AIS_OK) {
  1147. get_sa_error(result, result_buf, result_buf_len);
  1148. printf("ERROR: event Allocate result: %s\n", result_buf);
  1149. goto evt_close;
  1150. }
  1151. /*
  1152. * 1. Test multiple subscriptions on a single channel and receiving
  1153. * events.
  1154. *
  1155. * Subscribe twice with two different filters. Then send three events.
  1156. * One will match the first filter, the second will match the second
  1157. * filter, the third will match none. We will validate that we receive
  1158. * two events and that the subscription IDs match what we expect for the
  1159. * given pattern.
  1160. */
  1161. sub_filt.filters = filt1;
  1162. result = saEvtEventSubscribe(channel_handle,
  1163. &sub_filt,
  1164. sub1);
  1165. if (result != SA_AIS_OK) {
  1166. get_sa_error(result, result_buf, result_buf_len);
  1167. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1168. goto evt_free;
  1169. }
  1170. sub_filt.filters = filt2;
  1171. result = saEvtEventSubscribe(channel_handle,
  1172. &sub_filt,
  1173. sub2);
  1174. if (result != SA_AIS_OK) {
  1175. get_sa_error(result, result_buf, result_buf_len);
  1176. printf("ERROR: event subscribe(2) result: %s\n", result_buf);
  1177. goto evt_free;
  1178. }
  1179. retention_time = 0ULL;
  1180. evt_pat.patterns = &pat1;
  1181. result = saEvtEventAttributesSet(event_handle,
  1182. &evt_pat,
  1183. TEST_PRIORITY,
  1184. retention_time,
  1185. &test_pub_name);
  1186. if (result != SA_AIS_OK) {
  1187. get_sa_error(result, result_buf, result_buf_len);
  1188. printf("ERROR: event set attr result(1): %s\n", result_buf);
  1189. goto evt_free;
  1190. }
  1191. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1192. &event_id1);
  1193. if (result != SA_AIS_OK) {
  1194. get_sa_error(result, result_buf, result_buf_len);
  1195. printf("ERROR: event Publish result:(1) %s\n", result_buf);
  1196. goto evt_close;
  1197. }
  1198. evt_pat.patterns = &pat2;
  1199. result = saEvtEventAttributesSet(event_handle,
  1200. &evt_pat,
  1201. TEST_PRIORITY,
  1202. retention_time,
  1203. &test_pub_name);
  1204. if (result != SA_AIS_OK) {
  1205. get_sa_error(result, result_buf, result_buf_len);
  1206. printf("ERROR: event set attr result(2): %s\n", result_buf);
  1207. goto evt_free;
  1208. }
  1209. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1210. &event_id2);
  1211. if (result != SA_AIS_OK) {
  1212. get_sa_error(result, result_buf, result_buf_len);
  1213. printf("ERROR: event Publish result:(2) %s\n", result_buf);
  1214. goto evt_close;
  1215. }
  1216. evt_pat.patterns = &pat3;
  1217. result = saEvtEventAttributesSet(event_handle,
  1218. &evt_pat,
  1219. TEST_PRIORITY,
  1220. retention_time,
  1221. &test_pub_name);
  1222. if (result != SA_AIS_OK) {
  1223. get_sa_error(result, result_buf, result_buf_len);
  1224. printf("ERROR: event set attr result(3): %s\n", result_buf);
  1225. goto evt_free;
  1226. }
  1227. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1228. &event_id3);
  1229. if (result != SA_AIS_OK) {
  1230. get_sa_error(result, result_buf, result_buf_len);
  1231. printf("ERROR: event Publish result:(3) %s\n", result_buf);
  1232. goto evt_close;
  1233. }
  1234. /*
  1235. * See if we got the event
  1236. */
  1237. result = saEvtSelectionObjectGet(handle, &fd);
  1238. if (result != SA_AIS_OK) {
  1239. get_sa_error(result, result_buf, result_buf_len);
  1240. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1241. /* error */
  1242. return;
  1243. }
  1244. while(1) {
  1245. pfd.fd = fd;
  1246. pfd.events = POLLIN;
  1247. nfd = poll(&pfd, 1, timeout);
  1248. if (nfd == 0) {
  1249. break;
  1250. } else if (nfd < 0) {
  1251. perror("ERROR: poll error");
  1252. break;
  1253. }
  1254. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  1255. if (result != SA_AIS_OK) {
  1256. get_sa_error(result, result_buf, result_buf_len);
  1257. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1258. /* error */
  1259. goto evt_free;
  1260. }
  1261. }
  1262. if (call_count != 2) {
  1263. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  1264. goto evt_free;
  1265. }
  1266. /*
  1267. * Test cleanup
  1268. */
  1269. evt_free:
  1270. result = saEvtEventFree(event_handle);
  1271. if (result != SA_AIS_OK) {
  1272. get_sa_error(result, result_buf, result_buf_len);
  1273. printf("ERROR: event free result: %s\n", result_buf);
  1274. }
  1275. evt_close:
  1276. result = saEvtChannelClose(channel_handle);
  1277. if (result != SA_AIS_OK) {
  1278. get_sa_error(result, result_buf, result_buf_len);
  1279. printf("ERROR: channel close result: %s\n", result_buf);
  1280. }
  1281. evt_fin:
  1282. result = saEvtFinalize(handle);
  1283. if (result != SA_AIS_OK) {
  1284. get_sa_error(result, result_buf, result_buf_len);
  1285. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1286. }
  1287. printf("Done\n");
  1288. }
  1289. /*
  1290. * Handle call back for multi-test2
  1291. * Counts events received. Makes sure that we get one event from
  1292. * each subscription.
  1293. *
  1294. */
  1295. void
  1296. multi_test_callback2(SaEvtSubscriptionIdT my_subscription_id,
  1297. const SaEvtEventHandleT event_handle,
  1298. const SaSizeT my_event_data_size)
  1299. {
  1300. SaAisErrorT result;
  1301. SaUint8T my_priority;
  1302. SaTimeT my_retention_time;
  1303. SaNameT my_publisher_name = {0, {0}};
  1304. SaTimeT my_publish_time;
  1305. SaEvtEventIdT my_event_id;
  1306. SaEvtSubscriptionIdT last_sub_id = 0;
  1307. printf(" multi_test_callback2 called(%d)\n", ++call_count);
  1308. evt_pat_get_array.patternsNumber = 4;
  1309. result = saEvtEventAttributesGet(event_handle,
  1310. &evt_pat_get_array, /* patterns */
  1311. &my_priority, /* priority */
  1312. &my_retention_time, /* retention time */
  1313. &my_publisher_name, /* publisher name */
  1314. &my_publish_time, /* publish time */
  1315. &my_event_id /* event_id */
  1316. );
  1317. if (result != SA_AIS_OK) {
  1318. get_sa_error(result, result_buf, result_buf_len);
  1319. printf("ERROR: event get attr result: %s\n", result_buf);
  1320. goto evt_free;
  1321. }
  1322. if (my_event_id != event_id1) {
  1323. printf("ERROR: Received wrong event\n");
  1324. goto evt_free;
  1325. }
  1326. if (last_sub_id == 0) {
  1327. if (my_subscription_id != sub1 &&
  1328. my_subscription_id != sub2) {
  1329. printf("ERROR: Received bad subscription ID\n");
  1330. goto evt_free;
  1331. }
  1332. last_sub_id = my_subscription_id;
  1333. } else {
  1334. if (my_subscription_id == last_sub_id) {
  1335. printf("ERROR: Received subscription ID twice\n");
  1336. goto evt_free;
  1337. }
  1338. if (my_subscription_id != sub1 &&
  1339. my_subscription_id != sub2) {
  1340. printf("ERROR: Received bad subscription ID\n");
  1341. goto evt_free;
  1342. }
  1343. }
  1344. if (evt_pat_get_array.patternsNumber != 1) {
  1345. printf("ERROR: pattern array count not 1: %d\n",
  1346. evt_pat_get_array.patternsNumber);
  1347. }
  1348. evt_free:
  1349. result = saEvtEventFree(event_handle);
  1350. if (result != SA_AIS_OK) {
  1351. get_sa_error(result, result_buf, result_buf_len);
  1352. printf("ERROR: event free result: %s\n", result_buf);
  1353. }
  1354. }
  1355. void
  1356. test_multi_channel2()
  1357. {
  1358. SaEvtEventFilterT filt1[1] = {
  1359. {SA_EVT_EXACT_FILTER, {"ChanPat1", 8}},
  1360. };
  1361. SaEvtEventFilterArrayT sub_filt = {
  1362. NULL, 1
  1363. };
  1364. SaEvtEventPatternT pat1 = {"ChanPat1", 8};
  1365. SaEvtEventPatternArrayT evt_pat = {
  1366. NULL, 1
  1367. };
  1368. SaEvtHandleT handle;
  1369. SaEvtChannelHandleT channel_handle;
  1370. SaEvtChannelHandleT channel_handle1;
  1371. SaEvtEventHandleT event_handle;
  1372. SaEvtChannelOpenFlagsT flags;
  1373. SaNameT channel_name;
  1374. SaEvtCallbacksT multi_callbacks = {
  1375. 0,
  1376. multi_test_callback2
  1377. };
  1378. struct pollfd pfd;
  1379. int nfd;
  1380. int fd;
  1381. int timeout = 5000;
  1382. int result;
  1383. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1384. SA_EVT_CHANNEL_CREATE;
  1385. strcpy(channel_name.value, channel);
  1386. channel_name.length = strlen(channel);
  1387. /*
  1388. * 2. Test multiple openings of a single channel and receving events.
  1389. *
  1390. * Open and subscribe to a channel twice. When an event is sent, it
  1391. * should be delivered twice, once for each open channel.
  1392. */
  1393. printf("Test multiple opens/subscribes:\n");
  1394. result = saEvtInitialize (&handle, &multi_callbacks, versions[0].version);
  1395. if (result != SA_AIS_OK) {
  1396. get_sa_error(result, result_buf, result_buf_len);
  1397. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1398. return;
  1399. }
  1400. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1401. &channel_handle);
  1402. if (result != SA_AIS_OK) {
  1403. get_sa_error(result, result_buf, result_buf_len);
  1404. printf("ERROR: channel open(0) result: %s\n", result_buf);
  1405. goto evt_fin;
  1406. }
  1407. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1408. &channel_handle1);
  1409. if (result != SA_AIS_OK) {
  1410. get_sa_error(result, result_buf, result_buf_len);
  1411. printf("ERROR: channel open(1) result: %s\n", result_buf);
  1412. goto evt_fin;
  1413. }
  1414. result = saEvtEventAllocate(channel_handle, &event_handle);
  1415. if (result != SA_AIS_OK) {
  1416. get_sa_error(result, result_buf, result_buf_len);
  1417. printf("ERROR: event Allocate result: %s\n", result_buf);
  1418. goto evt_close;
  1419. }
  1420. sub_filt.filters = filt1;
  1421. result = saEvtEventSubscribe(channel_handle,
  1422. &sub_filt,
  1423. sub1);
  1424. if (result != SA_AIS_OK) {
  1425. get_sa_error(result, result_buf, result_buf_len);
  1426. printf("ERROR: event subscribe(0) result: %s\n", result_buf);
  1427. goto evt_free;
  1428. }
  1429. sub_filt.filters = filt1;
  1430. result = saEvtEventSubscribe(channel_handle1,
  1431. &sub_filt,
  1432. sub2);
  1433. if (result != SA_AIS_OK) {
  1434. get_sa_error(result, result_buf, result_buf_len);
  1435. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1436. goto evt_free;
  1437. }
  1438. retention_time = 0ULL;
  1439. evt_pat.patterns = &pat1;
  1440. result = saEvtEventAttributesSet(event_handle,
  1441. &evt_pat,
  1442. TEST_PRIORITY,
  1443. retention_time,
  1444. &test_pub_name);
  1445. if (result != SA_AIS_OK) {
  1446. get_sa_error(result, result_buf, result_buf_len);
  1447. printf("ERROR: event set attr result: %s\n", result_buf);
  1448. goto evt_free;
  1449. }
  1450. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1451. &event_id1);
  1452. if (result != SA_AIS_OK) {
  1453. get_sa_error(result, result_buf, result_buf_len);
  1454. printf("ERROR: event Publish result: %s\n", result_buf);
  1455. goto evt_close;
  1456. }
  1457. /*
  1458. * See if we got the event
  1459. */
  1460. result = saEvtSelectionObjectGet(handle, &fd);
  1461. if (result != SA_AIS_OK) {
  1462. get_sa_error(result, result_buf, result_buf_len);
  1463. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1464. /* error */
  1465. return;
  1466. }
  1467. call_count = 0;
  1468. while(1) {
  1469. pfd.fd = fd;
  1470. pfd.events = POLLIN;
  1471. nfd = poll(&pfd, 1, timeout);
  1472. if (nfd == 0) {
  1473. break;
  1474. } else if (nfd < 0) {
  1475. perror("ERROR: poll error");
  1476. break;
  1477. }
  1478. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  1479. if (result != SA_AIS_OK) {
  1480. get_sa_error(result, result_buf, result_buf_len);
  1481. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1482. /* error */
  1483. goto evt_free;
  1484. }
  1485. }
  1486. if (call_count != 2) {
  1487. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  1488. goto evt_free;
  1489. }
  1490. /*
  1491. * Test cleanup
  1492. */
  1493. evt_free:
  1494. result = saEvtEventFree(event_handle);
  1495. if (result != SA_AIS_OK) {
  1496. get_sa_error(result, result_buf, result_buf_len);
  1497. printf("ERROR: event free result: %s\n", result_buf);
  1498. }
  1499. evt_close:
  1500. result = saEvtChannelClose(channel_handle);
  1501. if (result != SA_AIS_OK) {
  1502. get_sa_error(result, result_buf, result_buf_len);
  1503. printf("ERROR: channel close result(0): %s\n", result_buf);
  1504. }
  1505. result = saEvtChannelClose(channel_handle1);
  1506. if (result != SA_AIS_OK) {
  1507. get_sa_error(result, result_buf, result_buf_len);
  1508. printf("ERROR: channel close result(1): %s\n", result_buf);
  1509. }
  1510. evt_fin:
  1511. result = saEvtFinalize(handle);
  1512. if (result != SA_AIS_OK) {
  1513. get_sa_error(result, result_buf, result_buf_len);
  1514. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1515. }
  1516. printf("Done\n");
  1517. }
  1518. /*
  1519. * Handle call back for multi-test3
  1520. * Verifies that the event recevied is associated with the correct
  1521. * subscription.
  1522. *
  1523. */
  1524. void
  1525. multi_test_callback3(SaEvtSubscriptionIdT my_subscription_id,
  1526. const SaEvtEventHandleT event_handle,
  1527. const SaSizeT my_event_data_size)
  1528. {
  1529. SaAisErrorT result;
  1530. SaUint8T my_priority;
  1531. SaTimeT my_retention_time;
  1532. SaNameT my_publisher_name = {0, {0}};
  1533. SaTimeT my_publish_time;
  1534. SaEvtEventIdT my_event_id;
  1535. printf(" multi_test_callback2 called(%d)\n", ++call_count);
  1536. evt_pat_get_array.patternsNumber = 4;
  1537. result = saEvtEventAttributesGet(event_handle,
  1538. &evt_pat_get_array, /* patterns */
  1539. &my_priority, /* priority */
  1540. &my_retention_time, /* retention time */
  1541. &my_publisher_name, /* publisher name */
  1542. &my_publish_time, /* publish time */
  1543. &my_event_id /* event_id */
  1544. );
  1545. if (result != SA_AIS_OK) {
  1546. get_sa_error(result, result_buf, result_buf_len);
  1547. printf("ERROR: event get attr result: %s\n", result_buf);
  1548. goto evt_free;
  1549. }
  1550. if ((my_subscription_id != sub1) && (my_subscription_id != sub2)) {
  1551. printf("ERROR: Received wrong subscription ID %x\n",
  1552. my_subscription_id);
  1553. printf(" sub1 %x, sub2 %x\n", sub1, sub2);
  1554. goto evt_free;
  1555. }
  1556. if ((my_event_id != event_id1) && (my_event_id != event_id2)) {
  1557. printf("ERROR: Received wrong event ID %llx\n",
  1558. (unsigned long long)my_event_id);
  1559. printf(" id1 %llx, id2 %llx\n",
  1560. (unsigned long long)event_id1,
  1561. (unsigned long long)event_id2);
  1562. goto evt_free;
  1563. }
  1564. if ((my_subscription_id == sub1) && (my_event_id != event_id1)) {
  1565. printf("ERROR: Received event on wrong subscription\n");
  1566. goto evt_free;
  1567. }
  1568. if ((my_subscription_id == sub2) && (my_event_id != event_id2)) {
  1569. printf("ERROR: Received event on wrong subscription\n");
  1570. goto evt_free;
  1571. }
  1572. if (evt_pat_get_array.patternsNumber != 1) {
  1573. printf("ERROR: pattern array count not 1: %d\n",
  1574. evt_pat_get_array.patternsNumber);
  1575. }
  1576. evt_free:
  1577. result = saEvtEventFree(event_handle);
  1578. if (result != SA_AIS_OK) {
  1579. get_sa_error(result, result_buf, result_buf_len);
  1580. printf("ERROR: event free result: %s\n", result_buf);
  1581. }
  1582. }
  1583. void
  1584. test_multi_channel3()
  1585. {
  1586. SaEvtEventFilterT filt1[1] = {
  1587. {SA_EVT_PREFIX_FILTER, {"ChanPat", 7}},
  1588. };
  1589. SaEvtEventFilterArrayT sub_filt = {
  1590. NULL, 1
  1591. };
  1592. SaEvtEventPatternT pat1 = {"ChanPat1", 8};
  1593. SaEvtEventPatternT pat2 = {"ChanPat2", 8};
  1594. SaEvtEventPatternArrayT evt_pat = {
  1595. NULL, 1
  1596. };
  1597. SaEvtHandleT handle;
  1598. SaEvtChannelHandleT channel_handle;
  1599. SaEvtChannelHandleT channel_handle1;
  1600. SaEvtEventHandleT event_handle;
  1601. SaEvtEventHandleT event_handle1;
  1602. SaEvtChannelOpenFlagsT flags;
  1603. SaNameT channel_name;
  1604. SaNameT channel_name1;
  1605. SaEvtCallbacksT multi_callbacks = {
  1606. 0,
  1607. multi_test_callback3
  1608. };
  1609. struct pollfd pfd;
  1610. int nfd;
  1611. int fd;
  1612. int timeout = 5000;
  1613. int result;
  1614. flags = SA_EVT_CHANNEL_PUBLISHER|SA_EVT_CHANNEL_SUBSCRIBER |
  1615. SA_EVT_CHANNEL_CREATE;
  1616. strcpy(channel_name.value, channel);
  1617. channel_name.length = strlen(channel_name.value);
  1618. strcpy(channel_name1.value, channel);
  1619. strcat(channel_name1.value, "_1");
  1620. channel_name1.length = strlen(channel_name1.value);
  1621. /*
  1622. * 3. Test opening of multiple channels and receiving events.
  1623. * Open and subscribe to two different channels twice.
  1624. * Subscribe to each channel with the same filters.
  1625. * Sending an event on one channel should be received in the
  1626. * call-back with the subscription ID corresponding to the sent
  1627. * channel.
  1628. */
  1629. printf("Test multiple different channels/subscribes:\n");
  1630. result = saEvtInitialize (&handle, &multi_callbacks, versions[0].version);
  1631. if (result != SA_AIS_OK) {
  1632. get_sa_error(result, result_buf, result_buf_len);
  1633. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1634. return;
  1635. }
  1636. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1637. &channel_handle);
  1638. if (result != SA_AIS_OK) {
  1639. get_sa_error(result, result_buf, result_buf_len);
  1640. printf("ERROR: channel open(0) result: %s\n", result_buf);
  1641. goto evt_fin;
  1642. }
  1643. result = saEvtChannelOpen(handle, &channel_name1, flags, 0,
  1644. &channel_handle1);
  1645. if (result != SA_AIS_OK) {
  1646. get_sa_error(result, result_buf, result_buf_len);
  1647. printf("ERROR: channel open(1) result: %s\n", result_buf);
  1648. goto evt_fin;
  1649. }
  1650. result = saEvtEventAllocate(channel_handle, &event_handle);
  1651. if (result != SA_AIS_OK) {
  1652. get_sa_error(result, result_buf, result_buf_len);
  1653. printf("ERROR: event Allocate(0) result: %s\n", result_buf);
  1654. goto evt_close;
  1655. }
  1656. result = saEvtEventAllocate(channel_handle1, &event_handle1);
  1657. if (result != SA_AIS_OK) {
  1658. get_sa_error(result, result_buf, result_buf_len);
  1659. printf("ERROR: event Allocate(1) result: %s\n", result_buf);
  1660. goto evt_close;
  1661. }
  1662. sub_filt.filters = filt1;
  1663. result = saEvtEventSubscribe(channel_handle,
  1664. &sub_filt,
  1665. sub1);
  1666. if (result != SA_AIS_OK) {
  1667. get_sa_error(result, result_buf, result_buf_len);
  1668. printf("ERROR: event subscribe(0) result: %s\n", result_buf);
  1669. goto evt_free;
  1670. }
  1671. sub_filt.filters = filt1;
  1672. result = saEvtEventSubscribe(channel_handle1,
  1673. &sub_filt,
  1674. sub2);
  1675. if (result != SA_AIS_OK) {
  1676. get_sa_error(result, result_buf, result_buf_len);
  1677. printf("ERROR: event subscribe(1) result: %s\n", result_buf);
  1678. goto evt_free;
  1679. }
  1680. retention_time = 0ULL;
  1681. evt_pat.patterns = &pat1;
  1682. result = saEvtEventAttributesSet(event_handle,
  1683. &evt_pat,
  1684. TEST_PRIORITY,
  1685. retention_time,
  1686. &test_pub_name);
  1687. if (result != SA_AIS_OK) {
  1688. get_sa_error(result, result_buf, result_buf_len);
  1689. printf("ERROR: event set attr(0) result: %s\n", result_buf);
  1690. goto evt_free;
  1691. }
  1692. evt_pat.patterns = &pat2;
  1693. result = saEvtEventAttributesSet(event_handle1,
  1694. &evt_pat,
  1695. TEST_PRIORITY,
  1696. retention_time,
  1697. &test_pub_name);
  1698. if (result != SA_AIS_OK) {
  1699. get_sa_error(result, result_buf, result_buf_len);
  1700. printf("ERROR: event set attr(1) result: %s\n", result_buf);
  1701. goto evt_free;
  1702. }
  1703. result = saEvtEventPublish(event_handle, exp_data, DATA_SIZE,
  1704. &event_id1);
  1705. if (result != SA_AIS_OK) {
  1706. get_sa_error(result, result_buf, result_buf_len);
  1707. printf("ERROR: event Publish result: %s\n", result_buf);
  1708. goto evt_close;
  1709. }
  1710. result = saEvtEventPublish(event_handle1, exp_data, DATA_SIZE,
  1711. &event_id2);
  1712. if (result != SA_AIS_OK) {
  1713. get_sa_error(result, result_buf, result_buf_len);
  1714. printf("ERROR: event Publish result: %s\n", result_buf);
  1715. goto evt_close;
  1716. }
  1717. /*
  1718. * See if we got the events
  1719. */
  1720. result = saEvtSelectionObjectGet(handle, &fd);
  1721. if (result != SA_AIS_OK) {
  1722. get_sa_error(result, result_buf, result_buf_len);
  1723. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1724. /* error */
  1725. return;
  1726. }
  1727. call_count = 0;
  1728. while(1) {
  1729. pfd.fd = fd;
  1730. pfd.events = POLLIN;
  1731. nfd = poll(&pfd, 1, timeout);
  1732. if (nfd == 0) {
  1733. break;
  1734. } else if (nfd < 0) {
  1735. perror("ERROR: poll error");
  1736. break;
  1737. }
  1738. result = saEvtDispatch(handle, SA_DISPATCH_ALL);
  1739. if (result != SA_AIS_OK) {
  1740. get_sa_error(result, result_buf, result_buf_len);
  1741. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1742. /* error */
  1743. goto evt_free;
  1744. }
  1745. }
  1746. if (call_count != 2) {
  1747. printf("ERROR: call back count: e=2, a=%d\n", call_count);
  1748. goto evt_free;
  1749. }
  1750. /*
  1751. * Test cleanup
  1752. */
  1753. evt_free:
  1754. result = saEvtEventFree(event_handle);
  1755. if (result != SA_AIS_OK) {
  1756. get_sa_error(result, result_buf, result_buf_len);
  1757. printf("ERROR: event free result: %s\n", result_buf);
  1758. }
  1759. result = saEvtEventFree(event_handle1);
  1760. if (result != SA_AIS_OK) {
  1761. get_sa_error(result, result_buf, result_buf_len);
  1762. printf("ERROR: event free result: %s\n", result_buf);
  1763. }
  1764. evt_close:
  1765. result = saEvtChannelClose(channel_handle);
  1766. if (result != SA_AIS_OK) {
  1767. get_sa_error(result, result_buf, result_buf_len);
  1768. printf("ERROR: channel close result(0): %s\n", result_buf);
  1769. }
  1770. result = saEvtChannelClose(channel_handle1);
  1771. if (result != SA_AIS_OK) {
  1772. get_sa_error(result, result_buf, result_buf_len);
  1773. printf("ERROR: channel close result(1): %s\n", result_buf);
  1774. }
  1775. evt_fin:
  1776. result = saEvtFinalize(handle);
  1777. if (result != SA_AIS_OK) {
  1778. get_sa_error(result, result_buf, result_buf_len);
  1779. printf("ERROR: Event Finalize result: %s\n", result_buf);
  1780. }
  1781. printf("Done\n");
  1782. }
  1783. /*
  1784. * Test event retention
  1785. * Test 1: publish the event with a retention time and then
  1786. * subscribe. If the event was retained, we should receive it.
  1787. *
  1788. * Test 2: Publish the event, sleep until it expires, then
  1789. * subscribe. We shouldn't get an event delivered.
  1790. *
  1791. * Test 3: Publish an event with a retention time.
  1792. * subscribe.
  1793. * wait for it.
  1794. * unsubscribe
  1795. * Clear it.
  1796. * Then subscribe and make sure that the messages isn't delivered.
  1797. *
  1798. */
  1799. #define EXPIRE_TIME 10 /* Seconds */
  1800. SaEvtEventIdT retained_id;
  1801. int got_event;
  1802. void
  1803. event_callback_retained(SaEvtSubscriptionIdT my_subscription_id,
  1804. const SaEvtEventHandleT event_handle,
  1805. const SaSizeT my_event_data_size)
  1806. {
  1807. SaAisErrorT result;
  1808. //printf("event_callback_retained called\n");
  1809. result = saEvtEventAttributesGet(event_handle,
  1810. 0, /* patterns */
  1811. 0, /* priority */
  1812. 0, /* retention time */
  1813. 0, /* publisher name */
  1814. 0, /* publish time */
  1815. &retained_id /* event_id */
  1816. );
  1817. if (result != SA_AIS_OK) {
  1818. get_sa_error(result, result_buf, result_buf_len);
  1819. printf("ERROR: callback attr get result: %s\n", result_buf);
  1820. return;
  1821. }
  1822. got_event = 1;
  1823. }
  1824. void
  1825. test_retention()
  1826. {
  1827. SaEvtHandleT handle;
  1828. SaEvtChannelHandleT channel_handle;
  1829. SaEvtEventHandleT event_handle;
  1830. SaEvtChannelOpenFlagsT flags;
  1831. SaNameT channel_name;
  1832. SaEvtCallbacksT callbacks_retain = {
  1833. 0,
  1834. event_callback_retained
  1835. };
  1836. struct pollfd pfd;
  1837. int nfd;
  1838. int fd;
  1839. int timeout = (EXPIRE_TIME + 5);
  1840. SaAisErrorT result;
  1841. flags = SA_EVT_CHANNEL_PUBLISHER |
  1842. SA_EVT_CHANNEL_SUBSCRIBER |
  1843. SA_EVT_CHANNEL_CREATE;
  1844. strcpy(channel_name.value, channel);
  1845. channel_name.length = strlen(channel);
  1846. printf("Test Event retention:\n");
  1847. result = saEvtInitialize (&handle, &callbacks_retain,
  1848. versions[0].version);
  1849. if (result != SA_AIS_OK) {
  1850. get_sa_error(result, result_buf, result_buf_len);
  1851. printf("ERROR: Event Initialize result: %s\n", result_buf);
  1852. return;
  1853. }
  1854. result = saEvtSelectionObjectGet(handle, &fd);
  1855. if (result != SA_AIS_OK) {
  1856. get_sa_error(result, result_buf, result_buf_len);
  1857. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1858. /* error */
  1859. return;
  1860. }
  1861. result = saEvtChannelOpen(handle, &channel_name, flags, 0,
  1862. &channel_handle);
  1863. if (result != SA_AIS_OK) {
  1864. get_sa_error(result, result_buf, result_buf_len);
  1865. printf("ERROR: channel open result: %s\n", result_buf);
  1866. goto evt_fin;
  1867. }
  1868. /*
  1869. * Allocate an event
  1870. */
  1871. result = saEvtEventAllocate(channel_handle, &event_handle);
  1872. if (result != SA_AIS_OK) {
  1873. get_sa_error(result, result_buf, result_buf_len);
  1874. printf("ERROR: event Allocate result: %s\n", result_buf);
  1875. goto evt_close;
  1876. }
  1877. retention_time = (EXPIRE_TIME)*1000000000ULL;
  1878. result = saEvtEventAttributesSet(event_handle,
  1879. &evt_pat_set_array,
  1880. TEST_PRIORITY,
  1881. retention_time,
  1882. &test_pub_name);
  1883. if (result != SA_AIS_OK) {
  1884. get_sa_error(result, result_buf, result_buf_len);
  1885. printf("ERROR: event set attr result: %s\n", result_buf);
  1886. goto evt_free;
  1887. }
  1888. /*
  1889. * Test 1: publish the event with a retention time and then
  1890. * subscribe. If the event was retained, we should receive it.
  1891. */
  1892. printf(" Receive retained event\n");
  1893. got_event=0;
  1894. retained_id=0;
  1895. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  1896. if (result != SA_AIS_OK) {
  1897. get_sa_error(result, result_buf, result_buf_len);
  1898. printf("ERROR: event Publish result(1): %s\n", result_buf);
  1899. goto evt_close;
  1900. }
  1901. result = saEvtEventSubscribe(channel_handle,
  1902. &subscribe_filters,
  1903. subscription_id);
  1904. if (result != SA_AIS_OK) {
  1905. get_sa_error(result, result_buf, result_buf_len);
  1906. printf("ERROR: event subscribe result: %s\n", result_buf);
  1907. goto evt_free;
  1908. }
  1909. pfd.fd = fd;
  1910. pfd.events = POLLIN;
  1911. nfd = poll(&pfd, 1, 1000);
  1912. if (nfd <= 0) {
  1913. printf("ERROR: poll fds %d\n", nfd);
  1914. if (nfd < 0) {
  1915. perror("ERROR: poll error");
  1916. }
  1917. /* Error */
  1918. goto evt_free;
  1919. }
  1920. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  1921. if (result != SA_AIS_OK) {
  1922. get_sa_error(result, result_buf, result_buf_len);
  1923. printf("ERROR: saEvtDispatch %s\n", result_buf);
  1924. /* error */
  1925. goto evt_free;
  1926. }
  1927. if (!got_event) {
  1928. printf("ERROR: retained event not recevied\n");
  1929. goto evt_free;
  1930. }
  1931. if (retained_id != event_id) {
  1932. printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
  1933. (unsigned long long)event_id,
  1934. (unsigned long long)retained_id);
  1935. goto evt_free;
  1936. }
  1937. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  1938. if (result != SA_AIS_OK) {
  1939. get_sa_error(result, result_buf, result_buf_len);
  1940. printf("ERROR: unsubscribe result: %s\n", result_buf);
  1941. goto evt_free;
  1942. }
  1943. /*
  1944. * Test 2: Publish the event, sleep until it expires, then
  1945. * subscribe. We shouldn't get an event delivered.
  1946. */
  1947. printf(" Expire retained event\n");
  1948. got_event=0;
  1949. retained_id=0;
  1950. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  1951. if (result != SA_AIS_OK) {
  1952. get_sa_error(result, result_buf, result_buf_len);
  1953. printf("ERROR: event Publish result(1): %s\n", result_buf);
  1954. goto evt_close;
  1955. }
  1956. result = saEvtSelectionObjectGet(handle, &fd);
  1957. if (result != SA_AIS_OK) {
  1958. get_sa_error(result, result_buf, result_buf_len);
  1959. printf("ERROR: saEvtSelectionObject get %s\n", result_buf);
  1960. /* error */
  1961. return;
  1962. }
  1963. /*
  1964. * Wait for the event to expire, then subscribe. We shouldn't get
  1965. * an event
  1966. */
  1967. sleep(timeout);
  1968. result = saEvtEventSubscribe(channel_handle,
  1969. &subscribe_filters,
  1970. subscription_id);
  1971. if (result != SA_AIS_OK) {
  1972. get_sa_error(result, result_buf, result_buf_len);
  1973. printf("ERROR: event subscribe result: %s\n", result_buf);
  1974. result = saEvtChannelClose(channel_handle);
  1975. goto evt_free;
  1976. }
  1977. pfd.fd = fd;
  1978. pfd.events = POLLIN;
  1979. nfd = poll(&pfd, 1, 1000);
  1980. if (nfd != 0) {
  1981. printf("ERROR: poll fds %d\n", nfd);
  1982. if (nfd < 0) {
  1983. perror("ERROR: poll error");
  1984. }
  1985. /* Error */
  1986. goto evt_free;
  1987. }
  1988. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  1989. if (result != SA_AIS_OK) {
  1990. get_sa_error(result, result_buf, result_buf_len);
  1991. printf("ERROR: unsubscribe result: %s\n", result_buf);
  1992. goto evt_free;
  1993. }
  1994. /*
  1995. * Test 3:
  1996. * Publish an event with a retention time.
  1997. * subscribe.
  1998. * wait for it.
  1999. * unsubscribe
  2000. * Clear it.
  2001. * Then subscribe and make sure that the messages isn't delivered.
  2002. */
  2003. printf(" Clear event retention time\n");
  2004. got_event=0;
  2005. retained_id=0;
  2006. result = saEvtEventPublish(event_handle, exp_data, 0, &event_id);
  2007. if (result != SA_AIS_OK) {
  2008. get_sa_error(result, result_buf, result_buf_len);
  2009. printf("ERROR: event Publish result(2): %s\n", result_buf);
  2010. goto evt_free;
  2011. }
  2012. result = saEvtEventSubscribe(channel_handle,
  2013. &subscribe_filters,
  2014. subscription_id);
  2015. if (result != SA_AIS_OK) {
  2016. get_sa_error(result, result_buf, result_buf_len);
  2017. printf("ERROR: event subscribe result: %s\n", result_buf);
  2018. goto evt_free;
  2019. }
  2020. pfd.fd = fd;
  2021. pfd.events = POLLIN;
  2022. nfd = poll(&pfd, 1, 1000);
  2023. if (nfd <= 0) {
  2024. printf("ERROR: poll fds %d\n", nfd);
  2025. if (nfd < 0) {
  2026. perror("ERROR: poll error");
  2027. }
  2028. /* Error */
  2029. goto evt_free;
  2030. }
  2031. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  2032. if (result != SA_AIS_OK) {
  2033. get_sa_error(result, result_buf, result_buf_len);
  2034. printf("ERROR: saEvtDispatch %s\n", result_buf);
  2035. /* error */
  2036. goto evt_free;
  2037. }
  2038. if (!got_event) {
  2039. printf("ERROR: retained event not recevied\n");
  2040. goto evt_free;
  2041. }
  2042. if (retained_id != event_id) {
  2043. printf("ERROR: received the wrong event: e=%llx, a=%llx\n",
  2044. (unsigned long long)event_id,
  2045. (unsigned long long)retained_id);
  2046. goto evt_free;
  2047. }
  2048. result = saEvtEventUnsubscribe(channel_handle, subscription_id);
  2049. if (result != SA_AIS_OK) {
  2050. get_sa_error(result, result_buf, result_buf_len);
  2051. printf("ERROR: unsubscribe result: %s\n", result_buf);
  2052. goto evt_free;
  2053. }
  2054. result = saEvtEventRetentionTimeClear(channel_handle, event_id);
  2055. if (result != SA_AIS_OK) {
  2056. get_sa_error(result, result_buf, result_buf_len);
  2057. printf("ERROR: clear retention time result: %s\n", result_buf);
  2058. goto evt_free;
  2059. }
  2060. result = saEvtEventSubscribe(channel_handle,
  2061. &subscribe_filters,
  2062. subscription_id);
  2063. if (result != SA_AIS_OK) {
  2064. get_sa_error(result, result_buf, result_buf_len);
  2065. printf("ERROR: event subscribe result: %s\n", result_buf);
  2066. goto evt_free;
  2067. }
  2068. pfd.fd = fd;
  2069. pfd.events = POLLIN;
  2070. nfd = poll(&pfd, 1, 1000);
  2071. if (nfd != 0) {
  2072. printf("ERROR: poll fds %d\n", nfd);
  2073. if (nfd < 0) {
  2074. perror("ERROR: poll error");
  2075. }
  2076. /* Error */
  2077. goto evt_free;
  2078. }
  2079. /*
  2080. * Test cleanup
  2081. */
  2082. evt_free:
  2083. result = saEvtEventFree(event_handle);
  2084. if (result != SA_AIS_OK) {
  2085. get_sa_error(result, result_buf, result_buf_len);
  2086. printf("ERROR: event free result: %s\n", result_buf);
  2087. }
  2088. evt_close:
  2089. result = saEvtChannelClose(channel_handle);
  2090. if (result != SA_AIS_OK) {
  2091. get_sa_error(result, result_buf, result_buf_len);
  2092. printf("ERROR: channel close result: %s\n", result_buf);
  2093. }
  2094. evt_fin:
  2095. result = saEvtFinalize(handle);
  2096. if (result != SA_AIS_OK) {
  2097. get_sa_error(result, result_buf, result_buf_len);
  2098. printf("ERROR: Event Finalize result: %s\n", result_buf);
  2099. }
  2100. printf("Done\n");
  2101. }
  2102. void
  2103. unlink_chan_callback(SaEvtSubscriptionIdT my_subscription_id,
  2104. const SaEvtEventHandleT event_handle,
  2105. const SaSizeT my_event_data_size)
  2106. {
  2107. SaAisErrorT result;
  2108. SaUint8T my_priority;
  2109. SaTimeT my_retention_time;
  2110. SaNameT my_publisher_name = {0, {0}};
  2111. SaTimeT my_publish_time;
  2112. SaEvtEventIdT my_event_id;
  2113. SaEvtSubscriptionIdT exp_sub_id;
  2114. printf(" unlink_chan_callback called(%d)\n", ++call_count);
  2115. evt_pat_get_array.patternsNumber = 4;
  2116. result = saEvtEventAttributesGet(event_handle,
  2117. &evt_pat_get_array, /* patterns */
  2118. &my_priority, /* priority */
  2119. &my_retention_time, /* retention time */
  2120. &my_publisher_name, /* publisher name */
  2121. &my_publish_time, /* publish time */
  2122. &my_event_id /* event_id */
  2123. );
  2124. if (result != SA_AIS_OK) {
  2125. get_sa_error(result, result_buf, result_buf_len);
  2126. printf("ERROR: event get attr result: %s\n", result_buf);
  2127. goto evt_free;
  2128. }
  2129. if (my_event_id == event_id1) {
  2130. exp_sub_id = sub1;
  2131. } else if (my_event_id == event_id2) {
  2132. exp_sub_id = sub2;
  2133. } else {
  2134. printf("ERROR: Received event %llx but not sent\n",
  2135. (unsigned long long)my_event_id);
  2136. goto evt_free;
  2137. }
  2138. if (my_subscription_id != exp_sub_id) {
  2139. printf("ERROR: sub ID: e=%x, a=%x\n",
  2140. exp_sub_id, my_subscription_id);
  2141. goto evt_free;
  2142. }
  2143. evt_free:
  2144. result = saEvtEventFree(event_handle);
  2145. if (result != SA_AIS_OK) {
  2146. get_sa_error(result, result_buf, result_buf_len);
  2147. printf("ERROR: event free result: %s\n", result_buf);
  2148. }
  2149. }
  2150. /*
  2151. * Test channel unlink operations.
  2152. * 1. Unlink channel.
  2153. * 2. Open/create a channel, close channel, open channel.
  2154. * 3. unlink channel, Open channel.
  2155. * 4. Open/create, unlink channel, close channel, open channel.
  2156. * 5. Open/create a channel, unlink channel, open/create channel, send
  2157. * event on each.
  2158. * 6. unlink all, close all.
  2159. */
  2160. SaEvtCallbacksT unlink_callbacks = {
  2161. open_callback,
  2162. unlink_chan_callback
  2163. };
  2164. void
  2165. test_unlink_channel()
  2166. {
  2167. SaEvtHandleT handle;
  2168. SaEvtChannelHandleT channel_handle1;
  2169. SaEvtChannelHandleT channel_handle2;
  2170. SaEvtEventHandleT event_handle1;
  2171. SaEvtEventHandleT event_handle2;
  2172. SaEvtChannelOpenFlagsT flags1, flags2;
  2173. SaNameT channel_name;
  2174. int result;
  2175. struct pollfd pfd;
  2176. int nfd;
  2177. int fd;
  2178. int timeout = 5000;
  2179. flags1 = SA_EVT_CHANNEL_PUBLISHER |
  2180. SA_EVT_CHANNEL_SUBSCRIBER |
  2181. SA_EVT_CHANNEL_CREATE;
  2182. flags2 = SA_EVT_CHANNEL_PUBLISHER |
  2183. SA_EVT_CHANNEL_SUBSCRIBER;
  2184. printf("Test Channel Unlink operations:\n");
  2185. result = saEvtInitialize (&handle, &unlink_callbacks, versions[0].version);
  2186. if (result != SA_AIS_OK) {
  2187. get_sa_error(result, result_buf, result_buf_len);
  2188. printf("ERROR: Event Initialize result: %s\n", result_buf);
  2189. goto unlink_exit;
  2190. }
  2191. /*
  2192. * 1. Unlink channel.
  2193. *
  2194. * Unlink previously opened channel should return OK.
  2195. * Unlink of non-existent channel should return error.
  2196. */
  2197. printf(" 1 Channel unlink:\n");
  2198. strcpy(channel_name.value, channel);
  2199. channel_name.length = strlen(channel);
  2200. result = saEvtChannelUnlink(handle, &channel_name);
  2201. if (result != SA_AIS_OK) {
  2202. get_sa_error(result, result_buf, result_buf_len);
  2203. printf("ERROR: channel unlink(1) result: %s\n", result_buf);
  2204. goto unlink_exit;
  2205. }
  2206. strcpy(channel_name.value, unlink_channel);
  2207. channel_name.length = strlen(unlink_channel);
  2208. result = saEvtChannelUnlink(handle, &channel_name);
  2209. if (result != SA_AIS_ERR_NOT_EXIST) {
  2210. get_sa_error(result, result_buf, result_buf_len);
  2211. printf("ERROR: channel unlink(2) result: %s\n", result_buf);
  2212. goto unlink_exit;
  2213. }
  2214. /*
  2215. * 2. Open/create a channel, close channel, open channel.
  2216. *
  2217. * Open/create the channel.
  2218. * Close the channel.
  2219. * Open without create. This should succeed in the B spec.
  2220. */
  2221. printf(" 2 Channel open/close/open:\n");
  2222. result = saEvtChannelOpen(handle, &channel_name, flags1, 0,
  2223. &channel_handle1);
  2224. if (result != SA_AIS_OK) {
  2225. get_sa_error(result, result_buf, result_buf_len);
  2226. printf("ERROR: channel open(1) result: %s\n", result_buf);
  2227. goto unlink_exit;
  2228. }
  2229. result = saEvtChannelClose(channel_handle1);
  2230. if (result != SA_AIS_OK) {
  2231. get_sa_error(result, result_buf, result_buf_len);
  2232. printf("ERROR: channel close(1) result: %s\n", result_buf);
  2233. goto unlink_exit;
  2234. }
  2235. result = saEvtChannelOpen(handle, &channel_name, flags2, 0,
  2236. &channel_handle1);
  2237. if (result != SA_AIS_OK) {
  2238. get_sa_error(result, result_buf, result_buf_len);
  2239. printf("ERROR: channel open(2) result: %s\n", result_buf);
  2240. goto unlink_exit;
  2241. }
  2242. /*
  2243. * 3. unlink channel, Open channel, close channel
  2244. *
  2245. * Unlink the channel. Should mark for deletion but not
  2246. * delete it since it is already open.
  2247. * Open the channel without create. This should fail since
  2248. * the channel is marked for deletion and a new version
  2249. * hasn't been created.
  2250. * Close channel.
  2251. */
  2252. printf(" 3 Channel unlink/open/close:\n");
  2253. result = saEvtChannelUnlink(handle, &channel_name);
  2254. if (result != SA_AIS_OK) {
  2255. get_sa_error(result, result_buf, result_buf_len);
  2256. printf("ERROR: channel unlink result: %s\n", result_buf);
  2257. goto unlink_exit;
  2258. }
  2259. result = saEvtChannelOpen(handle, &channel_name, flags2, 0,
  2260. &channel_handle2);
  2261. if (result != SA_AIS_ERR_NOT_EXIST) {
  2262. get_sa_error(result, result_buf, result_buf_len);
  2263. printf("ERROR: channel open result: %s\n", result_buf);
  2264. goto unlink_exit;
  2265. }
  2266. result = saEvtChannelClose(channel_handle1);
  2267. if (result != SA_AIS_OK) {
  2268. get_sa_error(result, result_buf, result_buf_len);
  2269. printf("ERROR: channel close(1) result: %s\n", result_buf);
  2270. goto unlink_exit;
  2271. }
  2272. /*
  2273. *
  2274. * 4. Open/create, unlink channel, close channel, open channel.
  2275. *
  2276. * Open/create the channel.
  2277. * unlink the channel.
  2278. * close the channel. This should delete the channel instance since
  2279. * it was marked for deletion.
  2280. * open the channel without create. This should fail since the
  2281. * channel doesn't exist anymore.
  2282. */
  2283. printf(" 4 Channel open/unlink/close/open:\n");
  2284. result = saEvtChannelOpen(handle, &channel_name, flags1, 0,
  2285. &channel_handle1);
  2286. if (result != SA_AIS_OK) {
  2287. get_sa_error(result, result_buf, result_buf_len);
  2288. printf("ERROR: channel open(1) result: %s\n", result_buf);
  2289. goto unlink_exit;
  2290. }
  2291. result = saEvtChannelUnlink(handle, &channel_name);
  2292. if (result != SA_AIS_OK) {
  2293. get_sa_error(result, result_buf, result_buf_len);
  2294. printf("ERROR: channel unlink result: %s\n", result_buf);
  2295. goto unlink_exit;
  2296. }
  2297. result = saEvtChannelClose(channel_handle1);
  2298. if (result != SA_AIS_OK) {
  2299. get_sa_error(result, result_buf, result_buf_len);
  2300. printf("ERROR: channel close(1) result: %s\n", result_buf);
  2301. goto unlink_exit;
  2302. }
  2303. result = saEvtChannelOpen(handle, &channel_name, flags2, 0,
  2304. &channel_handle1);
  2305. if (result != SA_AIS_ERR_NOT_EXIST) {
  2306. get_sa_error(result, result_buf, result_buf_len);
  2307. printf("ERROR: channel open(2) result: %s\n", result_buf);
  2308. goto unlink_exit;
  2309. }
  2310. /*
  2311. * 5. Open/create a channel, unlink channel, open/create channel, send
  2312. * event on each.
  2313. *
  2314. * Open/create.
  2315. * unlink. Mark for deletion.
  2316. * open/create. Create new channel of same name.
  2317. * send event on each open channel. The events should be received on
  2318. * separate channels.
  2319. */
  2320. printf(" 5 Channel open/unlink/open/send:\n");
  2321. result = saEvtChannelOpen(handle, &channel_name, flags1, 0,
  2322. &channel_handle1);
  2323. if (result != SA_AIS_OK) {
  2324. get_sa_error(result, result_buf, result_buf_len);
  2325. printf("ERROR: channel open result: %s\n", result_buf);
  2326. goto unlink_exit;
  2327. }
  2328. result = saEvtChannelUnlink(handle, &channel_name);
  2329. if (result != SA_AIS_OK) {
  2330. get_sa_error(result, result_buf, result_buf_len);
  2331. printf("ERROR: channel unlink result: %s\n", result_buf);
  2332. goto unlink_exit;
  2333. }
  2334. result = saEvtChannelOpen(handle, &channel_name, flags1, 0,
  2335. &channel_handle2);
  2336. if (result != SA_AIS_OK) {
  2337. get_sa_error(result, result_buf, result_buf_len);
  2338. printf("ERROR: channel open result: %s\n", result_buf);
  2339. goto unlink_exit;
  2340. }
  2341. result = saEvtEventSubscribe(channel_handle1,
  2342. &subscribe_filters,
  2343. sub1);
  2344. if (result != SA_AIS_OK) {
  2345. get_sa_error(result, result_buf, result_buf_len);
  2346. printf("ERROR: channel subscribe(1) result: %s\n", result_buf);
  2347. goto unlink_exit;
  2348. }
  2349. result = saEvtEventSubscribe(channel_handle2,
  2350. &subscribe_filters,
  2351. sub2);
  2352. if (result != SA_AIS_OK) {
  2353. get_sa_error(result, result_buf, result_buf_len);
  2354. printf("ERROR: channel subscribe(2) result: %s\n", result_buf);
  2355. goto unlink_exit;
  2356. }
  2357. retention_time = 0ULL;
  2358. result = saEvtEventAllocate(channel_handle1, &event_handle1);
  2359. if (result != SA_AIS_OK) {
  2360. get_sa_error(result, result_buf, result_buf_len);
  2361. printf("ERROR: event allocate(1) result: %s\n", result_buf);
  2362. goto unlink_exit;
  2363. }
  2364. result = saEvtEventAttributesSet(event_handle1,
  2365. &evt_pat_set_array,
  2366. TEST_PRIORITY,
  2367. retention_time,
  2368. &test_pub_name);
  2369. if (result != SA_AIS_OK) {
  2370. get_sa_error(result, result_buf, result_buf_len);
  2371. printf("ERROR: event set(1) result: %s\n", result_buf);
  2372. goto unlink_exit;
  2373. }
  2374. result = saEvtEventAllocate(channel_handle2, &event_handle2);
  2375. if (result != SA_AIS_OK) {
  2376. get_sa_error(result, result_buf, result_buf_len);
  2377. printf("ERROR: event allocate(2) result: %s\n", result_buf);
  2378. goto unlink_exit;
  2379. }
  2380. result = saEvtEventAttributesSet(event_handle2,
  2381. &evt_pat_set_array,
  2382. TEST_PRIORITY,
  2383. retention_time,
  2384. &test_pub_name);
  2385. if (result != SA_AIS_OK) {
  2386. get_sa_error(result, result_buf, result_buf_len);
  2387. printf("ERROR: event set(2) result: %s\n", result_buf);
  2388. goto unlink_exit;
  2389. }
  2390. result = saEvtEventPublish(event_handle1, 0, 0, &event_id1);
  2391. if (result != SA_AIS_OK) {
  2392. get_sa_error(result, result_buf, result_buf_len);
  2393. printf("ERROR: event publish(1) result: %s\n", result_buf);
  2394. goto unlink_exit;
  2395. }
  2396. result = saEvtEventPublish(event_handle2, 0, 0, &event_id2);
  2397. if (result != SA_AIS_OK) {
  2398. get_sa_error(result, result_buf, result_buf_len);
  2399. printf("ERROR: event publish(2) result: %s\n", result_buf);
  2400. goto unlink_exit;
  2401. }
  2402. result = saEvtSelectionObjectGet(handle, &fd);
  2403. if (result != SA_AIS_OK) {
  2404. get_sa_error(result, result_buf, result_buf_len);
  2405. printf("ERROR: select object get result: %s\n", result_buf);
  2406. goto unlink_exit;
  2407. }
  2408. /*
  2409. * We should see a total of two events processed, not four
  2410. * as if both events were recevied on both channels.
  2411. */
  2412. call_count = 0;
  2413. do {
  2414. pfd.fd = fd;
  2415. pfd.events = POLLIN;
  2416. nfd = poll(&pfd, 1, timeout);
  2417. if (nfd <= 0) {
  2418. if (nfd < 0) {
  2419. perror("ERROR: poll error");
  2420. goto unlink_exit;
  2421. }
  2422. } else {
  2423. result = saEvtDispatch(handle, SA_DISPATCH_ONE);
  2424. if (result != SA_AIS_OK) {
  2425. get_sa_error(result, result_buf, result_buf_len);
  2426. printf("ERROR: saEvtDispatch %s\n", result_buf);
  2427. goto unlink_exit;
  2428. }
  2429. }
  2430. } while (nfd > 0);
  2431. if (call_count != 2) {
  2432. printf("ERROR: processed %d events\n", call_count);
  2433. goto unlink_exit;
  2434. }
  2435. /*
  2436. * 6. unlink all, close all.
  2437. *
  2438. * close all open channels.
  2439. * unlink the channel.
  2440. * open without create the channel. Verify that the channel no
  2441. * longer exists.
  2442. */
  2443. printf(" 6 Channel unlink all/close all/open:\n");
  2444. unlink_exit:
  2445. saEvtChannelClose(channel_handle1);
  2446. saEvtChannelClose(channel_handle2);
  2447. saEvtChannelUnlink(handle, &channel_name);
  2448. result = saEvtFinalize(handle);
  2449. if (result != SA_AIS_OK) {
  2450. get_sa_error(result, result_buf, result_buf_len);
  2451. printf("ERROR: Event Finalize result: %s\n", result_buf);
  2452. }
  2453. printf("Done\n");
  2454. }
  2455. int main (void)
  2456. {
  2457. test_initialize ();
  2458. test_channel();
  2459. test_event();
  2460. test_multi_channel1();
  2461. test_multi_channel2();
  2462. test_multi_channel3();
  2463. test_retention();
  2464. test_unlink_channel();
  2465. return (0);
  2466. }