fairqueue_test2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @file fairqueue_test2.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 <misc/debug.h>
  31. #include <system/BReactor.h>
  32. #include <base/BLog.h>
  33. #include <system/BTime.h>
  34. #include <flow/PacketPassFairQueue.h>
  35. #include <examples/FastPacketSource.h>
  36. #include <examples/RandomPacketSink.h>
  37. #define SINK_TIMER 0
  38. int main ()
  39. {
  40. // initialize logging
  41. BLog_InitStdout();
  42. // init time
  43. BTime_Init();
  44. // initialize reactor
  45. BReactor reactor;
  46. if (!BReactor_Init(&reactor)) {
  47. DEBUG("BReactor_Init failed");
  48. return 1;
  49. }
  50. // initialize sink
  51. RandomPacketSink sink;
  52. RandomPacketSink_Init(&sink, &reactor, 500, SINK_TIMER);
  53. // initialize queue
  54. PacketPassFairQueue fq;
  55. if (!PacketPassFairQueue_Init(&fq, RandomPacketSink_GetInput(&sink), BReactor_PendingGroup(&reactor), 0, 1)) {
  56. DEBUG("PacketPassFairQueue_Init failed");
  57. return 1;
  58. }
  59. // initialize source 1
  60. PacketPassFairQueueFlow flow1;
  61. PacketPassFairQueueFlow_Init(&flow1, &fq);
  62. FastPacketSource source1;
  63. char data1[] = "data1";
  64. FastPacketSource_Init(&source1, PacketPassFairQueueFlow_GetInput(&flow1), (uint8_t *)data1, strlen(data1), BReactor_PendingGroup(&reactor));
  65. // initialize source 2
  66. PacketPassFairQueueFlow flow2;
  67. PacketPassFairQueueFlow_Init(&flow2, &fq);
  68. FastPacketSource source2;
  69. char data2[] = "data2data2";
  70. FastPacketSource_Init(&source2, PacketPassFairQueueFlow_GetInput(&flow2), (uint8_t *)data2, strlen(data2), BReactor_PendingGroup(&reactor));
  71. // initialize source 3
  72. PacketPassFairQueueFlow flow3;
  73. PacketPassFairQueueFlow_Init(&flow3, &fq);
  74. FastPacketSource source3;
  75. char data3[] = "data3data3data3data3data3data3data3data3data3";
  76. FastPacketSource_Init(&source3, PacketPassFairQueueFlow_GetInput(&flow3), (uint8_t *)data3, strlen(data3), BReactor_PendingGroup(&reactor));
  77. // run reactor
  78. int ret = BReactor_Exec(&reactor);
  79. BReactor_Free(&reactor);
  80. return ret;
  81. }