testcpg.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006 Red Hat Inc
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Patrick Caulfield <pcaulfie@redhat.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 "cpg.h"
  45. void print_cpgname (struct cpg_name *name)
  46. {
  47. int i;
  48. for (i = 0; i < name->length; i++) {
  49. printf ("%c", name->value[i]);
  50. }
  51. }
  52. void DeliverCallback (
  53. cpg_handle_t handle,
  54. struct cpg_name *groupName,
  55. uint32_t nodeid,
  56. uint32_t pid,
  57. void *msg,
  58. int msg_len)
  59. {
  60. printf("DeliverCallback: message (len=%d)from node/pid %d/%d: '%s'\n", msg_len, nodeid, pid, (char *)msg);
  61. }
  62. void ConfchgCallback (
  63. cpg_handle_t handle,
  64. struct cpg_name *groupName,
  65. struct cpg_address *member_list, int member_list_entries,
  66. struct cpg_address *left_list, int left_list_entries,
  67. struct cpg_address *joined_list, int joined_list_entries)
  68. {
  69. int i;
  70. printf("\nConfchgCallback: group '"); print_cpgname(groupName); printf("'\n");
  71. for (i=0; i<joined_list_entries; i++)
  72. printf("joined node/pid: %d/%d reason: %d\n", joined_list[i].nodeId, joined_list[i].pid, joined_list[i].reason);
  73. for (i=0; i<left_list_entries; i++)
  74. printf("left node/pid: %d/%d reason: %d\n", left_list[i].nodeId, left_list[i].pid, left_list[i].reason);
  75. printf("nodes in group now %d\n", member_list_entries);
  76. for (i=0; i<member_list_entries; i++) {
  77. printf("node/pid: %d/%d\n", member_list[i].nodeId, member_list[i].pid);
  78. }
  79. /* Is it us??
  80. NOTE: in reality we should also check the nodeid */
  81. if (left_list_entries && left_list[0].pid == getpid()) {
  82. printf("We have left the building\n");
  83. exit(0);
  84. }
  85. }
  86. cpg_callbacks_t callbacks = {
  87. .cpg_deliver_fn = DeliverCallback,
  88. .cpg_confchg_fn = ConfchgCallback,
  89. };
  90. void sigintr_handler (int signum) {
  91. exit (0);
  92. }
  93. static struct cpg_name group_name;
  94. int main (int argc, char *argv[]) {
  95. cpg_handle_t handle;
  96. fd_set read_fds;
  97. int select_fd;
  98. int result;
  99. if (argc > 1) {
  100. strcpy(group_name.value, argv[1]);
  101. group_name.length = strlen(argv[1])+1;
  102. }
  103. else {
  104. strcpy(group_name.value, "GROUP");
  105. group_name.length = 6;
  106. }
  107. result = cpg_initialize (&handle, &callbacks);
  108. if (result != SA_AIS_OK) {
  109. printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
  110. exit (1);
  111. }
  112. result = cpg_join(handle, &group_name);
  113. if (result != SA_AIS_OK) {
  114. printf ("Could not join process group, error %d\n", result);
  115. exit (1);
  116. }
  117. FD_ZERO (&read_fds);
  118. cpg_fd_get(handle, &select_fd);
  119. printf ("Type EXIT to finish\n");
  120. do {
  121. FD_SET (select_fd, &read_fds);
  122. FD_SET (STDIN_FILENO, &read_fds);
  123. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  124. if (result == -1) {
  125. perror ("select\n");
  126. }
  127. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  128. char inbuf[132];
  129. struct iovec iov;
  130. gets(inbuf);
  131. if (strncmp(inbuf, "EXIT", 4) == 0) {
  132. cpg_leave(handle, &group_name);
  133. }
  134. else {
  135. iov.iov_base = inbuf;
  136. iov.iov_len = strlen(inbuf)+1;
  137. cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1);
  138. }
  139. }
  140. if (FD_ISSET (select_fd, &read_fds)) {
  141. if (cpg_dispatch (handle, CPG_DISPATCH_ALL) != SA_AIS_OK)
  142. exit(1);
  143. }
  144. } while (result);
  145. result = cpg_finalize (handle);
  146. printf ("Finalize result is %d (should be 1)\n", result);
  147. return (0);
  148. }