sys_request_client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /**
  2. * @file sys_request_client.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. * Synopsis:
  32. * sys.request_client(string connect_addr)
  33. *
  34. * Description:
  35. * Connects to a request server (sys.request_server()).
  36. * Goes up when the connection, and dies with error when it is broken.
  37. * When requested to die, dies immediately, breaking the connection.
  38. *
  39. * The connect_addr argument must be one of:
  40. * - {"unix", socket_path}
  41. * Connects to a Unix socket.
  42. * - {"tcp", ip_address, port_number}
  43. * Connects to a TCP server. The address must be numeric and not a name.
  44. * For IPv6, the address must be enclosed in [].
  45. *
  46. * Synopsis:
  47. * sys.request_client::request(request_data, string reply_handler, string finished_handler, list args)
  48. *
  49. * Description:
  50. * Sends a request to the server and dispatches replies to the provided handlers.
  51. *
  52. * The 'request_data' argument is sent as part of the request and is used by the server
  53. * to determine what to do with the request.
  54. *
  55. * When a reply is received, a new template process is created from 'reply_handler' to process the
  56. * reply. This process can access the reply data sent by the server using '_reply.data'.
  57. * Similarly, if the server finishes the request, a process is created from 'finished_handler'.
  58. * In both cases, the process can access objects as seen from the request statement via "_caller".
  59. * Termination of these processes is initiated immediately after they completes. They are created
  60. * synchronously - if a reply or a finished message arrives before a previous process is has
  61. * finished, it is queued. Once the finished message has been processed by 'finished_handler', no
  62. * more processes will be created.
  63. *
  64. * When the request statement is requested to terminate, it initiates termination of the current
  65. * handler process and waits for it to terminate (if any is running), and then dies.
  66. * If the corresponding client statement dies after being requested to die, or as a result of
  67. * an error, the request statement will not react to this. It will dispatch any pending messages
  68. * and then proceed to do nothing. In this case, if a finished message was not received, it will
  69. * not be dispatched.
  70. *
  71. * The request statement may however die at any time due to errors. In this case, it will
  72. * initiate termination of the current process and wait for it to terminate (if any) before dying.
  73. *
  74. * The request protocol and the server allow the client the abort requests at any time, and to
  75. * have the client notified only after the request has been completely aborted (i.e. the handler
  76. * process of sys.request_server() has deinitialized completely). This client implementation will
  77. * automatically request abortion of active requests when the request statement is requested
  78. * to die. However, the request statement will not wait for the abortion to finish before dying.
  79. * This means, for instance, that if you initialize a request statement right after having
  80. * deinitiazed it, the requests may overlap on the server side.
  81. */
  82. #include <stdlib.h>
  83. #include <string.h>
  84. #include <inttypes.h>
  85. #include <limits.h>
  86. #include <misc/offset.h>
  87. #include <structure/LinkedList0.h>
  88. #include <structure/LinkedList1.h>
  89. #include <system/BAddr.h>
  90. #include <ncd/NCDModule.h>
  91. #include <ncd/NCDRequestClient.h>
  92. #include <ncd/value_utils.h>
  93. #include <generated/blog_channel_ncd_sys_request_client.h>
  94. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  95. #define CSTATE_CONNECTING 1
  96. #define CSTATE_CONNECTED 2
  97. #define RRSTATE_SENDING_REQUEST 1
  98. #define RRSTATE_READY 2
  99. #define RRSTATE_GONE_BAD 3
  100. #define RRSTATE_GONE_GOOD 4
  101. #define RPSTATE_NONE 1
  102. #define RPSTATE_WORKING 2
  103. #define RPSTATE_TERMINATING 3
  104. #define RDSTATE_NONE 1
  105. #define RDSTATE_DYING 2
  106. #define RDSTATE_DYING_ERROR 3
  107. struct instance {
  108. NCDModuleInst *i;
  109. NCDRequestClient client;
  110. LinkedList0 requests_list;
  111. int state;
  112. };
  113. struct request_instance {
  114. NCDModuleInst *i;
  115. NCDValRef reply_handler;
  116. NCDValRef finished_handler;
  117. NCDValRef args;
  118. struct instance *client;
  119. NCDRequestClientRequest request;
  120. LinkedList0Node requests_list_node;
  121. LinkedList1 replies_list;
  122. NCDModuleProcess process;
  123. int process_is_finished;
  124. NCDValMem process_reply_mem;
  125. NCDValRef process_reply_data;
  126. int rstate;
  127. int pstate;
  128. int dstate;
  129. };
  130. struct reply {
  131. LinkedList1Node replies_list_node;
  132. NCDValMem mem;
  133. NCDValRef val;
  134. };
  135. static void client_handler_error (struct instance *o);
  136. static void client_handler_connected (struct instance *o);
  137. static void request_handler_sent (struct request_instance *o);
  138. static void request_handler_reply (struct request_instance *o, NCDValMem reply_mem, NCDValRef reply_value);
  139. static void request_handler_finished (struct request_instance *o, int is_error);
  140. static void request_process_handler_event (NCDModuleProcess *process, int event);
  141. static int request_process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  142. static int request_process_caller_obj_func_getobj (struct request_instance *o, NCD_string_id_t name, NCDObject *out_object);
  143. static int request_process_reply_obj_func_getvar (struct request_instance *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out);
  144. static void request_gone (struct request_instance *o, int is_bad);
  145. static void request_terminate_process (struct request_instance *o);
  146. static void request_die (struct request_instance *o, int is_error);
  147. static void request_free_reply (struct request_instance *o, struct reply *r, int have_value);
  148. static int request_init_reply_process (struct request_instance *o, NCDValMem reply_mem, NCDValSafeRef reply_data);
  149. static int request_init_finished_process (struct request_instance *o);
  150. static int get_connect_addr (struct instance *o, NCDValRef connect_addr_arg, struct NCDRequestClient_addr *out_addr);
  151. static void instance_free (struct instance *o, int with_error);
  152. static void request_instance_free (struct request_instance *o, int with_error);
  153. enum {STRING_CALLER, STRING_REPLY, STRING_DATA};
  154. static struct NCD_string_request strings[] = {
  155. {"_caller"}, {"_reply"}, {"data"}, {NULL}
  156. };
  157. static void client_handler_error (struct instance *o)
  158. {
  159. ModuleLog(o->i, BLOG_ERROR, "client error");
  160. // free instance
  161. instance_free(o, 1);
  162. }
  163. static void client_handler_connected (struct instance *o)
  164. {
  165. ASSERT(o->state == CSTATE_CONNECTING)
  166. // signal up
  167. NCDModuleInst_Backend_Up(o->i);
  168. // set state connected
  169. o->state = CSTATE_CONNECTED;
  170. }
  171. static void request_handler_sent (struct request_instance *o)
  172. {
  173. ASSERT(o->rstate == RRSTATE_SENDING_REQUEST)
  174. // signal up
  175. NCDModuleInst_Backend_Up(o->i);
  176. // set rstate ready
  177. o->rstate = RRSTATE_READY;
  178. }
  179. static void request_handler_reply (struct request_instance *o, NCDValMem reply_mem, NCDValRef reply_value)
  180. {
  181. ASSERT(o->rstate == RRSTATE_READY)
  182. // queue reply if process is running
  183. if (o->pstate != RPSTATE_NONE) {
  184. struct reply *r = malloc(sizeof(*r));
  185. if (!r) {
  186. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  187. goto fail1;
  188. }
  189. r->mem = reply_mem;
  190. r->val = NCDVal_Moved(&r->mem, reply_value);
  191. LinkedList1_Append(&o->replies_list, &r->replies_list_node);
  192. return;
  193. }
  194. // start reply process
  195. if (!request_init_reply_process(o, reply_mem, NCDVal_ToSafe(reply_value))) {
  196. goto fail1;
  197. }
  198. return;
  199. fail1:
  200. NCDValMem_Free(&reply_mem);
  201. request_die(o, 1);
  202. }
  203. static void request_handler_finished (struct request_instance *o, int is_error)
  204. {
  205. ASSERT(o->rstate == RRSTATE_SENDING_REQUEST || o->rstate == RRSTATE_READY)
  206. ASSERT(is_error || o->rstate == RRSTATE_READY)
  207. if (is_error) {
  208. ModuleLog(o->i, BLOG_ERROR, "received error reply");
  209. goto fail;
  210. }
  211. // request gone good
  212. request_gone(o, 0);
  213. // start process for reporting finished, if possible
  214. if (o->pstate == RPSTATE_NONE) {
  215. if (!request_init_finished_process(o)) {
  216. goto fail;
  217. }
  218. }
  219. return;
  220. fail:
  221. request_die(o, 1);
  222. }
  223. static void request_process_handler_event (NCDModuleProcess *process, int event)
  224. {
  225. struct request_instance *o = UPPER_OBJECT(process, struct request_instance, process);
  226. ASSERT(o->pstate != RPSTATE_NONE)
  227. switch (event) {
  228. case NCDMODULEPROCESS_EVENT_UP: {
  229. ASSERT(o->pstate == RPSTATE_WORKING)
  230. // request process termination
  231. request_terminate_process(o);
  232. } break;
  233. case NCDMODULEPROCESS_EVENT_DOWN: {
  234. ASSERT(0)
  235. } break;
  236. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  237. ASSERT(o->pstate == RPSTATE_TERMINATING)
  238. ASSERT(o->rstate != RRSTATE_SENDING_REQUEST)
  239. // free process
  240. NCDModuleProcess_Free(&o->process);
  241. // free reply data
  242. if (!o->process_is_finished) {
  243. NCDValMem_Free(&o->process_reply_mem);
  244. }
  245. // set process state none
  246. o->pstate = RPSTATE_NONE;
  247. // die finally if requested
  248. if (o->dstate == RDSTATE_DYING || o->dstate == RDSTATE_DYING_ERROR) {
  249. request_instance_free(o, o->dstate == RDSTATE_DYING_ERROR);
  250. return;
  251. }
  252. if (!LinkedList1_IsEmpty(&o->replies_list)) {
  253. // get first reply
  254. struct reply *r = UPPER_OBJECT(LinkedList1_GetFirst(&o->replies_list), struct reply, replies_list_node);
  255. // start reply process
  256. if (!request_init_reply_process(o, r->mem, NCDVal_ToSafe(r->val))) {
  257. goto fail;
  258. }
  259. // free reply
  260. request_free_reply(o, r, 0);
  261. }
  262. else if (o->rstate == RRSTATE_GONE_GOOD && !o->process_is_finished) {
  263. // start process for reporting finished
  264. if (!request_init_finished_process(o)) {
  265. goto fail;
  266. }
  267. }
  268. return;
  269. fail:
  270. request_die(o, 1);
  271. } break;
  272. }
  273. }
  274. static int request_process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  275. {
  276. struct request_instance *o = UPPER_OBJECT(process, struct request_instance, process);
  277. ASSERT(o->pstate != RPSTATE_NONE)
  278. if (name == strings[STRING_CALLER].id) {
  279. *out_object = NCDObject_Build(-1, o, NULL, (NCDObject_func_getobj)request_process_caller_obj_func_getobj);
  280. return 1;
  281. }
  282. if (!o->process_is_finished && name == strings[STRING_REPLY].id) {
  283. *out_object = NCDObject_Build(-1, o, (NCDObject_func_getvar)request_process_reply_obj_func_getvar, NULL);
  284. return 1;
  285. }
  286. return 0;
  287. }
  288. static int request_process_caller_obj_func_getobj (struct request_instance *o, NCD_string_id_t name, NCDObject *out_object)
  289. {
  290. ASSERT(o->pstate != RPSTATE_NONE)
  291. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  292. }
  293. static int request_process_reply_obj_func_getvar (struct request_instance *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  294. {
  295. ASSERT(o->pstate != RPSTATE_NONE)
  296. ASSERT(!o->process_is_finished)
  297. if (name == strings[STRING_DATA].id) {
  298. *out = NCDVal_NewCopy(mem, o->process_reply_data);
  299. if (NCDVal_IsInvalid(*out)) {
  300. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  301. }
  302. return 1;
  303. }
  304. return 0;
  305. }
  306. static void request_gone (struct request_instance *o, int is_bad)
  307. {
  308. ASSERT(o->rstate != RRSTATE_GONE_BAD)
  309. ASSERT(o->rstate != RRSTATE_GONE_GOOD)
  310. // remove from requests list
  311. LinkedList0_Remove(&o->client->requests_list, &o->requests_list_node);
  312. // free request
  313. NCDRequestClientRequest_Free(&o->request);
  314. // set state over
  315. o->rstate = (is_bad ? RRSTATE_GONE_BAD : RRSTATE_GONE_GOOD);
  316. }
  317. static void request_terminate_process (struct request_instance *o)
  318. {
  319. ASSERT(o->pstate == RPSTATE_WORKING)
  320. // request process termination
  321. NCDModuleProcess_Terminate(&o->process);
  322. // set process state terminating
  323. o->pstate = RPSTATE_TERMINATING;
  324. }
  325. static void request_die (struct request_instance *o, int is_error)
  326. {
  327. // if we have no process, die right away, else we have to wait for process to terminate
  328. if (o->pstate == RPSTATE_NONE) {
  329. request_instance_free(o, is_error);
  330. return;
  331. }
  332. // release request
  333. if (o->rstate != RRSTATE_GONE_BAD && o->rstate != RRSTATE_GONE_GOOD) {
  334. request_gone(o, 1);
  335. }
  336. // initiate process termination, if needed
  337. if (o->pstate != RPSTATE_TERMINATING) {
  338. request_terminate_process(o);
  339. }
  340. // set dstate
  341. o->dstate = (is_error ? RDSTATE_DYING_ERROR : RDSTATE_DYING);
  342. }
  343. static void request_free_reply (struct request_instance *o, struct reply *r, int have_value)
  344. {
  345. // remove from replies list
  346. LinkedList1_Remove(&o->replies_list, &r->replies_list_node);
  347. // free value
  348. if (have_value) {
  349. NCDValMem_Free(&r->mem);
  350. }
  351. // free structure
  352. free(r);
  353. }
  354. static int request_init_reply_process (struct request_instance *o, NCDValMem reply_mem, NCDValSafeRef reply_data)
  355. {
  356. ASSERT(o->pstate == RPSTATE_NONE)
  357. // set parameters
  358. o->process_is_finished = 0;
  359. o->process_reply_mem = reply_mem;
  360. o->process_reply_data = NCDVal_FromSafe(&o->process_reply_mem, reply_data);
  361. // init process
  362. if (!NCDModuleProcess_InitValue(&o->process, o->i, o->reply_handler, o->args, request_process_handler_event)) {
  363. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  364. goto fail0;
  365. }
  366. // set special objects function
  367. NCDModuleProcess_SetSpecialFuncs(&o->process, request_process_func_getspecialobj);
  368. // set process state working
  369. o->pstate = RPSTATE_WORKING;
  370. return 1;
  371. fail0:
  372. return 0;
  373. }
  374. static int request_init_finished_process (struct request_instance *o)
  375. {
  376. ASSERT(o->pstate == RPSTATE_NONE)
  377. // set parameters
  378. o->process_is_finished = 1;
  379. // init process
  380. if (!NCDModuleProcess_InitValue(&o->process, o->i, o->finished_handler, o->args, request_process_handler_event)) {
  381. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  382. goto fail0;
  383. }
  384. // set special objects function
  385. NCDModuleProcess_SetSpecialFuncs(&o->process, request_process_func_getspecialobj);
  386. // set process state working
  387. o->pstate = RPSTATE_WORKING;
  388. return 1;
  389. fail0:
  390. return 0;
  391. }
  392. static int get_connect_addr (struct instance *o, NCDValRef connect_addr_arg, struct NCDRequestClient_addr *out_addr)
  393. {
  394. if (!NCDVal_IsList(connect_addr_arg)) {
  395. goto bad;
  396. }
  397. if (NCDVal_ListCount(connect_addr_arg) < 1) {
  398. goto bad;
  399. }
  400. NCDValRef type_arg = NCDVal_ListGet(connect_addr_arg, 0);
  401. if (!NCDVal_IsStringNoNulls(type_arg)) {
  402. goto bad;
  403. }
  404. const char *type = NCDVal_StringValue(type_arg);
  405. if (!strcmp(type, "unix")) {
  406. NCDValRef socket_path_arg;
  407. if (!NCDVal_ListRead(connect_addr_arg, 2, &type_arg, &socket_path_arg)) {
  408. goto bad;
  409. }
  410. if (!NCDVal_IsStringNoNulls(socket_path_arg)) {
  411. goto bad;
  412. }
  413. *out_addr = NCDREQUESTCLIENT_UNIX_ADDR(NCDVal_StringValue(socket_path_arg));
  414. }
  415. else if (!strcmp(type, "tcp")) {
  416. NCDValRef ip_address_arg;
  417. NCDValRef port_number_arg;
  418. if (!NCDVal_ListRead(connect_addr_arg, 3, &type_arg, &ip_address_arg, &port_number_arg)) {
  419. goto bad;
  420. }
  421. if (!NCDVal_IsStringNoNulls(ip_address_arg) || !NCDVal_IsString(port_number_arg)) {
  422. goto bad;
  423. }
  424. BIPAddr ipaddr;
  425. if (!BIPAddr_Resolve(&ipaddr, (char *)NCDVal_StringValue(ip_address_arg), 1)) {
  426. goto bad;
  427. }
  428. uintmax_t port;
  429. if (!ncd_read_uintmax(port_number_arg, &port) || port > UINT16_MAX) {
  430. goto bad;
  431. }
  432. BAddr addr;
  433. BAddr_InitFromIpaddrAndPort(&addr, ipaddr, hton16(port));
  434. *out_addr = NCDREQUESTCLIENT_TCP_ADDR(addr);
  435. }
  436. else {
  437. goto bad;
  438. }
  439. return 1;
  440. bad:
  441. ModuleLog(o->i, BLOG_ERROR, "bad connect address argument");
  442. return 0;
  443. }
  444. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  445. {
  446. struct instance *o = vo;
  447. o->i = i;
  448. // check arguments
  449. NCDValRef connect_addr_arg;
  450. if (!NCDVal_ListRead(params->args, 1, &connect_addr_arg)) {
  451. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  452. goto fail0;
  453. }
  454. // get address
  455. struct NCDRequestClient_addr addr;
  456. if (!get_connect_addr(o, connect_addr_arg, &addr)) {
  457. goto fail0;
  458. }
  459. // init client
  460. if (!NCDRequestClient_Init(&o->client, addr, i->params->iparams->reactor, o,
  461. (NCDRequestClient_handler_error)client_handler_error,
  462. (NCDRequestClient_handler_connected)client_handler_connected)) {
  463. ModuleLog(o->i, BLOG_ERROR, "NCDRequestClient_Init failed");
  464. goto fail0;
  465. }
  466. // init requests list
  467. LinkedList0_Init(&o->requests_list);
  468. // set state connecting
  469. o->state = CSTATE_CONNECTING;
  470. return;
  471. fail0:
  472. NCDModuleInst_Backend_SetError(i);
  473. NCDModuleInst_Backend_Dead(i);
  474. }
  475. static void instance_free (struct instance *o, int with_error)
  476. {
  477. // deal with requests
  478. LinkedList0Node *ln;
  479. while (ln = LinkedList0_GetFirst(&o->requests_list)) {
  480. struct request_instance *req = UPPER_OBJECT(ln, struct request_instance, requests_list_node);
  481. request_gone(req, 1);
  482. }
  483. // free client
  484. NCDRequestClient_Free(&o->client);
  485. if (with_error) {
  486. NCDModuleInst_Backend_SetError(o->i);
  487. }
  488. NCDModuleInst_Backend_Dead(o->i);
  489. }
  490. static void func_die (void *vo)
  491. {
  492. struct instance *o = vo;
  493. instance_free(o, 0);
  494. }
  495. static void request_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  496. {
  497. struct request_instance *o = vo;
  498. o->i = i;
  499. // check arguments
  500. NCDValRef request_data_arg;
  501. NCDValRef reply_handler_arg;
  502. NCDValRef finished_handler_arg;
  503. NCDValRef args_arg;
  504. if (!NCDVal_ListRead(params->args, 4, &request_data_arg, &reply_handler_arg, &finished_handler_arg, &args_arg)) {
  505. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  506. goto fail0;
  507. }
  508. if (!NCDVal_IsString(reply_handler_arg) || !NCDVal_IsString(finished_handler_arg) ||
  509. !NCDVal_IsList(args_arg)
  510. ) {
  511. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  512. goto fail0;
  513. }
  514. o->reply_handler = reply_handler_arg;
  515. o->finished_handler = finished_handler_arg;
  516. o->args = args_arg;
  517. // get client
  518. struct instance *client = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  519. o->client = client;
  520. // check client state
  521. if (client->state != CSTATE_CONNECTED) {
  522. ModuleLog(o->i, BLOG_ERROR, "client is not connected");
  523. goto fail0;
  524. }
  525. // init request
  526. if (!NCDRequestClientRequest_Init(&o->request, &client->client, request_data_arg, o,
  527. (NCDRequestClientRequest_handler_sent)request_handler_sent,
  528. (NCDRequestClientRequest_handler_reply)request_handler_reply,
  529. (NCDRequestClientRequest_handler_finished)request_handler_finished)) {
  530. ModuleLog(o->i, BLOG_ERROR, "NCDRequestClientRequest_Init failed");
  531. goto fail0;
  532. }
  533. // add to requests list
  534. LinkedList0_Prepend(&client->requests_list, &o->requests_list_node);
  535. // init replies list
  536. LinkedList1_Init(&o->replies_list);
  537. // set state
  538. o->rstate = RRSTATE_SENDING_REQUEST;
  539. o->pstate = RPSTATE_NONE;
  540. o->dstate = RDSTATE_NONE;
  541. return;
  542. fail0:
  543. NCDModuleInst_Backend_SetError(i);
  544. NCDModuleInst_Backend_Dead(i);
  545. }
  546. static void request_instance_free (struct request_instance *o, int with_error)
  547. {
  548. ASSERT(o->pstate == RPSTATE_NONE)
  549. // free replies
  550. LinkedList1Node *ln;
  551. while (ln = LinkedList1_GetFirst(&o->replies_list)) {
  552. struct reply *r = UPPER_OBJECT(ln, struct reply, replies_list_node);
  553. request_free_reply(o, r, 1);
  554. }
  555. // release request
  556. if (o->rstate != RRSTATE_GONE_BAD && o->rstate != RRSTATE_GONE_GOOD) {
  557. request_gone(o, 1);
  558. }
  559. if (with_error) {
  560. NCDModuleInst_Backend_SetError(o->i);
  561. }
  562. NCDModuleInst_Backend_Dead(o->i);
  563. }
  564. static void request_func_die (void *vo)
  565. {
  566. struct request_instance *o = vo;
  567. request_die(o, 0);
  568. }
  569. static struct NCDModule modules[] = {
  570. {
  571. .type = "sys.request_client",
  572. .func_new2 = func_new,
  573. .func_die = func_die,
  574. .alloc_size = sizeof(struct instance)
  575. }, {
  576. .type = "sys.request_client::request",
  577. .func_new2 = request_func_new,
  578. .func_die = request_func_die,
  579. .alloc_size = sizeof(struct request_instance)
  580. }, {
  581. .type = NULL
  582. }
  583. };
  584. const struct NCDModuleGroup ncdmodule_sys_request_client = {
  585. .modules = modules,
  586. .strings = strings
  587. };