call2.c 13 KB

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