call2.c 14 KB

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