jsonrpc.c 4.0 KB

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