testclm.c 6.2 KB

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