sys_request_client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 <misc/parse_number.h>
  88. #include <structure/LinkedList0.h>
  89. #include <structure/LinkedList1.h>
  90. #include <system/BAddr.h>
  91. #include <ncd/NCDModule.h>
  92. #include <ncd/NCDRequestClient.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. char *reply_handler;
  116. char *finished_handler;
  117. NCDValue *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. NCDValue process_reply_data;
  125. int rstate;
  126. int pstate;
  127. int dstate;
  128. };
  129. struct reply {
  130. LinkedList1Node replies_list_node;
  131. NCDValue val;
  132. };
  133. static void client_handler_error (struct instance *o);
  134. static void client_handler_connected (struct instance *o);
  135. static void request_handler_sent (struct request_instance *o);
  136. static void request_handler_reply (struct request_instance *o, NCDValue reply_data);
  137. static void request_handler_finished (struct request_instance *o, int is_error);
  138. static void request_process_handler_event (struct request_instance *o, int event);
  139. static int request_process_func_getspecialobj (struct request_instance *o, const char *name, NCDObject *out_object);
  140. static int request_process_caller_obj_func_getobj (struct request_instance *o, const char *name, NCDObject *out_object);
  141. static int request_process_reply_obj_func_getvar (struct request_instance *o, const char *name, NCDValue *out_value);
  142. static void request_gone (struct request_instance *o, int is_bad);
  143. static void request_terminate_process (struct request_instance *o);
  144. static void request_die (struct request_instance *o, int is_error);
  145. static void request_free_reply (struct request_instance *o, struct reply *r, int have_value);
  146. static int request_init_reply_process (struct request_instance *o, NCDValue reply_data);
  147. static int request_init_finished_process (struct request_instance *o);
  148. static int get_connect_addr (struct instance *o, NCDValue *connect_addr_arg, struct NCDRequestClient_addr *out_addr);
  149. static void instance_free (struct instance *o, int with_error);
  150. static void request_instance_free (struct request_instance *o, int with_error);
  151. static void client_handler_error (struct instance *o)
  152. {
  153. ModuleLog(o->i, BLOG_ERROR, "client error");
  154. // free instance
  155. instance_free(o, 1);
  156. }
  157. static void client_handler_connected (struct instance *o)
  158. {
  159. ASSERT(o->state == CSTATE_CONNECTING)
  160. // signal up
  161. NCDModuleInst_Backend_Up(o->i);
  162. // set state connected
  163. o->state = CSTATE_CONNECTED;
  164. }
  165. static void request_handler_sent (struct request_instance *o)
  166. {
  167. ASSERT(o->rstate == RRSTATE_SENDING_REQUEST)
  168. // signal up
  169. NCDModuleInst_Backend_Up(o->i);
  170. // set rstate ready
  171. o->rstate = RRSTATE_READY;
  172. }
  173. static void request_handler_reply (struct request_instance *o, NCDValue reply_data)
  174. {
  175. ASSERT(o->rstate == RRSTATE_READY)
  176. // queue reply if process is running
  177. if (o->pstate != RPSTATE_NONE) {
  178. struct reply *r = malloc(sizeof(*r));
  179. if (!r) {
  180. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  181. goto fail;
  182. }
  183. r->val = reply_data;
  184. LinkedList1_Append(&o->replies_list, &r->replies_list_node);
  185. return;
  186. }
  187. // start reply process
  188. if (!request_init_reply_process(o, reply_data)) {
  189. goto fail;
  190. }
  191. return;
  192. fail:
  193. NCDValue_Free(&reply_data);
  194. request_die(o, 1);
  195. }
  196. static void request_handler_finished (struct request_instance *o, int is_error)
  197. {
  198. ASSERT(o->rstate == RRSTATE_SENDING_REQUEST || o->rstate == RRSTATE_READY)
  199. ASSERT(is_error || o->rstate == RRSTATE_READY)
  200. if (is_error) {
  201. ModuleLog(o->i, BLOG_ERROR, "received error reply");
  202. goto fail;
  203. }
  204. // request gone good
  205. request_gone(o, 0);
  206. // start process for reporting finished, if possible
  207. if (o->pstate == RPSTATE_NONE) {
  208. if (!request_init_finished_process(o)) {
  209. goto fail;
  210. }
  211. }
  212. return;
  213. fail:
  214. request_die(o, 1);
  215. }
  216. static void request_process_handler_event (struct request_instance *o, int event)
  217. {
  218. ASSERT(o->pstate != RPSTATE_NONE)
  219. switch (event) {
  220. case NCDMODULEPROCESS_EVENT_UP: {
  221. ASSERT(o->pstate == RPSTATE_WORKING)
  222. // request process termination
  223. request_terminate_process(o);
  224. } break;
  225. case NCDMODULEPROCESS_EVENT_DOWN: {
  226. ASSERT(0)
  227. } break;
  228. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  229. ASSERT(o->pstate == RPSTATE_TERMINATING)
  230. ASSERT(o->rstate != RRSTATE_SENDING_REQUEST)
  231. // free process
  232. NCDModuleProcess_Free(&o->process);
  233. // free reply data
  234. if (!o->process_is_finished) {
  235. NCDValue_Free(&o->process_reply_data);
  236. }
  237. // set process state none
  238. o->pstate = RPSTATE_NONE;
  239. // die finally if requested
  240. if (o->dstate == RDSTATE_DYING || o->dstate == RDSTATE_DYING_ERROR) {
  241. request_instance_free(o, o->dstate == RDSTATE_DYING_ERROR);
  242. return;
  243. }
  244. if (!LinkedList1_IsEmpty(&o->replies_list)) {
  245. // get first reply
  246. struct reply *r = UPPER_OBJECT(LinkedList1_GetFirst(&o->replies_list), struct reply, replies_list_node);
  247. // start reply process
  248. if (!request_init_reply_process(o, r->val)) {
  249. goto fail;
  250. }
  251. // free reply
  252. request_free_reply(o, r, 0);
  253. }
  254. else if (o->rstate == RRSTATE_GONE_GOOD && !o->process_is_finished) {
  255. // start process for reporting finished
  256. if (!request_init_finished_process(o)) {
  257. goto fail;
  258. }
  259. }
  260. return;
  261. fail:
  262. request_die(o, 1);
  263. } break;
  264. }
  265. }
  266. static int request_process_func_getspecialobj (struct request_instance *o, const char *name, NCDObject *out_object)
  267. {
  268. ASSERT(o->pstate != RPSTATE_NONE)
  269. if (!strcmp(name, "_caller")) {
  270. *out_object = NCDObject_Build(NULL, o, NULL, (NCDObject_func_getobj)request_process_caller_obj_func_getobj);
  271. return 1;
  272. }
  273. if (!o->process_is_finished && !strcmp(name, "_reply")) {
  274. *out_object = NCDObject_Build(NULL, o, (NCDObject_func_getvar)request_process_reply_obj_func_getvar, NULL);
  275. return 1;
  276. }
  277. return 0;
  278. }
  279. static int request_process_caller_obj_func_getobj (struct request_instance *o, const char *name, NCDObject *out_object)
  280. {
  281. ASSERT(o->pstate != RPSTATE_NONE)
  282. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  283. }
  284. static int request_process_reply_obj_func_getvar (struct request_instance *o, const char *name, NCDValue *out_value)
  285. {
  286. ASSERT(o->pstate != RPSTATE_NONE)
  287. ASSERT(!o->process_is_finished)
  288. if (!strcmp(name, "data")) {
  289. if (!NCDValue_InitCopy(out_value, &o->process_reply_data)) {
  290. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  291. return 0;
  292. }
  293. return 1;
  294. }
  295. return 0;
  296. }
  297. static void request_gone (struct request_instance *o, int is_bad)
  298. {
  299. ASSERT(o->rstate != RRSTATE_GONE_BAD)
  300. ASSERT(o->rstate != RRSTATE_GONE_GOOD)
  301. // remove from requests list
  302. LinkedList0_Remove(&o->client->requests_list, &o->requests_list_node);
  303. // free request
  304. NCDRequestClientRequest_Free(&o->request);
  305. // set state over
  306. o->rstate = (is_bad ? RRSTATE_GONE_BAD : RRSTATE_GONE_GOOD);
  307. }
  308. static void request_terminate_process (struct request_instance *o)
  309. {
  310. ASSERT(o->pstate == RPSTATE_WORKING)
  311. // request process termination
  312. NCDModuleProcess_Terminate(&o->process);
  313. // set process state terminating
  314. o->pstate = RPSTATE_TERMINATING;
  315. }
  316. static void request_die (struct request_instance *o, int is_error)
  317. {
  318. // if we have no process, die right away, else we have to wait for process to terminate
  319. if (o->pstate == RPSTATE_NONE) {
  320. request_instance_free(o, is_error);
  321. return;
  322. }
  323. // release request
  324. if (o->rstate != RRSTATE_GONE_BAD && o->rstate != RRSTATE_GONE_GOOD) {
  325. request_gone(o, 1);
  326. }
  327. // initiate process termination, if needed
  328. if (o->pstate != RPSTATE_TERMINATING) {
  329. request_terminate_process(o);
  330. }
  331. // set dstate
  332. o->dstate = (is_error ? RDSTATE_DYING_ERROR : RDSTATE_DYING);
  333. }
  334. static void request_free_reply (struct request_instance *o, struct reply *r, int have_value)
  335. {
  336. // remove from replies list
  337. LinkedList1_Remove(&o->replies_list, &r->replies_list_node);
  338. // free value
  339. if (have_value) {
  340. NCDValue_Free(&r->val);
  341. }
  342. // free structure
  343. free(r);
  344. }
  345. static int request_init_reply_process (struct request_instance *o, NCDValue reply_data)
  346. {
  347. ASSERT(o->pstate == RPSTATE_NONE)
  348. // set parameters
  349. o->process_is_finished = 0;
  350. o->process_reply_data = reply_data;
  351. // copy arguments
  352. NCDValue args;
  353. if (!NCDValue_InitCopy(&args, o->args)) {
  354. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  355. goto fail0;
  356. }
  357. // init process
  358. if (!NCDModuleProcess_Init(&o->process, o->i, o->reply_handler, args, o, (NCDModuleProcess_handler_event)request_process_handler_event)) {
  359. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  360. NCDValue_Free(&args);
  361. goto fail0;
  362. }
  363. // set special objects function
  364. NCDModuleProcess_SetSpecialFuncs(&o->process, (NCDModuleProcess_func_getspecialobj)request_process_func_getspecialobj);
  365. // set process state working
  366. o->pstate = RPSTATE_WORKING;
  367. return 1;
  368. fail0:
  369. return 0;
  370. }
  371. static int request_init_finished_process (struct request_instance *o)
  372. {
  373. ASSERT(o->pstate == RPSTATE_NONE)
  374. // set parameters
  375. o->process_is_finished = 1;
  376. // copy arguments
  377. NCDValue args;
  378. if (!NCDValue_InitCopy(&args, o->args)) {
  379. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  380. goto fail0;
  381. }
  382. // init process
  383. if (!NCDModuleProcess_Init(&o->process, o->i, o->finished_handler, args, o, (NCDModuleProcess_handler_event)request_process_handler_event)) {
  384. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  385. NCDValue_Free(&args);
  386. goto fail0;
  387. }
  388. // set special objects function
  389. NCDModuleProcess_SetSpecialFuncs(&o->process, (NCDModuleProcess_func_getspecialobj)request_process_func_getspecialobj);
  390. // set process state working
  391. o->pstate = RPSTATE_WORKING;
  392. return 1;
  393. fail0:
  394. return 0;
  395. }
  396. static int get_connect_addr (struct instance *o, NCDValue *connect_addr_arg, struct NCDRequestClient_addr *out_addr)
  397. {
  398. if (NCDValue_Type(connect_addr_arg) != NCDVALUE_LIST) {
  399. goto bad;
  400. }
  401. if (NCDValue_ListCount(connect_addr_arg) < 1) {
  402. goto bad;
  403. }
  404. NCDValue *type_arg = NCDValue_ListFirst(connect_addr_arg);
  405. if (!NCDValue_IsStringNoNulls(type_arg)) {
  406. goto bad;
  407. }
  408. const char *type = NCDValue_StringValue(type_arg);
  409. if (!strcmp(type, "unix")) {
  410. NCDValue *socket_path_arg;
  411. if (!NCDValue_ListRead(connect_addr_arg, 2, &type_arg, &socket_path_arg)) {
  412. goto bad;
  413. }
  414. if (!NCDValue_IsStringNoNulls(socket_path_arg)) {
  415. goto bad;
  416. }
  417. *out_addr = NCDREQUESTCLIENT_UNIX_ADDR(NCDValue_StringValue(socket_path_arg));
  418. }
  419. else if (!strcmp(type, "tcp")) {
  420. NCDValue *ip_address_arg;
  421. NCDValue *port_number_arg;
  422. if (!NCDValue_ListRead(connect_addr_arg, 3, &type_arg, &ip_address_arg, &port_number_arg)) {
  423. goto bad;
  424. }
  425. if (!NCDValue_IsStringNoNulls(ip_address_arg) || !NCDValue_IsStringNoNulls(port_number_arg)) {
  426. goto bad;
  427. }
  428. BIPAddr ipaddr;
  429. if (!BIPAddr_Resolve(&ipaddr, NCDValue_StringValue(ip_address_arg), 1)) {
  430. goto bad;
  431. }
  432. uintmax_t port;
  433. if (!parse_unsigned_integer(NCDValue_StringValue(port_number_arg), &port) || port > UINT16_MAX) {
  434. goto bad;
  435. }
  436. BAddr addr;
  437. BAddr_InitFromIpaddrAndPort(&addr, ipaddr, hton16(port));
  438. *out_addr = NCDREQUESTCLIENT_TCP_ADDR(addr);
  439. }
  440. else {
  441. goto bad;
  442. }
  443. return 1;
  444. bad:
  445. ModuleLog(o->i, BLOG_ERROR, "bad connect address argument");
  446. return 0;
  447. }
  448. static void func_new (NCDModuleInst *i)
  449. {
  450. // allocate structure
  451. struct instance *o = malloc(sizeof(*o));
  452. if (!o) {
  453. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  454. goto fail0;
  455. }
  456. o->i = i;
  457. NCDModuleInst_Backend_SetUser(i, o);
  458. // check arguments
  459. NCDValue *connect_addr_arg;
  460. if (!NCDValue_ListRead(i->args, 1, &connect_addr_arg)) {
  461. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  462. goto fail1;
  463. }
  464. // get address
  465. struct NCDRequestClient_addr addr;
  466. if (!get_connect_addr(o, connect_addr_arg, &addr)) {
  467. goto fail1;
  468. }
  469. // init client
  470. if (!NCDRequestClient_Init(&o->client, addr, i->iparams->reactor, o,
  471. (NCDRequestClient_handler_error)client_handler_error,
  472. (NCDRequestClient_handler_connected)client_handler_connected)) {
  473. ModuleLog(o->i, BLOG_ERROR, "NCDRequestClient_Init failed");
  474. goto fail1;
  475. }
  476. // init requests list
  477. LinkedList0_Init(&o->requests_list);
  478. // set state connecting
  479. o->state = CSTATE_CONNECTING;
  480. return;
  481. fail1:
  482. free(o);
  483. fail0:
  484. NCDModuleInst_Backend_SetError(i);
  485. NCDModuleInst_Backend_Dead(i);
  486. }
  487. static void instance_free (struct instance *o, int with_error)
  488. {
  489. NCDModuleInst *i = o->i;
  490. // deal with requests
  491. LinkedList0Node *ln;
  492. while (ln = LinkedList0_GetFirst(&o->requests_list)) {
  493. struct request_instance *req = UPPER_OBJECT(ln, struct request_instance, requests_list_node);
  494. request_gone(req, 1);
  495. }
  496. // free client
  497. NCDRequestClient_Free(&o->client);
  498. // free structure
  499. free(o);
  500. if (with_error) {
  501. NCDModuleInst_Backend_SetError(i);
  502. }
  503. NCDModuleInst_Backend_Dead(i);
  504. }
  505. static void func_die (void *vo)
  506. {
  507. struct instance *o = vo;
  508. instance_free(o, 0);
  509. }
  510. static void request_func_new (NCDModuleInst *i)
  511. {
  512. // allocate structure
  513. struct request_instance *o = malloc(sizeof(*o));
  514. if (!o) {
  515. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  516. goto fail0;
  517. }
  518. o->i = i;
  519. NCDModuleInst_Backend_SetUser(i, o);
  520. // check arguments
  521. NCDValue *request_data_arg;
  522. NCDValue *reply_handler_arg;
  523. NCDValue *finished_handler_arg;
  524. NCDValue *args_arg;
  525. if (!NCDValue_ListRead(i->args, 4, &request_data_arg, &reply_handler_arg, &finished_handler_arg, &args_arg)) {
  526. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  527. goto fail1;
  528. }
  529. if (!NCDValue_IsStringNoNulls(reply_handler_arg) || !NCDValue_IsStringNoNulls(finished_handler_arg) ||
  530. NCDValue_Type(args_arg) != NCDVALUE_LIST
  531. ) {
  532. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  533. goto fail1;
  534. }
  535. o->reply_handler = NCDValue_StringValue(reply_handler_arg);
  536. o->finished_handler = NCDValue_StringValue(finished_handler_arg);
  537. o->args = args_arg;
  538. // get client
  539. struct instance *client = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  540. o->client = client;
  541. // check client state
  542. if (client->state != CSTATE_CONNECTED) {
  543. ModuleLog(o->i, BLOG_ERROR, "client is not connected");
  544. goto fail1;
  545. }
  546. // init request
  547. if (!NCDRequestClientRequest_Init(&o->request, &client->client, request_data_arg, o,
  548. (NCDRequestClientRequest_handler_sent)request_handler_sent,
  549. (NCDRequestClientRequest_handler_reply)request_handler_reply,
  550. (NCDRequestClientRequest_handler_finished)request_handler_finished)) {
  551. ModuleLog(o->i, BLOG_ERROR, "NCDRequestClientRequest_Init failed");
  552. goto fail1;
  553. }
  554. // add to requests list
  555. LinkedList0_Prepend(&client->requests_list, &o->requests_list_node);
  556. // init replies list
  557. LinkedList1_Init(&o->replies_list);
  558. // set state
  559. o->rstate = RRSTATE_SENDING_REQUEST;
  560. o->pstate = RPSTATE_NONE;
  561. o->dstate = RDSTATE_NONE;
  562. return;
  563. fail1:
  564. free(o);
  565. fail0:
  566. NCDModuleInst_Backend_SetError(i);
  567. NCDModuleInst_Backend_Dead(i);
  568. }
  569. static void request_instance_free (struct request_instance *o, int with_error)
  570. {
  571. ASSERT(o->pstate == RPSTATE_NONE)
  572. NCDModuleInst *i = o->i;
  573. // free replies
  574. LinkedList1Node *ln;
  575. while (ln = LinkedList1_GetFirst(&o->replies_list)) {
  576. struct reply *r = UPPER_OBJECT(ln, struct reply, replies_list_node);
  577. request_free_reply(o, r, 1);
  578. }
  579. // release request
  580. if (o->rstate != RRSTATE_GONE_BAD && o->rstate != RRSTATE_GONE_GOOD) {
  581. request_gone(o, 1);
  582. }
  583. // free structure
  584. free(o);
  585. if (with_error) {
  586. NCDModuleInst_Backend_SetError(i);
  587. }
  588. NCDModuleInst_Backend_Dead(i);
  589. }
  590. static void request_func_die (void *vo)
  591. {
  592. struct request_instance *o = vo;
  593. request_die(o, 0);
  594. }
  595. static const struct NCDModule modules[] = {
  596. {
  597. .type = "sys.request_client",
  598. .func_new = func_new,
  599. .func_die = func_die
  600. }, {
  601. .type = "sys.request_client::request",
  602. .func_new = request_func_new,
  603. .func_die = request_func_die
  604. }, {
  605. .type = NULL
  606. }
  607. };
  608. const struct NCDModuleGroup ncdmodule_sys_request_client = {
  609. .modules = modules
  610. };