Unify gateway printing between 'sofia xmlstatus gateway' and 'sofia xmlstatus gateway <gatewayname>'

This commit is contained in:
Andrew Thompson 2010-08-26 18:35:22 -04:00
parent 3be9460406
commit 37c224670e
1 changed files with 45 additions and 47 deletions

View File

@ -2658,67 +2658,11 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t cmd_xml_status(char **argv, int argc, switch_stream_handle_t *stream)
static void xml_gateway_status(sofia_gateway_t *gp, switch_stream_handle_t *stream)
{
sofia_profile_t *profile = NULL;
sofia_gateway_t *gp;
switch_hash_index_t *hi;
void *val;
const void *vvar;
const int buflen = 2096;
char xmlbuf[2096];
int c = 0;
int ac = 0;
const char *header = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
const int buflen = 2096;
if (argc > 0) {
if (argc == 1) {
/* show summary of all gateways */
stream->write_function(stream, "%s\n", header);
stream->write_function(stream, "<gateways>\n", header);
switch_mutex_lock(mod_sofia_globals.hash_mutex);
for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
switch_hash_this(hi, &vvar, NULL, &val);
profile = (sofia_profile_t *) val;
if (sofia_test_pflag(profile, PFLAG_RUNNING)) {
if (!strcmp(vvar, profile->name)) { /* not an alias */
for (gp = profile->gateways; gp; gp = gp->next) {
switch_assert(gp->state < REG_STATE_LAST);
stream->write_function(stream, "\t<gateway>\n");
stream->write_function(stream, "\t\t<profile>%s</profile>\n", profile->name);
stream->write_function(stream, "\t\t<to>%s</to>\n", gp->register_to);
stream->write_function(stream, "\t\t<state>%s</state>\n", sofia_state_names[gp->state]);
stream->write_function(stream, "\t\t<calls-in>%ld</calls-in>\n", gp->ib_calls);
stream->write_function(stream, "\t\t<calls-out>%ld</calls-out>\n", gp->ob_calls);
stream->write_function(stream, "\t\t<failed-calls-in>%ld</failed-calls-in>\n", gp->ib_failed_calls);
stream->write_function(stream, "\t\t<failed-calls-out>%ld</failed-calls-out>\n", gp->ob_failed_calls);
if (gp->state == REG_STATE_FAILED || gp->state == REG_STATE_TRYING) {
time_t now = switch_epoch_time_now(NULL);
if (gp->retry > now) {
stream->write_function(stream, "\t\t<retry>%ds</retry>\n", gp->retry - now);
} else {
stream->write_function(stream, "\t\t<retry>NEVER</retry>\n");
}
}
stream->write_function(stream, "\t</gateway>\n");
}
}
}
}
switch_mutex_unlock(mod_sofia_globals.hash_mutex);
stream->write_function(stream, "</gateways>\n");
return SWITCH_STATUS_SUCCESS;
}
if (!strcasecmp(argv[0], "gateway")) {
if ((gp = sofia_reg_find_gateway(argv[1]))) {
switch_assert(gp->state < REG_STATE_LAST);
stream->write_function(stream, "%s\n", header);
stream->write_function(stream, " <gateway>\n");
stream->write_function(stream, " <name>%s</name>\n", switch_str_nil(gp->name));
stream->write_function(stream, " <profile>%s</profile>\n", gp->profile->name);
@ -2742,7 +2686,61 @@ static switch_status_t cmd_xml_status(char **argv, int argc, switch_stream_handl
stream->write_function(stream, " <calls-out>%d</calls-out>\n", gp->ob_calls);
stream->write_function(stream, " <failed-calls-in>%d</failed-calls-in>\n", gp->ib_failed_calls);
stream->write_function(stream, " <failed-calls-out>%d</failed-calls-out>\n", gp->ob_failed_calls);
if (gp->state == REG_STATE_FAILED || gp->state == REG_STATE_TRYING) {
time_t now = switch_epoch_time_now(NULL);
if (gp->retry > now) {
stream->write_function(stream, " <retry>%ds</retry>\n", gp->retry - now);
} else {
stream->write_function(stream, " <retry>NEVER</retry>\n");
}
}
stream->write_function(stream, " </gateway>\n");
}
static switch_status_t cmd_xml_status(char **argv, int argc, switch_stream_handle_t *stream)
{
sofia_profile_t *profile = NULL;
sofia_gateway_t *gp;
switch_hash_index_t *hi;
void *val;
const void *vvar;
int c = 0;
int ac = 0;
const char *header = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
if (argc > 0) {
if (argc == 1) {
/* show summary of all gateways */
stream->write_function(stream, "%s\n", header);
stream->write_function(stream, "<gateways>\n", header);
switch_mutex_lock(mod_sofia_globals.hash_mutex);
for (hi = switch_hash_first(NULL, mod_sofia_globals.profile_hash); hi; hi = switch_hash_next(hi)) {
switch_hash_this(hi, &vvar, NULL, &val);
profile = (sofia_profile_t *) val;
if (sofia_test_pflag(profile, PFLAG_RUNNING)) {
if (!strcmp(vvar, profile->name)) { /* not an alias */
for (gp = profile->gateways; gp; gp = gp->next) {
switch_assert(gp->state < REG_STATE_LAST);
xml_gateway_status(gp, stream);
}
}
}
}
switch_mutex_unlock(mod_sofia_globals.hash_mutex);
stream->write_function(stream, "</gateways>\n");
} else if (argc == 1 && !strcasecmp(argv[0], "profile")) {
} else if (!strcasecmp(argv[0], "gateway")) {
if ((gp = sofia_reg_find_gateway(argv[1]))) {
switch_assert(gp->state < REG_STATE_LAST);
stream->write_function(stream, "%s\n", header);
xml_gateway_status(gp, stream);
sofia_reg_release_gateway(gp);
} else {
stream->write_function(stream, "Invalid Gateway!\n");