testmsg.c 4.9 KB

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