stdin_input.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file stdin_input.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. * @section DESCRIPTION
  23. *
  24. * Example program which reads stdin and waits for SIGINT and SIGTERM.
  25. */
  26. #include <stdio.h>
  27. #include <stddef.h>
  28. #include <system/BReactor.c>
  29. #include <system/BSocket.h>
  30. #include <system/BUnixSignal.h>
  31. #include <base/DebugObject.h>
  32. #include <flowextra/StreamSocketSource.h>
  33. #define BUF_SIZE 64
  34. BReactor reactor;
  35. BSocket pipe_bsock;
  36. BUnixSignal usignal;
  37. FlowErrorDomain errdomain;
  38. StreamSocketSource source;
  39. StreamRecvInterface *source_if;
  40. uint8_t buf[BUF_SIZE + 1];
  41. static void signal_handler (void *user, int signo)
  42. {
  43. fprintf(stderr, "received %s, exiting\n", (signo == SIGINT ? "SIGINT" : "SIGTERM"));
  44. // exit event loop
  45. BReactor_Quit(&reactor, 1);
  46. }
  47. static void source_error_handler (void *user, int component, int code)
  48. {
  49. if (code == 0) {
  50. fprintf(stderr, "pipe closed\n");
  51. } else {
  52. fprintf(stderr, "pipe error\n");
  53. }
  54. // exit event loop
  55. BReactor_Quit(&reactor, (code == 0 ? 0 : 1));
  56. }
  57. static void input_handler_done (void *user, int data_len)
  58. {
  59. // receive next chunk
  60. StreamRecvInterface_Receiver_Recv(source_if, buf, BUF_SIZE);
  61. // print this chunk
  62. buf[data_len] = '\0';
  63. printf("Received: '%s'\n", buf);
  64. }
  65. int main ()
  66. {
  67. int ret = 1;
  68. BLog_InitStdout();
  69. // init reactor (event loop)
  70. if (!BReactor_Init(&reactor)) {
  71. fprintf(stderr, "BReactor_Init failed\n");
  72. goto fail1;
  73. }
  74. // init signal handling
  75. sigset_t set;
  76. sigemptyset(&set);
  77. sigaddset(&set, SIGINT);
  78. sigaddset(&set, SIGTERM);
  79. if (!BUnixSignal_Init(&usignal, &reactor, set, signal_handler, NULL)) {
  80. fprintf(stderr, "BUnixSignal_Init failed\n");
  81. goto fail2;
  82. }
  83. // init BSocket object backed by the stdin fd
  84. if (BSocket_InitPipe(&pipe_bsock, &reactor, 0) < 0) {
  85. fprintf(stderr, "BSocket_InitPipe failed\n");
  86. goto fail3;
  87. }
  88. // init error handler
  89. FlowErrorDomain_Init(&errdomain, source_error_handler, NULL);
  90. // init source (object for reading from a stream BSocket using StreamRecvInterface)
  91. StreamSocketSource_Init(&source, FlowErrorReporter_Create(&errdomain, 0), &pipe_bsock, BReactor_PendingGroup(&reactor));
  92. source_if = StreamSocketSource_GetOutput(&source);
  93. // init receive done callback
  94. StreamRecvInterface_Receiver_Init(source_if, input_handler_done, NULL);
  95. // receive first chunk
  96. StreamRecvInterface_Receiver_Recv(source_if, buf, BUF_SIZE);
  97. // run event loop
  98. ret = BReactor_Exec(&reactor);
  99. StreamSocketSource_Free(&source);
  100. BSocket_Free(&pipe_bsock);
  101. fail3:
  102. BUnixSignal_Free(&usignal, 0);
  103. fail2:
  104. BReactor_Free(&reactor);
  105. fail1:
  106. BLog_Free();
  107. DebugObjectGlobal_Finish();
  108. return ret;
  109. }