From de432ce258c0cc7e6bc97cec86d5afc1db28e577 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Thu, 17 May 2007 16:57:26 +0000
Subject: [PATCH] update

git-svn-id: http://svn.openzap.org/svn/openzap/trunk@15 a93c3328-9c30-0410-af19-c9cd2b2d52af
---
 libs/openzap/src/Makefile                    |  3 +
 libs/openzap/src/hashtable.c                 | 10 +--
 libs/openzap/src/hashtable_itr.c             | 12 ++++
 libs/openzap/src/include/hashtable.h         | 11 ++-
 libs/openzap/src/include/hashtable_itr.h     | 12 +++-
 libs/openzap/src/include/hashtable_private.h | 12 ++--
 libs/openzap/src/include/openzap.h           | 31 ++++++---
 libs/openzap/src/openzap.c                   | 70 ++++++++++++++------
 libs/openzap/src/testapp.c                   |  9 +++
 libs/openzap/src/zap_wanpipe.c               | 25 +++++--
 10 files changed, 145 insertions(+), 50 deletions(-)
 create mode 100644 libs/openzap/src/testapp.c

diff --git a/libs/openzap/src/Makefile b/libs/openzap/src/Makefile
index 2f7055b09c..fa8c8d601c 100644
--- a/libs/openzap/src/Makefile
+++ b/libs/openzap/src/Makefile
@@ -42,6 +42,9 @@ $(MYLIB): $(OBJS)
 	ar rcs $(MYLIB) $(OBJS)
 	ranlib $(MYLIB)
 
+testapp: testapp.c $(MYLIB)
+	$(CC) -L. -Iinclude testapp.c -o testapp -lopenzap -lm
+
 openzap.o: openzap.c
 	$(CC) $(MOD_CFLAGS) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
 
diff --git a/libs/openzap/src/hashtable.c b/libs/openzap/src/hashtable.c
index e03013ba10..763357edce 100644
--- a/libs/openzap/src/hashtable.c
+++ b/libs/openzap/src/hashtable.c
@@ -22,13 +22,13 @@ static const unsigned int primes[] = {
 805306457, 1610612741
 };
 const unsigned int prime_table_length = sizeof(primes)/sizeof(primes[0]);
