fairqueue_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @file fairqueue_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include <stddef.h>
  32. #include <misc/debug.h>
  33. #include <system/BReactor.h>
  34. #include <base/BLog.h>
  35. #include <system/BTime.h>
  36. #include <flow/PacketPassFairQueue.h>
  37. #include <examples/FastPacketSource.h>
  38. #include <examples/TimerPacketSink.h>
  39. #define OUTPUT_INTERVAL 0
  40. #define REMOVE_INTERVAL 1
  41. #define NUM_INPUTS 3
  42. BReactor reactor;
  43. TimerPacketSink sink;
  44. PacketPassFairQueue fq;
  45. PacketPassFairQueueFlow flows[NUM_INPUTS];
  46. FastPacketSource sources[NUM_INPUTS];
  47. char *data[] = {"0 data", "1 datadatadata", "2 datadatadatadatadata"};
  48. BTimer timer;
  49. int current_cancel;
  50. static void init_input (int i)
  51. {
  52. PacketPassFairQueueFlow_Init(&flows[i], &fq);
  53. FastPacketSource_Init(&sources[i], PacketPassFairQueueFlow_GetInput(&flows[i]), (uint8_t *)data[i], strlen(data[i]), BReactor_PendingGroup(&reactor));
  54. }
  55. static void free_input (int i)
  56. {
  57. FastPacketSource_Free(&sources[i]);
  58. PacketPassFairQueueFlow_Free(&flows[i]);
  59. }
  60. static void reset_input (void)
  61. {
  62. PacketPassFairQueueFlow_AssertFree(&flows[current_cancel]);
  63. printf("removing %d\n", current_cancel);
  64. // remove flow
  65. free_input(current_cancel);
  66. // init flow
  67. init_input(current_cancel);
  68. // increment cancel
  69. current_cancel = (current_cancel + 1) % NUM_INPUTS;
  70. // reset timer
  71. BReactor_SetTimer(&reactor, &timer);
  72. }
  73. static void flow_handler_busy (void *user)
  74. {
  75. PacketPassFairQueueFlow_AssertFree(&flows[current_cancel]);
  76. reset_input();
  77. }
  78. static void timer_handler (void *user)
  79. {
  80. // if flow is busy, request cancel and wait for it
  81. if (PacketPassFairQueueFlow_IsBusy(&flows[current_cancel])) {
  82. printf("cancelling %d\n", current_cancel);
  83. PacketPassFairQueueFlow_RequestCancel(&flows[current_cancel]);
  84. PacketPassFairQueueFlow_SetBusyHandler(&flows[current_cancel], flow_handler_busy, NULL);
  85. return;
  86. }
  87. reset_input();
  88. }
  89. int main ()
  90. {
  91. // initialize logging
  92. BLog_InitStdout();
  93. // init time
  94. BTime_Init();
  95. // initialize reactor
  96. if (!BReactor_Init(&reactor)) {
  97. DEBUG("BReactor_Init failed");
  98. return 1;
  99. }
  100. // initialize sink
  101. TimerPacketSink_Init(&sink, &reactor, 500, OUTPUT_INTERVAL);
  102. // initialize queue
  103. if (!PacketPassFairQueue_Init(&fq, TimerPacketSink_GetInput(&sink), BReactor_PendingGroup(&reactor), 1, 1)) {
  104. DEBUG("PacketPassFairQueue_Init failed");
  105. return 1;
  106. }
  107. // initialize inputs
  108. for (int i = 0; i < NUM_INPUTS; i++) {
  109. init_input(i);
  110. }
  111. // init cancel timer
  112. BTimer_Init(&timer, REMOVE_INTERVAL, timer_handler, NULL);
  113. BReactor_SetTimer(&reactor, &timer);
  114. // init cancel counter
  115. current_cancel = 0;
  116. // run reactor
  117. int ret = BReactor_Exec(&reactor);
  118. BReactor_Free(&reactor);
  119. return ret;
  120. }