testevs.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2004 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 <sys/socket.h>
  37. #include <netinet/in.h>
  38. #include <arpa/inet.h>
  39. #include <errno.h>
  40. #include "../include/evs.h"
  41. char *delivery_string;
  42. int deliveries = 0;
  43. void evs_deliver_fn (struct evs_address *source_addr, void *msg, int msg_len)
  44. {
  45. char *buf = msg;
  46. // buf += 100000;
  47. // printf ("Delivery callback\n");
  48. printf ("API '%s' msg '%s'\n", delivery_string, buf);
  49. deliveries++;
  50. }
  51. void evs_confchg_fn (
  52. struct evs_address *member_list, int member_list_entries,
  53. struct evs_address *left_list, int left_list_entries,
  54. struct evs_address *joined_list, int joined_list_entries)
  55. {
  56. int i;
  57. char buf[256];
  58. printf ("CONFIGURATION CHANGE\n");
  59. printf ("--------------------\n");
  60. printf ("New configuration\n");
  61. for (i = 0; i < member_list_entries; i++) {
  62. inet_ntop (member_list[i].family, member_list[i].addr,
  63. buf, sizeof (buf));
  64. printf ("%s\n", buf);
  65. }
  66. printf ("Members Left:\n");
  67. for (i = 0; i < left_list_entries; i++) {
  68. inet_ntop (left_list[i].family, left_list[i].addr,
  69. buf, sizeof (buf));
  70. printf ("%s\n", buf);
  71. }
  72. printf ("Members Joined:\n");
  73. for (i = 0; i < joined_list_entries; i++) {
  74. inet_ntop (joined_list[i].family, joined_list[i].addr,
  75. buf, sizeof (buf));
  76. printf ("%s\n", buf);
  77. }
  78. }
  79. evs_callbacks_t callbacks = {
  80. evs_deliver_fn,
  81. evs_confchg_fn
  82. };
  83. struct evs_group groups[3] = {
  84. { "key1" },
  85. { "key2" },
  86. { "key3" }
  87. };
  88. char buffer[200000];
  89. struct iovec iov = {
  90. .iov_base = buffer,
  91. .iov_len = sizeof (buffer)
  92. };
  93. int main (void)
  94. {
  95. evs_handle_t handle;
  96. evs_error_t result;
  97. int i = 0;
  98. int fd;
  99. struct evs_address member_list[16];
  100. struct evs_address local_addr;
  101. int member_list_entries = sizeof (member_list) / sizeof (struct in_addr);
  102. char buf[1024];
  103. result = evs_initialize (&handle, &callbacks);
  104. if (result != EVS_OK) {
  105. printf ("Couldn't initialize EVS service %d\n", result);
  106. exit (0);
  107. }
  108. result = evs_membership_get (handle, &local_addr,
  109. member_list, &member_list_entries);
  110. printf ("Current membership from evs_membership_get entries %d\n",
  111. member_list_entries);
  112. for (i = 0; i < member_list_entries; i++) {
  113. inet_ntop (member_list[i].family, member_list[i].addr,
  114. buf, sizeof (buf));
  115. printf ("member [%d] is %s\n", i, buf);
  116. }
  117. inet_ntop (local_addr.family, local_addr.addr,
  118. buf, sizeof (buf));
  119. printf ("local processor from evs_membership_get %s\n", buf);
  120. printf ("Init result %d\n", result);
  121. result = evs_join (handle, groups, 3);
  122. printf ("Join result %d\n", result);
  123. result = evs_leave (handle, &groups[0], 1);
  124. printf ("Leave 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 == EVS_ERR_TRY_AGAIN) {
  141. //printf ("try again\n");
  142. goto try_again_one;
  143. }
  144. result = evs_dispatch (handle, EVS_DISPATCH_ALL);
  145. }
  146. do {
  147. result = evs_dispatch (handle, EVS_DISPATCH_ALL);
  148. } while (deliveries < 20);
  149. /*
  150. * Demonstrate evs_mcast_joined
  151. */
  152. delivery_string = "evs_mcast_groups";
  153. for (i = 0; i < 500; i++) {
  154. sprintf (buffer, "evs_mcast_groups: This is message %d", i);
  155. try_again_two:
  156. result = evs_mcast_groups (handle, EVS_TYPE_AGREED,
  157. &groups[1], 1, &iov, 1);
  158. if (result == EVS_ERR_TRY_AGAIN) {
  159. goto try_again_two;
  160. }
  161. result = evs_dispatch (handle, EVS_DISPATCH_ALL);
  162. }
  163. /*
  164. * Flush any pending callbacks
  165. */
  166. do {
  167. result = evs_dispatch (handle, EVS_DISPATCH_ALL);
  168. } while (deliveries < 900);
  169. evs_fd_get (handle, &fd);
  170. evs_finalize (handle);
  171. return (0);
  172. }