testclm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2002-2003 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Sun Microsystems, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include <signal.h>
  40. #include <unistd.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/select.h>
  44. #include <sys/un.h>
  45. #include "saAis.h"
  46. #include "saClm.h"
  47. void printSaClmNodeAddressT (SaClmNodeAddressT *nodeAddress) {
  48. int i;
  49. printf ("family=%d - address=", nodeAddress->family);
  50. for (i = 0; i < nodeAddress->length; i++) {
  51. printf ("%c", nodeAddress->value[i]);
  52. }
  53. }
  54. void printSaNameT (SaNameT *name)
  55. {
  56. int i;
  57. for (i = 0; i < name->length; i++) {
  58. printf ("%c", name->value[i]);
  59. }
  60. }
  61. void printSaClmClusterNodeT (char *description, SaClmClusterNodeT *clusterNode) {
  62. printf ("Node Information for %s\n", description);
  63. printf ("\tnode id is %x\n", (int)clusterNode->nodeId);
  64. printf ("\tnode address is ");
  65. printSaClmNodeAddressT (&clusterNode->nodeAddress);
  66. printf ("\n");
  67. printf ("\tNode name is ");
  68. printSaNameT (&clusterNode->nodeName);
  69. printf ("\n");
  70. printf ("\tMember is %d\n", clusterNode->member);
  71. printf ("\tTimestamp is %llx nanoseconds\n", (unsigned long long)clusterNode->bootTimestamp);
  72. }
  73. void NodeGetCallback (
  74. SaInvocationT invocation,
  75. const SaClmClusterNodeT *clusterNode,
  76. SaAisErrorT error)
  77. {
  78. char buf[128];
  79. if (error != SA_AIS_OK) {
  80. printf ("Node for invocation %llu not found (%d)\n",
  81. (unsigned long long)invocation, error);
  82. } else {
  83. sprintf (buf, "NODEGETCALLBACK %llu\n", (unsigned long long)invocation);
  84. printSaClmClusterNodeT (buf, (SaClmClusterNodeT *)clusterNode);
  85. }
  86. }
  87. void TrackCallback (
  88. const SaClmClusterNotificationBufferT *notificationBuffer,
  89. SaUint32T numberOfMembers,
  90. SaAisErrorT error)
  91. {
  92. int i;
  93. printf ("Track callback\n");
  94. printf ("Calling track callback %p\n", notificationBuffer);
  95. for (i = 0; i < numberOfMembers; i++) {
  96. switch (notificationBuffer->notification[i].clusterChange) {
  97. case SA_CLM_NODE_NO_CHANGE:
  98. printf ("NODE STATE NO CHANGE.\n");
  99. break;
  100. case SA_CLM_NODE_JOINED:
  101. printf ("NODE STATE JOINED.\n");
  102. break;
  103. case SA_CLM_NODE_LEFT:
  104. printf ("NODE STATE LEFT.\n");
  105. break;
  106. case SA_CLM_NODE_RECONFIGURED:
  107. printf ("NODE STATE RECONFIGURED.\n");
  108. break;
  109. }
  110. printSaClmClusterNodeT ("TRACKING",
  111. &notificationBuffer->notification[i].clusterNode);
  112. }
  113. printf ("Done calling trackCallback\n");
  114. }
  115. SaClmCallbacksT callbacks = {
  116. .saClmClusterNodeGetCallback = NodeGetCallback,
  117. .saClmClusterTrackCallback = TrackCallback
  118. };
  119. SaVersionT version = { 'B', 1, 1 };
  120. void sigintr_handler (int signum) {
  121. exit (0);
  122. }
  123. int main (void) {
  124. SaClmHandleT handle;
  125. fd_set read_fds;
  126. SaSelectionObjectT select_fd;
  127. int result;
  128. SaClmClusterNotificationT clusterNotification[64];
  129. SaClmClusterNotificationBufferT clusterNotificationBuffer;
  130. SaClmClusterNodeT clusterNode;
  131. int i;
  132. clusterNotificationBuffer.notification = clusterNotification;
  133. clusterNotificationBuffer.numberOfItems = 64;
  134. signal (SIGINT, sigintr_handler);
  135. result = saClmInitialize (&handle, &callbacks, &version);
  136. if (result != SA_AIS_OK) {
  137. printf ("Could not initialize Cluster Membership API instance error %d\n", result);
  138. exit (1);
  139. }
  140. result = saClmClusterNodeGet (handle, SA_CLM_LOCAL_NODE_ID, SA_TIME_END, &clusterNode);
  141. printf ("Result of saClmClusterNodeGet %d\n", result);
  142. printSaClmClusterNodeT ("saClmClusterNodeGet SA_CLM_LOCAL_NODE_ID result %d", &clusterNode);
  143. result = saClmClusterNodeGetAsync (handle, 55, SA_CLM_LOCAL_NODE_ID);
  144. printf ("result is %d\n", result);
  145. result = saClmClusterNodeGetAsync (handle, 60, 0x6201a8c0);
  146. printf ("result is %d\n", result);
  147. result = saClmClusterNodeGetAsync (handle, 61, 0x6a01a8f0);
  148. printf ("result is %d\n", result);
  149. result = saClmClusterNodeGetAsync (handle, 59, SA_CLM_LOCAL_NODE_ID);
  150. printf ("result is %d\n", result);
  151. result = saClmClusterNodeGetAsync (handle, 57, SA_CLM_LOCAL_NODE_ID);
  152. printf ("result is %d\n", result);
  153. result = saClmClusterTrack (handle, SA_TRACK_CURRENT | SA_TRACK_CHANGES,
  154. &clusterNotificationBuffer);
  155. printf ("track result is %d\n", result);
  156. for (i = 0; i < clusterNotificationBuffer.numberOfItems; i++) {
  157. printSaClmClusterNodeT ("Results from SA_TRACK_CURRENT:",
  158. &clusterNotificationBuffer.notification[i].clusterNode);
  159. }
  160. saClmSelectionObjectGet (handle, &select_fd);
  161. printf ("select fd is %llu\n", (unsigned long long)select_fd);
  162. FD_ZERO (&read_fds);
  163. printf ("press the enter key to exit with track stop and finalize.\n");
  164. do {
  165. FD_SET (select_fd, &read_fds);
  166. FD_SET (STDIN_FILENO, &read_fds);
  167. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  168. if (result == -1) {
  169. perror ("select\n");
  170. }
  171. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  172. break;
  173. }
  174. printf ("done with select\n");
  175. saClmDispatch (handle, SA_DISPATCH_ALL);
  176. } while (result);
  177. result = saClmClusterTrackStop (handle);
  178. printf ("TrackStop result is %d (should be 1)\n", result);
  179. result = saClmFinalize (handle);
  180. printf ("Finalize result is %d (should be 1)\n", result);
  181. return (0);
  182. }