[Freeswitch-branches] [commit] r2655 - freeswitch/branches/james/src/mod/languages/mod_mono

Freeswitch SVN james at freeswitch.org
Tue Sep 12 04:58:24 EDT 2006


Author: james
Date: Tue Sep 12 04:58:23 2006
New Revision: 2655

Modified:
   freeswitch/branches/james/src/mod/languages/mod_mono/Makefile
   freeswitch/branches/james/src/mod/languages/mod_mono/mod_mono.c

Log:
Cleaning up, adding error checking, etc before release.


Modified: freeswitch/branches/james/src/mod/languages/mod_mono/Makefile
==============================================================================
--- freeswitch/branches/james/src/mod/languages/mod_mono/Makefile	(original)
+++ freeswitch/branches/james/src/mod/languages/mod_mono/Makefile	Tue Sep 12 04:58:23 2006
@@ -1,14 +1,22 @@
+VERSION = mono-1.1.13.8
+TARBALL = mono-1.1.13.8.tar.gz
 CFLAGS += `pkg-config --cflags mono`
 LDFLAGS += `pkg-config --libs mono` 
 
-all: $(MODNAME).so 
+all:	depends $(MODNAME).$(DYNAMIC_LIB_EXTEN)
 
-$(MODNAME).so: $(MODNAME).c
+depends:
+	MAKE=$(MAKE) $(BASE)/build/buildlib.sh $(BASE) install $(TARBALL) --prefix=$(PREFIX) --with-pic
+
+%.o:  %.c
+	$(CC) -fPIC $(CFLAGS) -c -o $@ $<
+
+$(MODNAME).$(DYNAMIC_LIB_EXTEN): $(MODNAME).c
 	$(CC) $(CFLAGS) -fPIC -c $(MODNAME).c -o $(MODNAME).o 
-	$(CC) $(SOLINK) -o $(MODNAME).so $(MODNAME).o $(LDFLAGS)
+	$(LINKER) $(SOLINK) -o $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(MODNAME).o $(LDFLAGS)
 
 clean:
-	rm -fr *.so *.o *~
+	rm -fr *.$(DYNAMIC_LIB_EXTEN) *.o *~
 
 install:
-	cp -f $(MODNAME).so $(PREFIX)/mod
+	cp -f $(MODNAME).$(DYNAMIC_LIB_EXTEN) $(PREFIX)/mod

Modified: freeswitch/branches/james/src/mod/languages/mod_mono/mod_mono.c
==============================================================================
--- freeswitch/branches/james/src/mod/languages/mod_mono/mod_mono.c	(original)
+++ freeswitch/branches/james/src/mod/languages/mod_mono/mod_mono.c	Tue Sep 12 04:58:23 2006
@@ -39,27 +39,23 @@
 #include <gc/gc.h>
 #include <switch.h>
 
-#define SWITCH_MONO_MODULES    "/usr/local/freeswitch/mod/mono"
-#define SWITCH_MONO_ASSEMBLY   "Freeswitch.dll"
+#define SWITCH_MONO_MODULES  "mono/" 
+#define SWITCH_MONO_LIBDIR   "lib/"
+#define SWITCH_MONO_ASSEMBLY "Freeswitch.dll"
 
-switch_status_t mod_mono_load_plugins();
-void mono_switch_console_printf(switch_text_channel_t channel, char *file, const char *func, int line, char *fmt, char *msg);
-MonoClass* find_assembly_class(MonoImage *image);
+/* Module functions */
+switch_status_t mod_mono_load_modules(const char *module_dir);
+MonoClass* mod_mono_find_assembly_class(MonoImage *image);
 
+/* Managed functions */
+void mod_mono_switch_console_printf(switch_text_channel_t channel, char *file, const char *func, int line, char *fmt, char *msg);
+
 static const char modname[] = "mod_mono";
+static switch_memory_pool_t *mono_pool = NULL;
 
-switch_memory_pool_t *mono_pool;
-switch_hash_t *mono_plugins;
-
 static struct {
 	MonoDomain *domain;
-	MonoObject *object;
-	MonoImage *image;
-	MonoClass *class;
-	MonoAssembly *assembly;
-	MonoMethod *event;
-	MonoMethod *api;
-	MonoMethod *application;
+	switch_hash_t *plugins;
 } globals;
 
 typedef struct {
@@ -72,11 +68,6 @@
 	/*.module_name */ modname,
 };
 
-void mono_switch_console_printf(switch_text_channel_t channel, char *file, const char *func, int line, char *fmt, char *msg)
-{
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, msg);
-}
-
 /*
  * Load mod_mono module and initialise domain
  *
@@ -85,53 +76,89 @@
  */
 SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **interface, char *filename)
 {
-	apr_hash_index_t *plugin;
-	
 	*interface = &mono_module_interface;
 
 	/* Initialise memory pool */
 	if (switch_core_new_memory_pool(&mono_pool) != SWITCH_STATUS_SUCCESS) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Could not allocate memory pool\n");
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not allocate memory pool\n");
 		return SWITCH_STATUS_FALSE;
 	}
 	
 	/* Initialise plugin hash */
