[sr-dev] git:master: modules/websocket: moved some configuration across to the cfg framework
Peter Dunkley
peter.dunkley at crocodile-rcs.com
Sat Mar 30 21:28:47 CET 2013
Module: sip-router
Branch: master
Commit: 688f2902977ee52d0c3d89a1f6459ca0d8c624f9
URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=688f2902977ee52d0c3d89a1f6459ca0d8c624f9
Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date: Sat Mar 30 20:28:03 2013 +0000
modules/websocket: moved some configuration across to the cfg framework
- As suggested by @oej
---
modules/websocket/ws_frame.c | 6 +---
modules/websocket/ws_frame.h | 3 --
modules/websocket/ws_handshake.c | 6 ++--
modules/websocket/ws_mod.c | 47 ++++++++++++++++++++++++++++---------
modules/websocket/ws_mod.h | 8 +++++-
5 files changed, 47 insertions(+), 23 deletions(-)
diff --git a/modules/websocket/ws_frame.c b/modules/websocket/ws_frame.c
index 4e2b91f..4212158 100644
--- a/modules/websocket/ws_frame.c
+++ b/modules/websocket/ws_frame.c
@@ -93,9 +93,6 @@ typedef enum
#define OPCODE_PONG (0xa)
/* 0xb - 0xf are reserved for further control frames */
-/* Time (in seconds) after which to send a keepalive on an idle connection */
-int ws_keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
-int ws_keepalive_mechanism = DEFAULT_KEEPALIVE_MECHANISM;
str ws_ping_application_data = {0, 0};
stat_var *ws_failed_connections;
@@ -812,7 +809,8 @@ struct mi_root *ws_mi_pong(struct mi_root *cmd, void *param)
void ws_keepalive(unsigned int ticks, void *param)
{
- int check_time = (int) time(NULL) - ws_keepalive_timeout;
+ int check_time = (int) time(NULL)
+ - cfg_get(websocket, ws_cfg, keepalive_timeout);
ws_connection_t *wsc = wsconn_used_list->head;
while (wsc && wsc->last_used < check_time)
diff --git a/modules/websocket/ws_frame.h b/modules/websocket/ws_frame.h
index b674d07..2a6942e 100644
--- a/modules/websocket/ws_frame.h
+++ b/modules/websocket/ws_frame.h
@@ -36,9 +36,6 @@ typedef enum
REMOTE_CLOSE
} ws_close_type_t;
-#define DEFAULT_KEEPALIVE_TIMEOUT 180 /* seconds */
-extern int ws_keepalive_timeout;
-
enum
{
KEEPALIVE_MECHANISM_NONE = 0,
diff --git a/modules/websocket/ws_handshake.c b/modules/websocket/ws_handshake.c
index 15d3181..739e7cf 100644
--- a/modules/websocket/ws_handshake.c
+++ b/modules/websocket/ws_handshake.c
@@ -128,7 +128,7 @@ int ws_handle_handshake(struct sip_msg *msg)
msg->rpl_send_flags.f |= SND_F_CON_CLOSE;
msg->rpl_send_flags.f |= SND_F_FORCE_CON_REUSE;
- if (*ws_enabled == 0)
+ if (cfg_get(websocket, ws_cfg, enabled) == 0)
{
LM_INFO("disabled: bouncing handshake\n");
ws_send_reply(msg, 503, &str_status_service_unavailable,
@@ -396,14 +396,14 @@ int ws_handle_handshake(struct sip_msg *msg)
struct mi_root *ws_mi_disable(struct mi_root *cmd, void *param)
{
- *ws_enabled = 0;
+ cfg_get(websocket, ws_cfg, enabled) = 0;
LM_WARN("disabling websockets - new connections will be dropped\n");
return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
}
struct mi_root *ws_mi_enable(struct mi_root *cmd, void *param)
{
- *ws_enabled = 1;
+ cfg_get(websocket, ws_cfg, enabled) = 1;
LM_WARN("enabling websockets\n");
return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
}
diff --git a/modules/websocket/ws_mod.c b/modules/websocket/ws_mod.c
index d7262c9..54ea209 100644
--- a/modules/websocket/ws_mod.c
+++ b/modules/websocket/ws_mod.c
@@ -28,6 +28,7 @@
#include "../../sr_module.h"
#include "../../tcp_conn.h"
#include "../../timer_proc.h"
+#include "../../cfg/cfg.h"
#include "../../lib/kcore/kstats_wrapper.h"
#include "../../lib/kmi/mi.h"
#include "../../mem/mem.h"
@@ -47,11 +48,13 @@ static int child_init(int rank);
static void destroy(void);
sl_api_t ws_slb;
-int *ws_enabled;
#define DEFAULT_KEEPALIVE_INTERVAL 1
static int ws_keepalive_interval = DEFAULT_KEEPALIVE_INTERVAL;
+#define DEFAULT_KEEPALIVE_TIMEOUT 180 /* seconds */
+static int ws_keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
+
#define DEFAULT_KEEPALIVE_PROCESSES 1
static int ws_keepalive_processes = DEFAULT_KEEPALIVE_PROCESSES;
@@ -151,6 +154,30 @@ struct module_exports exports=
child_init /* per-child initialization function */
};
+static cfg_def_t ws_cfg_def[] =
+{
+ /* ws_frame.c */
+ { "keepalive_timeout", CFG_VAR_INT | CFG_ATOMIC,
+ 0, 0, 0, 0,
+ "Time (in seconds) after which to send a keep-alive on idle"
+ " WebSocket connections." },
+
+ /* ws_handshake.c */
+ { "enabled", CFG_VAR_INT | CFG_ATOMIC,
+ 0, 0, 0, 0,
+ "Shows whether WebSockets are enabled or not." },
+
+ { 0, 0, 0, 0, 0, 0 }
+};
+
+struct cfg_group_websocket default_ws_cfg =
+{
+ DEFAULT_KEEPALIVE_TIMEOUT, /* keepalive_timeout */
+ 1 /* enabled */
+};
+void *ws_cfg = &default_ws_cfg;
+
+
static int mod_init(void)
{
if (sl_load_api(&ws_slb) != 0)
@@ -189,14 +216,6 @@ static int mod_init(void)
goto error;
}
- if ((ws_enabled = (int *) shm_malloc(sizeof(int))) == NULL)
- {
- LM_ERR("allocating shared memory\n");
- goto error;
- }
- *ws_enabled = 1;
-
-
if (ws_ping_application_data.s != 0)
ws_ping_application_data.len =
strlen(ws_ping_application_data.s);
@@ -249,12 +268,17 @@ static int mod_init(void)
goto error;
}
+ if (cfg_declare("websocket", ws_cfg_def, &default_ws_cfg,
+ cfg_sizeof(websocket), &ws_cfg)) {
+ LM_ERR("declaring configuration\n");
+ return -1;
+ }
+ cfg_get(websocket, ws_cfg, keepalive_timeout) = ws_keepalive_timeout;
+
return 0;
error:
wsconn_destroy();
- shm_free(ws_enabled);
-
return -1;
}
@@ -287,5 +311,4 @@ static int child_init(int rank)
static void destroy(void)
{
wsconn_destroy();
- shm_free(ws_enabled);
}
diff --git a/modules/websocket/ws_mod.h b/modules/websocket/ws_mod.h
index fb96212..fa47b3b 100644
--- a/modules/websocket/ws_mod.h
+++ b/modules/websocket/ws_mod.h
@@ -35,9 +35,15 @@ enum
};
extern sl_api_t ws_slb;
-extern int *ws_enabled;
extern gen_lock_t *ws_stats_lock;
extern int ws_ping_interval; /* time (in seconds) between sending Pings */
+struct cfg_group_websocket
+{
+ int keepalive_timeout;
+ int enabled;
+};
+extern void *ws_cfg; /* module configuration structure */
+
#endif /* _WS_MOD_H */
More information about the sr-dev
mailing list