call2.c 14 KB

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