call2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /**
  2. * @file call2.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. * call2(string template, list args)
  33. * call2_if(string cond, string template, list args)
  34. * call2_ifelse(string cond, string template, string else_template, list args)
  35. * embcall2(string template)
  36. * embcall2_if(string cond, string template)
  37. * embcall2_ifelse(string cond, string template, string else_template)
  38. * embcall2_multif(string cond1, string template1, ..., [string else_template])
  39. */
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <misc/debug.h>
  43. #include <ncd/NCDModule.h>
  44. #include <generated/blog_channel_ncd_call2.h>
  45. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  46. #define STATE_WORKING 1
  47. #define STATE_UP 2
  48. #define STATE_WAITING 3
  49. #define STATE_TERMINATING 4
  50. #define STATE_NONE 5
  51. struct instance {
  52. NCDModuleInst *i;
  53. NCDModuleProcess process;
  54. int embed;
  55. int state;
  56. };
  57. static void process_handler_event (struct instance *o, int event);
  58. static int process_func_getspecialobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
  59. static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object);
  60. static void func_new_templ (void *vo, NCDModuleInst *i, const char *template_name, NCDValRef args, int embed);
  61. static void instance_free (struct instance *o);
  62. enum {STRING_CALLER};
  63. static struct NCD_string_request strings[] = {
  64. {"_caller"}, {NULL}
  65. };
  66. static void process_handler_event (struct instance *o, int event)
  67. {
  68. switch (event) {
  69. case NCDMODULEPROCESS_EVENT_UP: {
  70. ASSERT(o->state == STATE_WORKING)
  71. // signal up
  72. NCDModuleInst_Backend_Up(o->i);
  73. // set state up
  74. o->state = STATE_UP;
  75. } break;
  76. case NCDMODULEPROCESS_EVENT_DOWN: {
  77. ASSERT(o->state == STATE_UP)
  78. // signal down
  79. NCDModuleInst_Backend_Down(o->i);
  80. // set state waiting
  81. o->state = STATE_WAITING;
  82. } break;
  83. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  84. ASSERT(o->state == STATE_TERMINATING)
  85. // die finally
  86. instance_free(o);
  87. return;
  88. } break;
  89. default: ASSERT(0);
  90. }
  91. }
  92. static int process_func_getspecialobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  93. {
  94. if (o->embed) {
  95. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  96. }
  97. if (name == strings[STRING_CALLER].id) {
  98. *out_object = NCDObject_Build(NULL, o, NULL, (NCDObject_func_getobj)caller_obj_func_getobj);
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. static int caller_obj_func_getobj (struct instance *o, NCD_string_id_t name, NCDObject *out_object)
  104. {
  105. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  106. }
  107. static void func_new_templ (void *vo, NCDModuleInst *i, const char *template_name, NCDValRef args, int embed)
  108. {
  109. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  110. ASSERT(embed == !!embed)
  111. struct instance *o = vo;
  112. o->i = i;
  113. // remember embed
  114. o->embed = embed;
  115. if (!template_name || !strcmp(template_name, "<none>")) {
  116. // signal up
  117. NCDModuleInst_Backend_Up(o->i);
  118. // set state none
  119. o->state = STATE_NONE;
  120. } else {
  121. // create process
  122. if (!NCDModuleProcess_Init(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  123. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  124. goto fail0;
  125. }
  126. // set special functions
  127. NCDModuleProcess_SetSpecialFuncs(&o->process, (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  128. // set state working
  129. o->state = STATE_WORKING;
  130. }
  131. return;
  132. fail0:
  133. NCDModuleInst_Backend_SetError(i);
  134. NCDModuleInst_Backend_Dead(i);
  135. }
  136. static void instance_free (struct instance *o)
  137. {
  138. // free process
  139. if (o->state != STATE_NONE) {
  140. NCDModuleProcess_Free(&o->process);
  141. }
  142. NCDModuleInst_Backend_Dead(o->i);
  143. }
  144. static void func_new_call (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  145. {
  146. NCDValRef template_arg;
  147. NCDValRef args_arg;
  148. if (!NCDVal_ListRead(params->args, 2, &template_arg, &args_arg)) {
  149. ModuleLog(i, BLOG_ERROR, "wrong arity");
  150. goto fail0;
  151. }
  152. if (!NCDVal_IsStringNoNulls(template_arg) || !NCDVal_IsList(args_arg)) {
  153. ModuleLog(i, BLOG_ERROR, "wrong type");
  154. goto fail0;
  155. }
  156. func_new_templ(vo, i, NCDVal_StringValue(template_arg), args_arg, 0);
  157. return;
  158. fail0:
  159. NCDModuleInst_Backend_SetError(i);
  160. NCDModuleInst_Backend_Dead(i);
  161. }
  162. static void func_new_embcall (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  163. {
  164. NCDValRef template_arg;
  165. if (!NCDVal_ListRead(params->args, 1, &template_arg)) {
  166. ModuleLog(i, BLOG_ERROR, "wrong arity");
  167. goto fail0;
  168. }
  169. if (!NCDVal_IsStringNoNulls(template_arg)) {
  170. ModuleLog(i, BLOG_ERROR, "wrong type");
  171. goto fail0;
  172. }
  173. func_new_templ(vo, i, NCDVal_StringValue(template_arg), NCDVal_NewInvalid(), 1);
  174. return;
  175. fail0:
  176. NCDModuleInst_Backend_SetError(i);
  177. NCDModuleInst_Backend_Dead(i);
  178. }
  179. static void func_new_call_if (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  180. {
  181. NCDValRef cond_arg;
  182. NCDValRef template_arg;
  183. NCDValRef args_arg;
  184. if (!NCDVal_ListRead(params->args, 3, &cond_arg, &template_arg, &args_arg)) {
  185. ModuleLog(i, BLOG_ERROR, "wrong arity");
  186. goto fail0;
  187. }
  188. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsStringNoNulls(template_arg) || !NCDVal_IsList(args_arg)) {
  189. ModuleLog(i, BLOG_ERROR, "wrong type");
  190. goto fail0;
  191. }
  192. const char *template_name = NULL;
  193. if (NCDVal_StringEquals(cond_arg, "true")) {
  194. template_name = NCDVal_StringValue(template_arg);
  195. }
  196. func_new_templ(vo, i, template_name, args_arg, 0);
  197. return;
  198. fail0:
  199. NCDModuleInst_Backend_SetError(i);
  200. NCDModuleInst_Backend_Dead(i);
  201. }
  202. static void func_new_embcall_if (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  203. {
  204. NCDValRef cond_arg;
  205. NCDValRef template_arg;
  206. if (!NCDVal_ListRead(params->args, 2, &cond_arg, &template_arg)) {
  207. ModuleLog(i, BLOG_ERROR, "wrong arity");
  208. goto fail0;
  209. }
  210. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsStringNoNulls(template_arg)) {
  211. ModuleLog(i, BLOG_ERROR, "wrong type");
  212. goto fail0;
  213. }
  214. const char *template_name = NULL;
  215. if (NCDVal_StringEquals(cond_arg, "true")) {
  216. template_name = NCDVal_StringValue(template_arg);
  217. }
  218. func_new_templ(vo, i, template_name, NCDVal_NewInvalid(), 1);
  219. return;
  220. fail0:
  221. NCDModuleInst_Backend_SetError(i);
  222. NCDModuleInst_Backend_Dead(i);
  223. }
  224. static void func_new_call_ifelse (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  225. {
  226. NCDValRef cond_arg;
  227. NCDValRef template_arg;
  228. NCDValRef else_template_arg;
  229. NCDValRef args_arg;
  230. if (!NCDVal_ListRead(params->args, 4, &cond_arg, &template_arg, &else_template_arg, &args_arg)) {
  231. ModuleLog(i, BLOG_ERROR, "wrong arity");
  232. goto fail0;
  233. }
  234. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsStringNoNulls(template_arg) || !NCDVal_IsStringNoNulls(else_template_arg) || !NCDVal_IsList(args_arg)) {
  235. ModuleLog(i, BLOG_ERROR, "wrong type");
  236. goto fail0;
  237. }
  238. const char *template_name;
  239. if (NCDVal_StringEquals(cond_arg, "true")) {
  240. template_name = NCDVal_StringValue(template_arg);
  241. } else {
  242. template_name = NCDVal_StringValue(else_template_arg);
  243. }
  244. func_new_templ(vo, i, template_name, args_arg, 0);
  245. return;
  246. fail0:
  247. NCDModuleInst_Backend_SetError(i);
  248. NCDModuleInst_Backend_Dead(i);
  249. }
  250. static void func_new_embcall_ifelse (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  251. {
  252. NCDValRef cond_arg;
  253. NCDValRef template_arg;
  254. NCDValRef else_template_arg;
  255. if (!NCDVal_ListRead(params->args, 3, &cond_arg, &template_arg, &else_template_arg)) {
  256. ModuleLog(i, BLOG_ERROR, "wrong arity");
  257. goto fail0;
  258. }
  259. if (!NCDVal_IsString(cond_arg) || !NCDVal_IsStringNoNulls(template_arg) || !NCDVal_IsStringNoNulls(else_template_arg)) {
  260. ModuleLog(i, BLOG_ERROR, "wrong type");
  261. goto fail0;
  262. }
  263. const char *template_name;
  264. if (NCDVal_StringEquals(cond_arg, "true")) {
  265. template_name = NCDVal_StringValue(template_arg);
  266. } else {
  267. template_name = NCDVal_StringValue(else_template_arg);
  268. }
  269. func_new_templ(vo, i, template_name, NCDVal_NewInvalid(), 1);
  270. return;
  271. fail0:
  272. NCDModuleInst_Backend_SetError(i);
  273. NCDModuleInst_Backend_Dead(i);
  274. }
  275. static void func_new_embcall_multif (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  276. {
  277. const char *template_name = NULL;
  278. size_t count = NCDVal_ListCount(params->args);
  279. size_t j = 0;
  280. while (j < count) {
  281. NCDValRef arg = NCDVal_ListGet(params->args, j);
  282. if (j == count - 1) {
  283. if (!NCDVal_IsStringNoNulls(arg)) {
  284. ModuleLog(i, BLOG_ERROR, "bad arguments");
  285. goto fail0;
  286. }
  287. template_name = NCDVal_StringValue(arg);
  288. break;
  289. }
  290. NCDValRef arg2 = NCDVal_ListGet(params->args, j + 1);
  291. if (!NCDVal_IsString(arg) || !NCDVal_IsStringNoNulls(arg2)) {
  292. ModuleLog(i, BLOG_ERROR, "bad arguments");
  293. goto fail0;
  294. }
  295. if (NCDVal_StringEquals(arg, "true")) {
  296. template_name = NCDVal_StringValue(arg2);
  297. break;
  298. }
  299. j += 2;
  300. }
  301. func_new_templ(vo, i, template_name, NCDVal_NewInvalid(), 1);
  302. return;
  303. fail0:
  304. NCDModuleInst_Backend_SetError(i);
  305. NCDModuleInst_Backend_Dead(i);
  306. }
  307. static void func_die (void *vo)
  308. {
  309. struct instance *o = vo;
  310. ASSERT(o->state != STATE_TERMINATING)
  311. // if none, die now
  312. if (o->state == STATE_NONE) {
  313. instance_free(o);
  314. return;
  315. }
  316. // request process to terminate
  317. NCDModuleProcess_Terminate(&o->process);
  318. // set state terminating
  319. o->state = STATE_TERMINATING;
  320. }
  321. static void func_clean (void *vo)
  322. {
  323. struct instance *o = vo;
  324. if (o->state != STATE_WAITING) {
  325. return;
  326. }
  327. // allow process to continue
  328. NCDModuleProcess_Continue(&o->process);
  329. // set state working
  330. o->state = STATE_WORKING;
  331. }
  332. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  333. {
  334. struct instance *o = vo;
  335. if (o->state == STATE_NONE) {
  336. return 0;
  337. }
  338. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  339. }
  340. static const struct NCDModule modules[] = {
  341. {
  342. .type = "call2",
  343. .func_new2 = func_new_call,
  344. .func_die = func_die,
  345. .func_clean = func_clean,
  346. .func_getobj = func_getobj,
  347. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  348. .alloc_size = sizeof(struct instance)
  349. }, {
  350. .type = "call2_if",
  351. .func_new2 = func_new_call_if,
  352. .func_die = func_die,
  353. .func_clean = func_clean,
  354. .func_getobj = func_getobj,
  355. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  356. .alloc_size = sizeof(struct instance)
  357. }, {
  358. .type = "call2_ifelse",
  359. .func_new2 = func_new_call_ifelse,
  360. .func_die = func_die,
  361. .func_clean = func_clean,
  362. .func_getobj = func_getobj,
  363. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  364. .alloc_size = sizeof(struct instance)
  365. }, {
  366. .type = "embcall2",
  367. .func_new2 = func_new_embcall,
  368. .func_die = func_die,
  369. .func_clean = func_clean,
  370. .func_getobj = func_getobj,
  371. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  372. .alloc_size = sizeof(struct instance)
  373. }, {
  374. .type = "embcall2_if",
  375. .func_new2 = func_new_embcall_if,
  376. .func_die = func_die,
  377. .func_clean = func_clean,
  378. .func_getobj = func_getobj,
  379. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  380. .alloc_size = sizeof(struct instance)
  381. }, {
  382. .type = "embcall2_ifelse",
  383. .func_new2 = func_new_embcall_ifelse,
  384. .func_die = func_die,
  385. .func_clean = func_clean,
  386. .func_getobj = func_getobj,
  387. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  388. .alloc_size = sizeof(struct instance)
  389. }, {
  390. .type = "embcall2_multif",
  391. .func_new2 = func_new_embcall_multif,
  392. .func_die = func_die,
  393. .func_clean = func_clean,
  394. .func_getobj = func_getobj,
  395. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  396. .alloc_size = sizeof(struct instance)
  397. }, {
  398. .type = NULL
  399. }
  400. };
  401. struct NCDModuleGroup ncdmodule_call2 = {
  402. .modules = modules,
  403. .strings = strings
  404. };