testclm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <sys/select.h>
  40. #include <sys/un.h>
  41. #include "ais_types.h"
  42. #include "ais_clm.h"
  43. void printSaClmNodeAddressT (SaClmNodeAddressT *nodeAddress) {
  44. int i;
  45. for (i = 0; i < nodeAddress->length; i++) {
  46. printf ("%d.", nodeAddress->value[i]);
  47. }
  48. }
  49. void printSaNameT (SaNameT *name)
  50. {
  51. int i;
  52. for (i = 0; i < name->length; i++) {
  53. printf ("%c", name->value[i]);
  54. }
  55. }
  56. void printSaClmClusterNodeT (char *description, SaClmClusterNodeT *clusterNode) {
  57. printf ("Node Information for %s\n", description);
  58. printf ("\tnode id is %x\n", (int)clusterNode->nodeId);
  59. printf ("\tnode address is ");
  60. printSaClmNodeAddressT (&clusterNode->nodeAddress);
  61. printf ("\n");
  62. printf ("\tNode name is ");
  63. printSaNameT (&clusterNode->nodeName);
  64. printf ("\n");
  65. printf ("\tCluster name is ");
  66. printSaNameT (&clusterNode->clusterName);
  67. printf ("\n");
  68. printf ("\tMember is %d\n", clusterNode->member);
  69. printf ("\tTimestamp is %llx nanoseconds\n", clusterNode->bootTimestamp);
  70. }
  71. void NodeGetCallback (
  72. SaInvocationT invocation,
  73. SaClmClusterNodeT *clusterNode,
  74. SaErrorT error)
  75. {
  76. char buf[128];
  77. if (invocation == 0x60) {
  78. sprintf (buf, "NodeGetCallback different machine invocation %x", invocation);
  79. } else {
  80. sprintf (buf, "NodeGetCallback local machine %x", invocation);
  81. }
  82. printSaClmClusterNodeT (buf, clusterNode);
  83. }
  84. void TrackCallback (
  85. SaClmClusterNotificationT *notificationBuffer,
  86. SaUint32T numberOfItems,
  87. SaUint32T numberOfMembers,
  88. SaUint64T viewNumber,
  89. SaErrorT error)
  90. {
  91. int i;
  92. printf ("Calling track callback %p\n", notificationBuffer);
  93. for (i = 0; i < numberOfItems; i++) {
  94. switch (notificationBuffer[i].clusterChanges) {
  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. }
  105. printSaClmClusterNodeT ("TRACKING", &notificationBuffer[i].clusterNode);
  106. }
  107. printf ("Done calling trackCallback\n");
  108. }
  109. SaClmCallbacksT callbacks = {
  110. NodeGetCallback,
  111. TrackCallback
  112. };
  113. SaVersionT version = { 'A', 1, 1 };
  114. int main (void) {
  115. SaClmHandleT handle;
  116. fd_set read_fds;
  117. int select_fd;
  118. int result;
  119. SaClmClusterNotificationT clusterNotificationBuffer[64];
  120. result = saClmInitialize (&handle, &callbacks, &version);
  121. if (result != SA_OK) {
  122. printf ("Could not initialize Cluster Membership API instance error %d\n", result);
  123. exit (1);
  124. }
  125. // result = saClmClusterNodeGet (0x6201a8c0, 0, &clusterNode);
  126. // printSaClmClusterNodeT ("saClmClusterNodeGet ip 192.168.1.98", &clusterNode);
  127. #ifdef COMPILE_OUT
  128. result = saClmClusterNodeGet (SA_CLM_LOCAL_NODE_ID, 0, &clusterNode);
  129. printSaClmClusterNodeT ("saClmClusterNodeGet SA_CLM_LOCAL_NODE_ID", &clusterNode);
  130. result = saClmClusterNodeGetAsync (&handle, 0x55, SA_CLM_LOCAL_NODE_ID, &clusterNode);
  131. printf ("result is %d\n", result);
  132. result = saClmClusterNodeGetAsync (&handle, 0x60, 0x6201a8c0, &clusterNode);
  133. printf ("result is %d\n", result);
  134. result = saClmClusterNodeGetAsync (&handle, 0x59, SA_CLM_LOCAL_NODE_ID, &clusterNode);
  135. printf ("result is %d\n", result);
  136. result = saClmClusterNodeGetAsync (&handle, 0x57, SA_CLM_LOCAL_NODE_ID, &clusterNode);
  137. printf ("result is %d\n", result);
  138. #endif
  139. printf ("notify buffer is %p\n", clusterNotificationBuffer);
  140. saClmClusterTrackStart (&handle, SA_TRACK_CURRENT | SA_TRACK_CHANGES_ONLY, clusterNotificationBuffer, 64);
  141. saClmSelectionObjectGet (&handle, &select_fd);
  142. printf ("select fd is %d\n", select_fd);
  143. FD_ZERO (&read_fds);
  144. FD_SET (select_fd, &read_fds);
  145. do {
  146. printf ("starting select\n");
  147. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  148. if (result == -1) {
  149. perror ("select\n");
  150. }
  151. printf ("done with select\n");
  152. saClmDispatch (&handle, SA_DISPATCH_ALL);
  153. } while (result);
  154. saClmClusterTrackStop (&handle);
  155. saClmFinalize (&handle);
  156. return (0);
  157. }