Revision e67b71f9 janus.c

View differences:

janus.c
109 109
static struct janus_json_parameter debug_parameters[] = {
110 110
	{"debug", JANUS_JSON_BOOL, JANUS_JSON_PARAM_REQUIRED}
111 111
};
112
static struct janus_json_parameter timeout_parameters[] = {
113
	{"timeout", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
114
};
112 115
static struct janus_json_parameter level_parameters[] = {
113 116
	{"level", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
114 117
};
......
154 157
static gchar *server_name = NULL;
155 158

  
156 159

  
160
/* The default timeout for sessions is 60 seconds: this means that, if
161
 * we don't get any activity (i.e., no request) on this session for more
162
 * than 60 seconds, then it's considered expired and we destroy it. That's
163
 * why we have a keep-alive method in the API. This can be overridden in
164
 * either janus.cfg or from the command line. Setting this to 0 will
165
 * disable the timeout mechanism, which is NOT suggested as it may risk
166
 * having orphaned sessions (sessions not controlled by any transport
167
 * and never freed). Besides, notice that if you make this shorter than
168
 * 30s, you'll have to update the timers in janus.js when the long
169
 * polling mechanism is used and shorten them as well, or you'll risk
170
 * incurring in unexpected timeouts (when HTTP is used in janus.js, the
171
 * long poll is used as a keepalive mechanism). */
172
#define DEFAULT_SESSION_TIMEOUT		60
173
static uint session_timeout = DEFAULT_SESSION_TIMEOUT;
174

  
175

  
157 176
/* Information */
158 177
json_t *janus_info(const char *transaction);
159 178
json_t *janus_info(const char *transaction) {
......
175 194
#else
176 195
	json_object_set_new(info, "data_channels", json_false());
177 196
#endif
197
	json_object_set_new(info, "session-timeout", json_integer(session_timeout));
178 198
	json_object_set_new(info, "server-name", json_string(server_name ? server_name : JANUS_SERVER_NAME));
179 199
	json_object_set_new(info, "local-ip", json_string(local_ip));
180 200
	if(public_ip != NULL)
......
371 391
static GMainContext *sessions_watchdog_context = NULL;
372 392

  
373 393

  
374
#define SESSION_TIMEOUT		60		/* FIXME Should this be higher, e.g., 120 seconds? */
375

  
376 394
static gboolean janus_cleanup_session(gpointer user_data) {
377 395
	janus_session *session = (janus_session *) user_data;
378 396

  
......
383 401
}
384 402

  
385 403
static gboolean janus_check_sessions(gpointer user_data) {
404
	if(session_timeout < 1)		/* Session timeouts are disabled */
405
		return G_SOURCE_CONTINUE;
386 406
	GMainContext *watchdog_context = (GMainContext *) user_data;
387 407
	janus_mutex_lock(&sessions_mutex);
388 408
	if(sessions && g_hash_table_size(sessions) > 0) {
......
395 415
				continue;
396 416
			}
397 417
			gint64 now = janus_get_monotonic_time();
398
			if (now - session->last_activity >= SESSION_TIMEOUT * G_USEC_PER_SEC && !session->timeout) {
418
			if (now - session->last_activity >= session_timeout * G_USEC_PER_SEC && !session->timeout) {
399 419
				JANUS_LOG(LOG_INFO, "Timeout expired for session %"SCNu64"...\n", session->session_id);
400 420

  
401 421
				/* Notify the transport */
......
1526 1546
			json_object_set_new(reply, "transaction", json_string(transaction_text));
1527 1547
			json_t *status = json_object();
1528 1548
			json_object_set_new(status, "token_auth", janus_auth_is_enabled() ? json_true() : json_false());
1549
			json_object_set_new(status, "session_timeout", json_integer(session_timeout));
1529 1550
			json_object_set_new(status, "log_level", json_integer(janus_log_level));
1530 1551
			json_object_set_new(status, "log_timestamps", janus_log_timestamps ? json_true() : json_false());
1531 1552
			json_object_set_new(status, "log_colors", janus_log_colors ? json_true() : json_false());
......
1536 1557
			/* Send the success reply */
1537 1558
			ret = janus_process_success(request, reply);
1538 1559
			goto jsondone;
1560
		} else if(!strcasecmp(message_text, "set_session_timeout")) {
1561
			/* Change the session timeout value */
1562
			JANUS_VALIDATE_JSON_OBJECT(root, timeout_parameters,
1563
				error_code, error_cause, FALSE,
1564
				JANUS_ERROR_MISSING_MANDATORY_ELEMENT, JANUS_ERROR_INVALID_ELEMENT_TYPE);
1565
			if(error_code != 0) {
1566
				ret = janus_process_error_string(request, session_id, transaction_text, error_code, error_cause);
1567
				goto jsondone;
1568
			}
1569
			json_t *timeout = json_object_get(root, "timeout");
1570
			int timeout_num = json_integer_value(timeout);
1571
			if(timeout_num < 0) {
1572
				ret = janus_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_ELEMENT_TYPE, "Invalid element type (timeout should be a positive integer)");
1573
				goto jsondone;
1574
			}
1575
			session_timeout = timeout_num;
1576
			/* Prepare JSON reply */
1577
			json_t *reply = json_object();
1578
			json_object_set_new(reply, "janus", json_string("success"));
1579
			json_object_set_new(reply, "transaction", json_string(transaction_text));
1580
			json_object_set_new(reply, "timeout", json_integer(session_timeout));
1581
			/* Send the success reply */
1582
			ret = janus_process_success(request, reply);
1583
			goto jsondone;
1539 1584
		} else if(!strcasecmp(message_text, "set_log_level")) {
1540 1585
			/* Change the debug logging level */
1541 1586
			JANUS_VALIDATE_JSON_OBJECT(root, level_parameters,
......
3305 3350
	if(args_info.server_name_given) {
3306 3351
		janus_config_add_item(config, "general", "server_name", args_info.server_name_arg);
3307 3352
	}
3353
	if(args_info.session_timeout_given) {
3354
		char st[20];
3355
		g_snprintf(st, 20, "%d", args_info.session_timeout_arg);
3356
		janus_config_add_item(config, "general", "session_timeout", st);
3357
	}
3308 3358
 	if(args_info.interface_given) {
3309 3359
		janus_config_add_item(config, "general", "interface", args_info.interface_arg);
3310 3360
	}
......
3478 3528
		server_name = g_strdup(item->value);
3479 3529
	}
3480 3530

  
3531
	/* Check if a custom session timeout value was specified */
3532
	item = janus_config_get_item_drilldown(config, "general", "session_timeout");
3533
	if(item && item->value) {
3534
		int st = atoi(item->value);
3535
		if(st < 0) {
3536
			JANUS_LOG(LOG_WARN, "Ignoring session_timeout value as it's not a positive integer\n");
3537
		} else if(st == 0) {
3538
			JANUS_LOG(LOG_WARN, "Session timeouts have been disabled (note, may result in orphaned sessions)\n");
3539
		} else {
3540
			session_timeout = st;
3541
		}
3542
	}
3543

  
3481 3544
	/* Is there any API secret to consider? */
3482 3545
	api_secret = NULL;
3483 3546
	item = janus_config_get_item_drilldown(config, "general", "api_secret");

Also available in: Unified diff