stdin_input.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file stdin_input.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. * @section DESCRIPTION
  30. *
  31. * Example program which reads stdin and waits for SIGINT and SIGTERM.
  32. */
  33. #include <stdio.h>
  34. #include <stddef.h>
  35. #include <base/DebugObject.h>
  36. #include <system/BReactor.h>
  37. #include <system/BNetwork.h>
  38. #include <system/BConnection.h>
  39. #include <system/BUnixSignal.h>
  40. #define BUF_SIZE 64
  41. BReactor reactor;
  42. BConnection pipe_con;
  43. BUnixSignal usignal;
  44. StreamRecvInterface *source_if;
  45. uint8_t buf[BUF_SIZE + 1];
  46. static void signal_handler (void *user, int signo)
  47. {
  48. fprintf(stderr, "received %s, exiting\n", (signo == SIGINT ? "SIGINT" : "SIGTERM"));
  49. // exit event loop
  50. BReactor_Quit(&reactor, 1);
  51. }
  52. static void connection_handler (void *user, int event)
  53. {
  54. if (event == BCONNECTION_EVENT_RECVCLOSED) {
  55. fprintf(stderr, "pipe closed\n");
  56. } else {
  57. fprintf(stderr, "pipe error\n");
  58. }
  59. // exit event loop
  60. BReactor_Quit(&reactor, (event == BCONNECTION_EVENT_RECVCLOSED ? 0 : 1));
  61. }
  62. static void input_handler_done (void *user, int data_len)
  63. {
  64. // receive next chunk
  65. StreamRecvInterface_Receiver_Recv(source_if, buf, BUF_SIZE);
  66. // print this chunk
  67. buf[data_len] = '\0';
  68. printf("Received: '%s'\n", buf);
  69. }
  70. int main ()
  71. {
  72. int ret = 1;
  73. BLog_InitStdout();
  74. // init network
  75. if (!BNetwork_GlobalInit()) {
  76. fprintf(stderr, "BNetwork_GlobalInit failed\n");
  77. goto fail1;
  78. }
  79. // init reactor (event loop)
  80. if (!BReactor_Init(&reactor)) {
  81. fprintf(stderr, "BReactor_Init failed\n");
  82. goto fail1;
  83. }
  84. // init signal handling
  85. sigset_t set;
  86. sigemptyset(&set);
  87. sigaddset(&set, SIGINT);
  88. sigaddset(&set, SIGTERM);
  89. if (!BUnixSignal_Init(&usignal, &reactor, set, signal_handler, NULL)) {
  90. fprintf(stderr, "BUnixSignal_Init failed\n");
  91. goto fail2;
  92. }
  93. // init BConnection object backed by the stdin fd
  94. if (!BConnection_Init(&pipe_con, BConnection_source_pipe(0, 0), &reactor, NULL, connection_handler)) {
  95. fprintf(stderr, "BConnection_Init failed\n");
  96. goto fail3;
  97. }
  98. // init connection receive interface
  99. BConnection_RecvAsync_Init(&pipe_con);
  100. source_if = BConnection_RecvAsync_GetIf(&pipe_con);
  101. // init receive done callback
  102. StreamRecvInterface_Receiver_Init(source_if, input_handler_done, NULL);
  103. // receive first chunk
  104. StreamRecvInterface_Receiver_Recv(source_if, buf, BUF_SIZE);
  105. // run event loop
  106. ret = BReactor_Exec(&reactor);
  107. BConnection_RecvAsync_Free(&pipe_con);
  108. BConnection_Free(&pipe_con);
  109. fail3:
  110. BUnixSignal_Free(&usignal, 0);
  111. fail2:
  112. BReactor_Free(&reactor);
  113. fail1:
  114. BLog_Free();
  115. DebugObjectGlobal_Finish();
  116. return ret;
  117. }