testclm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 %llu not found (%d)\n",
  79. (unsigned long long)invocation, error);
  80. } else {
  81. sprintf (buf, "NODEGETCALLBACK %llu\n", (unsigned long long)invocation);
  82. printSaClmClusterNodeT (buf, (SaClmClusterNodeT *)clusterNode);
  83. }
  84. }
  85. void TrackCallback (
  86. const SaClmClusterNotificationBufferT *notificationBuffer,
  87. SaUint32T numberOfMembers,
  88. SaAisErrorT error)
  89. {
  90. int i;
  91. printf ("Track callback\n");
  92. printf ("Calling track callback %p\n", notificationBuffer);
  93. for (i = 0; i < numberOfMembers; i++) {
  94. switch (notificationBuffer->notification[i].clusterChange) {
  95. case SA_CLM_NODE_NO_CHANGE:
  96. printf ("NODE STATE NO CHANGE.\n");
  97. break;
  98. case SA_CLM_NODE_JOINED:
  99. printf ("NODE STATE JOINED.\n");
  100. break;
  101. case SA_CLM_NODE_LEFT:
  102. printf ("NODE STATE LEFT.\n");
  103. break;
  104. case SA_CLM_NODE_RECONFIGURED:
  105. printf ("NODE STATE RECONFIGURED.\n");
  106. break;
  107. }
  108. printSaClmClusterNodeT ("TRACKING",
  109. &notificationBuffer->notification[i].clusterNode);
  110. }
  111. printf ("Done calling trackCallback\n");
  112. }
  113. SaClmCallbacksT callbacks = {
  114. .saClmClusterNodeGetCallback = NodeGetCallback,
  115. .saClmClusterTrackCallback = TrackCallback
  116. };
  117. SaVersionT version = { 'B', 1, 1 };
  118. void sigintr_handler (int signum) {
  119. exit (0);
  120. }
  121. int main (void) {
  122. SaClmHandleT handle;
  123. fd_set read_fds;
  124. SaSelectionObjectT select_fd;
  125. int result;
  126. SaClmClusterNotificationT clusterNotification[64];
  127. SaClmClusterNotificationBufferT clusterNotificationBuffer;
  128. SaClmClusterNodeT clusterNode;
  129. int i;
  130. clusterNotificationBuffer.notification = clusterNotification;
  131. clusterNotificationBuffer.numberOfItems = 64;
  132. signal (SIGINT, sigintr_handler);
  133. result = saClmInitialize (&handle, &callbacks, &version);
  134. if (result != SA_AIS_OK) {
  135. printf ("Could not initialize Cluster Membership API instance error %d\n", result);
  136. exit (1);
  137. }
  138. result = saClmClusterNodeGet (handle, SA_CLM_LOCAL_NODE_ID, SA_TIME_END, &clusterNode);
  139. printf ("Result of saClmClusterNodeGet %d\n", result);
  140. printSaClmClusterNodeT ("saClmClusterNodeGet SA_CLM_LOCAL_NODE_ID result %d", &clusterNode);
  141. result = saClmClusterNodeGetAsync (handle, 55, SA_CLM_LOCAL_NODE_ID);
  142. printf ("result is %d\n", result);
  143. result = saClmClusterNodeGetAsync (handle, 60, 0x6201a8c0);
  144. printf ("result is %d\n", result);
  145. result = saClmClusterNodeGetAsync (handle, 61, 0x6a01a8f0);
  146. printf ("result is %d\n", result);
  147. result = saClmClusterNodeGetAsync (handle, 59, SA_CLM_LOCAL_NODE_ID);
  148. printf ("result is %d\n", result);
  149. result = saClmClusterNodeGetAsync (handle, 57, SA_CLM_LOCAL_NODE_ID);
  150. printf ("result is %d\n", result);
  151. result = saClmClusterTrack (handle, SA_TRACK_CURRENT | SA_TRACK_CHANGES,
  152. &clusterNotificationBuffer);
  153. printf ("track result is %d\n", result);
  154. for (i = 0; i < clusterNotificationBuffer.numberOfItems; i++) {
  155. printSaClmClusterNodeT ("Results from SA_TRACK_CURRENT:",
  156. &clusterNotificationBuffer.notification[i].clusterNode);
  157. }
  158. saClmSelectionObjectGet (handle, &select_fd);
  159. printf ("select fd is %llu\n", (unsigned long long)select_fd);
  160. FD_ZERO (&read_fds);
  161. printf ("press the enter key to exit with track stop and finalize.\n");
  162. do {
  163. FD_SET (select_fd, &read_fds);
  164. FD_SET (STDIN_FILENO, &read_fds);
  165. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  166. if (result == -1) {
  167. perror ("select\n");
  168. }
  169. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  170. break;
  171. }
  172. printf ("done with select\n");
  173. saClmDispatch (handle, SA_DISPATCH_ALL);
  174. } while (result);
  175. result = saClmClusterTrackStop (handle);
  176. printf ("TrackStop result is %d (should be 1)\n", result);
  177. result = saClmFinalize (handle);
  178. printf ("Finalize result is %d (should be 1)\n", result);
  179. return (0);
  180. }