dbtext.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * $Id$
  3. *
  4. * DBText module interface
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2003-01-30 created by Daniel
  27. * 2003-03-11 New module interface (janakj)
  28. * 2003-03-16 flags export parameter added (janakj)
  29. *
  30. */
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include "../../sr_module.h"
  34. #include "../../lib/srdb1/db.h"
  35. #include "../../rpc_lookup.h"
  36. #include "dbtext.h"
  37. #include "dbt_lib.h"
  38. #include "dbt_api.h"
  39. MODULE_VERSION
  40. static int mod_init(void);
  41. static void destroy(void);
  42. /*
  43. * Module parameter variables
  44. */
  45. int db_mode = 0; /* Database usage mode: 0 = cache, 1 = no cache */
  46. int dbt_bind_api(db_func_t *dbb);
  47. /*
  48. * Exported functions
  49. */
  50. static cmd_export_t cmds[] = {
  51. {"db_bind_api", (cmd_function)dbt_bind_api, 0, 0, 0, 0},
  52. {0, 0, 0, 0, 0, 0}
  53. };
  54. /*
  55. * Exported parameters
  56. */
  57. static param_export_t params[] = {
  58. {"db_mode", INT_PARAM, &db_mode},
  59. {0, 0, 0}
  60. };
  61. static rpc_export_t rpc_methods[];
  62. struct module_exports exports = {
  63. "db_text",
  64. DEFAULT_DLFLAGS, /* dlopen flags */
  65. cmds, /* Exported functions */
  66. params, /* Exported parameters */
  67. 0, /* exported statistics */
  68. 0, /* exported MI functions */
  69. 0, /* exported pseudo-variables */
  70. 0, /* extra processes */
  71. mod_init, /* module initialization function */
  72. 0, /* response function*/
  73. destroy, /* destroy function */
  74. 0 /* per-child init function */
  75. };
  76. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  77. {
  78. if (rpc_register_array(rpc_methods)!=0) {
  79. LM_ERR("failed to register RPC commands\n");
  80. return -1;
  81. }
  82. if(db_api_init()<0)
  83. return -1;
  84. return 0;
  85. }
  86. static int mod_init(void)
  87. {
  88. if(dbt_init_cache())
  89. return -1;
  90. /* return make_demo(); */
  91. return 0;
  92. }
  93. static void destroy(void)
  94. {
  95. LM_DBG("destroy ...\n");
  96. dbt_cache_print(0);
  97. dbt_cache_destroy();
  98. }
  99. int dbt_bind_api(db_func_t *dbb)
  100. {
  101. if(dbb==NULL)
  102. return -1;
  103. memset(dbb, 0, sizeof(db_func_t));
  104. dbb->use_table = dbt_use_table;
  105. dbb->init = dbt_init;
  106. dbb->close = dbt_close;
  107. dbb->query = (db_query_f)dbt_query;
  108. dbb->free_result = dbt_free_result;
  109. dbb->insert = (db_insert_f)dbt_insert;
  110. dbb->delete = (db_delete_f)dbt_delete;
  111. dbb->update = (db_update_f)dbt_update;
  112. return 0;
  113. }
  114. /* rpc function documentation */
  115. static const char *rpc_dump_doc[2] = {
  116. "Write back to disk modified tables", 0
  117. };
  118. /* rpc function implementations */
  119. static void rpc_dump(rpc_t *rpc, void *c) {
  120. if (0!=dbt_cache_print(0))
  121. rpc->rpl_printf(c, "Dump failed");
  122. else
  123. rpc->rpl_printf(c, "Dump OK");
  124. return;
  125. }
  126. static rpc_export_t rpc_methods[] = {
  127. {"db_text.dump", rpc_dump, rpc_dump_doc, 0},
  128. {0, 0, 0, 0}
  129. };