testmsg.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.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 <errno.h>
  37. #include <signal.h>
  38. #include <unistd.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/select.h>
  42. #include <sys/un.h>
  43. #include "saAis.h"
  44. #include "saMsg.h"
  45. void QueueOpenCallback (
  46. SaInvocationT invocation,
  47. SaMsgQueueHandleT queueHandle,
  48. SaAisErrorT error)
  49. {
  50. }
  51. void QueueGroupTrackCallback (
  52. const SaNameT *queueGroupName,
  53. const SaMsgQueueGroupNotificationBufferT *notificationBuffer,
  54. SaUint32T numberOfMembers,
  55. SaAisErrorT error)
  56. {
  57. }
  58. void MessageDeliveredCallback (
  59. SaInvocationT invocation,
  60. SaAisErrorT error)
  61. {
  62. }
  63. void MessageReceivedCallback (
  64. SaMsgQueueHandleT queueHandle)
  65. {
  66. }
  67. SaMsgCallbacksT callbacks = {
  68. .saMsgQueueOpenCallback = QueueOpenCallback,
  69. .saMsgQueueGroupTrackCallback = QueueGroupTrackCallback,
  70. .saMsgMessageDeliveredCallback = MessageDeliveredCallback,
  71. .saMsgMessageReceivedCallback = MessageReceivedCallback
  72. };
  73. SaVersionT version = { 'B', 1, 1 };
  74. SaMsgQueueCreationAttributesT creation_attributes = {
  75. SA_MSG_QUEUE_PERSISTENT,
  76. {128000, 128000, 128000},
  77. SA_TIME_END
  78. };
  79. void setSaNameT (SaNameT *name, char *str) {
  80. name->length = strlen (str);
  81. memcpy (name->value, str, name->length);
  82. }
  83. void sigintr_handler (int signum) {
  84. exit (0);
  85. }
  86. int main (void) {
  87. SaMsgHandleT handle;
  88. SaMsgQueueHandleT queue_handle;
  89. fd_set read_fds;
  90. SaSelectionObjectT select_fd;
  91. int result;
  92. SaNameT queue_name;
  93. SaNameT queue_group_name;
  94. signal (SIGINT, sigintr_handler);
  95. result = saMsgInitialize (&handle, &callbacks, &version);
  96. if (result != SA_OK) {
  97. printf ("Could not initialize Cluster Membership API instance error %d\n", result);
  98. exit (1);
  99. }
  100. saMsgSelectionObjectGet (handle, &select_fd);
  101. setSaNameT (&queue_name, "queue");
  102. result = saMsgQueueOpen (handle,
  103. &queue_name,
  104. &creation_attributes,
  105. SA_MSG_QUEUE_CREATE,
  106. SA_TIME_END,
  107. &queue_handle);
  108. printf ("saMsgQueueOpen result is %d (should be 1)\n", result);
  109. setSaNameT (&queue_group_name, "queue_group");
  110. result = saMsgQueueGroupCreate (
  111. handle,
  112. &queue_group_name,
  113. SA_MSG_QUEUE_GROUP_ROUND_ROBIN);
  114. printf ("saMsgQueueGroupCreate result is %d (should be 1)\n", result);
  115. result = saMsgQueueGroupCreate (
  116. handle,
  117. &queue_group_name,
  118. SA_MSG_QUEUE_GROUP_ROUND_ROBIN);
  119. printf ("saMsgQueueGroupCreate result is %d (should be 14)\n", result);
  120. result = saMsgQueueGroupInsert (
  121. handle,
  122. &queue_group_name,
  123. &queue_name);
  124. printf ("saMsgQueueGroupInsert result is %d (should be 1)\n", result);
  125. FD_ZERO (&read_fds);
  126. do {
  127. FD_SET (select_fd, &read_fds);
  128. FD_SET (STDIN_FILENO, &read_fds);
  129. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  130. if (result == -1) {
  131. perror ("select\n");
  132. }
  133. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  134. break;
  135. }
  136. saMsgDispatch (handle, SA_DISPATCH_ALL);
  137. } while (result);
  138. result = saMsgQueueGroupRemove (
  139. handle,
  140. &queue_group_name,
  141. &queue_name);
  142. printf ("saMsgQueueGroupRemove result is %d (should be 1)\n", result);
  143. result = saMsgQueueGroupDelete (handle,
  144. &queue_group_name);
  145. printf ("saMsgQueueGroupDelete result is %d (should be 1)\n", result);
  146. result = saMsgQueueClose (queue_handle);
  147. printf ("saMsgQueueClose result is %d (should be 1)\n", result);
  148. result = saMsgFinalize (handle);
  149. printf ("Finalize result is %d (should be 1)\n", result);
  150. return (0);
  151. }