FS-7603: Implement failover for socket application

This commit is contained in:
ico 2015-01-04 13:56:30 +02:00
parent 0bec209a9b
commit 7289701815
1 changed files with 47 additions and 32 deletions

View File

@ -400,8 +400,12 @@ SWITCH_STANDARD_APP(socket_function)
listener_t *listener; listener_t *listener;
int argc = 0, x = 0; int argc = 0, x = 0;
char *argv[80] = { 0 }; char *argv[80] = { 0 };
char *hosts[50] = { 0 };
unsigned int hosts_count = 0;
switch_status_t connected = SWITCH_STATUS_FALSE;
char *mydata; char *mydata;
switch_channel_t *channel = NULL; switch_channel_t *channel = NULL;
char errbuf[512] = {0};
channel = switch_core_session_get_channel(session); channel = switch_core_session_get_channel(session);
@ -414,13 +418,18 @@ SWITCH_STANDARD_APP(socket_function)
return; return;
} }
host = argv[0]; hosts_count = switch_split(argv[0], '|', hosts);
for(x = 0; x < hosts_count; x++) {
host = hosts[x];
if (zstr(host)) { if (zstr(host)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Missing Host!\n"); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Missing Host!\n");
return; continue;
} }
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Trying host: %s\n", host);
if ((port_name = strrchr(host, ':'))) { if ((port_name = strrchr(host, ':'))) {
*port_name++ = '\0'; *port_name++ = '\0';
port = (switch_port_t) atoi(port_name); port = (switch_port_t) atoi(port_name);
@ -435,13 +444,13 @@ SWITCH_STANDARD_APP(socket_function)
if (switch_sockaddr_info_get(&sa, host, SWITCH_UNSPEC, port, 0, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) { if (switch_sockaddr_info_get(&sa, host, SWITCH_UNSPEC, port, 0, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Socket Error!\n"); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Socket Error!\n");
return; continue;
} }
if (switch_socket_create(&new_sock, switch_sockaddr_get_family(sa), SOCK_STREAM, SWITCH_PROTO_TCP, switch_core_session_get_pool(session)) if (switch_socket_create(&new_sock, switch_sockaddr_get_family(sa), SOCK_STREAM, SWITCH_PROTO_TCP, switch_core_session_get_pool(session))
!= SWITCH_STATUS_SUCCESS) { != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Socket Error!\n"); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Socket Error!\n");
return; continue;
} }
switch_socket_opt_set(new_sock, SWITCH_SO_KEEPALIVE, 1); switch_socket_opt_set(new_sock, SWITCH_SO_KEEPALIVE, 1);
@ -449,12 +458,18 @@ SWITCH_STANDARD_APP(socket_function)
switch_socket_opt_set(new_sock, SWITCH_SO_TCP_KEEPIDLE, 30); switch_socket_opt_set(new_sock, SWITCH_SO_TCP_KEEPIDLE, 30);
switch_socket_opt_set(new_sock, SWITCH_SO_TCP_KEEPINTVL, 30); switch_socket_opt_set(new_sock, SWITCH_SO_TCP_KEEPINTVL, 30);
if (switch_socket_connect(new_sock, sa) != SWITCH_STATUS_SUCCESS) { if ((connected = switch_socket_connect(new_sock, sa)) == SWITCH_STATUS_SUCCESS) {
break;
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Socket Error: %s\n", switch_strerror(errno, errbuf, sizeof(errbuf)));
}//end hosts loop
if (connected != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Socket Error!\n"); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Socket Error!\n");
return; return;
} }
if (!(listener = switch_core_session_alloc(session, sizeof(*listener)))) { if (!(listener = switch_core_session_alloc(session, sizeof(*listener)))) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Memory Error\n"); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Memory Error\n");
return; return;