ncd-request.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @file ncd-request.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 <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <base/BLog.h>
  34. #include <base/DebugObject.h>
  35. #include <system/BNetwork.h>
  36. #include <system/BReactor.h>
  37. #include <ncd/NCDValueParser.h>
  38. #include <ncd/NCDValueGenerator.h>
  39. #include <ncd/NCDRequestClient.h>
  40. #include <generated/blog_channel_ncd_request.h>
  41. static void client_handler_error (void *user);
  42. static void client_handler_connected (void *user);
  43. static void request_handler_sent (void *user);
  44. static void request_handler_reply (void *user, NCDValue reply_data);
  45. static void request_handler_finished (void *user, int is_error);
  46. static int write_all (int fd, const uint8_t *data, size_t len);
  47. NCDValue request_payload;
  48. BReactor reactor;
  49. NCDRequestClient client;
  50. NCDRequestClientRequest request;
  51. int have_request;
  52. int main (int argc, char *argv[])
  53. {
  54. int res = 1;
  55. if (argc != 3) {
  56. fprintf(stderr, "Usage: %s <socket_path> <request_payload>\n", (argc > 0 ? argv[0] : ""));
  57. goto fail0;
  58. }
  59. char *socket_path = argv[1];
  60. char *request_payload_string = argv[2];
  61. BLog_InitStderr();
  62. BTime_Init();
  63. if (!NCDValueParser_Parse(request_payload_string, strlen(request_payload_string), &request_payload)) {
  64. BLog(BLOG_ERROR, "BReactor_Init failed");
  65. goto fail1;
  66. }
  67. if (!BNetwork_GlobalInit()) {
  68. BLog(BLOG_ERROR, "BNetwork_Init failed");
  69. goto fail2;
  70. }
  71. if (!BReactor_Init(&reactor)) {
  72. BLog(BLOG_ERROR, "BReactor_Init failed");
  73. goto fail2;
  74. }
  75. if (!NCDRequestClient_Init(&client, socket_path, &reactor, NULL, client_handler_error, client_handler_connected)) {
  76. BLog(BLOG_ERROR, "NCDRequestClient_Init failed");
  77. goto fail3;
  78. }
  79. have_request = 0;
  80. res = BReactor_Exec(&reactor);
  81. if (have_request) {
  82. NCDRequestClientRequest_Free(&request);
  83. }
  84. NCDRequestClient_Free(&client);
  85. fail3:
  86. BReactor_Free(&reactor);
  87. fail2:
  88. NCDValue_Free(&request_payload);
  89. fail1:
  90. BLog_Free();
  91. fail0:
  92. DebugObjectGlobal_Finish();
  93. return res;
  94. }
  95. static void client_handler_error (void *user)
  96. {
  97. BLog(BLOG_ERROR, "client error");
  98. BReactor_Quit(&reactor, 1);
  99. }
  100. static void client_handler_connected (void *user)
  101. {
  102. ASSERT(!have_request)
  103. if (!NCDRequestClientRequest_Init(&request, &client, &request_payload, NULL, request_handler_sent, request_handler_reply, request_handler_finished)) {
  104. BLog(BLOG_ERROR, "NCDRequestClientRequest_Init failed");
  105. BReactor_Quit(&reactor, 1);
  106. return;
  107. }
  108. have_request = 1;
  109. }
  110. static void request_handler_sent (void *user)
  111. {
  112. ASSERT(have_request)
  113. }
  114. static void request_handler_reply (void *user, NCDValue reply_data)
  115. {
  116. ASSERT(have_request)
  117. char *str = NCDValueGenerator_Generate(&reply_data);
  118. if (!str) {
  119. BLog(BLOG_ERROR, "NCDValueGenerator_Generate failed");
  120. goto fail0;
  121. }
  122. if (!write_all(1, (uint8_t *)str, strlen(str))) {
  123. goto fail1;
  124. }
  125. if (!write_all(1, (const uint8_t *)"\n", 1)) {
  126. goto fail1;
  127. }
  128. free(str);
  129. NCDValue_Free(&reply_data);
  130. return;
  131. fail1:
  132. free(str);
  133. fail0:
  134. NCDValue_Free(&reply_data);
  135. BReactor_Quit(&reactor, 1);
  136. }
  137. static void request_handler_finished (void *user, int is_error)
  138. {
  139. if (is_error) {
  140. BLog(BLOG_ERROR, "request error");
  141. BReactor_Quit(&reactor, 1);
  142. return;
  143. }
  144. BReactor_Quit(&reactor, 0);
  145. }
  146. static int write_all (int fd, const uint8_t *data, size_t len)
  147. {
  148. while (len > 0) {
  149. ssize_t res = write(fd, data, len);
  150. if (res <= 0) {
  151. BLog(BLOG_ERROR, "write failed");
  152. return 0;
  153. }
  154. data += res;
  155. len -= res;
  156. }
  157. return 1;
  158. }