inital checkin of unimrcp svn revision 985 from http://unimrcp.googlecode.com/svn/trunk/

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@13790 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Michael Jerris committed 2009-06-16 22:31:19 +00:00
1 parent ec90221783
commit da925bddf8
349 files changed
+47866

No files matched your search

@@ -0,0 +1,198 @@
/*
* Copyright 2008 Arsen Chaloyan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <apr_getopt.h>
#include <apr_file_info.h>
#include "apt_pool.h"
#include "apt_dir_layout.h"
#include "apt_log.h"
typedef struct {
const char *root_dir_path;
apt_bool_t foreground;
apt_log_priority_e log_priority;
apt_log_output_e log_output;
} server_options_t;
#ifdef WIN32
apt_bool_t uni_service_run(apt_dir_layout_t *dir_layout, apr_pool_t *pool);
#else
apt_bool_t uni_daemon_run(apt_dir_layout_t *dir_layout, apr_pool_t *pool);
#endif
apt_bool_t uni_cmdline_run(apt_dir_layout_t *dir_layout, apr_pool_t *pool);
static void usage()
{
printf(
"\n"
"Usage:\n"
"\n"
" unimrcpserver [options]\n"
"\n"
" Available options:\n"
"\n"
" -r [--root-dir] path : Set the project root directory path.\n"
"\n"
" -l [--log-prio] priority : Set the log priority.\n"
" (0-emergency, ..., 7-debug)\n"
"\n"
" -o [--log-output] mode : Set the log output mode.\n"
" (0-none, 1-console only, 2-file only, 3-both)\n"
"\n"
#ifdef WIN32
" -s [--service] : Run as the Windows service.\n"
"\n"
#else
" -d [--daemon] : Run as the daemon.\n"
"\n"
#endif
" -h [--help] : Show the help.\n"
"\n");
}
static apt_bool_t options_load(server_options_t *options, int argc, const char * const *argv, apr_pool_t *pool)
{
apr_status_t rv;
apr_getopt_t *opt = NULL;
int optch;
const char *optarg;
const apr_getopt_option_t opt_option[] = {
/* long-option, short-option, has-arg flag, description */
{ "root-dir", 'r', TRUE, "path to root dir" }, /* -r arg or --root-dir arg */
{ "log-prio", 'l', TRUE, "log priority" }, /* -l arg or --log-prio arg */
{ "log-output", 'o', TRUE, "log output mode" }, /* -o arg or --log-output arg */
#ifdef WIN32
{ "service", 's', FALSE, "run as service" }, /* -s or --service */
#else
{ "daemon", 'd', FALSE, "start as daemon" }, /* -d or --daemon */
#endif
{ "help", 'h', FALSE, "show help" }, /* -h or --help */
{ NULL, 0, 0, NULL }, /* end */
};
rv = apr_getopt_init(&opt, pool , argc, argv);
if(rv != APR_SUCCESS) {
return FALSE;
}
while((rv = apr_getopt_long(opt, opt_option, &optch, &optarg)) == APR_SUCCESS) {
switch(optch) {
case 'r':
options->root_dir_path = optarg;
break;
case 'l':
if(optarg) {
options->log_priority = atoi(optarg);
}
break;
case 'o':
if(optarg) {
options->log_output = atoi(optarg);
}
break;
#ifdef WIN32
case 's':
options->foreground = FALSE;
break;
#else
case 'd':
options->foreground = FALSE;
break;
#endif
case 'h':
usage();
return FALSE;
}
}
if(rv != APR_EOF) {
usage();
return FALSE;
}
return TRUE;
}
int main(int argc, const char * const *argv)
{
apr_pool_t *pool = NULL;
server_options_t options;
apt_dir_layout_t *dir_layout;
/* APR global initialization */
if(apr_initialize() != APR_SUCCESS) {
apr_terminate();
return 0;
}
/* create APR pool */
pool = apt_pool_create();
if(!pool) {
apr_terminate();
return 0;
}
/* set the default options */
options.root_dir_path = "../";
options.foreground = TRUE;
options.log_priority = APT_PRIO_INFO;
options.log_output = APT_LOG_OUTPUT_CONSOLE;
/* load options */
if(options_load(&options,argc,argv,pool) != TRUE) {
apr_pool_destroy(pool);
apr_terminate();
return 0;
}
/* create the structure of default directories layout */
dir_layout = apt_default_dir_layout_create(options.root_dir_path,pool);
/* create singleton logger */
apt_log_instance_create(options.log_output,options.log_priority,pool);
if((options.log_output & APT_LOG_OUTPUT_FILE) == APT_LOG_OUTPUT_FILE) {
/* open the log file */
apt_log_file_open(dir_layout->log_dir_path,"unimrcpserver",MAX_LOG_FILE_SIZE,MAX_LOG_FILE_COUNT,pool);
}
if(options.foreground == TRUE) {
/* run command line */
uni_cmdline_run(dir_layout,pool);
}
#ifdef WIN32
else {
/* run as windows service */
uni_service_run(dir_layout,pool);
}
#else
else {
/* run as daemon */
uni_daemon_run(dir_layout,pool);
}
#endif
/* destroy singleton logger */
apt_log_instance_destroy();
/* destroy APR pool */
apr_pool_destroy(pool);
/* APR global termination */
apr_terminate();
return 0;
}
@@ -0,0 +1,81 @@
/*
* Copyright 2008 Arsen Chaloyan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include "unimrcp_server.h"
#include "apt_log.h"
static apt_bool_t cmdline_process(char *cmdline)
{
apt_bool_t running = TRUE;
char *name;
char *last;
name = apr_strtok(cmdline, " ", &last);
if(strcasecmp(name,"loglevel") == 0) {
char *priority = apr_strtok(NULL, " ", &last);
if(priority) {
apt_log_priority_set(atol(priority));
}
}
else if(strcasecmp(name,"exit") == 0 || strcmp(name,"quit") == 0) {
running = FALSE;
}
else if(strcasecmp(name,"help") == 0) {
printf("usage:\n");
printf("- loglevel [level] (set loglevel, one of 0,1...7)\n");
printf("- quit, exit\n");
}
else {
printf("unknown command: %s (input help for usage)\n",name);
}
return running;
}
apt_bool_t uni_cmdline_run(apt_dir_layout_t *dir_layout, apr_pool_t *pool)
{
apt_bool_t running = TRUE;
char cmdline[1024];
int i;
mrcp_server_t *server;
/* start server */
server = unimrcp_server_start(dir_layout);
if(!server) {
return FALSE;
}
do {
printf(">");
memset(&cmdline, 0, sizeof(cmdline));
for(i = 0; i < sizeof(cmdline); i++) {
cmdline[i] = (char) getchar();
if(cmdline[i] == '\n') {
cmdline[i] = '\0';
break;
}
}
if(*cmdline) {
running = cmdline_process(cmdline);
}
}
while(running != 0);
/* shutdown server */
unimrcp_server_shutdown(server);
return TRUE;
}
@@ -0,0 +1,50 @@
/*
* Copyright 2008 Arsen Chaloyan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <apr_signal.h>
#include <apr_thread_proc.h>
#include "unimrcp_server.h"
#include "apt_log.h"
static apt_bool_t daemon_running;
static void sigterm_handler(int signo)
{
daemon_running = FALSE;
}
apt_bool_t uni_daemon_run(apt_dir_layout_t *dir_layout, apr_pool_t *pool)
{
mrcp_server_t *server;
daemon_running = TRUE;
apr_signal(SIGTERM,sigterm_handler);
apt_log(APT_LOG_MARK,APT_PRIO_NOTICE,"Run as Daemon");
apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
/* start server */
server = unimrcp_server_start(dir_layout);
if(!server) {
return FALSE;
}
while(daemon_running) apr_sleep(1000000);
/* shutdown server */
unimrcp_server_shutdown(server);
return TRUE;
}
@@ -0,0 +1,104 @@
/*
* Copyright 2008 Arsen Chaloyan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <windows.h>
#include <apr_lib.h>
#include "unimrcp_server.h"
#include "apt_log.h"
#define WIN_SERVICE_NAME "unimrcp"
static SERVICE_STATUS_HANDLE win_service_status_handle = NULL;
static SERVICE_STATUS win_service_status;
static mrcp_server_t *server = NULL;
static apt_dir_layout_t *service_dir_layout = NULL;
/** SCM state change handler */
static void WINAPI win_service_handler(DWORD control)
{
apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Service Handler %d",control);
switch (control)
{
case SERVICE_CONTROL_INTERROGATE:
break;
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
if(server) {
win_service_status.dwCurrentState = SERVICE_STOP_PENDING;
if(!SetServiceStatus(win_service_status_handle, &win_service_status)) {
apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Set Service Status %d",GetLastError());
}
/* shutdown server */
unimrcp_server_shutdown(server);
}
win_service_status.dwCurrentState = SERVICE_STOPPED;
win_service_status.dwCheckPoint = 0;
win_service_status.dwWaitHint = 0;
break;
}
if(!SetServiceStatus(win_service_status_handle, &win_service_status)) {
apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Set Service Status %d",GetLastError());
}
}
static void WINAPI win_service_main(DWORD argc, LPTSTR *argv)
{
apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Service Main");
win_service_status.dwServiceType = SERVICE_WIN32;
win_service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
win_service_status.dwWin32ExitCode = 0;
win_service_status.dwServiceSpecificExitCode = 0;
win_service_status.dwCheckPoint = 0;
win_service_status.dwWaitHint = 0;
win_service_status_handle = RegisterServiceCtrlHandler(WIN_SERVICE_NAME, win_service_handler);
if(win_service_status_handle == (SERVICE_STATUS_HANDLE)0) {
apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Register Service Control Handler %d",GetLastError());
return;
}
win_service_status.dwCurrentState = SERVICE_START_PENDING;
if(!SetServiceStatus(win_service_status_handle, &win_service_status)) {
apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Set Service Status %d",GetLastError());
}
/* start server */
server = unimrcp_server_start(service_dir_layout);
win_service_status.dwCurrentState = server ? SERVICE_RUNNING : SERVICE_STOPPED;
if(!SetServiceStatus(win_service_status_handle, &win_service_status)) {
apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Set Service Status %d",GetLastError());
}
}
/** Run SCM service */
apt_bool_t uni_service_run(apt_dir_layout_t *dir_layout, apr_pool_t *pool)
{
SERVICE_TABLE_ENTRY win_service_table[] = {
{ WIN_SERVICE_NAME, win_service_main },
{ NULL, NULL }
};
service_dir_layout = dir_layout;
apt_log(APT_LOG_MARK,APT_PRIO_INFO,"Run as Service");
if(!StartServiceCtrlDispatcher(win_service_table)) {
apt_log(APT_LOG_MARK,APT_PRIO_WARNING,"Failed to Connect to SCM %d",GetLastError());
}
return TRUE;
}