tweaks for daemon mode and wanpipe

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@665 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2006-02-24 19:11:49 +00:00
parent a54191c42f
commit 50b78f79ee
4 changed files with 83 additions and 26 deletions

View File

@ -103,9 +103,10 @@ struct switch_core_runtime;
///\{ ///\{
/*! /*!
\brief Initilize the core \brief Initilize the core
\param console optional FILE stream for output
\note to be called at application startup \note to be called at application startup
*/ */
SWITCH_DECLARE(switch_status) switch_core_init(void); SWITCH_DECLARE(switch_status) switch_core_init(FILE *console);
/*! /*!
\brief Destroy the core \brief Destroy the core
@ -883,6 +884,12 @@ SWITCH_DECLARE(switch_status) switch_core_directory_close(switch_directory_handl
*/ */
SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel channel); SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel channel);
/*!
\brief Set the output console to the desired FILE stream
\param handle the FILE stream
*/
SWITCH_DECLARE(void) switch_core_set_console(FILE *handle);
/*! /*!
\brief Launch a thread \brief Launch a thread
*/ */

View File

@ -63,7 +63,7 @@ typedef enum {
TFLAG_SWITCH = (1 << 9), TFLAG_SWITCH = (1 << 9),
} TFLAGS; } TFLAGS;
#define PACKET_LEN 160
#define DEFAULT_MTU 160 #define DEFAULT_MTU 160
static struct { static struct {
@ -205,7 +205,7 @@ static int check_flags(struct sangoma_pri *spri);
static int on_restart(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event); static int on_restart(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event);
static int on_anything(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event); static int on_anything(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event);
static void *pri_thread_run(switch_thread *thread, void *obj); static void *pri_thread_run(switch_thread *thread, void *obj);
static int config_wanpipe(int reload); static switch_status config_wanpipe(int reload);
/* /*
@ -219,6 +219,7 @@ static switch_status wanpipe_on_init(switch_core_session *session)
switch_channel *channel = NULL; switch_channel *channel = NULL;
wanpipe_tdm_api_t tdm_api; wanpipe_tdm_api_t tdm_api;
int err = 0; int err = 0;
int mtu_mru;
unsigned int rate = 8000; unsigned int rate = 8000;
channel = switch_core_session_get_channel(session); channel = switch_core_session_get_channel(session);
@ -229,10 +230,15 @@ static switch_status wanpipe_on_init(switch_core_session *session)
tech_pvt->read_frame.data = tech_pvt->databuf; tech_pvt->read_frame.data = tech_pvt->databuf;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WANPIPE INIT\n");
err = sangoma_tdm_set_codec(tech_pvt->socket, &tdm_api, WP_SLINEAR); err = sangoma_tdm_set_codec(tech_pvt->socket, &tdm_api, WP_SLINEAR);
mtu_mru = sangoma_tdm_get_usr_mtu_mru(tech_pvt->socket, &tdm_api);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WANPIPE INIT MTU is %d\n", mtu_mru);
if (mtu_mru != globals.mtu) {
sangoma_tdm_set_usr_period(tech_pvt->socket, &tdm_api, (globals.mtu / 8) / 2);
mtu_mru = sangoma_tdm_get_usr_mtu_mru(tech_pvt->socket, &tdm_api);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "ADJUSTED MTU is %d\n", mtu_mru);
}
if (switch_core_codec_init if (switch_core_codec_init
(&tech_pvt->read_codec, "L16", rate, 20, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, (&tech_pvt->read_codec, "L16", rate, 20, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
@ -455,8 +461,6 @@ static switch_status wanpipe_outgoing_channel(switch_core_session *session, swit
if (spri && (tech_pvt->call = pri_new_call(spri->pri))) { if (spri && (tech_pvt->call = pri_new_call(spri->pri))) {
struct pri_sr *sr; struct pri_sr *sr;
int mtu_mru;
wanpipe_tdm_api_t tdm_api;
sr = pri_sr_new(); sr = pri_sr_new();
pri_sr_set_channel(sr, channo, 0, 0); pri_sr_set_channel(sr, channo, 0, 0);
@ -487,9 +491,6 @@ static switch_status wanpipe_outgoing_channel(switch_core_session *session, swit
pri_sr_free(sr); pri_sr_free(sr);
chanmap->map[channo] = *new_session; chanmap->map[channo] = *new_session;
tech_pvt->spri = spri; tech_pvt->spri = spri;
mtu_mru = sangoma_tdm_get_usr_mtu_mru(tech_pvt->socket, &tdm_api);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "MTU is %d\n", mtu_mru);
} }
} else { } else {
@ -608,8 +609,8 @@ static switch_status wanpipe_write_frame(switch_core_session *session, switch_fr
assert(tech_pvt != NULL); assert(tech_pvt != NULL);
while (tech_pvt->dtmf_buffer && bwrote < frame->datalen && bytes > 0 && (inuse = switch_buffer_inuse(tech_pvt->dtmf_buffer)) > 0) { while (tech_pvt->dtmf_buffer && bwrote < frame->datalen && bytes > 0 && (inuse = switch_buffer_inuse(tech_pvt->dtmf_buffer)) > 0) {
if ((bread = switch_buffer_read(tech_pvt->dtmf_buffer, dtmf, PACKET_LEN)) < PACKET_LEN) { if ((bread = switch_buffer_read(tech_pvt->dtmf_buffer, dtmf, globals.mtu)) < globals.mtu) {
while (bread < PACKET_LEN) { while (bread < globals.mtu) {
dtmf[bread++] = 0; dtmf[bread++] = 0;
} }
} }
@ -637,11 +638,11 @@ static switch_status wanpipe_write_frame(switch_core_session *session, switch_fr
while (bytes > 0) { while (bytes > 0) {
sangoma_socket_waitfor(tech_pvt->socket, -1, POLLOUT | POLLERR | POLLHUP); sangoma_socket_waitfor(tech_pvt->socket, -1, POLLOUT | POLLERR | POLLHUP);
res = sangoma_sendmsg_socket(tech_pvt->socket, res = sangoma_sendmsg_socket(tech_pvt->socket,
&tech_pvt->hdrframe, sizeof(tech_pvt->hdrframe), bp, PACKET_LEN, 0); &tech_pvt->hdrframe, sizeof(tech_pvt->hdrframe), bp, globals.mtu, 0);
if (res < 0) { if (res < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, switch_console_printf(SWITCH_CHANNEL_CONSOLE,
"Bad Write frame len %d write %d bytes returned %d (%s)!\n", frame->datalen, "Bad Write frame len %d write %d bytes returned %d (%s)!\n", frame->datalen,
PACKET_LEN, res, strerror(errno)); globals.mtu, res, strerror(errno));
if (errno == EBUSY) { if (errno == EBUSY) {
continue; continue;
} }
@ -753,8 +754,19 @@ static const switch_loadable_module_interface wanpipe_module_interface = {
/*.application_interface */ NULL /*.application_interface */ NULL
}; };
static void s_pri_error(struct pri *pri, char *s)
{
switch_console_printf(SWITCH_CHANNEL_CONSOLE, s);
}
static void s_pri_message(struct pri *pri, char *s)
{
s_pri_error(pri, s);
}
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{ {
switch_status status = SWITCH_STATUS_SUCCESS;
memset(SPANS, 0, sizeof(SPANS)); memset(SPANS, 0, sizeof(SPANS));
@ -764,13 +776,19 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
} }
/* start the pri's */ /* start the pri's */
config_wanpipe(0); if ((status = config_wanpipe(0) != SWITCH_STATUS_SUCCESS)) {
return status;
}
pri_set_error(s_pri_error);
pri_set_message(s_pri_message);
/* connect my internal structure to the blank pointer passed to me */ /* connect my internal structure to the blank pointer passed to me */
*interface = &wanpipe_module_interface; *interface = &wanpipe_module_interface;
/* indicate that the module should continue to be loaded */ /* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS; return status;
} }
@ -1076,7 +1094,7 @@ static void pri_thread_launch(struct sangoma_pri *spri)
} }
static int config_wanpipe(int reload) static switch_status config_wanpipe(int reload)
{ {
switch_config cfg; switch_config cfg;
char *var, *val; char *var, *val;

View File

@ -43,8 +43,36 @@ int main(int argc, char *argv[])
{ {
char *err = NULL; char *err = NULL;
switch_event *event; switch_event *event;
int bg = 0;
FILE *out = NULL;
if (switch_core_init() != SWITCH_STATUS_SUCCESS) { if (argv[1] && !strcmp(argv[1], "-nc")) {
bg++;
}
if (bg) {
int pid;
#ifdef WIN32
char *path = ".\\freeswitch.log";
#else
char *path = "/var/log/freeswitch.log";
nice(-20);
#endif
if ((out = fopen(path, "a")) == 0) {
fprintf(stderr, "Cannot open output file.\n");
return 255;
}
(void) signal(SIGHUP, (void *) handle_SIGHUP);
if ((pid = fork())) {
fprintf(stderr, "%d Backgrounding.\n", (int)pid);
exit(0);
}
}
if (switch_core_init(out) != SWITCH_STATUS_SUCCESS) {
err = "Cannot Initilize\n"; err = "Cannot Initilize\n";
} }
@ -68,13 +96,12 @@ int main(int argc, char *argv[])
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "freeswitch Version %s Started\n\n", SWITCH_VERSION_FULL); switch_console_printf(SWITCH_CHANNEL_CONSOLE, "freeswitch Version %s Started\n\n", SWITCH_VERSION_FULL);
if (argv[1] && !strcmp(argv[1], "-nc")) { if (bg) {
(void) signal(SIGHUP, (void *) handle_SIGHUP);
RUNNING = 1; RUNNING = 1;
while(RUNNING) { while(RUNNING) {
switch_yield(10000); switch_yield(10000);
} }
} else { } else {
/* wait for console input */ /* wait for console input */
switch_console_loop(); switch_console_loop();
} }

View File

@ -157,6 +157,11 @@ SWITCH_DECLARE(switch_core_db *) switch_core_db_open_file(char *filename)
return db; return db;
} }
SWITCH_DECLARE(void) switch_core_set_console(FILE *handle)
{
runtime.console = handle;
}
SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel channel) SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel channel)
{ {
FILE *handle = stdout; FILE *handle = stdout;
@ -167,7 +172,7 @@ SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel channel)
handle = runtime.console; handle = runtime.console;
break; break;
default: default:
handle = stdout; handle = runtime.console;
break; break;
} }
@ -2190,13 +2195,13 @@ static void core_event_handler(switch_event *event)
} }
} }
SWITCH_DECLARE(switch_status) switch_core_init(void) SWITCH_DECLARE(switch_status) switch_core_init(FILE *console)
{ {
#ifdef EMBED_PERL #ifdef EMBED_PERL
PerlInterpreter *my_perl; PerlInterpreter *my_perl;
#endif #endif
runtime.console = stdout; runtime.console = console ? console : stdout;
/* INIT APR and Create the pool context */ /* INIT APR and Create the pool context */
if (apr_initialize() != APR_SUCCESS) { if (apr_initialize() != APR_SUCCESS) {