-	if (switch_core_hash_init(&mono_plugins, mono_pool) != SWITCH_STATUS_SUCCESS) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Could not initialise plugins hash.\n");
+	if (switch_core_hash_init(&globals.plugins, mono_pool) != SWITCH_STATUS_SUCCESS) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not initialise plugins hash\n");
 		return SWITCH_STATUS_FALSE;
 	}		
 	
-	/* Initialise the mono domain */
-	if (!(globals.domain = mono_jit_init("freeswitch"))) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Could not initialise mono runtime.\n");
+	/* Construct the path to the Freeswitch assembly, then check to make sure it exists */
+	switch_size_t assembly_dir_len  = strlen(SWITCH_GLOBAL_dirs.base_dir) + strlen(SWITCH_MONO_LIBDIR) + 2; /* Account for / and \0 */
+	switch_size_t assembly_file_len = assembly_dir_len + strlen(SWITCH_MONO_ASSEMBLY);
+	char *assembly_dir  = (char *) switch_core_alloc(mono_pool, assembly_dir_len);
+	char *assembly_file = (char *) switch_core_alloc(mono_pool, assembly_file_len);
+
+	apr_finfo_t *finfo = (apr_finfo_t *) switch_core_alloc(mono_pool, sizeof(*finfo));
+
+	snprintf(assembly_dir, assembly_dir_len, "%s/%s", SWITCH_GLOBAL_dirs.base_dir, SWITCH_MONO_LIBDIR);
+	snprintf(assembly_file, assembly_file_len, "%s/%s%s", SWITCH_GLOBAL_dirs.base_dir, SWITCH_MONO_LIBDIR, SWITCH_MONO_ASSEMBLY);
+
+	if (apr_stat(finfo, assembly_file, 0, mono_pool) != SWITCH_STATUS_SUCCESS) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Could not find Freeswitch.NET assembly");
 		return SWITCH_STATUS_FALSE;
 	}
+
+	/* Mono wants to know where it will be able to find the Freeswitch assembly if it's not in the GAC */
+	if (getenv("MONO_PATH") != NULL) {
+		switch_size_t mono_path_len = strlen(getenv("MONO_PATH")) + strlen(assembly_dir) + 2; /* Account for : and \0 */ 
+		char *mono_path = (char *) switch_core_alloc(mono_pool, mono_path_len);
 	
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Initialised mono runtime.\n");
+		snprintf(mono_path, mono_path_len, "%s:%s", getenv("MONO_PATH"), assembly_dir);
+
+		setenv("MONO_PATH", mono_path, 1);
+	}
+	else
+		setenv("MONO_PATH", assembly_dir, 1);
+
+	/* Now find where our managed modules are */
+	switch_size_t module_dir_len = strlen(SWITCH_GLOBAL_dirs.mod_dir) + strlen(SWITCH_MONO_MODULES) + 2; /* Account for / and \0 */
+	char *module_dir = (char *) switch_core_alloc(mono_pool, module_dir_len);
+
+	snprintf(module_dir, module_dir_len, "%s/%s", SWITCH_GLOBAL_dirs.mod_dir, SWITCH_MONO_MODULES);
 	
-	/* Load our plugins */
-	if (mod_mono_load_plugins() != SWITCH_STATUS_SUCCESS) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error loading plugins.\n");
+	/* Initialise the mono domain */
+	if (!(globals.domain = mono_jit_init("freeswitch"))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error initialising mono runtime\n");
 		return SWITCH_STATUS_FALSE;
 	}
 
-	for (plugin = switch_hash_first(mono_pool, mono_plugins); plugin; plugin = switch_hash_next(plugin)) {
-		mono_plugin *p = (mono_plugin *)switch_core_alloc(mono_pool, sizeof(*p));
-		apr_ssize_t *key_length = NULL;
-		const void *k;
-		void *v;
+	/* Let user know everything initialised fine */
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Initialised mono runtime\n");
 
-		switch_hash_this(plugin, &k, key_length, &v);
-		p = (mono_plugin *)v;
+	/* Load our modules */
+	if (mod_mono_load_modules(module_dir) != SWITCH_STATUS_SUCCESS) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error loading modules\n");
+		return SWITCH_STATUS_FALSE;
+	}
+
+	/* Finally, go through and initialise each plugin before returning SUCCESS */ 
+	switch_hash_index_t *p = NULL;
+
+	for (p = switch_hash_first(mono_pool, globals.plugins); p; p = switch_hash_next(p)) {
+		mono_plugin *plugin = (mono_plugin *) switch_core_alloc(mono_pool, sizeof(*plugin));
+		switch_size_t *key_length = NULL;
+		const void *key = NULL;
+		void *value = NULL;
+
+		switch_hash_this(p, &key, key_length, &value);
+		plugin = (mono_plugin *) value;
 		
-		mono_runtime_object_init(p->object);
+		mono_runtime_object_init(plugin->object);
 	}
 	
 	return SWITCH_STATUS_SUCCESS;
 }
 
 /*
- * Function for cleaning shutting down mod_mono
+ * Function for cleanly shutting down mod_mono
  * 
  */
 SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void)
