testevs.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.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 <stdio.h>
  36. #include <stdlib.h>
  37. #include <sys/socket.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <errno.h>
  41. #include <corosync/corotypes.h>
  42. #include <corosync/evs.h>
  43. char *delivery_string;
  44. int deliveries = 0;
  45. void evs_deliver_fn (
  46. unsigned int nodeid,
  47. void *msg,
  48. int msg_len)
  49. {
  50. char *buf = msg;
  51. // buf += 100000;
  52. // printf ("Delivery callback\n");
  53. printf ("API '%s' msg '%s'\n", delivery_string, buf);
  54. deliveries++;
  55. }
  56. void evs_confchg_fn (
  57. unsigned int *member_list, int member_list_entries,
  58. unsigned int *left_list, int left_list_entries,
  59. unsigned int *joined_list, int joined_list_entries)
  60. {
  61. int i;
  62. printf ("CONFIGURATION CHANGE\n");
  63. printf ("--------------------\n");
  64. printf ("New configuration\n");
  65. for (i = 0; i < member_list_entries; i++) {
  66. printf ("%x\n", member_list[i]);
  67. }
  68. printf ("Members Left:\n");
  69. for (i = 0; i < left_list_entries; i++) {
  70. printf ("%x\n", left_list[i]);
  71. }
  72. printf ("Members Joined:\n");
  73. for (i = 0; i < joined_list_entries; i++) {
  74. printf ("%x\n", joined_list[i]);
  75. }
  76. }
  77. evs_callbacks_t callbacks = {
  78. evs_deliver_fn,
  79. evs_confchg_fn
  80. };
  81. struct evs_group groups[3] = {
  82. { "key1" },
  83. { "key2" },
  84. { "key3" }
  85. };
  86. char buffer[200000];
  87. struct iovec iov = {
  88. .iov_base = buffer,
  89. .iov_len = sizeof (buffer)
  90. };
  91. int main (void)
  92. {
  93. evs_handle_t handle;
  94. cs_error_t result;
  95. int i = 0;
  96. int fd;
  97. unsigned int member_list[32];
  98. unsigned int local_nodeid;
  99. unsigned int member_list_entries = 32;
  100. result = evs_initialize (&handle, &callbacks);
  101. if (result != CS_OK) {
  102. printf ("Couldn't initialize EVS service %d\n", result);
  103. exit (0);
  104. }
  105. result = evs_membership_get (handle, &local_nodeid,
  106. member_list, &member_list_entries);
  107. printf ("Current membership from evs_membership_get entries %d\n",
  108. member_list_entries);
  109. for (i = 0; i < member_list_entries; i++) {
  110. printf ("member [%d] is %x\n", i, member_list[i]);
  111. }
  112. printf ("local processor from evs_membership_get %x\n", local_nodeid);
  113. printf ("Init result %d\n", result);
  114. result = evs_join (handle, groups, 3);
  115. printf ("Join result %d\n", result);
  116. result = evs_leave (handle, &groups[0], 1);
  117. printf ("Leave result %d\n", result);
  118. delivery_string = "evs_mcast_joined";
  119. /*
  120. * Demonstrate evs_mcast_joined
  121. */
  122. for (i = 0; i < 500; i++) {
  123. sprintf (buffer, "evs_mcast_joined: This is message %d", i);
  124. #ifdef COMPILE_OUT
  125. sprintf (buffer,
  126. "%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
  127. i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i,
  128. i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i);
  129. #endif
  130. try_again_one:
  131. result = evs_mcast_joined (handle, EVS_TYPE_AGREED,
  132. &iov, 1);
  133. if (result == CS_ERR_TRY_AGAIN) {
  134. //printf ("try again\n");
  135. goto try_again_one;
  136. }
  137. result = evs_dispatch (handle, CS_DISPATCH_ALL);
  138. }
  139. do {
  140. result = evs_dispatch (handle, CS_DISPATCH_ALL);
  141. } while (deliveries < 20);
  142. /*
  143. * Demonstrate evs_mcast_joined
  144. */
  145. delivery_string = "evs_mcast_groups";
  146. for (i = 0; i < 500; i++) {
  147. sprintf (buffer, "evs_mcast_groups: This is message %d", i);
  148. try_again_two:
  149. result = evs_mcast_groups (handle, EVS_TYPE_AGREED,
  150. &groups[1], 1, &iov, 1);
  151. if (result == CS_ERR_TRY_AGAIN) {
  152. goto try_again_two;
  153. }
  154. result = evs_dispatch (handle, CS_DISPATCH_ALL);
  155. }
  156. /*
  157. * Flush any pending callbacks
  158. */
  159. do {
  160. result = evs_dispatch (handle, CS_DISPATCH_ALL);
  161. } while (deliveries < 500);
  162. evs_fd_get (handle, &fd);
  163. evs_finalize (handle);
  164. return (0);
  165. }