testevsth.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@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 <config.h>
  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 <pthread.h>
  42. #include "../include/evs.h"
  43. char *delivery_string;
  44. #define CALLBACKS 200000
  45. int callback_count = 0;
  46. void evs_deliver_fn (struct in_addr source_addr, const void *msg, size_t msg_len)
  47. {
  48. #ifdef PRINT_OUTPUT
  49. char *buf;
  50. buf += 100000;
  51. printf ("Delivery callback\n");
  52. printf ("callback %d '%s' msg '%s'\n", callback_count, delivery_string, buf);
  53. #endif
  54. callback_count += 1;
  55. if (callback_count % 50 == 0) {
  56. printf ("Callback %d\n", callback_count);
  57. }
  58. }
  59. void evs_confchg_fn (
  60. const struct in_addr *member_list, size_t member_list_entries,
  61. const struct in_addr *left_list, size_t left_list_entries,
  62. const struct in_addr *joined_list, size_t joined_list_entries)
  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 ("%s\n", inet_ntoa (member_list[i]));
  70. }
  71. printf ("Members Left:\n");
  72. for (i = 0; i < left_list_entries; i++) {
  73. printf ("%s\n", inet_ntoa (left_list[i]));
  74. }
  75. printf ("Members Joined:\n");
  76. for (i = 0; i < joined_list_entries; i++) {
  77. printf ("%s\n", inet_ntoa (joined_list[i]));
  78. }
  79. }
  80. evs_callbacks_t callbacks = {
  81. evs_deliver_fn,
  82. evs_confchg_fn
  83. };
  84. struct evs_group groups[3] = {
  85. { "key1" },
  86. { "key2" },
  87. { "key3" }
  88. };
  89. char buffer[1000];
  90. struct iovec iov = {
  91. .iov_base = buffer,
  92. .iov_len = sizeof (buffer)
  93. };
  94. void *th_dispatch (void *arg)
  95. {
  96. cs_error_t result;
  97. evs_handle_t handle = *(evs_handle_t *)arg;
  98. printf ("THREAD DISPATCH starting.\n");
  99. result = evs_dispatch (handle, CS_DISPATCH_BLOCKING);
  100. printf ("THREAD DISPATCH return result is %d\n", result);
  101. return (0);
  102. }
  103. static struct sched_param sched_param = {
  104. sched_priority: 99
  105. };
  106. int main (void)
  107. {
  108. evs_handle_t handle;
  109. cs_error_t result;
  110. int i = 0;
  111. pthread_t dispatch_thread;
  112. pthread_attr_t dispatch_thread_attribute;
  113. result = evs_initialize (&handle, &callbacks);
  114. if (result != CS_OK) {
  115. printf ("Couldn't initialize EVS service %d\n", result);
  116. exit (0);
  117. }
  118. pthread_attr_init (&dispatch_thread_attribute);
  119. pthread_attr_setschedpolicy (&dispatch_thread_attribute, SCHED_FIFO);
  120. pthread_attr_setschedparam (&dispatch_thread_attribute, &sched_param);
  121. pthread_create (&dispatch_thread, NULL, th_dispatch, &handle);
  122. printf ("Init result %d\n", result);
  123. result = evs_join (handle, groups, 3);
  124. printf ("Join result %d\n", result);
  125. result = evs_leave (handle, &groups[0], 1);
  126. printf ("Leave result %d\n", result);
  127. delivery_string = "evs_mcast_joined";
  128. /*
  129. * Demonstrate evs_mcast_joined
  130. */
  131. for (i = 0; i < CALLBACKS/2; i++) {
  132. sprintf (buffer, "evs_mcast_joined: This is message %d", i);
  133. try_again_one:
  134. result = evs_mcast_joined (handle, EVS_TYPE_AGREED, &iov, 1);
  135. if (result == CS_ERR_TRY_AGAIN) {
  136. goto try_again_one;
  137. } else
  138. if (result != CS_OK) {
  139. printf ("Got error result, exiting %d\n", result);
  140. exit (1);
  141. }
  142. }
  143. /*
  144. * Demonstrate evs_mcast_joined
  145. */
  146. delivery_string = "evs_mcast_groups";
  147. for (i = 0; i < CALLBACKS/2; i++) {
  148. sprintf (buffer, "evs_mcast_groups: This is message %d", i);
  149. try_again_two:
  150. result = evs_mcast_groups (handle, EVS_TYPE_AGREED,
  151. &groups[1], 1, &iov, 1);
  152. if (result == CS_ERR_TRY_AGAIN) {
  153. goto try_again_two;
  154. }
  155. }
  156. /*
  157. * Wait until all callbacks have been executed by dispatch thread
  158. */
  159. for (;;) {
  160. if (callback_count == CALLBACKS) {
  161. printf ("Test completed successfully\n");
  162. exit (0);
  163. }
  164. }
  165. return (0);
  166. }