testevs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <errno.h>
  6. #include "../include/evs.h"
  7. char *delivery_string;
  8. void evs_deliver_fn (struct in_addr source_addr, void *msg, int msg_len)
  9. {
  10. char *buf = msg;
  11. // buf += 100000;
  12. // printf ("Delivery callback\n");
  13. printf ("API '%s' msg '%s'\n", delivery_string, buf);
  14. }
  15. void evs_confchg_fn (
  16. struct in_addr *member_list, int member_list_entries,
  17. struct in_addr *left_list, int left_list_entries,
  18. struct in_addr *joined_list, int joined_list_entries)
  19. {
  20. int i;
  21. printf ("CONFIGURATION CHANGE\n");
  22. printf ("--------------------\n");
  23. printf ("New configuration\n");
  24. for (i = 0; i < member_list_entries; i++) {
  25. printf ("%s\n", inet_ntoa (member_list[i]));
  26. }
  27. printf ("Members Left:\n");
  28. for (i = 0; i < left_list_entries; i++) {
  29. printf ("%s\n", inet_ntoa (left_list[i]));
  30. }
  31. printf ("Members Joined:\n");
  32. for (i = 0; i < joined_list_entries; i++) {
  33. printf ("%s\n", inet_ntoa (joined_list[i]));
  34. }
  35. }
  36. evs_callbacks_t callbacks = {
  37. evs_deliver_fn,
  38. evs_confchg_fn
  39. };
  40. struct evs_group groups[3] = {
  41. { "key1" },
  42. { "key2" },
  43. { "key3" }
  44. };
  45. char buffer[1000];
  46. struct iovec iov = {
  47. .iov_base = buffer,
  48. .iov_len = sizeof (buffer)
  49. };
  50. int main (void)
  51. {
  52. unsigned int handle;
  53. evs_error_t result;
  54. int i = 0;
  55. result = evs_initialize (&handle, &callbacks);
  56. printf ("Init result %d\n", result);
  57. result = evs_join (&handle, groups, 3);
  58. printf ("Join result %d\n", result);
  59. result = evs_leave (&handle, &groups[0], 1);
  60. printf ("Leave result %d\n", result);
  61. delivery_string = "evs_mcast_joined";
  62. /*
  63. * Demonstrate evs_mcast_joined
  64. */
  65. for (i = 0; i < 500; i++) {
  66. sprintf (buffer, "evs_mcast_joined: This is message %d", i);
  67. try_again_one:
  68. result = evs_mcast_joined (&handle, EVS_TYPE_AGREED, EVS_PRIO_LOW,
  69. &iov, 1);
  70. if (result == EVS_ERR_TRY_AGAIN) {
  71. goto try_again_one;
  72. }
  73. result = evs_dispatch (&handle, EVS_DISPATCH_ALL);
  74. }
  75. // result = evs_leave (&handle, &groups[1], 2); This causes an assertion
  76. /*
  77. * Demonstrate evs_mcast_joined
  78. */
  79. delivery_string = "evs_mcast_groups";
  80. for (i = 0; i < 500; i++) {
  81. sprintf (buffer, "evs_mcast_groups: This is message %d", i);
  82. try_again_two:
  83. result = evs_mcast_groups (&handle, EVS_TYPE_AGREED, EVS_PRIO_LOW,
  84. &groups[1], 1, &iov, 1);
  85. if (result == EVS_ERR_TRY_AGAIN) {
  86. goto try_again_two;
  87. }
  88. result = evs_dispatch (&handle, EVS_DISPATCH_ALL);
  89. }
  90. return (0);
  91. }