testcpg.c 4.9 KB

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