jsonrpc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. *
  3. * Copyright (C) 2011 Flowroute LLC (flowroute.com)
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * This file is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. *
  13. * This file 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
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include "../../sr_module.h"
  28. #include "../../mem/mem.h"
  29. #include "jsonrpc.h"
  30. jsonrpc_request_t * request_table[JSONRPC_DEFAULT_HTABLE_SIZE] = {0};
  31. int next_id = 1;
  32. jsonrpc_request_t* get_request(int id);
  33. int store_request(jsonrpc_request_t* req);
  34. jsonrpc_request_t* build_jsonrpc_request(char *method, json_object *params, char *cbdata, int (*cbfunc)(json_object*, char*, int))
  35. {
  36. if (next_id>JSONRPC_MAX_ID) {
  37. next_id = 1;
  38. } else {
  39. next_id++;
  40. }
  41. jsonrpc_request_t *req = pkg_malloc(sizeof(jsonrpc_request_t));
  42. if (!req) {
  43. LM_ERR("Out of memory!");
  44. return 0;
  45. }
  46. req->id = next_id;
  47. req->cbfunc = cbfunc;
  48. req->cbdata = cbdata;
  49. req->next = NULL;
  50. req->timer_ev = NULL;
  51. if (!store_request(req))
  52. return 0;
  53. req->payload = json_object_new_object();
  54. json_object_object_add(req->payload, "id", json_object_new_int(next_id));
  55. json_object_object_add(req->payload, "jsonrpc", json_object_new_string("2.0"));
  56. json_object_object_add(req->payload, "method", json_object_new_string(method));
  57. json_object_object_add(req->payload, "params", params);
  58. return req;
  59. }
  60. json_object* build_jsonrpc_notification(char *method, json_object *params)
  61. {
  62. json_object *req = json_object_new_object();
  63. json_object_object_add(req, "jsonrpc", json_object_new_string("2.0"));
  64. json_object_object_add(req, "method", json_object_new_string(method));
  65. json_object_object_add(req, "params", params);
  66. return req;
  67. }
  68. int handle_jsonrpc_response(json_object *response)
  69. {
  70. jsonrpc_request_t *req;
  71. json_object *_id = json_object_object_get(response, "id");
  72. int id = json_object_get_int(_id);
  73. if (!(req = get_request(id))) {
  74. json_object_put(response);
  75. return -1;
  76. }
  77. json_object *result = json_object_object_get(response, "result");
  78. if (result) {
  79. req->cbfunc(result, req->cbdata, 0);
  80. } else {
  81. json_object *error = json_object_object_get(response, "error");
  82. if (error) {
  83. req->cbfunc(error, req->cbdata, 1);
  84. } else {
  85. LM_ERR("Response received with neither a result nor an error.\n");
  86. return -1;
  87. }
  88. }
  89. if (req->timer_ev) {
  90. close(req->timerfd);
  91. event_del(req->timer_ev);
  92. pkg_free(req->timer_ev);
  93. } else {
  94. LM_ERR("No timer for req id %d\n", id);
  95. }
  96. pkg_free(req);
  97. return 1;
  98. }
  99. int id_hash(int id) {
  100. return (id % JSONRPC_DEFAULT_HTABLE_SIZE);
  101. }
  102. jsonrpc_request_t* get_request(int id) {
  103. int key = id_hash(id);
  104. jsonrpc_request_t *req, *prev_req = NULL;
  105. req = request_table[key];
  106. while (req && req->id != id) {
  107. prev_req = req;
  108. if (!(req = req->next)) {
  109. break;
  110. };
  111. }
  112. if (req && req->id == id) {
  113. if (prev_req != NULL) {
  114. prev_req-> next = req->next;
  115. } else {
  116. request_table[key] = NULL;
  117. }
  118. return req;
  119. }
  120. return 0;
  121. }
  122. void void_jsonrpc_request(int id) {
  123. get_request(id);
  124. }
  125. int store_request(jsonrpc_request_t* req) {
  126. int key = id_hash(req->id);
  127. jsonrpc_request_t* existing;
  128. if ((existing = request_table[key])) { /* collision */
  129. jsonrpc_request_t* i;
  130. for(i=existing; i; i=i->next) {
  131. if (i == NULL) {
  132. i = req;
  133. LM_ERR("!!!!!!!");
  134. return 1;
  135. }
  136. if (i->next == NULL) {
  137. i->next = req;
  138. return 1;
  139. }
  140. }
  141. } else {
  142. request_table[key] = req;
  143. }
  144. return 1;
  145. }