jsonrpc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = json_object_object_get(response, "id");
  73. int id = json_object_get_int(_id);
  74. if (!(req = get_request(id))) {
  75. json_object_put(response);
  76. return -1;
  77. }
  78. json_object *result = json_object_object_get(response, "result");
  79. if (result) {
  80. req->cbfunc(result, req->cbdata, 0);
  81. } else {
  82. json_object *error = json_object_object_get(response, "error");
  83. if (error) {
  84. req->cbfunc(error, req->cbdata, 1);
  85. } else {
  86. LM_ERR("Response received with neither a result nor an error.\n");
  87. return -1;
  88. }
  89. }
  90. if (req->timer_ev) {
  91. close(req->timerfd);
  92. event_del(req->timer_ev);
  93. pkg_free(req->timer_ev);
  94. } else {
  95. LM_ERR("No timer for req id %d\n", id);
  96. }
  97. pkg_free(req);
  98. return 1;
  99. }
  100. int id_hash(int id) {
  101. return (id % JSONRPC_DEFAULT_HTABLE_SIZE);
  102. }
  103. jsonrpc_request_t* get_request(int id) {
  104. int key = id_hash(id);
  105. jsonrpc_request_t *req, *prev_req = NULL;
  106. req = request_table[key];
  107. while (req && req->id != id) {
  108. prev_req = req;
  109. if (!(req = req->next)) {
  110. break;
  111. };
  112. }
  113. if (req && req->id == id) {
  114. if (prev_req != NULL) {
  115. prev_req-> next = req->next;
  116. } else {
  117. request_table[key] = NULL;
  118. }
  119. return req;
  120. }
  121. return 0;
  122. }
  123. void void_jsonrpc_request(int id) {
  124. get_request(id);
  125. }
  126. int store_request(jsonrpc_request_t* req) {
  127. int key = id_hash(req->id);
  128. jsonrpc_request_t* existing;
  129. if ((existing = request_table[key])) { /* collision */
  130. jsonrpc_request_t* i;
  131. for(i=existing; i; i=i->next) {
  132. if (i == NULL) {
  133. i = req;
  134. LM_ERR("!!!!!!!");
  135. return 1;
  136. }
  137. if (i->next == NULL) {
  138. i->next = req;
  139. return 1;
  140. }
  141. }
  142. } else {
  143. request_table[key] = req;
  144. }
  145. return 1;
  146. }