testclm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "ais_clm.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 ("\tCluster name is ");
  68. printSaNameT (&clusterNode->clusterName);
  69. printf ("\n");
  70. printf ("\tMember is %d\n", clusterNode->member);
  71. printf ("\tTimestamp is %llx nanoseconds\n", clusterNode->bootTimestamp);
  72. }
  73. void NodeGetCallback (
  74. SaInvocationT invocation,
  75. SaClmClusterNodeT *clusterNode,
  76. SaErrorT error)
  77. {
  78. char buf[128];
  79. // if (invocation == 0x60) {
  80. sprintf (buf, "NodeGetCallback different machine invocation %x", invocation);
  81. // } else {
  82. sprintf (buf, "NodeGetCallback local machine %x", invocation);
  83. // }
  84. printSaClmClusterNodeT (buf, clusterNode);
  85. }
  86. void TrackCallback (
  87. SaClmClusterNotificationT *notificationBuffer,
  88. SaUint32T numberOfItems,
  89. SaUint32T numberOfMembers,
  90. SaUint64T viewNumber,
  91. SaErrorT error)
  92. {
  93. int i;
  94. printf ("Calling track callback %p\n", notificationBuffer);
  95. for (i = 0; i < numberOfItems; i++) {
  96. switch (notificationBuffer[i].clusterChanges) {
  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. }
  107. printSaClmClusterNodeT ("TRACKING", &notificationBuffer[i].clusterNode);
  108. }
  109. printf ("Done calling trackCallback\n");
  110. }
  111. SaClmCallbacksT callbacks = {
  112. NodeGetCallback,
  113. TrackCallback
  114. };
  115. SaVersionT version = { 'A', 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. int select_fd;
  123. int result;
  124. SaClmClusterNotificationT clusterNotificationBuffer[64];
  125. SaClmClusterNodeT clusterNode;
  126. signal (SIGINT, sigintr_handler);
  127. result = saClmInitialize (&handle, &callbacks, &version);
  128. if (result != SA_OK) {
  129. printf ("Could not initialize Cluster Membership API instance error %d\n", result);
  130. exit (1);
  131. }
  132. result = saClmClusterNodeGet (SA_CLM_LOCAL_NODE_ID, 0, &clusterNode);
  133. printf ("Result of saClmClusterNodeGet %d\n", result);
  134. printSaClmClusterNodeT ("saClmClusterNodeGet SA_CLM_LOCAL_NODE_ID result %d", &clusterNode);
  135. result = saClmClusterNodeGetAsync (&handle, 0x55, SA_CLM_LOCAL_NODE_ID, &clusterNode);
  136. printf ("result is %d\n", result);
  137. result = saClmClusterNodeGetAsync (&handle, 0x60, 0x6201a8c0, &clusterNode);
  138. printf ("result is %d\n", result);
  139. result = saClmClusterNodeGetAsync (&handle, 0x59, SA_CLM_LOCAL_NODE_ID, &clusterNode);
  140. printf ("result is %d\n", result);
  141. result = saClmClusterNodeGetAsync (&handle, 0x57, SA_CLM_LOCAL_NODE_ID, &clusterNode);
  142. printf ("result is %d\n", result);
  143. saClmClusterTrackStart (&handle, SA_TRACK_CURRENT | SA_TRACK_CHANGES_ONLY, clusterNotificationBuffer, 64);
  144. saClmSelectionObjectGet (&handle, &select_fd);
  145. printf ("select fd is %d\n", select_fd);
  146. FD_ZERO (&read_fds);
  147. printf ("press the enter key to exit with track stop and finalize.\n");
  148. do {
  149. FD_SET (select_fd, &read_fds);
  150. FD_SET (STDIN_FILENO, &read_fds);
  151. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  152. if (result == -1) {
  153. perror ("select\n");
  154. }
  155. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  156. break;
  157. }
  158. printf ("done with select\n");
  159. saClmDispatch (&handle, SA_DISPATCH_ALL);
  160. } while (result);
  161. result = saClmClusterTrackStop (&handle);
  162. printf ("TrackStop result is %d (should be 1)\n", result);
  163. result = saClmFinalize (&handle);
  164. printf ("Finalize result is %d (should be 1)\n", result);
  165. return (0);
  166. }