@@ -146,39 +173,38 @@
 }
 
 /*
- * This function will load the managed portion of mod_mono.
+ * This function will load the managed modules
+ *
  */ 
-switch_status_t mod_mono_load_plugins()
+switch_status_t mod_mono_load_modules(const char *module_dir)
 {
+	apr_dir_t *module_dir_handle;
 	char *file;
 	size_t len;
 	char *ptr;
 	apr_finfo_t finfo;
-	apr_dir_t *module_dir_handle;
 	apr_int32_t finfo_flags = APR_FINFO_DIRENT | APR_FINFO_TYPE | APR_FINFO_NAME;
 	const char *ext = ".dll";
 	const char *EXT = ".DLL";
-	apr_pool_t *mp;
-	apr_pool_create(&mp, NULL);
 	MonoAssembly *assembly;
 	MonoImage *image;
 	gpointer iter;
 	iter=NULL;
 	mono_plugin *plugin=NULL;
 
-	if (apr_dir_open(&module_dir_handle, SWITCH_MONO_MODULES, mp) != APR_SUCCESS)
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Could not open directory: %s\n", SWITCH_MONO_MODULES);
+	if (apr_dir_open(&module_dir_handle, module_dir, mono_pool) != SWITCH_STATUS_SUCCESS)
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Could not open directory: %s\n", module_dir);
 
 	/* Read the modules directory */
-	while (apr_dir_read(&finfo, finfo_flags, module_dir_handle) == APR_SUCCESS)
+	while (apr_dir_read(&finfo, finfo_flags, module_dir_handle) == SWITCH_STATUS_SUCCESS)
 	{
-		assembly = (MonoAssembly *)switch_core_alloc(mono_pool, sizeof(assembly));
-		image = (MonoImage *)switch_core_alloc(mono_pool, sizeof(image));
+		assembly = (MonoAssembly *) switch_core_alloc(mono_pool, sizeof(assembly));
+		image = (MonoImage *) switch_core_alloc(mono_pool, sizeof(image));
 		const char *fname = finfo.fname;
 
 		if (finfo.filetype != APR_REG)
 			continue;
-
+                        
 		if (!fname)
 			fname = finfo.name;
 
@@ -188,9 +214,9 @@
 		if (!strstr(fname, ext) && !strstr(fname, EXT))
 			continue;
 
-		len = strlen(SWITCH_MONO_MODULES) + strlen(fname) + 3;
+		len = strlen(module_dir) + strlen(fname) + 2;
 		file = (char *)switch_core_alloc(mono_pool, len);
-		snprintf(file, len, "%s/%s", SWITCH_MONO_MODULES, fname);
+		snprintf(file, len, "%s%s", module_dir, fname);
 
 		/* Attempt to open the assembly */
 		assembly = mono_domain_assembly_open(globals.domain, file);
@@ -206,21 +232,24 @@
 		plugin = (mono_plugin *)switch_core_alloc(mono_pool, sizeof(*plugin));
 
 		plugin->assembly = assembly;
-		plugin->class    = find_assembly_class(mono_assembly_get_image(plugin->assembly));
+		plugin->class    = mod_mono_find_assembly_class(mono_assembly_get_image(plugin->assembly));
 
 		if (!plugin->class)
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "No plugin class\n");
 
 		plugin->object = mono_object_new(globals.domain, plugin->class);
 
-		switch_core_hash_insert(mono_plugins, (char *) mono_image_get_name(mono_assembly_get_image(assembly)), plugin);
+		switch_core_hash_insert(globals.plugins, (char *) mono_image_get_name(mono_assembly_get_image(assembly)), plugin);
 
 		plugin = NULL;
 	}	
 	return SWITCH_STATUS_SUCCESS;
 }
 
-MonoClass* find_assembly_class(MonoImage *image)
+/*
+ *
+ */ 
+MonoClass* mod_mono_find_assembly_class(MonoImage *image)
 {
 	MonoClass *class, *parent_class = NULL;
 	int i, total;
@@ -238,4 +267,12 @@
 	}
 						
 	return NULL;
+}
+
+/*
+ *
+ */
+void mono_switch_console_printf(switch_text_channel_t channel, char *file, const char *func, int line, char *fmt, char *msg)
+{
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, msg);
 }



More information about the Freeswitch-branches mailing list