fairqueue_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file fairqueue_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stddef.h>
  25. #include <misc/debug.h>
  26. #include <system/BReactor.h>
  27. #include <base/BLog.h>
  28. #include <system/BTime.h>
  29. #include <flow/PacketPassFairQueue.h>
  30. #include <examples/FastPacketSource.h>
  31. #include <examples/TimerPacketSink.h>
  32. #define OUTPUT_INTERVAL 0
  33. #define REMOVE_INTERVAL 1
  34. #define NUM_INPUTS 3
  35. BReactor reactor;
  36. TimerPacketSink sink;
  37. PacketPassFairQueue fq;
  38. PacketPassFairQueueFlow flows[NUM_INPUTS];
  39. FastPacketSource sources[NUM_INPUTS];
  40. char *data[] = {"0 data", "1 datadatadata", "2 datadatadatadatadata"};
  41. BTimer timer;
  42. int current_cancel;
  43. static void init_input (int i)
  44. {
  45. PacketPassFairQueueFlow_Init(&flows[i], &fq);
  46. FastPacketSource_Init(&sources[i], PacketPassFairQueueFlow_GetInput(&flows[i]), (uint8_t *)data[i], strlen(data[i]), BReactor_PendingGroup(&reactor));
  47. }
  48. static void free_input (int i)
  49. {
  50. FastPacketSource_Free(&sources[i]);
  51. PacketPassFairQueueFlow_Free(&flows[i]);
  52. }
  53. static void reset_input (void)
  54. {
  55. PacketPassFairQueueFlow_AssertFree(&flows[current_cancel]);
  56. printf("removing %d\n", current_cancel);
  57. // remove flow
  58. free_input(current_cancel);
  59. // init flow
  60. init_input(current_cancel);
  61. // increment cancel
  62. current_cancel = (current_cancel + 1) % NUM_INPUTS;
  63. // reset timer
  64. BReactor_SetTimer(&reactor, &timer);
  65. }
  66. static void flow_handler_busy (void *user)
  67. {
  68. PacketPassFairQueueFlow_AssertFree(&flows[current_cancel]);
  69. reset_input();
  70. }
  71. static void timer_handler (void *user)
  72. {
  73. // if flow is busy, request cancel and wait for it
  74. if (PacketPassFairQueueFlow_IsBusy(&flows[current_cancel])) {
  75. printf("cancelling %d\n", current_cancel);
  76. PacketPassFairQueueFlow_RequestCancel(&flows[current_cancel]);
  77. PacketPassFairQueueFlow_SetBusyHandler(&flows[current_cancel], flow_handler_busy, NULL);
  78. return;
  79. }
  80. reset_input();
  81. }
  82. int main ()
  83. {
  84. // initialize logging
  85. BLog_InitStdout();
  86. // init time
  87. BTime_Init();
  88. // initialize reactor
  89. if (!BReactor_Init(&reactor)) {
  90. DEBUG("BReactor_Init failed");
  91. return 1;
  92. }
  93. // initialize sink
  94. TimerPacketSink_Init(&sink, &reactor, 500, OUTPUT_INTERVAL);
  95. // initialize queue
  96. if (!PacketPassFairQueue_Init(&fq, TimerPacketSink_GetInput(&sink), BReactor_PendingGroup(&reactor), 1, 1)) {
  97. DEBUG("PacketPassFairQueue_Init failed");
  98. return 1;
  99. }
  100. // initialize inputs
  101. for (int i = 0; i < NUM_INPUTS; i++) {
  102. init_input(i);
  103. }
  104. // init cancel timer
  105. BTimer_Init(&timer, REMOVE_INTERVAL, timer_handler, NULL);
  106. BReactor_SetTimer(&reactor, &timer);
  107. // init cancel counter
  108. current_cancel = 0;
  109. // run reactor
  110. int ret = BReactor_Exec(&reactor);
  111. BReactor_Free(&reactor);
  112. return ret;
  113. }