NCDModule.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @file NCDModule.h
  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. #ifndef BADVPN_NCD_NCDMODULE_H
  23. #define BADVPN_NCD_NCDMODULE_H
  24. #include <misc/debug.h>
  25. #include <system/BReactor.h>
  26. #include <system/BPending.h>
  27. #include <system/BLog.h>
  28. #include <process/BProcess.h>
  29. #include <ncd/NCDValue.h>
  30. #define NCDMODULE_EVENT_UP 1
  31. #define NCDMODULE_EVENT_DOWN 2
  32. #define NCDMODULE_EVENT_DEAD 3
  33. #define NCDMODULE_TOEVENT_DIE 101
  34. #define NCDMODULE_TOEVENT_CLEAN 102
  35. struct NCDModuleInst_s;
  36. struct NCDModuleProcess_s;
  37. typedef void (*NCDModule_handler_event) (void *user, int event);
  38. typedef int (*NCDModule_handler_getvar) (void *user, const char *varname, NCDValue *out);
  39. typedef struct NCDModuleInst_s * (*NCDModule_handler_getobj) (void *user, const char *objname);
  40. typedef int (*NCDModule_handler_initprocess) (void *user, struct NCDModuleProcess_s *p, const char *template_name, NCDValue args);
  41. typedef void (*NCDModule_handler_log) (void *user);
  42. typedef void (*NCDModuleProcess_handler_dead) (void *user);
  43. typedef void (*NCDModuleProcess_interp_handler_die) (void *user);
  44. struct NCDModule;
  45. struct NCDModuleInitParams {
  46. BReactor *reactor;
  47. BProcessManager *manager;
  48. };
  49. typedef struct NCDModuleInst_s {
  50. const struct NCDModule *m;
  51. struct NCDModuleInst_s *method_object;
  52. NCDValue *args;
  53. BReactor *reactor;
  54. BProcessManager *manager;
  55. void *user;
  56. NCDModule_handler_event handler_event;
  57. NCDModule_handler_getvar handler_getvar;
  58. NCDModule_handler_getobj handler_getobj;
  59. NCDModule_handler_initprocess handler_initprocess;
  60. NCDModule_handler_log handler_log;
  61. BPending init_job;
  62. BPending uninit_job;
  63. BPending die_job;
  64. BPending clean_job;
  65. int state;
  66. void *inst_user;
  67. int is_error;
  68. DebugObject d_obj;
  69. } NCDModuleInst;
  70. typedef struct NCDModuleProcess_s {
  71. NCDModuleInst *n;
  72. void *user;
  73. NCDModuleProcess_handler_dead handler_dead;
  74. void *interp_user;
  75. NCDModuleProcess_interp_handler_die interp_handler_die;
  76. BPending die_job;
  77. BPending dead_job;
  78. int state;
  79. DebugObject d_obj;
  80. } NCDModuleProcess;
  81. void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, NCDModuleInst *method_object, NCDValue *args, BReactor *reactor, BProcessManager *manager, void *user,
  82. NCDModule_handler_event handler_event,
  83. NCDModule_handler_getvar handler_getvar,
  84. NCDModule_handler_getobj handler_getobj,
  85. NCDModule_handler_initprocess handler_initprocess,
  86. NCDModule_handler_log handler_log);
  87. void NCDModuleInst_Free (NCDModuleInst *n);
  88. void NCDModuleInst_Event (NCDModuleInst *n, int event);
  89. int NCDModuleInst_GetVar (NCDModuleInst *n, const char *name, NCDValue *out) WARN_UNUSED;
  90. NCDModuleInst * NCDModuleInst_GetObj (NCDModuleInst *n, const char *objname) WARN_UNUSED;
  91. int NCDModuleInst_HaveError (NCDModuleInst *n);
  92. void NCDModuleInst_Backend_SetUser (NCDModuleInst *n, void *user);
  93. void NCDModuleInst_Backend_Event (NCDModuleInst *n, int event);
  94. int NCDModuleInst_Backend_GetVar (NCDModuleInst *n, const char *varname, NCDValue *out) WARN_UNUSED;
  95. NCDModuleInst * NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *objname) WARN_UNUSED;
  96. void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const char *fmt, ...);
  97. void NCDModuleInst_Backend_SetError (NCDModuleInst *n);
  98. int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValue args, void *user, NCDModuleProcess_handler_dead handler_dead);
  99. void NCDModuleProcess_Free (NCDModuleProcess *o);
  100. void NCDModuleProcess_Die (NCDModuleProcess *o);
  101. void NCDModuleProcess_Interp_SetHandlers (NCDModuleProcess *o, void *interp_user, NCDModuleProcess_interp_handler_die interp_handler_die);
  102. void NCDModuleProcess_Interp_Dead (NCDModuleProcess *o);
  103. typedef int (*NCDModule_func_globalinit) (const struct NCDModuleInitParams params);
  104. typedef void (*NCDModule_func_globalfree) (void);
  105. typedef void (*NCDModule_func_new) (NCDModuleInst *params);
  106. typedef void (*NCDModule_func_die) (void *o);
  107. typedef int (*NCDModule_func_getvar) (void *o, const char *name, NCDValue *out);
  108. typedef NCDModuleInst * (*NCDModule_func_getobj) (void *o, const char *objname);
  109. typedef void (*NCDModule_func_clean) (void *o);
  110. struct NCDModule {
  111. const char *type;
  112. NCDModule_func_new func_new;
  113. NCDModule_func_die func_die;
  114. NCDModule_func_getvar func_getvar;
  115. NCDModule_func_getobj func_getobj;
  116. NCDModule_func_clean func_clean;
  117. };
  118. struct NCDModuleGroup {
  119. NCDModule_func_globalinit func_globalinit;
  120. NCDModule_func_globalfree func_globalfree;
  121. const struct NCDModule *modules;
  122. };
  123. #endif