call2.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. * call(string template, list args)
  33. * embcall2_multif(string cond1, string template1, ..., [string else_template])
  34. */
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <misc/debug.h>
  38. #include <misc/offset.h>
  39. #include <ncd/NCDModule.h>
  40. #include <ncd/static_strings.h>
  41. #include <ncd/extra/value_utils.h>
  42. #include <generated/blog_channel_ncd_call2.h>
  43. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  44. #define STATE_WORKING 1
  45. #define STATE_UP 2
  46. #define STATE_WAITING 3
  47. #define STATE_TERMINATING 4
  48. #define STATE_NONE 5
  49. struct instance {
  50. NCDModuleInst *i;
  51. NCDModuleProcess process;
  52. int embed;
  53. int state;
  54. };
  55. static void process_handler_event (NCDModuleProcess *process, int event);
  56. static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  57. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object);
  58. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed);
  59. static void instance_free (struct instance *o);
  60. enum {STRING_CALLER};
  61. static struct NCD_string_request strings[] = {
  62. {"_caller"}, {NULL}
  63. };
  64. static void process_handler_event (NCDModuleProcess *process, int event)
  65. {
  66. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  67. switch (event) {
  68. case NCDMODULEPROCESS_EVENT_UP: {
  69. ASSERT(o->state == STATE_WORKING)
  70. // signal up
  71. NCDModuleInst_Backend_Up(o->i);
  72. // set state up
  73. o->state = STATE_UP;
  74. } break;
  75. case NCDMODULEPROCESS_EVENT_DOWN: {
  76. ASSERT(o->state == STATE_UP)
  77. // signal down
  78. NCDModuleInst_Backend_Down(o->i);
  79. // set state waiting
  80. o->state = STATE_WAITING;
  81. } break;
  82. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  83. ASSERT(o->state == STATE_TERMINATING)
  84. // die finally
  85. instance_free(o);
  86. return;
  87. } break;
  88. default: ASSERT(0);
  89. }
  90. }
  91. static int process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  92. {
  93. struct instance *o = UPPER_OBJECT(process, struct instance, process);
  94. if (o->embed) {
  95. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  96. }
  97. if (name == strings[STRING_CALLER].id) {
  98. *out_object = NCDObject_Build(-1, o, NCDObject_no_getvar, caller_obj_func_getobj);
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. static int caller_obj_func_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
  104. {
  105. struct instance *o = NCDObject_DataPtr(obj);
  106. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  107. }
  108. static void func_new_templ (void *vo, NCDModuleInst *i, NCDValRef template_name, NCDValRef args, int embed)
  109. {
  110. ASSERT(NCDVal_IsInvalid(template_name) || NCDVal_IsString(template_name))
  111. ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
  112. ASSERT(embed == !!embed)
  113. struct instance *o = vo;
  114. o->i = i;
  115. // remember embed
  116. o->embed = embed;
  117. if (NCDVal_IsInvalid(template_name) || ncd_is_none(template_name)) {
  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_InitValue(&o->process, o->i, template_name, args, process_handler_event)) {
  125. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  126. goto fail0;
  127. }
  128. // set special functions
  129. NCDModuleProcess_SetSpecialFuncs(&o->process, process_func_getspecialobj);
  130. // set state working
  131. o->state = STATE_WORKING;
  132. }
  133. return;
  134. fail0:
  135. NCDModuleInst_Backend_DeadError(i);
  136. }
  137. static void instance_free (struct instance *o)
  138. {
  139. // free process
  140. if (o->state != STATE_NONE) {
  141. NCDModuleProcess_Free(&o->process);
  142. }
  143. NCDModuleInst_Backend_Dead(o->i);
  144. }
  145. static void func_new_call (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  146. {
  147. NCDValRef template_arg;
  148. NCDValRef args_arg;
  149. if (!NCDVal_ListRead(params->args, 2, &template_arg, &args_arg)) {
  150. ModuleLog(i, BLOG_ERROR, "wrong arity");
  151. goto fail0;
  152. }
  153. if (!NCDVal_IsString(template_arg) || !NCDVal_IsList(args_arg)) {
  154. ModuleLog(i, BLOG_ERROR, "wrong type");
  155. goto fail0;
  156. }
  157. func_new_templ(vo, i, template_arg, args_arg, 0);
  158. return;
  159. fail0:
  160. NCDModuleInst_Backend_DeadError(i);
  161. }
  162. static void func_new_embcall_multif (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  163. {
  164. NCDValRef args = params->args;
  165. NCDValRef template_value = NCDVal_NewInvalid();
  166. size_t count = NCDVal_ListCount(args);
  167. size_t j = 0;
  168. while (j < count) {
  169. NCDValRef arg = NCDVal_ListGet(args, j);
  170. if (j == count - 1) {
  171. if (!NCDVal_IsString(arg)) {
  172. ModuleLog(i, BLOG_ERROR, "bad arguments");
  173. goto fail0;
  174. }
  175. template_value = arg;
  176. break;
  177. }
  178. NCDValRef arg2 = NCDVal_ListGet(args, j + 1);
  179. if (!NCDVal_IsString(arg) || !NCDVal_IsString(arg2)) {
  180. ModuleLog(i, BLOG_ERROR, "bad arguments");
  181. goto fail0;
  182. }
  183. if (ncd_read_boolean(arg)) {
  184. template_value = arg2;
  185. break;
  186. }
  187. j += 2;
  188. }
  189. func_new_templ(vo, i, template_value, NCDVal_NewInvalid(), 1);
  190. return;
  191. fail0:
  192. NCDModuleInst_Backend_DeadError(i);
  193. }
  194. static void func_die (void *vo)
  195. {
  196. struct instance *o = vo;
  197. ASSERT(o->state != STATE_TERMINATING)
  198. // if none, die now
  199. if (o->state == STATE_NONE) {
  200. instance_free(o);
  201. return;
  202. }
  203. // request process to terminate
  204. NCDModuleProcess_Terminate(&o->process);
  205. // set state terminating
  206. o->state = STATE_TERMINATING;
  207. }
  208. static void func_clean (void *vo)
  209. {
  210. struct instance *o = vo;
  211. if (o->state != STATE_WAITING) {
  212. return;
  213. }
  214. // allow process to continue
  215. NCDModuleProcess_Continue(&o->process);
  216. // set state working
  217. o->state = STATE_WORKING;
  218. }
  219. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  220. {
  221. struct instance *o = vo;
  222. if (o->state == STATE_NONE) {
  223. return 0;
  224. }
  225. return NCDModuleProcess_GetObj(&o->process, name, out_object);
  226. }
  227. static struct NCDModule modules[] = {
  228. {
  229. .type = "call",
  230. .func_new2 = func_new_call,
  231. .func_die = func_die,
  232. .func_clean = func_clean,
  233. .func_getobj = func_getobj,
  234. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  235. .alloc_size = sizeof(struct instance)
  236. }, {
  237. .type = "embcall2_multif",
  238. .func_new2 = func_new_embcall_multif,
  239. .func_die = func_die,
  240. .func_clean = func_clean,
  241. .func_getobj = func_getobj,
  242. .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
  243. .alloc_size = sizeof(struct instance)
  244. }, {
  245. .type = NULL
  246. }
  247. };
  248. const struct NCDModuleGroup ncdmodule_call2 = {
  249. .modules = modules,
  250. .strings = strings
  251. };