janssonrpc_server.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (C) 2013 Flowroute LLC (flowroute.com)
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. *
  12. * This file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #ifndef _JANSSONRPC_SERVER_H_
  23. #define _JANSSONRPC_SERVER_H_
  24. #include <stdbool.h>
  25. #include <event.h>
  26. #include <event2/bufferevent.h>
  27. #include <event2/buffer.h>
  28. #include "../../locking.h"
  29. #include "netstring.h"
  30. /* interval (in seconds) at which failed servers are retried */
  31. #define JSONRPC_RECONNECT_INTERVAL 3
  32. /* default values */
  33. #define JSONRPC_DEFAULT_PRIORITY 0
  34. #define JSONRPC_DEFAULT_WEIGHT 1
  35. #define JSONRPC_DEFAULT_HWM 0 /* unlimited */
  36. typedef struct jsonrpc_server {
  37. str conn, addr, srv; /* shared mem */
  38. int port;
  39. unsigned int status, ttl, hwm;
  40. unsigned int req_count;
  41. unsigned int priority, weight;
  42. bool added;
  43. struct bufferevent* bev; /* local mem */
  44. netstring_t* buffer;
  45. } jsonrpc_server_t;
  46. typedef enum {
  47. CONN_GROUP,
  48. PRIORITY_GROUP,
  49. WEIGHT_GROUP
  50. } server_group_t;
  51. /* servers are organized in the following order:
  52. * 1) conn
  53. * 2) priority
  54. * 3) weight
  55. ***/
  56. typedef struct jsonrpc_server_group {
  57. server_group_t type;
  58. struct jsonrpc_server_group* sub_group; // NULL when type is WEIGHT_GROUP
  59. union {
  60. str conn; // when type is CONN_GROUP
  61. unsigned int priority; // when type is PRIORITY_GROUP
  62. unsigned int weight; //when type is WEIGHT_GROUP
  63. };
  64. jsonrpc_server_t* server; // only when type is WEIGHT_GROUP
  65. struct jsonrpc_server_group* next;
  66. } jsonrpc_server_group_t;
  67. gen_lock_t* jsonrpc_server_group_lock;
  68. typedef struct server_list {
  69. jsonrpc_server_t* server;
  70. struct server_list* next;
  71. } server_list_t;
  72. /* where all the servers are stored */
  73. jsonrpc_server_group_t** global_server_group;
  74. int jsonrpc_parse_server(char *_server, jsonrpc_server_group_t** group_ptr);
  75. int jsonrpc_server_from_srv(str conn, str srv,
  76. unsigned int hwm, jsonrpc_server_group_t** group_ptr);
  77. void close_server(jsonrpc_server_t* server);
  78. /* Do not call close_server() from outside the IO process.
  79. * Server's have a bufferevent that is part of local memory and free'd
  80. * at disconnect */
  81. jsonrpc_server_t* create_server();
  82. void free_server(jsonrpc_server_t* server);
  83. int create_server_group(server_group_t type, jsonrpc_server_group_t** new_grp);
  84. int jsonrpc_add_server(jsonrpc_server_t* server, jsonrpc_server_group_t** group);
  85. unsigned int server_group_size(jsonrpc_server_group_t* group);
  86. void free_server_group(jsonrpc_server_group_t** grp);
  87. int server_eq(jsonrpc_server_t* a, jsonrpc_server_t* b);
  88. void addto_server_list(jsonrpc_server_t* server, server_list_t** list);
  89. void free_server_list(server_list_t* list);
  90. #define INIT_SERVER_LOOP \
  91. jsonrpc_server_group_t* cgroup = NULL; \
  92. jsonrpc_server_group_t* pgroup = NULL; \
  93. jsonrpc_server_group_t* wgroup = NULL; \
  94. jsonrpc_server_t* server = NULL;
  95. #define FOREACH_SERVER_IN(ii) \
  96. if(ii == NULL) { \
  97. cgroup = NULL; \
  98. } else { \
  99. cgroup = *(ii); \
  100. } \
  101. pgroup = NULL; \
  102. wgroup = NULL; \
  103. server = NULL; \
  104. for(; cgroup!=NULL; cgroup=cgroup->next) { \
  105. for(pgroup=cgroup->sub_group; pgroup!=NULL; pgroup=pgroup->next) { \
  106. for(wgroup=pgroup->sub_group; wgroup!=NULL; wgroup=wgroup->next) { \
  107. server = wgroup->server;
  108. #define ENDFOR }}}
  109. /* debugging only */
  110. void print_server(jsonrpc_server_t* server);
  111. void print_group(jsonrpc_server_group_t** group);
  112. #endif /* _JSONRPC_SERVER_H_ */