call2.c 14 KB

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