a little more cleanup

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9827 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Michael Jerris 2008-10-04 07:01:30 +00:00
parent 3d9545c1b5
commit ed5dddb684

View File

@ -157,11 +157,15 @@ switch_status_t setMonoDirs()
#endif #endif
} }
switch_status_t loadModMonoManaged() switch_status_t loadRuntime()
{ {
/* Find and load mod_mono_managed.exe */ /* Find and load mod_mono_managed.exe */
char filename[256]; char filename[256];
if (setMonoDirs() != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
switch_snprintf(filename, 256, "%s%s%s", SWITCH_GLOBAL_dirs.mod_dir, SWITCH_PATH_SEPARATOR, MOD_MANAGED_DLL); switch_snprintf(filename, 256, "%s%s%s", SWITCH_GLOBAL_dirs.mod_dir, SWITCH_PATH_SEPARATOR, MOD_MANAGED_DLL);
globals.domain = mono_jit_init(filename); globals.domain = mono_jit_init(filename);
@ -251,59 +255,6 @@ switch_status_t findLoader()
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Found all loader functions.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Found all loader functions.\n");
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
SWITCH_MODULE_LOAD_FUNCTION(mod_managed_load)
{
/* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Loading mod_managed (Common Language Infrastructure), Mono Version\n");
globals.pool = pool;
if (setMonoDirs() != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
if (loadModMonoManaged() != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
if (findLoader() != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
/* Not sure if this is necesary on the loading thread */
mono_thread_attach(globals.domain);
/* Run loader */
MonoObject * objResult;
MonoObject * exception = NULL;
objResult = mono_runtime_invoke(globals.loadMethod, NULL, NULL, &exception);
if (exception) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Load threw an exception.\n");
mono_print_unhandled_exception(exception);
return SWITCH_STATUS_FALSE;
}
if (*(int *) mono_object_unbox(objResult)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Load completed successfully.\n");
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Load did not return true.\n");
return SWITCH_STATUS_FALSE;
}
/* We're good to register */
switch_api_interface_t *api_interface;
switch_application_interface_t *app_interface;
SWITCH_ADD_API(api_interface, "managedrun", "Run a module (ExecuteBackground)", managedrun_api_function, "<module> [<args>]");
SWITCH_ADD_API(api_interface, "managed", "Run a module as an API function (Execute)", managed_api_function, "<module> [<args>]");
SWITCH_ADD_APP(app_interface, "managed", "Run Mono IVR", "Run a Mono IVR on a channel", managed_app_function, "<modulename> [<args>]", SAF_NONE);
return SWITCH_STATUS_SUCCESS;
}
#endif #endif
/********************************************************** /**********************************************************
@ -326,7 +277,7 @@ SWITCH_MOD_DECLARE(void) InitManagedSession(ManagedSession *session, inputFuncti
session->hangupDelegate = hangupDelegate; session->hangupDelegate = hangupDelegate;
} }
switch_status_t loadModDotnetManaged() switch_status_t loadRuntime()
{ {
/* Find and load mod_dotnet_managed.dll */ /* Find and load mod_dotnet_managed.dll */
char filename[256]; char filename[256];
@ -362,26 +313,29 @@ switch_status_t findLoader()
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Found all FreeSWITCH.Loader functions.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Found all FreeSWITCH.Loader functions.\n");
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
#endif
SWITCH_MODULE_LOAD_FUNCTION(mod_managed_load) SWITCH_MODULE_LOAD_FUNCTION(mod_managed_load)
{ {
int success;
/* connect my internal structure to the blank pointer passed to me */ /* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname); *module_interface = switch_loadable_module_create_module_interface(pool, modname);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Loading mod_managed (Common Language Infrastructure), Microsoft CLR Version\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Loading mod_managed (Common Language Infrastructure), Microsoft CLR Version\n");
globals.pool = pool; globals.pool = pool;
if (loadModDotnetManaged() != SWITCH_STATUS_SUCCESS) { if (loadRuntime() != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
if (findLoader() != SWITCH_STATUS_SUCCESS) { if (findLoader() != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
#ifdef _MANAGED
Object ^objResult; Object ^objResult;
try { try {
objResult = FreeSwitchManaged::loadMethod->Invoke(nullptr, nullptr); objResult = FreeSwitchManaged::loadMethod->Invoke(nullptr, nullptr);
success = *reinterpret_cast<bool^>(objResult);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Load completed successfully.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Load completed successfully.\n");
} }
catch(Exception^ ex) { catch(Exception^ ex) {
@ -390,8 +344,24 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_managed_load)
Marshal::FreeHGlobal(msg); Marshal::FreeHGlobal(msg);
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
#else
/* Not sure if this is necesary on the loading thread */
mono_thread_attach(globals.domain);
if (*reinterpret_cast<bool^>(objResult)) { /* Run loader */
MonoObject * objResult;
MonoObject * exception = NULL;
objResult = mono_runtime_invoke(globals.loadMethod, NULL, NULL, &exception);
success = *(int *) mono_object_unbox(objResult);
if (exception) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Load threw an exception.\n");
mono_print_unhandled_exception(exception);
return SWITCH_STATUS_FALSE;
}
#endif
if (success) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Load completed successfully.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Load completed successfully.\n");
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Load did not return true.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Load did not return true.\n");
@ -407,7 +377,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_managed_load)
SWITCH_ADD_APP(app_interface, "managed", "Run CLI App", "Run an App on a channel", managed_app_function, "<modulename> [<args>]", SAF_NONE); SWITCH_ADD_APP(app_interface, "managed", "Run CLI App", "Run an App on a channel", managed_app_function, "<modulename> [<args>]", SAF_NONE);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
#endif
SWITCH_STANDARD_API(managedrun_api_function) SWITCH_STANDARD_API(managedrun_api_function)
{ {
@ -420,6 +389,7 @@ SWITCH_STANDARD_API(managedrun_api_function)
Object ^objResult; Object ^objResult;
try { try {
objResult = FreeSwitchManaged::executeBackgroundMethod->Invoke(nullptr, gcnew array<Object^> { gcnew String(cmd) } ); objResult = FreeSwitchManaged::executeBackgroundMethod->Invoke(nullptr, gcnew array<Object^> { gcnew String(cmd) } );
success = *reinterpret_cast<bool^>(objResult);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Load completed successfully.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Load completed successfully.\n");
} catch(Exception^ ex) { } catch(Exception^ ex) {
IntPtr msg = Marshal::StringToHGlobalAnsi(ex->ToString()); IntPtr msg = Marshal::StringToHGlobalAnsi(ex->ToString());
@ -428,7 +398,6 @@ SWITCH_STANDARD_API(managedrun_api_function)
Marshal::FreeHGlobal(msg); Marshal::FreeHGlobal(msg);
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
success = *reinterpret_cast<bool^>(objResult);
#else #else
mono_thread_attach(globals.domain); mono_thread_attach(globals.domain);
void *args[1]; void *args[1];
@ -436,13 +405,13 @@ SWITCH_STANDARD_API(managedrun_api_function)
args[0] = mono_string_new(globals.domain, cmd); args[0] = mono_string_new(globals.domain, cmd);
MonoObject * exception = NULL; MonoObject * exception = NULL;
MonoObject * objResult = mono_runtime_invoke(globals.executeBackgroundMethod, NULL, args, &exception); MonoObject * objResult = mono_runtime_invoke(globals.executeBackgroundMethod, NULL, args, &exception);
success = *(int *) mono_object_unbox(objResult);
if (exception) { if (exception) {
stream->write_function(stream, "-ERR FreeSWITCH.Loader.ExecuteBackground threw an exception.\n"); stream->write_function(stream, "-ERR FreeSWITCH.Loader.ExecuteBackground threw an exception.\n");
mono_print_unhandled_exception(exception); mono_print_unhandled_exception(exception);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
success = *(int *) mono_object_unbox(objResult);
#endif #endif
if (success) { if (success) {
stream->write_function(stream, "+OK\n"); stream->write_function(stream, "+OK\n");
@ -466,13 +435,13 @@ SWITCH_STANDARD_API(managed_api_function)
try { try {
objResult = FreeSwitchManaged::executeMethod->Invoke(nullptr, gcnew array<Object^>{gcnew String(cmd),gcnew IntPtr(stream), gcnew IntPtr(stream->param_event)}); objResult = FreeSwitchManaged::executeMethod->Invoke(nullptr, gcnew array<Object^>{gcnew String(cmd),gcnew IntPtr(stream), gcnew IntPtr(stream->param_event)});
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Execute completed successfully.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Execute completed successfully.\n");
success = *reinterpret_cast<bool^>(objResult);
} catch(Exception ^ex) { } catch(Exception ^ex) {
IntPtr msg = Marshal::StringToHGlobalAnsi(ex->ToString()); IntPtr msg = Marshal::StringToHGlobalAnsi(ex->ToString());
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute cli %s: %s\n", cmd, static_cast<const char*>(msg.ToPointer())); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute cli %s: %s\n", cmd, static_cast<const char*>(msg.ToPointer()));
Marshal::FreeHGlobal(msg); Marshal::FreeHGlobal(msg);
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
} }
success = *reinterpret_cast<bool^>(objResult);
#else #else
mono_thread_attach(globals.domain); mono_thread_attach(globals.domain);
void *args[3]; void *args[3];
@ -483,12 +452,12 @@ SWITCH_STANDARD_API(managed_api_function)
MonoObject * exception = NULL; MonoObject * exception = NULL;
MonoObject * objResult = mono_runtime_invoke(globals.executeMethod, NULL, args, &exception); MonoObject * objResult = mono_runtime_invoke(globals.executeMethod, NULL, args, &exception);
success = *(int *) mono_object_unbox(objResult);
if (exception) { if (exception) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute mono %s.\n", cmd); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute mono %s.\n", cmd);
mono_print_unhandled_exception(exception); mono_print_unhandled_exception(exception);
} }
success = *(int *) mono_object_unbox(objResult);
#endif #endif
if (!success) { if (!success) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Execute failed for %s (unknown module?).\n", cmd); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Execute failed for %s (unknown module?).\n", cmd);
@ -508,6 +477,7 @@ SWITCH_STANDARD_APP(managed_app_function)
try { try {
objResult = FreeSwitchManaged::runMethod->Invoke(nullptr, gcnew array<Object^>{gcnew String(data),gcnew IntPtr(session)}); objResult = FreeSwitchManaged::runMethod->Invoke(nullptr, gcnew array<Object^>{gcnew String(data),gcnew IntPtr(session)});
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "RunMethod completed successfully.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "RunMethod completed successfully.\n");
success = *reinterpret_cast<bool^>(objResult);
} }
catch(Exception ^ex) { catch(Exception ^ex) {
IntPtr msg = Marshal::StringToHGlobalAnsi(ex->ToString()); IntPtr msg = Marshal::StringToHGlobalAnsi(ex->ToString());
@ -515,7 +485,6 @@ SWITCH_STANDARD_APP(managed_app_function)
Marshal::FreeHGlobal(msg); Marshal::FreeHGlobal(msg);
return; return;
} }
success = *reinterpret_cast<bool^>(objResult);
#else #else
mono_thread_attach(globals.domain); mono_thread_attach(globals.domain);
void *args[2]; void *args[2];
@ -525,12 +494,12 @@ SWITCH_STANDARD_APP(managed_app_function)
MonoObject * exception = NULL; MonoObject * exception = NULL;
MonoObject * objResult = mono_runtime_invoke(globals.runMethod, NULL, args, &exception); MonoObject * objResult = mono_runtime_invoke(globals.runMethod, NULL, args, &exception);
success = *(int *) mono_object_unbox(objResult);
if (exception) { if (exception) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute application mono %s.\n", data); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception trying to execute application mono %s.\n", data);
mono_print_unhandled_exception(exception); mono_print_unhandled_exception(exception);
} }
success = *(int *) mono_object_unbox(objResult);
#endif #endif
if (!success) { if (!success) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Application run failed for %s (unknown module?).\n", data); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Application run failed for %s (unknown module?).\n", data);