testclm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2002-2003 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 "saClm.h"
  45. void printSaClmNodeAddressT (SaClmNodeAddressT *nodeAddress) {
  46. int i;
  47. printf ("family=%d - address=", nodeAddress->family);
  48. for (i = 0; i < nodeAddress->length; i++) {
  49. printf ("%c", nodeAddress->value[i]);
  50. }
  51. }
  52. void printSaNameT (SaNameT *name)
  53. {
  54. int i;
  55. for (i = 0; i < name->length; i++) {
  56. printf ("%c", name->value[i]);
  57. }
  58. }
  59. void printSaClmClusterNodeT (char *description, SaClmClusterNodeT *clusterNode) {
  60. printf ("Node Information for %s\n", description);
  61. printf ("\tnode id is %x\n", (int)clusterNode->nodeId);
  62. printf ("\tnode address is ");
  63. printSaClmNodeAddressT (&clusterNode->nodeAddress);
  64. printf ("\n");
  65. printf ("\tNode name is ");
  66. printSaNameT (&clusterNode->nodeName);
  67. printf ("\n");
  68. printf ("\tMember is %d\n", clusterNode->member);
  69. printf ("\tTimestamp is %llx nanoseconds\n", (unsigned long long)clusterNode->bootTimestamp);
  70. }
  71. void NodeGetCallback (
  72. SaInvocationT invocation,
  73. const SaClmClusterNodeT *clusterNode,
  74. SaAisErrorT error)
  75. {
  76. char buf[128];
  77. if (error != SA_AIS_OK) {
  78. printf ("Node for invocation %lld not found (%d)\n", invocation, error);
  79. } else {
  80. sprintf (buf, "NODEGETCALLBACK %lld\n", invocation);
  81. printSaClmClusterNodeT (buf, (SaClmClusterNodeT *)clusterNode);
  82. }
  83. }
  84. void TrackCallback (
  85. const SaClmClusterNotificationBufferT *notificationBuffer,
  86. SaUint32T numberOfMembers,
  87. SaAisErrorT error)
  88. {
  89. int i;
  90. printf ("Track callback\n");
  91. printf ("Calling track callback %p\n", notificationBuffer);
  92. for (i = 0; i < numberOfMembers; i++) {
  93. switch (notificationBuffer->notification[i].clusterChange) {
  94. case SA_CLM_NODE_NO_CHANGE:
  95. printf ("NODE STATE NO CHANGE.\n");
  96. break;
  97. case SA_CLM_NODE_JOINED:
  98. printf ("NODE STATE JOINED.\n");
  99. break;
  100. case SA_CLM_NODE_LEFT:
  101. printf ("NODE STATE LEFT.\n");
  102. break;
  103. case SA_CLM_NODE_RECONFIGURED:
  104. printf ("NODE STATE RECONFIGURED.\n");
  105. break;
  106. }
  107. printSaClmClusterNodeT ("TRACKING",
  108. &notificationBuffer->notification[i].clusterNode);
  109. }
  110. printf ("Done calling trackCallback\n");
  111. }
  112. SaClmCallbacksT callbacks = {
  113. .saClmClusterNodeGetCallback = NodeGetCallback,
  114. .saClmClusterTrackCallback = TrackCallback
  115. };
  116. SaVersionT version = { 'B', 1, 1 };
  117. void sigintr_handler (int signum) {
  118. exit (0);
  119. }
  120. int main (void) {
  121. SaClmHandleT handle;
  122. fd_set read_fds;
  123. SaSelectionObjectT select_fd;
  124. int result;
  125. SaClmClusterNotificationT clusterNotification[64];
  126. SaClmClusterNotificationBufferT clusterNotificationBuffer;
  127. SaClmClusterNodeT clusterNode;
  128. int i;
  129. clusterNotificationBuffer.notification = clusterNotification;
  130. clusterNotificationBuffer.numberOfItems = 64;
  131. signal (SIGINT, sigintr_handler);
  132. result = saClmInitialize (&handle, &callbacks, &version);
  133. if (result != SA_OK) {
  134. printf ("Could not initialize Cluster Membership API instance error %d\n", result);
  135. exit (1);
  136. }
  137. result = saClmClusterNodeGet (handle, SA_CLM_LOCAL_NODE_ID, SA_TIME_END, &clusterNode);
  138. printf ("Result of saClmClusterNodeGet %d\n", result);
  139. printSaClmClusterNodeT ("saClmClusterNodeGet SA_CLM_LOCAL_NODE_ID result %d", &clusterNode);
  140. result = saClmClusterNodeGetAsync (handle, 55, SA_CLM_LOCAL_NODE_ID);
  141. printf ("result is %d\n", result);
  142. result = saClmClusterNodeGetAsync (handle, 60, 0x6201a8c0);
  143. printf ("result is %d\n", result);
  144. result = saClmClusterNodeGetAsync (handle, 61, 0x6a01a8f0);
  145. printf ("result is %d\n", result);
  146. result = saClmClusterNodeGetAsync (handle, 59, SA_CLM_LOCAL_NODE_ID);
  147. printf ("result is %d\n", result);
  148. result = saClmClusterNodeGetAsync (handle, 57, SA_CLM_LOCAL_NODE_ID);
  149. printf ("result is %d\n", result);
  150. result = saClmClusterTrack (handle, SA_TRACK_CURRENT | SA_TRACK_CHANGES,
  151. &clusterNotificationBuffer);
  152. printf ("track result is %d\n", result);
  153. for (i = 0; i < clusterNotificationBuffer.numberOfItems; i++) {
  154. printSaClmClusterNodeT ("Results from SA_TRACK_CURRENT:",
  155. &clusterNotificationBuffer.notification[i].clusterNode);
  156. }
  157. saClmSelectionObjectGet (handle, &select_fd);
  158. printf ("select fd is %lld\n", select_fd);
  159. FD_ZERO (&read_fds);
  160. printf ("press the enter key to exit with track stop and finalize.\n");
  161. do {
  162. FD_SET (select_fd, &read_fds);
  163. FD_SET (STDIN_FILENO, &read_fds);
  164. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  165. if (result == -1) {
  166. perror ("select\n");
  167. }
  168. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  169. break;
  170. }
  171. printf ("done with select\n");
  172. saClmDispatch (handle, SA_DISPATCH_ALL);
  173. } while (result);
  174. result = saClmClusterTrackStop (handle);
  175. printf ("TrackStop result is %d (should be 1)\n", result);
  176. result = saClmFinalize (handle);
  177. printf ("Finalize result is %d (should be 1)\n", result);
  178. return (0);
  179. }