SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_rayo_shutdown);
SWITCH_MODULE_LOAD_FUNCTION(mod_rayo_load);
-SWITCH_MODULE_DEFINITION(mod_rayo, mod_rayo_load, mod_rayo_shutdown, NULL);
+SWITCH_MODULE_RUNTIME_FUNCTION(mod_rayo_runtime);
+SWITCH_MODULE_DEFINITION(mod_rayo, mod_rayo_load, mod_rayo_shutdown, mod_rayo_runtime);
#define RAYO_CAUSE_HANGUP SWITCH_CAUSE_NORMAL_CLEARING
#define RAYO_CAUSE_DECLINE SWITCH_CAUSE_CALL_REJECTED
int offer_uri;
/** if true, pause inbound calling if all clients are offline */
int pause_when_offline;
+ /** flag to reduce log noise */
+ int offline_logged;
} globals;
/**
}
}
+static void pause_inbound_calling(void)
+{
+ int32_t arg = 1;
+ switch_mutex_lock(globals.clients_mutex);
+ switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg);
+ if (!globals.offline_logged) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Pausing inbound calling\n");
+ globals.offline_logged = 1;
+ }
+ switch_mutex_unlock(globals.clients_mutex);
+}
+
+static void resume_inbound_calling(void)
+{
+ int32_t arg = 0;
+ switch_mutex_lock(globals.clients_mutex);
+ switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg);
+ if (globals.offline_logged) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Resuming inbound calling\n");
+ globals.offline_logged = 0;
+ }
+ switch_mutex_unlock(globals.clients_mutex);
+}
+
+/**
+ * Check online status of rayo client(s) and pause/resume the server
+ */
+static void pause_when_offline(void)
+{
+ if (globals.pause_when_offline) {
+ int is_online = 0;
+ switch_hash_index_t *hi;
+
+ switch_mutex_lock(globals.clients_mutex);
+
+ for (hi = switch_core_hash_first(globals.clients_roster); hi; hi = switch_core_hash_next(hi)) {
+ const void *key;
+ void *client;
+ switch_core_hash_this(hi, &key, NULL, &client);
+ switch_assert(client);
+ if (RAYO_CLIENT(client)->availability == PS_ONLINE) {
+ is_online = 1;
+ break;
+ }
+ }
+
+ if (is_online) {
+ resume_inbound_calling();
+ } else {
+ pause_inbound_calling();
+ }
+
+ switch_mutex_unlock(globals.clients_mutex);
+ }
+}
+
/**
* Send event to clients
* @param from event sender
return component;
}
-/**
- * @return true if at least one rayo client is online
- */
-static int is_rayo_client_online(void)
-{
- int is_online = 0;
- switch_hash_index_t *hi;
-
- switch_mutex_lock(globals.clients_mutex);
-
- for (hi = switch_core_hash_first(globals.clients_roster); hi; hi = switch_core_hash_next(hi)) {
- const void *key;
- void *client;
- switch_core_hash_this(hi, &key, NULL, &client);
- switch_assert(client);
- if (RAYO_CLIENT(client)->availability == PS_ONLINE) {
- is_online = 1;
- break;
- }
- }
- switch_mutex_unlock(globals.clients_mutex);
- return is_online;
-}
-
/**
* Send XMPP message to client
*/
}
switch_mutex_unlock(globals.clients_mutex);
- if (globals.pause_when_offline && !is_rayo_client_online()) {
- /* pause inbound calling */
- int32_t arg = 1;
- switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg);
- }
+ pause_when_offline();
}
/**
switch_mutex_unlock(globals.clients_mutex);
}
- if (globals.pause_when_offline && is_rayo_client_online()) {
- /* resume inbound calling */
- int32_t arg = 0;
- switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg);
- }
+ pause_when_offline();
return client;
}
RAYO_UNLOCK(rclient);
}
- if (globals.pause_when_offline) {
- if (is_rayo_client_online()) {
- /* resume inbound calling */
- int32_t arg = 0;
- switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg);
- } else {
- /* pause inbound calling */
- int32_t arg = 1;
- switch_core_session_ctl(SCSC_PAUSE_INBOUND, &arg);
- }
- }
+ pause_when_offline();
}
/**
/* nobody to offer to */
if (!ok) {
+ pause_when_offline();
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Rejecting rayo call - there are no online rayo clients to offer call to\n");
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE);
}
switch_core_hash_init(&globals.cmd_aliases, pool);
switch_thread_rwlock_create(&globals.shutdown_rwlock, pool);
switch_queue_create(&globals.msg_queue, 25000, pool);
+ globals.offline_logged = 1;
/* server commands */
rayo_actor_command_handler_add(RAT_SERVER, "", "get:"IKS_NS_XMPP_PING":ping", on_iq_xmpp_ping);
return SWITCH_STATUS_SUCCESS;
}
+/**
+ * Checks status of connected clients
+ */
+SWITCH_MODULE_RUNTIME_FUNCTION(mod_rayo_runtime)
+{
+ if (globals.pause_when_offline) {
+ switch_thread_rwlock_rdlock(globals.shutdown_rwlock);
+ while (!globals.shutdown) {
+ switch_sleep(1000 * 1000); /* 1 second */
+ pause_when_offline();
+ }
+ switch_thread_rwlock_unlock(globals.shutdown_rwlock);
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Runtime thread is done\n");
+ }
+ return SWITCH_STATUS_TERM;
+}
+
/* For Emacs:
* Local Variables:
* mode:c