testmsg.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@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. SaMsgQueueHandleT async_handle;
  47. void QueueOpenCallback (
  48. SaInvocationT invocation,
  49. SaMsgQueueHandleT queueHandle,
  50. SaAisErrorT error)
  51. {
  52. /* DEBUG */
  53. printf ("[DEBUG]: testmsg (QueueOpenCallback)\n");
  54. printf ("[DEBUG]: \t { queueHandle = %llx }\n",
  55. (unsigned long long) queueHandle);
  56. async_handle = queueHandle;
  57. }
  58. void QueueGroupTrackCallback (
  59. const SaNameT *queueGroupName,
  60. const SaMsgQueueGroupNotificationBufferT *notificationBuffer,
  61. SaUint32T numberOfMembers,
  62. SaAisErrorT error)
  63. {
  64. /* DEBUG */
  65. printf ("[DEBUG]: testmsg (QueueGroupTrackCallback)\n");
  66. }
  67. void MessageDeliveredCallback (
  68. SaInvocationT invocation,
  69. SaAisErrorT error)
  70. {
  71. /* DEBUG */
  72. printf ("[DEBUG]: testmsg (MessageDeliveredCallback)\n");
  73. printf ("[DEBUG]: \t { invocation = %llx }\n",
  74. (unsigned long long) invocation);
  75. }
  76. void MessageReceivedCallback (
  77. SaMsgQueueHandleT queueHandle)
  78. {
  79. /* DEBUG */
  80. printf ("[DEBUG]: testmsg (MessageReceivedCallback)\n");
  81. }
  82. SaMsgCallbacksT callbacks = {
  83. .saMsgQueueOpenCallback = QueueOpenCallback,
  84. .saMsgQueueGroupTrackCallback = QueueGroupTrackCallback,
  85. .saMsgMessageDeliveredCallback = MessageDeliveredCallback,
  86. .saMsgMessageReceivedCallback = MessageReceivedCallback
  87. };
  88. SaVersionT version = { 'B', 1, 1 };
  89. SaMsgQueueCreationAttributesT creation_attributes = {
  90. SA_MSG_QUEUE_PERSISTENT,
  91. { 128000, 128000, 128000 },
  92. SA_TIME_END
  93. };
  94. void setSaNameT (SaNameT *name, char *str) {
  95. name->length = strlen (str);
  96. strcpy (name->value, str);
  97. }
  98. void setSaMsgMessageT (SaMsgMessageT *message, char *data) {
  99. message->type = 1;
  100. message->version = 2;
  101. message->size = strlen (data) + 1;
  102. message->senderName = NULL;
  103. message->data = strdup (data);
  104. message->priority = 0;
  105. }
  106. void sigintr_handler (int signum) {
  107. exit (0);
  108. }
  109. int main (void) {
  110. SaMsgHandleT handle;
  111. SaMsgMessageT message;
  112. SaMsgQueueHandleT queue_handle;
  113. SaSelectionObjectT select_fd;
  114. SaInvocationT invocation = 3;
  115. fd_set read_fds;
  116. int result;
  117. SaNameT async_name;
  118. SaNameT queue_name;
  119. SaNameT queue_group_name;
  120. SaTimeT time;
  121. SaMsgSenderIdT id;
  122. SaMsgMessageT msg_a;
  123. SaMsgMessageT msg_b;
  124. SaMsgMessageT msg_c;
  125. memset (&msg_a, 0, sizeof (SaMsgMessageT));
  126. memset (&msg_b, 0, sizeof (SaMsgMessageT));
  127. memset (&msg_c, 0, sizeof (SaMsgMessageT));
  128. signal (SIGINT, sigintr_handler);
  129. result = saMsgInitialize (&handle, &callbacks, &version);
  130. if (result != SA_AIS_OK) {
  131. printf ("Could not initialize Cluster Membership API instance error %d\n", result);
  132. exit (1);
  133. }
  134. saMsgSelectionObjectGet (handle, &select_fd);
  135. setSaNameT (&async_name, "async");
  136. setSaNameT (&queue_name, "queue");
  137. result = saMsgQueueOpen (handle,
  138. &queue_name,
  139. &creation_attributes,
  140. SA_MSG_QUEUE_CREATE,
  141. SA_TIME_END,
  142. &queue_handle);
  143. printf ("saMsgQueueOpen result is %d (should be 1)\n", result);
  144. printf ("saMsgQueueOpen { queue_handle = %llx }\n", queue_handle);
  145. result = saMsgQueueOpenAsync (handle,
  146. invocation,
  147. &async_name,
  148. &creation_attributes,
  149. SA_MSG_QUEUE_CREATE);
  150. printf ("saMsgQueueOpenAsync result is %d (should be 1)\n", result);
  151. printf ("saMsgQueueOpen { async_handle = %llx }\n", async_handle);
  152. setSaNameT (&queue_group_name, "queue_group");
  153. result = saMsgQueueGroupCreate (
  154. handle,
  155. &queue_group_name,
  156. SA_MSG_QUEUE_GROUP_ROUND_ROBIN);
  157. printf ("saMsgQueueGroupCreate result is %d (should be 1)\n", result);
  158. result = saMsgQueueGroupCreate (
  159. handle,
  160. &queue_group_name,
  161. SA_MSG_QUEUE_GROUP_ROUND_ROBIN);
  162. printf ("saMsgQueueGroupCreate result is %d (should be 14)\n", result);
  163. result = saMsgQueueGroupInsert (
  164. handle,
  165. &queue_group_name,
  166. &queue_name);
  167. printf ("saMsgQueueGroupInsert result is %d (should be 1)\n", result);
  168. saMsgDispatch (handle, SA_DISPATCH_ALL);
  169. /*
  170. FD_ZERO (&read_fds);
  171. do {
  172. FD_SET (select_fd, &read_fds);
  173. FD_SET (STDIN_FILENO, &read_fds);
  174. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  175. if (result == -1) {
  176. perror ("select\n");
  177. }
  178. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  179. break;
  180. }
  181. saMsgDispatch (handle, SA_DISPATCH_ALL);
  182. } while (result);
  183. */
  184. setSaMsgMessageT (&message, "test_msg_01");
  185. result = saMsgMessageSend (handle, &queue_name, &message, SA_TIME_ONE_SECOND);
  186. printf ("saMsgMessageSend [1] result is %d (should be 1)\n", result);
  187. setSaMsgMessageT (&message, "test_msg_02");
  188. result = saMsgMessageSend (handle, &queue_name, &message, SA_TIME_ONE_SECOND);
  189. printf ("saMsgMessageSend [2] result is %d (should be 1)\n", result);
  190. setSaMsgMessageT (&message, "test_msg_03");
  191. result = saMsgMessageSend (handle, &queue_name, &message, SA_TIME_ONE_SECOND);
  192. printf ("saMsgMessageSend [3] result is %d (should be 1)\n", result);
  193. setSaMsgMessageT (&message, "test_msg_04");
  194. result = saMsgMessageSendAsync (handle, invocation, &queue_name, &message,
  195. SA_MSG_MESSAGE_DELIVERED_ACK);
  196. printf ("saMsgMessageSendAsync [4] result is %d (should be 1)\n", result);
  197. setSaMsgMessageT (&message, "test_msg_05");
  198. result = saMsgMessageSendAsync (handle, invocation, &queue_name, &message,
  199. SA_MSG_MESSAGE_DELIVERED_ACK);
  200. printf ("saMsgMessageSendAsync [5] result is %d (should be 1)\n", result);
  201. saMsgDispatch (handle, SA_DISPATCH_ALL);
  202. result = saMsgMessageGet (queue_handle, &msg_a, &time, &id, SA_TIME_ONE_MINUTE);
  203. printf ("saMsgMessageGet [a] result is %d (should be 1)\n", result);
  204. result = saMsgMessageGet (queue_handle, &msg_b, &time, &id, SA_TIME_ONE_MINUTE);
  205. printf ("saMsgMessageGet [b] result is %d (should be 1)\n", result);
  206. result = saMsgMessageGet (queue_handle, &msg_c, &time, &id, SA_TIME_ONE_MINUTE);
  207. printf ("saMsgMessageGet [c] result is %d (should be 1)\n", result);
  208. printf ("saMsgMessageGet { (a) data = %s }\n", (char *)(msg_a.data));
  209. printf ("saMsgMessageGet { (b) data = %s }\n", (char *)(msg_b.data));
  210. printf ("saMsgMessageGet { (c) data = %s }\n", (char *)(msg_c.data));
  211. result = saMsgQueueGroupRemove (handle, &queue_group_name, &queue_name);
  212. printf ("saMsgQueueGroupRemove result is %d (should be 1)\n", result);
  213. result = saMsgQueueGroupDelete (handle, &queue_group_name);
  214. printf ("saMsgQueueGroupDelete result is %d (should be 1)\n", result);
  215. printf ("saMsgQueueClose { queue_handle = %llx }\n", queue_handle);
  216. result = saMsgQueueClose (queue_handle);
  217. printf ("saMsgQueueClose result is %d (should be 1)\n", result);
  218. printf ("saMsgQueueClose { async_handle = %llx }\n", async_handle);
  219. result = saMsgQueueClose (async_handle);
  220. printf ("saMsgQueueClose result is %d (should be 1)\n", result);
  221. result = saMsgFinalize (handle);
  222. printf ("Finalize result is %d (should be 1)\n", result);
  223. return (0);
  224. }