fairqueue_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <misc/debug.h>
  25. #include <system/BReactor.h>
  26. #include <system/BLog.h>
  27. #include <system/BTime.h>
  28. #include <flow/PacketPassFairQueue.h>
  29. #include <examples/FastPacketSource.h>
  30. #include <examples/TimerPacketSink.h>
  31. #define OUTPUT_INTERVAL 0
  32. #define REMOVE_INTERVAL 1
  33. #define NUM_INPUTS 3
  34. BReactor reactor;
  35. TimerPacketSink sink;
  36. PacketPassFairQueue fq;
  37. PacketPassFairQueueFlow flows[NUM_INPUTS];
  38. FastPacketSource sources[NUM_INPUTS];
  39. char *data[] = {"0 data", "1 datadatadata", "2 datadatadatadatadata"};
  40. BTimer timer;
  41. int current_cancel;
  42. static void init_input (int i)
  43. {
  44. PacketPassFairQueueFlow_Init(&flows[i], &fq);
  45. FastPacketSource_Init(&sources[i], PacketPassFairQueueFlow_GetInput(&flows[i]), (uint8_t *)data[i], strlen(data[i]), BReactor_PendingGroup(&reactor));
  46. }
  47. static void free_input (int i)
  48. {
  49. FastPacketSource_Free(&sources[i]);
  50. PacketPassFairQueueFlow_Free(&flows[i]);
  51. }
  52. static void timer_handler (void *user)
  53. {
  54. printf("removing %d\n", current_cancel);
  55. // release flow
  56. if (PacketPassFairQueueFlow_IsBusy(&flows[current_cancel])) {
  57. PacketPassFairQueueFlow_Release(&flows[current_cancel]);
  58. }
  59. // remove flow
  60. free_input(current_cancel);
  61. // init flow
  62. init_input(current_cancel);
  63. // increment cancel
  64. current_cancel = (current_cancel + 1) % NUM_INPUTS;
  65. // reset timer
  66. BReactor_SetTimer(&reactor, &timer);
  67. }
  68. int main ()
  69. {
  70. // initialize logging
  71. BLog_InitStdout();
  72. // init time
  73. BTime_Init();
  74. // initialize reactor
  75. if (!BReactor_Init(&reactor)) {
  76. DEBUG("BReactor_Init failed");
  77. return 1;
  78. }
  79. // initialize sink
  80. TimerPacketSink_Init(&sink, &reactor, 500, OUTPUT_INTERVAL);
  81. // initialize queue
  82. PacketPassFairQueue_Init(&fq, TimerPacketSink_GetInput(&sink), BReactor_PendingGroup(&reactor));
  83. PacketPassFairQueue_EnableCancel(&fq);
  84. // initialize inputs
  85. for (int i = 0; i < NUM_INPUTS; i++) {
  86. init_input(i);
  87. }
  88. // init cancel timer
  89. BTimer_Init(&timer, REMOVE_INTERVAL, timer_handler, NULL);
  90. BReactor_SetTimer(&reactor, &timer);
  91. // init cancel counter
  92. current_cancel = 0;
  93. // run reactor
  94. int ret = BReactor_Exec(&reactor);
  95. BReactor_Free(&reactor);
  96. return ret;
  97. }