-const float max_load_factor = 0.65f;
+const float max_load_factor = 0.65;
 
 /*****************************************************************************/
 struct hashtable *
 create_hashtable(unsigned int minsize,
-                 unsigned int (*hashf) (const void*),
-                 int (*eqf) (const void*,const void*))
+                 unsigned int (*hashf) (void*),
+                 int (*eqf) (void*,void*))
 {
     struct hashtable *h;
     unsigned int pindex, size = primes[0];
@@ -54,7 +54,7 @@ create_hashtable(unsigned int minsize,
 
 /*****************************************************************************/
 unsigned int
-hash(struct hashtable *h, const void *k)
+hash(struct hashtable *h, void *k)
 {
     /* Aim to protect against poor hash functions by adding logic here
      * - logic taken from java 1.4 hashtable source */
@@ -160,7 +160,7 @@ hashtable_insert(struct hashtable *h, void *k, void *v)
 
 /*****************************************************************************/
 void * /* returns value associated with key */
-hashtable_search(struct hashtable *h, const void *k)
+hashtable_search(struct hashtable *h, void *k)
 {
     struct entry *e;
     unsigned int hashvalue, index;
diff --git a/libs/openzap/src/hashtable_itr.c b/libs/openzap/src/hashtable_itr.c
index defac691f0..5dced841f3 100644
--- a/libs/openzap/src/hashtable_itr.c
+++ b/libs/openzap/src/hashtable_itr.c
@@ -34,6 +34,18 @@ hashtable_iterator(struct hashtable *h)
     return itr;
 }
 
+/*****************************************************************************/
+/* key      - return the key of the (key,value) pair at the current position */
+/* value    - return the value of the (key,value) pair at the current position */
+
+void *
+hashtable_iterator_key(struct hashtable_itr *i)
+{ return i->e->k; }
+
+void *
+hashtable_iterator_value(struct hashtable_itr *i)
+{ return i->e->v; }
+
 /*****************************************************************************/
 /* advance - advance the iterator to the next element
  *           returns zero if advanced to end of table */
diff --git a/libs/openzap/src/include/hashtable.h b/libs/openzap/src/include/hashtable.h
index 88d76572e8..e6d23ff52f 100644
--- a/libs/openzap/src/include/hashtable.h
+++ b/libs/openzap/src/include/hashtable.h
@@ -2,6 +2,11 @@
 
 #ifndef __HASHTABLE_CWC22_H__
 #define __HASHTABLE_CWC22_H__
+#ifdef _MSC_VER
+#ifndef __inline__
+#define __inline__ __inline
+#endif
+#endif
 
 struct hashtable;
 
@@ -73,8 +78,8 @@ struct hashtable;
 
 struct hashtable *
 create_hashtable(unsigned int minsize,
-                 unsigned int (*hashfunction) (const void*),
-                 int (*key_eq_fn) (const void*,const void*));
+                 unsigned int (*hashfunction) (void*),
+                 int (*key_eq_fn) (void*,void*));
 
 /*****************************************************************************
  * hashtable_insert
@@ -114,7 +119,7 @@ int fnname (struct hashtable *h, keytype *k, valuetype *v) \
  */
 
 void *
-hashtable_search(struct hashtable *h, const void *k);
+hashtable_search(struct hashtable *h, void *k);
 
 #define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \
 valuetype * fnname (struct hashtable *h, keytype *k) \
diff --git a/libs/openzap/src/include/hashtable_itr.h b/libs/openzap/src/include/hashtable_itr.h
index 50ea10420a..aacf86825a 100644
--- a/libs/openzap/src/include/hashtable_itr.h
+++ b/libs/openzap/src/include/hashtable_itr.h
@@ -28,12 +28,20 @@ hashtable_iterator(struct hashtable *h);
 /* hashtable_iterator_key
  * - return the value of the (key,value) pair at the current position */
 
-#define hashtable_iterator_key(i) (void *)i->e->k
+extern __inline__ void *
+hashtable_iterator_key(struct hashtable_itr *i)
+{
+    return i->e->k;
+}
 
 /*****************************************************************************/
 /* value - return the value of the (key,value) pair at the current position */
 
-#define hashtable_iterator_value (void *)i->e->v
+extern __inline__ void *
+hashtable_iterator_value(struct hashtable_itr *i)
+{
+    return i->e->v;
+}
 
 /*****************************************************************************/
 /* advance - advance the iterator to the next element
diff --git a/libs/openzap/src/include/hashtable_private.h b/libs/openzap/src/include/hashtable_private.h
index 8668d58e32..edf59efe18 100644
--- a/libs/openzap/src/include/hashtable_private.h
+++ b/libs/openzap/src/include/hashtable_private.h
@@ -5,6 +5,7 @@
 
 #include "hashtable.h"
 
+
 /*****************************************************************************/
 struct entry
 {
@@ -19,17 +20,20 @@ struct hashtable {
     unsigned int entrycount;
     unsigned int loadlimit;
     unsigned int primeindex;
-    unsigned int (*hashfn) (const void *k);
-    int (*eqfn) (const void *k1, const void *k2);
+    unsigned int (*hashfn) (void *k);
+    int (*eqfn) (void *k1, void *k2);
 };
 
 /*****************************************************************************/
 unsigned int
-hash(struct hashtable *h, const void *k);
+hash(struct hashtable *h, void *k);
 
 /*****************************************************************************/
 /* indexFor */
-#define indexFor(hashvalue, tablelength) (unsigned int)(hashvalue % tablelength)
+static __inline__ unsigned int
+indexFor(unsigned int tablelength, unsigned int hashvalue) {
+    return (hashvalue % tablelength);
+}
 
 /* Only works if tablelength == 2^N */
 /*static inline unsigned int
diff --git a/libs/openzap/src/include/openzap.h b/libs/openzap/src/include/openzap.h
index 07d0f20f2f..3e9d74a830 100644
--- a/libs/openzap/src/include/openzap.h
+++ b/libs/openzap/src/include/openzap.h
@@ -209,18 +209,27 @@ typedef zap_status_t (*zint_write_t) ZINT_WRITE_ARGS ;
 #define ZINT_WRITE_MUZZLE assert(zchan != NULL); assert(data != NULL); assert(datalen != NULL)
 
 #define ZAP_PRE __FILE__, __FUNCTION__, __LINE__
-#define ZAP_LOG_DEBUG ZAP_PRE, 7
-#define ZAP_LOG_INFO ZAP_PRE, 6
-#define ZAP_LOG_NOTICE ZAP_PRE, 5
-#define ZAP_LOG_WARNING ZAP_PRE, 4
-#define ZAP_LOG_ERROR ZAP_PRE, 3
-#define ZAP_LOG_CRIT ZAP_PRE, 2
-#define ZAP_LOG_ALERT ZAP_PRE, 1
-#define ZAP_LOG_EMERG ZAP_PRE, 0
+
+#define ZAP_LOG_LEVEL_DEBUG 7
+#define ZAP_LOG_LEVEL_INFO 6
+#define ZAP_LOG_LEVEL_NOTICE 5
+#define ZAP_LOG_LEVEL_WARNING 4
+#define ZAP_LOG_LEVEL_ERROR 3
+#define ZAP_LOG_LEVEL_CRIT 2
+#define ZAP_LOG_LEVEL_ALERT 1
+#define ZAP_LOG_LEVEL_EMERG 0
+
+#define ZAP_LOG_DEBUG ZAP_PRE, ZAP_LOG_LEVEL_DEBUG
+#define ZAP_LOG_INFO ZAP_PRE, ZAP_LOG_LEVEL_INFO
+#define ZAP_LOG_NOTICE ZAP_PRE, ZAP_LOG_LEVEL_NOTICE
+#define ZAP_LOG_WARNING ZAP_PRE, ZAP_LOG_LEVEL_WARNING
+#define ZAP_LOG_ERROR ZAP_PRE, ZAP_LOG_LEVEL_ERROR
+#define ZAP_LOG_CRIT ZAP_PRE, ZAP_LOG_LEVEL_CRIT
+#define ZAP_LOG_ALERT ZAP_PRE, ZAP_LOG_LEVEL_ALERT
+#define ZAP_LOG_EMERG ZAP_PRE, ZAP_LOG_LEVEL_EMERG
 
 typedef void (*zap_logger_t)(char *file, const char *func, int line, int level, char *fmt, ...);
-extern zap_logger_t global_logger;
-#define zap_log global_logger;
+extern zap_logger_t zap_log;
 
 struct zap_software_interface {
 	const char *name;
@@ -252,7 +261,7 @@ zap_status_t zap_channel_write(zap_channel_t *zchan, void *data, zap_size_t *dat
 zap_status_t zap_global_init(void);
 zap_status_t zap_global_destroy(void);
 void zap_global_set_logger(zap_logger_t logger);
-void zap_global_set_default_logger(void);
+void zap_global_set_default_logger(int level);
 
 typedef struct hashtable zap_hash_t;
 
diff --git a/libs/openzap/src/openzap.c b/libs/openzap/src/openzap.c
index 6626ccef77..a56ced9db6 100644
--- a/libs/openzap/src/openzap.c
+++ b/libs/openzap/src/openzap.c
@@ -74,18 +74,25 @@ static char *cut_path(char *in)
 
 static void null_logger(char *file, const char *func, int line, int level, char *fmt, ...)
 {
-	if (file && func && line && level && fmt) {
-		return;
+	if (0) {
+		null_logger(file, func, line, level, fmt, fmt + sizeof(fmt));
 	}
-	return;
 }
 
+static int zap_log_level;
+
 static void default_logger(char *file, const char *func, int line, int level, char *fmt, ...)
 {
 	char *fp;
 	char data[1024];
-
 	va_list ap;
+
+	if (level < 0 || level > 7) {
+		level = 7;
+	}
+	if (level > zap_log_level) {
+		return;
+	}
 	
 	fp = cut_path(file);
 
@@ -93,9 +100,6 @@ static void default_logger(char *file, const char *func, int line, int level, ch
 
 	vsnprintf(data, sizeof(data), fmt, ap);
 
-	if (level < 0 || level > 7) {
-		level = 7;
-	}
 
 	fprintf(stderr, "[%s] %s:%d %s() %s", LEVEL_NAMES[level], file, line, func, data);
 
@@ -103,37 +107,42 @@ static void default_logger(char *file, const char *func, int line, int level, ch
 
 }
 
-zap_logger_t global_logger = null_logger;
+zap_logger_t zap_log = null_logger;
 
 void zap_global_set_logger(zap_logger_t logger)
 {
 	if (logger) {
-		global_logger = logger;
+		zap_log = logger;
 	} else {
-		global_logger = null_logger;
+		zap_log = null_logger;
 	}
 }
 
-void zap_global_set_default_logger(void)
+void zap_global_set_default_logger(int level)
 {
-	global_logger = default_logger;
+	if (level < 0 || level > 7) {
+		level = 7;
+	}
+
+	zap_log = default_logger;
+	zap_log_level = level;
 }
 
-static int equalkeys(const void *k1, const void *k2)
+static int equalkeys(void *k1, void *k2)
 {
     return strcmp((char *) k1, (char *) k2) ? 0 : 1;
 }
 
-static unsigned hashfromstring(const void *ky)
+static unsigned hashfromstring(void *ky)
 {
 	unsigned char *str = (unsigned char *) ky;
 	unsigned hash = 0;
     int c;
-
+	
 	while ((c = *str++)) {
         hash = c + (hash << 6) + (hash << 16) - hash;
 	}
-	
+
     return hash;
 }
 
@@ -176,7 +185,7 @@ zap_status_t zap_span_destroy(zap_span_t **span);
 
 zap_status_t zap_channel_open(const char *name, unsigned span_id, unsigned chan_id, zap_channel_t **zchan)
 {
-	zap_software_interface_t *zint = (zap_software_interface_t *) hashtable_search(globals.interface_hash, name);
+	zap_software_interface_t *zint = (zap_software_interface_t *) hashtable_search(globals.interface_hash, (char *)name);
 
 	if (span_id < ZAP_MAX_SPANS_INTERFACE && chan_id < ZAP_MAX_CHANNELS_SPAN && zint) {
 		zap_channel_t *check;
@@ -303,37 +312,51 @@ zap_status_t zap_global_init(void)
 	char *var, *val;
 	unsigned configured = 0;
 	zap_software_interface_t *zint;
+	int modcount;
 
 	globals.interface_hash = create_hashtable(16, hashfromstring, equalkeys);
 	zint = NULL;
+	modcount = 0;
 
 #ifdef ZAP_WANPIPE_SUPPORT
 	if (wanpipe_init(&zint) == ZAP_SUCCESS) {
 		hashtable_insert(globals.interface_hash, (void *)zint->name, zint);
+		modcount++;
+	} else {
+		zap_log(ZAP_LOG_ERROR, "Error initilizing wanpipe.\n");	
 	}
 #endif
 	zint = NULL;
 #ifdef ZAP_ZT_SUPPORT
 	if (zt_init(&zint) == ZAP_SUCCESS) {
 		hashtable_insert(globals.interface_hash, (void *)zint->name, zint);
+		modcount++;
+	} else {
+		zap_log(ZAP_LOG_ERROR, "Error initilizing zt.\n");	
 	}
 #endif
 
-	
-	if (!zap_config_open_file(&cfg, "openzap.conf")) {
+	if (!modcount) {
+		zap_log(ZAP_LOG_ERROR, "Error initilizing anything.\n");	
 		return ZAP_FAIL;
 	}
 
+	if (!zap_config_open_file(&cfg, "openzap.conf")) {
+		return ZAP_FAIL;
+	}
+	
 	while (zap_config_next_pair(&cfg, &var, &val)) {
 		if (!strcasecmp(cfg.category, "openzap")) {
 			if (!strcmp(var, "load")) {
 				zap_software_interface_t *zint;
-				
+		
 				zint = (zap_software_interface_t *) hashtable_search(globals.interface_hash, val);
 				if (zint) {
 					if (zint->configure(zint) == ZAP_SUCCESS) {
 						configured++;
 					}
+				} else {
+					zap_log(ZAP_LOG_WARNING, "Attempted to load Non-Existant module '%s'\n", val);
 				}
 			}
 		}
@@ -341,7 +364,12 @@ zap_status_t zap_global_init(void)
 
 	zap_config_close_file(&cfg);
 
-	return configured ? ZAP_SUCCESS : ZAP_FAIL;
+	if (configured) {
+		return ZAP_SUCCESS;
+	}
+
+	zap_log(ZAP_LOG_ERROR, "No modules configured!\n");
+	return ZAP_FAIL;
 }
 
 zap_status_t zap_global_destroy(void)
diff --git a/libs/openzap/src/testapp.c b/libs/openzap/src/testapp.c
new file mode 100644
index 0000000000..c53bfed6f5
--- /dev/null
+++ b/libs/openzap/src/testapp.c
@@ -0,0 +1,9 @@
+#include "openzap.h"
+
+int main(int argc, char *argv[])
+{
+	printf("hello\n");
+
+	zap_global_set_default_logger(ZAP_LOG_LEVEL_DEBUG);
+	zap_global_init();
+}
diff --git a/libs/openzap/src/zap_wanpipe.c b/libs/openzap/src/zap_wanpipe.c
index 945382c412..e699626b3f 100644
--- a/libs/openzap/src/zap_wanpipe.c
+++ b/libs/openzap/src/zap_wanpipe.c
@@ -71,6 +71,8 @@ typedef unsigned __int32 u_int32_t;
 #include <sdla_front_end.h>
 #include <sdla_aft_te1.h>
 
+static zap_software_interface_t wanpipe_interface;
+
 struct wanpipe_channel {
 	struct zap_channel zchan;
 	int x;
@@ -81,18 +83,35 @@ static ZINT_CONFIGURE_FUNCTION(wanpipe_configure)
 	zap_config_t cfg;
 	char *var, *val;
 	int catno = -1;
+	zap_span_t *span = NULL;
 
 	ZINT_CONFIGURE_MUZZLE;
 
+	zap_log(ZAP_LOG_DEBUG, "configuring wanpipe\n");
 	if (!zap_config_open_file(&cfg, "wanpipe.conf")) {
 		return ZAP_FAIL;
 	}
-
+	
 	while (zap_config_next_pair(&cfg, &var, &val)) {
 		if (!strcasecmp(cfg.category, "span")) {
 			if (cfg.catno != catno) {
+				zap_log(ZAP_LOG_DEBUG, "found config for span %d\n", cfg.catno);
+				catno = cfg.catno;
+				if (zap_span_create(&wanpipe_interface, &span) == ZAP_SUCCESS) {
+					zap_log(ZAP_LOG_DEBUG, "created span %d\n", span->span_id);
+				} else {
+					zap_log(ZAP_LOG_CRIT, "failure creating span\n");
+					span = NULL;
+				}
 				
+				continue;
 			}
+			
+			if (!span) {
+				continue;
+			}
+
+			zap_log(ZAP_LOG_DEBUG, "span %d [%s]=[%s]\n", span->span_id, var, val);
 		}
 	}
 	zap_config_close_file(&cfg);
@@ -142,8 +161,6 @@ static ZINT_WRITE_FUNCTION(wanpipe_write)
 	return ZAP_FAIL;
 }
 
-static zap_software_interface_t wanpipe_interface;
-
 zap_status_t wanpipe_init(zap_software_interface_t **zint)
 {
 	assert(zint != NULL);
@@ -160,7 +177,7 @@ zap_status_t wanpipe_init(zap_software_interface_t **zint)
 	wanpipe_interface.write = wanpipe_write;
 	*zint = &wanpipe_interface;
 
-	return ZAP_FAIL;
+	return ZAP_SUCCESS;
 }
 
 zap_status_t wanpipe_destroy(void)