testevt.c 87 KB

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