testevs.c 5.6 KB

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