call2.c 14 KB

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