call.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * @file call.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Synopsis:
  25. * callrefhere()
  26. *
  27. * Description:
  28. * Exposes variables and objects to call() statements as seen from this
  29. * callrefhere() statement.
  30. *
  31. * Synopsis:
  32. * call(string template_name, list args)
  33. * callhefhere::call(string template_name, list args)
  34. *
  35. * Description:
  36. * Module which allows using a single statement to represent multiple statements
  37. * in a process template, allowing reuse of repetitive code.
  38. * The created template process can access variables and objects as seen from the
  39. * call statement via "_caller.variable".
  40. * The second form also exposes variables and objects from the corresponding
  41. * callrefhere() statement via "_ref.variable".
  42. *
  43. * Variables:
  44. * Exposes variables as seen from the end of the called process template.
  45. *
  46. * Behavior in detail:
  47. * - On initialization, creates a new process from the template named
  48. * template_name, with arguments args.
  49. * - When all the statements in the created process go UP, transitions UP.
  50. * - When one of the statements is no longer UP, transitions DOWN. The
  51. * created process remais paused until the call statement receives the
  52. * clean signal, to wait for following statements to deinitialize.
  53. * - On deinitialization, initiates termination of the created process and waits
  54. * for all its statements to deinitialize.
  55. */
  56. #include <stdlib.h>
  57. #include <misc/string_begins_with.h>
  58. #include <misc/offset.h>
  59. #include <structure/LinkedList0.h>
  60. #include <ncd/NCDModule.h>
  61. #include <generated/blog_channel_ncd_call.h>
  62. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  63. #define STATE_WORKING 1
  64. #define STATE_UP 2
  65. #define STATE_WAITING 3
  66. #define STATE_TERMINATING 4
  67. struct callrefhere_instance {
  68. NCDModuleInst *i;
  69. LinkedList0 calls_list;
  70. };
  71. struct instance {
  72. NCDModuleInst *i;
  73. NCDModuleProcess process;
  74. int state;
  75. struct callrefhere_instance *crh;
  76. LinkedList0Node calls_list_node;
  77. };
  78. static void instance_free (struct instance *o);
  79. static void process_handler_event (struct instance *o, int event)
  80. {
  81. switch (event) {
  82. case NCDMODULEPROCESS_EVENT_UP: {
  83. ASSERT(o->state == STATE_WORKING)
  84. // signal up
  85. NCDModuleInst_Backend_Up(o->i);
  86. // set state up
  87. o->state = STATE_UP;
  88. } break;
  89. case NCDMODULEPROCESS_EVENT_DOWN: {
  90. ASSERT(o->state == STATE_UP)
  91. // signal down
  92. NCDModuleInst_Backend_Down(o->i);
  93. // set state waiting
  94. o->state = STATE_WAITING;
  95. } break;
  96. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  97. ASSERT(o->state == STATE_TERMINATING)
  98. // die finally
  99. instance_free(o);
  100. return;
  101. } break;
  102. default: ASSERT(0);
  103. }
  104. }
  105. static int process_func_getspecialvar (struct instance *o, const char *name, NCDValue *out)
  106. {
  107. size_t p;
  108. if (p = string_begins_with(name, "_caller.")) {
  109. return NCDModuleInst_Backend_GetVar(o->i, name + p, out);
  110. }
  111. if (o->crh && (p = string_begins_with(name, "_ref."))) {
  112. return NCDModuleInst_Backend_GetVar(o->crh->i, name + p, out);
  113. }
  114. return 0;
  115. }
  116. static NCDModuleInst * process_func_getspecialobj (struct instance *o, const char *name)
  117. {
  118. size_t p;
  119. if (p = string_begins_with(name, "_caller.")) {
  120. return NCDModuleInst_Backend_GetObj(o->i, name + p);
  121. }
  122. if (o->crh && (p = string_begins_with(name, "_ref."))) {
  123. return NCDModuleInst_Backend_GetObj(o->crh->i, name + p);
  124. }
  125. return NULL;
  126. }
  127. static void callrefhere_func_new (NCDModuleInst *i)
  128. {
  129. // allocate instance
  130. struct callrefhere_instance *o = malloc(sizeof(*o));
  131. if (!o) {
  132. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  133. goto fail0;
  134. }
  135. NCDModuleInst_Backend_SetUser(i, o);
  136. // set arguments
  137. o->i = i;
  138. // init calls list
  139. LinkedList0_Init(&o->calls_list);
  140. // signal up
  141. NCDModuleInst_Backend_Up(o->i);
  142. return;
  143. fail0:
  144. NCDModuleInst_Backend_SetError(i);
  145. NCDModuleInst_Backend_Dead(i);
  146. }
  147. static void callrefhere_func_die (void *vo)
  148. {
  149. struct callrefhere_instance *o = vo;
  150. NCDModuleInst *i = o->i;
  151. // disconnect calls
  152. while (!LinkedList0_IsEmpty(&o->calls_list)) {
  153. struct instance *inst = UPPER_OBJECT(LinkedList0_GetFirst(&o->calls_list), struct instance, calls_list_node);
  154. ASSERT(inst->crh == o)
  155. LinkedList0_Remove(&o->calls_list, &inst->calls_list_node);
  156. inst->crh = NULL;
  157. }
  158. // free instance
  159. free(o);
  160. NCDModuleInst_Backend_Dead(i);
  161. }
  162. static void func_new (NCDModuleInst *i)
  163. {
  164. // allocate instance
  165. struct instance *o = malloc(sizeof(*o));
  166. if (!o) {
  167. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  168. goto fail0;
  169. }
  170. NCDModuleInst_Backend_SetUser(i, o);
  171. // init arguments
  172. o->i = i;
  173. // check arguments
  174. NCDValue *template_name_arg;
  175. NCDValue *args_arg;
  176. if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  177. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  178. goto fail1;
  179. }
  180. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  181. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  182. goto fail1;
  183. }
  184. // copy arguments
  185. NCDValue args;
  186. if (!NCDValue_InitCopy(&args, args_arg)) {
  187. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  188. goto fail1;
  189. }
  190. // create process
  191. if (!NCDModuleProcess_Init(&o->process, o->i, NCDValue_StringValue(template_name_arg), args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
  192. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  193. NCDValue_Free(&args);
  194. goto fail1;
  195. }
  196. // set special functions
  197. NCDModuleProcess_SetSpecialFuncs(&o->process,
  198. (NCDModuleProcess_func_getspecialvar)process_func_getspecialvar,
  199. (NCDModuleProcess_func_getspecialobj)process_func_getspecialobj);
  200. // set callrefhere
  201. o->crh = (o->i->method_object ? o->i->method_object->inst_user : NULL);
  202. // add to callrefhere's calls list
  203. if (o->crh) {
  204. LinkedList0_Prepend(&o->crh->calls_list, &o->calls_list_node);
  205. }
  206. // set state working
  207. o->state = STATE_WORKING;
  208. return;
  209. fail1:
  210. free(o);
  211. fail0:
  212. NCDModuleInst_Backend_SetError(i);
  213. NCDModuleInst_Backend_Dead(i);
  214. }
  215. void instance_free (struct instance *o)
  216. {
  217. NCDModuleInst *i = o->i;
  218. // remove from callrefhere's calls list
  219. if (o->crh) {
  220. LinkedList0_Remove(&o->crh->calls_list, &o->calls_list_node);
  221. }
  222. // free process
  223. NCDModuleProcess_Free(&o->process);
  224. // free instance
  225. free(o);
  226. NCDModuleInst_Backend_Dead(i);
  227. }
  228. static void func_die (void *vo)
  229. {
  230. struct instance *o = vo;
  231. ASSERT(o->state != STATE_TERMINATING)
  232. // request process to terminate
  233. NCDModuleProcess_Terminate(&o->process);
  234. // set state terminating
  235. o->state = STATE_TERMINATING;
  236. }
  237. static void func_clean (void *vo)
  238. {
  239. struct instance *o = vo;
  240. if (o->state != STATE_WAITING) {
  241. return;
  242. }
  243. // allow process to continue
  244. NCDModuleProcess_Continue(&o->process);
  245. // set state working
  246. o->state = STATE_WORKING;
  247. }
  248. static int func_getvar (void *vo, const char *name, NCDValue *out)
  249. {
  250. struct instance *o = vo;
  251. ASSERT(o->state == STATE_UP)
  252. return NCDModuleProcess_GetVar(&o->process, name, out);
  253. }
  254. static NCDModuleInst * func_getobj (void *vo, const char *name)
  255. {
  256. struct instance *o = vo;
  257. ASSERT(o->state == STATE_UP)
  258. return NCDModuleProcess_GetObj(&o->process, name);
  259. }
  260. static const struct NCDModule modules[] = {
  261. {
  262. .type = "callrefhere",
  263. .func_new = callrefhere_func_new,
  264. .func_die = callrefhere_func_die
  265. }, {
  266. .type = "call",
  267. .func_new = func_new,
  268. .func_die = func_die,
  269. .func_clean = func_clean,
  270. .func_getvar = func_getvar,
  271. .func_getobj = func_getobj
  272. }, {
  273. .type = "callrefhere::call",
  274. .func_new = func_new,
  275. .func_die = func_die,
  276. .func_clean = func_clean,
  277. .func_getvar = func_getvar,
  278. .func_getobj = func_getobj
  279. }, {
  280. .type = NULL
  281. }
  282. };
  283. const struct NCDModuleGroup ncdmodule_call = {
  284. .modules = modules
  285. };