testmsg2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) 2008 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Ryan O'Hara (rohara@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <signal.h>
  39. #include <unistd.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/select.h>
  43. #include <sys/un.h>
  44. #include "saAis.h"
  45. #include "saMsg.h"
  46. void QueueOpenCallback (
  47. SaInvocationT invocation,
  48. SaMsgQueueHandleT queueHandle,
  49. SaAisErrorT error)
  50. {
  51. /* DEBUG */
  52. printf ("[TEST]: testmsg2 (QueueOpenCallback)\n");
  53. printf ("[TEST]: \t { invocation = %llx }\n",
  54. (unsigned long long)(invocation));
  55. printf ("[TEST]: \t { queueHandle = %llx }\n",
  56. (unsigned long long)(queueHandle));
  57. printf ("[TEST]: \t { error = %u }\n",
  58. (unsigned int)(error));
  59. }
  60. void QueueGroupTrackCallback (
  61. const SaNameT *queueGroupName,
  62. const SaMsgQueueGroupNotificationBufferT *notificationBuffer,
  63. SaUint32T numberOfMembers,
  64. SaAisErrorT error)
  65. {
  66. int i = 0;
  67. /* DEBUG */
  68. printf ("[TEST]: testmsg2 (QueueGroupTrackCallback)\n");
  69. printf ("[TEST]: \t { queueGroupName = %s }\n",
  70. (char *)(queueGroupName->value));
  71. printf ("[TEST]: \t { numberOfMembers = %u }\n",
  72. (unsigned int)(numberOfMembers));
  73. printf ("[TEST]: \t { error = %u }\n",
  74. (unsigned int)(error));
  75. printf ("[TEST]: \t { notificationBuffer->numberOfItems = %u }\n",
  76. (unsigned int)(notificationBuffer->numberOfItems));
  77. for (i = 0; i < notificationBuffer->numberOfItems; i++) {
  78. printf ("[TEST]: \t { item #%d => %s (%u) }\n", i,
  79. (char *)(notificationBuffer->notification[i].member.queueName.value),
  80. (unsigned int)(notificationBuffer->notification[i].change));
  81. }
  82. }
  83. void MessageDeliveredCallback (
  84. SaInvocationT invocation,
  85. SaAisErrorT error)
  86. {
  87. /* DEBUG */
  88. printf ("[TEST]: testmsg2 (MessageDeliveredCallback)\n");
  89. printf ("[TEST]: \t { invocation = %llx }\n",
  90. (unsigned long long)(invocation));
  91. printf ("[TEST]: \t { error = %u }\n",
  92. (unsigned int)(error));
  93. }
  94. void MessageReceivedCallback (
  95. SaMsgQueueHandleT queueHandle)
  96. {
  97. /* DEBUG */
  98. printf ("[TEST]: testmsg2 (MessageReceivedCallback)\n");
  99. printf ("[TEST]: \t { queueHandle = %llx }\n",
  100. (unsigned long long)(queueHandle));
  101. }
  102. SaMsgCallbacksT callbacks = {
  103. .saMsgQueueOpenCallback = QueueOpenCallback,
  104. .saMsgQueueGroupTrackCallback = QueueGroupTrackCallback,
  105. .saMsgMessageDeliveredCallback = MessageDeliveredCallback,
  106. .saMsgMessageReceivedCallback = MessageReceivedCallback
  107. };
  108. SaVersionT version = { 'B', 1, 1 };
  109. SaMsgQueueCreationAttributesT creation_attributes = {
  110. SA_MSG_QUEUE_PERSISTENT,
  111. { 128000, 128000, 128000 },
  112. SA_TIME_END
  113. };
  114. void setSaNameT (SaNameT *name, char *str) {
  115. name->length = strlen (str);
  116. strcpy (name->value, str);
  117. }
  118. int main (void)
  119. {
  120. int result;
  121. SaSelectionObjectT select_obj;
  122. SaMsgHandleT handle;
  123. SaMsgQueueHandleT queue_handle_a;
  124. SaMsgQueueHandleT queue_handle_b;
  125. SaMsgQueueHandleT queue_handle_c;
  126. SaMsgQueueHandleT queue_handle_x;
  127. SaMsgQueueHandleT queue_handle_y;
  128. SaMsgQueueHandleT queue_handle_z;
  129. SaNameT queue_name_a;
  130. SaNameT queue_name_b;
  131. SaNameT queue_name_c;
  132. SaNameT queue_name_x;
  133. SaNameT queue_name_y;
  134. SaNameT queue_name_z;
  135. SaNameT queue_group_one;
  136. SaNameT queue_group_two;
  137. setSaNameT(&queue_name_a, "QUEUE_A");
  138. setSaNameT(&queue_name_b, "QUEUE_B");
  139. setSaNameT(&queue_name_c, "QUEUE_C");
  140. setSaNameT(&queue_name_x, "QUEUE_X");
  141. setSaNameT(&queue_name_y, "QUEUE_Y");
  142. setSaNameT(&queue_name_z, "QUEUE_Z");
  143. setSaNameT(&queue_group_one, "GROUP_ONE");
  144. setSaNameT(&queue_group_two, "GROUP_TWO");
  145. result = saMsgInitialize (&handle, &callbacks, &version);
  146. if (result != SA_AIS_OK) {
  147. printf ("[ERROR]: (%d) saMsgInitialize\n", result);
  148. exit (1);
  149. }
  150. saMsgSelectionObjectGet (handle, &select_obj);
  151. /*
  152. * Create message queues
  153. */
  154. result = saMsgQueueOpen (handle, &queue_name_a, &creation_attributes,
  155. SA_MSG_QUEUE_CREATE, SA_TIME_END, &queue_handle_a);
  156. printf ("[DEBUG]: (%d) saMsgQueueOpen { %s }\n",
  157. result, (char *)(queue_name_a.value));
  158. result = saMsgQueueOpen (handle, &queue_name_b, &creation_attributes,
  159. SA_MSG_QUEUE_CREATE, SA_TIME_END, &queue_handle_b);
  160. printf ("[DEBUG]: (%d) saMsgQueueOpen { %s }\n",
  161. result, (char *)(queue_name_b.value));
  162. result = saMsgQueueOpen (handle, &queue_name_c, &creation_attributes,
  163. SA_MSG_QUEUE_CREATE, SA_TIME_END, &queue_handle_c);
  164. printf ("[DEBUG]: (%d) saMsgQueueOpen { %s }\n",
  165. result, (char *)(queue_name_c.value));
  166. result = saMsgQueueOpen (handle, &queue_name_x, &creation_attributes,
  167. SA_MSG_QUEUE_CREATE, SA_TIME_END, &queue_handle_x);
  168. printf ("[DEBUG]: (%d) saMsgQueueOpen { %s }\n",
  169. result, (char *)(queue_name_x.value));
  170. result = saMsgQueueOpen (handle, &queue_name_y, &creation_attributes,
  171. SA_MSG_QUEUE_CREATE, SA_TIME_END, &queue_handle_y);
  172. printf ("[DEBUG]: (%d) saMsgQueueOpen { %s }\n",
  173. result, (char *)(queue_name_y.value));
  174. result = saMsgQueueOpen (handle, &queue_name_z, &creation_attributes,
  175. SA_MSG_QUEUE_CREATE, SA_TIME_END, &queue_handle_z);
  176. printf ("[DEBUG]: (%d) saMsgQueueOpen { %s }\n",
  177. result, (char *)(queue_name_z.value));
  178. /*
  179. * Create queue groups
  180. */
  181. result = saMsgQueueGroupCreate (handle, &queue_group_one,
  182. SA_MSG_QUEUE_GROUP_ROUND_ROBIN);
  183. printf ("[DEBUG]: (%d) saMsgQueueGroupCreate { %s }\n",
  184. result, (char *)(queue_group_one.value));
  185. result = saMsgQueueGroupCreate (handle, &queue_group_two,
  186. SA_MSG_QUEUE_GROUP_ROUND_ROBIN);
  187. printf ("[DEBUG]: (%d) saMsgQueueGroupCreate { %s }\n",
  188. result, (char *)(queue_group_two.value));
  189. /*
  190. * Track GROUP_ONE with SA_TRACK_CHANGES
  191. */
  192. result = saMsgQueueGroupTrack (handle, &queue_group_one,
  193. SA_TRACK_CHANGES, NULL);
  194. printf ("[DEBUG]: (%d) saMsgQueueGroupTrack { %s }\n",
  195. result, (char *)(queue_group_one.value));
  196. /*
  197. * Track GROUP_TWO with SA_TRACK_CHANGES_ONLY
  198. */
  199. result = saMsgQueueGroupTrack (handle, &queue_group_two,
  200. SA_TRACK_CHANGES_ONLY, NULL);
  201. printf ("[DEBUG]: (%d) saMsgQueueGroupTrack { %s }\n",
  202. result, (char *)(queue_group_two.value));
  203. /*
  204. * Add queues to GROUP_ONE
  205. */
  206. result = saMsgQueueGroupInsert (handle, &queue_group_one, &queue_name_a);
  207. printf ("[DEBUG]: (%d) saMsgQueueGroupInsert { group: %s + queue: %s }\n",
  208. result, (char *)(queue_group_one.value), (char *)(queue_name_a.value));
  209. result = saMsgQueueGroupInsert (handle, &queue_group_one, &queue_name_b);
  210. printf ("[DEBUG]: (%d) saMsgQueueGroupInsert { group: %s + queue: %s }\n",
  211. result, (char *)(queue_group_one.value), (char *)(queue_name_b.value));
  212. result = saMsgQueueGroupInsert (handle, &queue_group_one, &queue_name_c);
  213. printf ("[DEBUG]: (%d) saMsgQueueGroupInsert { group: %s + queue: %s }\n",
  214. result, (char *)(queue_group_one.value), (char *)(queue_name_c.value));
  215. /*
  216. * Add queues to GROUP_TWO
  217. */
  218. result = saMsgQueueGroupInsert (handle, &queue_group_two, &queue_name_x);
  219. printf ("[DEBUG]: (%d) saMsgQueueGroupInsert { group: %s + queue: %s }\n",
  220. result, (char *)(queue_group_two.value), (char *)(queue_name_x.value));
  221. result = saMsgQueueGroupInsert (handle, &queue_group_two, &queue_name_y);
  222. printf ("[DEBUG]: (%d) saMsgQueueGroupInsert { group: %s + queue: %s }\n",
  223. result, (char *)(queue_group_two.value), (char *)(queue_name_y.value));
  224. result = saMsgQueueGroupInsert (handle, &queue_group_two, &queue_name_z);
  225. printf ("[DEBUG]: (%d) saMsgQueueGroupInsert { group: %s + queue: %s }\n",
  226. result, (char *)(queue_group_two.value), (char *)(queue_name_z.value));
  227. /*
  228. * Track queue groups with SA_TRACK_CURRENT
  229. */
  230. result = saMsgQueueGroupTrack (handle, &queue_group_one,
  231. SA_TRACK_CURRENT, NULL);
  232. printf ("[DEBUG]: (%d) saMsgQueueGroupTrack { %s }\n",
  233. result, (char *)(queue_group_one.value));
  234. result = saMsgQueueGroupTrack (handle, &queue_group_two,
  235. SA_TRACK_CURRENT, NULL);
  236. printf ("[DEBUG]: (%d) saMsgQueueGroupTrack { %s }\n",
  237. result, (char *)(queue_group_two.value));
  238. /*
  239. * Dispatch callbacks
  240. */
  241. result = saMsgDispatch (handle, SA_DISPATCH_ALL);
  242. printf ("[DEBUG]: (%d) saMsgDispatch\n", result);
  243. /*
  244. * Remove queues from GROUP_ONE
  245. */
  246. result = saMsgQueueGroupRemove (handle, &queue_group_one, &queue_name_a);
  247. printf ("[DEBUG]: (%d) saMsgQueueGroupRemove { group: %s - queue: %s }\n",
  248. result, (char *)(queue_group_one.value), (char *)(queue_name_a.value));
  249. result = saMsgQueueGroupRemove (handle, &queue_group_one, &queue_name_b);
  250. printf ("[DEBUG]: (%d) saMsgQueueGroupRemove { group: %s - queue: %s }\n",
  251. result, (char *)(queue_group_one.value), (char *)(queue_name_b.value));
  252. result = saMsgQueueGroupRemove (handle, &queue_group_one, &queue_name_c);
  253. printf ("[DEBUG]: (%d) saMsgQueueGroupRemove { group: %s - queue: %s }\n",
  254. result, (char *)(queue_group_one.value), (char *)(queue_name_c.value));
  255. /*
  256. * Remove queues from GROUP_TWO
  257. */
  258. result = saMsgQueueGroupRemove (handle, &queue_group_two, &queue_name_x);
  259. printf ("[DEBUG]: (%d) saMsgQueueGroupRemove { group: %s - queue: %s }\n",
  260. result, (char *)(queue_group_two.value), (char *)(queue_name_x.value));
  261. result = saMsgQueueGroupRemove (handle, &queue_group_two, &queue_name_y);
  262. printf ("[DEBUG]: (%d) saMsgQueueGroupRemove { group: %s - queue: %s }\n",
  263. result, (char *)(queue_group_two.value), (char *)(queue_name_y.value));
  264. result = saMsgQueueGroupRemove (handle, &queue_group_two, &queue_name_z);
  265. printf ("[DEBUG]: (%d) saMsgQueueGroupRemove { group: %s - queue: %s }\n",
  266. result, (char *)(queue_group_two.value), (char *)(queue_name_z.value));
  267. /*
  268. * Dispatch callbacks
  269. */
  270. result = saMsgDispatch (handle, SA_DISPATCH_ALL);
  271. printf ("[DEBUG]: (%d) saMsgDispatch\n", result);
  272. /*
  273. * Stop tracking GROUP_ONE
  274. */
  275. result = saMsgQueueGroupTrackStop (handle, &queue_group_one);
  276. printf ("[DEBUG]: (%d) saMsgQueueGroupTrackStop { %s }\n",
  277. result, (char *)(queue_group_one.value));
  278. /*
  279. * Stop tracking GROUP_TWO
  280. */
  281. result = saMsgQueueGroupTrackStop (handle, &queue_group_two);
  282. printf ("[DEBUG]: (%d) saMsgQueueGroupTrackStop { %s }\n",
  283. result, (char *)(queue_group_two.value));
  284. /*
  285. * Close message queues
  286. */
  287. result = saMsgQueueClose (queue_handle_a);
  288. printf ("[DEBUG]: (%d) saMsgQueueClose { %llx }\n",
  289. result, ((unsigned long long) queue_handle_a));
  290. result = saMsgQueueClose (queue_handle_b);
  291. printf ("[DEBUG]: (%d) saMsgQueueClose { %llx }\n",
  292. result, ((unsigned long long) queue_handle_b));
  293. result = saMsgQueueClose (queue_handle_c);
  294. printf ("[DEBUG]: (%d) saMsgQueueClose { %llx }\n",
  295. result, ((unsigned long long) queue_handle_c));
  296. result = saMsgQueueClose (queue_handle_x);
  297. printf ("[DEBUG]: (%d) saMsgQueueClose { %llx }\n",
  298. result, ((unsigned long long) queue_handle_x));
  299. result = saMsgQueueClose (queue_handle_y);
  300. printf ("[DEBUG]: (%d) saMsgQueueClose { %llx }\n",
  301. result, ((unsigned long long) queue_handle_y));
  302. result = saMsgQueueClose (queue_handle_z);
  303. printf ("[DEBUG]: (%d) saMsgQueueClose { %llx }\n",
  304. result, ((unsigned long long) queue_handle_z));
  305. /*
  306. * Delete queue groups
  307. */
  308. result = saMsgQueueGroupDelete (handle, &queue_group_one);
  309. printf ("[DEBUG]: (%d) saMsgQueueGroupDelete { %s }\n",
  310. result, (char *)(queue_group_one.value));
  311. result = saMsgQueueGroupDelete (handle, &queue_group_two);
  312. printf ("[DEBUG]: (%d) saMsgQueueGroupDelete { %s }\n",
  313. result, (char *)(queue_group_two.value));
  314. result = saMsgFinalize (handle);
  315. if (result != SA_AIS_OK) {
  316. printf ("[ERROR]: (%d) saMsgFinalize\n", result);
  317. exit (1);
  318. }
  319. return (0);
  320. }