testevs.c 5.2 KB

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