testevs.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. void evs_deliver_fn (struct in_addr source_addr, void *msg, int msg_len)
  43. {
  44. char *buf = msg;
  45. // buf += 100000;
  46. // printf ("Delivery callback\n");
  47. printf ("API '%s' msg '%s'\n", delivery_string, buf);
  48. }
  49. void evs_confchg_fn (
  50. struct in_addr *member_list, int member_list_entries,
  51. struct in_addr *left_list, int left_list_entries,
  52. struct in_addr *joined_list, int joined_list_entries)
  53. {
  54. int i;
  55. printf ("CONFIGURATION CHANGE\n");
  56. printf ("--------------------\n");
  57. printf ("New configuration\n");
  58. for (i = 0; i < member_list_entries; i++) {
  59. printf ("%s\n", inet_ntoa (member_list[i]));
  60. }
  61. printf ("Members Left:\n");
  62. for (i = 0; i < left_list_entries; i++) {
  63. printf ("%s\n", inet_ntoa (left_list[i]));
  64. }
  65. printf ("Members Joined:\n");
  66. for (i = 0; i < joined_list_entries; i++) {
  67. printf ("%s\n", inet_ntoa (joined_list[i]));
  68. }
  69. }
  70. evs_callbacks_t callbacks = {
  71. evs_deliver_fn,
  72. evs_confchg_fn
  73. };
  74. struct evs_group groups[3] = {
  75. { "key1" },
  76. { "key2" },
  77. { "key3" }
  78. };
  79. char buffer[1000];
  80. struct iovec iov = {
  81. .iov_base = buffer,
  82. .iov_len = sizeof (buffer)
  83. };
  84. int main (void)
  85. {
  86. unsigned int handle;
  87. evs_error_t result;
  88. int i = 0;
  89. result = evs_initialize (&handle, &callbacks);
  90. if (result != EVS_OK) {
  91. printf ("Couldn't initialize EVS service %d\n", result);
  92. exit (0);
  93. }
  94. printf ("Init result %d\n", result);
  95. result = evs_join (&handle, groups, 3);
  96. printf ("Join result %d\n", result);
  97. result = evs_leave (&handle, &groups[0], 1);
  98. printf ("Leave result %d\n", result);
  99. delivery_string = "evs_mcast_joined";
  100. /*
  101. * Demonstrate evs_mcast_joined
  102. */
  103. for (i = 0; i < 500; i++) {
  104. sprintf (buffer, "evs_mcast_joined: This is message %d", i);
  105. try_again_one:
  106. result = evs_mcast_joined (&handle, EVS_TYPE_AGREED, EVS_PRIO_LOW,
  107. &iov, 1);
  108. if (result == EVS_ERR_TRY_AGAIN) {
  109. goto try_again_one;
  110. }
  111. result = evs_dispatch (&handle, EVS_DISPATCH_ALL);
  112. }
  113. // result = evs_leave (&handle, &groups[1], 2); This causes an assertion
  114. /*
  115. * Demonstrate evs_mcast_joined
  116. */
  117. delivery_string = "evs_mcast_groups";
  118. for (i = 0; i < 500; i++) {
  119. sprintf (buffer, "evs_mcast_groups: This is message %d", i);
  120. try_again_two:
  121. result = evs_mcast_groups (&handle, EVS_TYPE_AGREED, EVS_PRIO_LOW,
  122. &groups[1], 1, &iov, 1);
  123. if (result == EVS_ERR_TRY_AGAIN) {
  124. goto try_again_two;
  125. }
  126. result = evs_dispatch (&handle, EVS_DISPATCH_ALL);
  127. }
  128. return (0);
  129. }