Hi List,

attached you can find a patch that:
1) remove check for report while loading of store-file (this check has no 
sense to me because if bearerbox crashed or was down, we want deliver DLR's 
to smsbox instead of silently deleting)
2) improve store status query
3) fixes some race conditions

Comments and votes please...

P.S. This patch depends on previously posted by me patch:
[PATCH] add new report_mt sms type

-- 
Best regards / Mit besten Gr��en aus D�sseldorf

Dipl.-Ing.
Alexander Malysh
___________________________________________

Centrium GmbH
Vogelsanger Weg 80
40470 D�sseldorf

Fon: +49 (0211) 74 84 51 80
Fax: +49 (0211) 277 49 109

email: [EMAIL PROTECTED]
web: www.centrium.de
msn: [EMAIL PROTECTED]
icq: 98063111
___________________________________________

Please avoid sending me Word, Excel or PowerPoint attachments.
See http://www.fsf.org/philosophy/no-word-attachments.html
Index: gw/bb_store.c
===================================================================
RCS file: /home/cvs/gateway/gw/bb_store.c,v
retrieving revision 1.27
diff -a -u -b -r1.27 bb_store.c
--- gw/bb_store.c	15 Jan 2004 17:16:31 -0000	1.27
+++ gw/bb_store.c	15 Jan 2004 18:40:11 -0000
@@ -74,6 +74,7 @@
 #include "gwlib/gwlib.h"
 #include "msg.h"
 #include "bearerbox.h"
+#include "sms.h"
 
 
 /* passed from bearerbox core */
@@ -90,7 +91,6 @@
 static Mutex *file_mutex = NULL;
 static long cleanup_thread;
 
-
 static List *sms_store;
 static List *ack_store;
 
@@ -201,7 +201,9 @@
     
     while((ack = list_consume(ack_store)) != NULL) {
 
+        list_lock(sms_store);
 	match = list_extract_matching(sms_store, ack, cmp_msgs);
+        list_unlock(sms_store);
 	msg_destroy(ack);
 
 	if (match == NULL) {
@@ -209,6 +211,7 @@
 		    "from store, strange?");
 	    continue;
 	}
+
 	if (list_len(match) > 1)
 	    warning(0, "bb-store cleanup: Found %ld matches!?",
 		    list_len(match));
@@ -239,7 +242,10 @@
     
     list_destroy(ack_store, msg_destroy_item);
     list_destroy(sms_store, msg_destroy_item);
-    sms_store = NULL;
+    /* set all vars to NULL */
+    filename = newfile = bakfile = NULL;
+    file_mutex = NULL;
+    ack_store = sms_store = NULL;
 
     list_remove_producer(flow_threads);
 }
@@ -251,22 +257,22 @@
 Octstr *store_status(int status_type)
 {
     char *frmt;
-    char buf[1024], p[22], id[UUID_STR_LEN + 1];
-    Octstr *ret, *str, *t;
+    Octstr *ret;
     unsigned long l;
     struct tm tm;
     Msg *msg;
+    char id[UUID_STR_LEN + 1];
 
     ret = octstr_create("");
 
     /* set the type based header */
     if (status_type == BBSTATUS_HTML) {
         octstr_append_cstr(ret, "<table border=1>\n"
-            "<tr><td>SMS ID</td><td>Sender</td><td>Receiver</td>"
-            "<td>SMSC ID</td><td>UDH</td><td>Message</td>"
-            "<td>Time</td></tr>\n");
+            "<tr><td>SMS ID</td><td>Type</td><td>Time</td><td>Sender</td><td>Receiver</td>"
+            "<td>SMSC ID</td><td>BOX ID</td><td>UDH</td><td>Message</td>"
+            "</tr>\n");
     } else if (status_type == BBSTATUS_TEXT) {
-        octstr_append_cstr(ret, "[SMS ID] [Sender] [Receiver] [SMSC ID] [UDH] [Message] [Time]\n");
+        octstr_append_cstr(ret, "[SMS ID] [Type] [Time] [Sender] [Receiver] [SMSC ID] [BOX ID] [UDH] [Message]\n");
     }
 
     /* if there is no store-file, then don't loop in sms_store */
@@ -280,15 +286,20 @@
         if (msg_type(msg) == sms) {
 
             if (status_type == BBSTATUS_HTML) {
-                frmt = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>"
+                frmt = "<tr><td>%s</td><td>%s</td>"
+                       "<td>%04d-%02d-%02d %02d:%02d:%02d</td>"
+                       "<td>%s</td><td>%s</td><td>%s</td>"
                        "<td>%s</td><td>%s</td><td>%s</td></tr>\n";
             } else if (status_type == BBSTATUS_XML) {
-                frmt = "<message>\n\t<id>%s</id>\n\t<sender>%s</sender>\n\t"
+                frmt = "<message>\n\t<id>%s</id>\n\t<type>%s</type>\n\t"
+                       "<time>%04d-%02d-%02d %02d:%02d:%02d</time>\n\t"
+                       "<sender>%s</sender>\n\t"
                        "<receiver>%s</receiver>\n\t<smsc-id>%s</smsc-id>\n\t"
+                       "<box-id>%s</box-id>\n\t"
                        "<udh-data>%s</udh-data>\n\t<msg-data>%s</msg-data>\n\t"
-                       "<time>%s</time>\n</message>\n";
+                       "</message>\n";
             } else {
-                frmt = "[%s] [%s] [%s] [%s] [%s] [%s] [%s]\n";
+                frmt = "[%s] [%s] [%04d-%02d-%02d %02d:%02d:%02d] [%s] [%s] [%s] [%s] [%s] [%s]\n";
             }
 
             /* transform the time value */
@@ -297,23 +308,37 @@
 #else
             tm = gw_gmtime(msg->sms.time);
 #endif
-            sprintf(p, "%04d-%02d-%02d %02d:%02d:%02d",
-                    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
-                    tm.tm_hour, tm.tm_min, tm.tm_sec);
-            t = octstr_create(p);
+            if (msg->sms.udhdata)
+                octstr_binary_to_hex(msg->sms.udhdata, 1);
+            if (msg->sms.msgdata &&
+                (msg->sms.coding == DC_8BIT || msg->sms.coding == DC_UCS2 ||
+                (msg->sms.coding == DC_UNDEF && msg->sms.udhdata)))
+                octstr_binary_to_hex(msg->sms.msgdata, 1);
+
             uuid_unparse(msg->sms.id, id);
 
-            sprintf(buf, frmt,
+            octstr_format_append(ret, frmt,
                 id,
+		(msg->sms.sms_type == mo ? "MO" :
+		 msg->sms.sms_type == mt_push ? "MT-PUSH" :
+		 msg->sms.sms_type == mt_reply ? "MT-REPLY" :
+		 msg->sms.sms_type == report_mo ? "DLR-MO" :
+		 msg->sms.sms_type == report_mt ? "DLR-MT" : ""),
+                 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
+                 tm.tm_hour, tm.tm_min, tm.tm_sec,
                 (msg->sms.sender ? octstr_get_cstr(msg->sms.sender) : ""),
                 (msg->sms.receiver ? octstr_get_cstr(msg->sms.receiver) : ""),
                 (msg->sms.smsc_id ? octstr_get_cstr(msg->sms.smsc_id) : ""),
+                (msg->sms.boxc_id ? octstr_get_cstr(msg->sms.boxc_id) : ""),
                 (msg->sms.udhdata ? octstr_get_cstr(msg->sms.udhdata) : ""),
-                (msg->sms.msgdata ? octstr_get_cstr(msg->sms.msgdata) : ""),
-                (t ? octstr_get_cstr(t) : ""));
-            octstr_destroy(t);
-            str = octstr_create(buf);
-            octstr_append(ret, str);
+                (msg->sms.msgdata ? octstr_get_cstr(msg->sms.msgdata) : ""));
+
+            if (msg->sms.udhdata)
+                octstr_hex_to_binary(msg->sms.udhdata);
+            if (msg->sms.msgdata &&
+                (msg->sms.coding == DC_8BIT || msg->sms.coding == DC_UCS2 ||
+                (msg->sms.coding == DC_UNDEF && msg->sms.udhdata)))
+                octstr_hex_to_binary(msg->sms.msgdata);
         }
     }
     list_unlock(sms_store);
@@ -330,7 +355,7 @@
 
 long store_messages(void)
 {
-    return (sms_store ? list_len(sms_store) : -1);
+    return list_len(sms_store);
 }
 
 
@@ -342,7 +367,7 @@
     if (msg_type(msg) == sms && uuid_is_null(msg->sms.id))
 	uuid_generate(msg->sms.id);
 
-    if (msg_type(msg) == sms && msg->sms.time == MSG_PARAM_UNDEFINED)
+    if (msg_type(msg) == sms && !msg->sms.time)
 	msg->sms.time = time(NULL);
 
     if (filename == NULL)
@@ -359,6 +384,7 @@
     else
 	return -1;
 
+
     /* write to file, too */
     mutex_lock(file_mutex);
     write_msg(msg);
@@ -379,13 +405,13 @@
     int retval, msgs;
     long end, pos;
     long store_size;
-    char buf[UUID_STR_LEN + 1];
+    char id[UUID_STR_LEN + 1];
     
     if (filename == NULL)
 	return 0;
 
-    list_lock(sms_store);
     list_lock(ack_store);
+    list_lock(sms_store);
 
     while((msg = list_extract_first(sms_store))!=NULL)
 	msg_destroy(msg);
@@ -424,40 +450,38 @@
     info(0, "Store-file size %ld, starting to unpack%s", octstr_len(store_file),
 	 octstr_len(store_file) > 10000 ? " (may take awhile)" : "");
 
-    msg_hash = dict_create(101, NULL);  /* XXX should be different? */
+    msg_hash = dict_create(101, msg_destroy_item);  /* XXX should be different? */
 	
     pos = 0;
     msgs = 0;
     
-    while ((end = octstr_search(store_file, octstr_imm("\n"), pos)) != -1) {
+    while ((end = octstr_search_char(store_file, '\n', pos)) != -1) {
 
 	pack = octstr_copy(store_file, pos, end-pos);
 	pos = end+1;
 	
 	if (octstr_url_decode(pack) == -1) {
 	    debug("bb.store", 0, "Garbage at store-file, skipped");
+            octstr_destroy(pack);
 	    continue;
 	}
 
 	msg = msg_unpack(pack);
-	if (msg == NULL) 
-	    continue;
-
-	if (msg_type(msg) == sms) {
-	    if (msg->sms.sms_type == report) {
 		octstr_destroy(pack);
-                msg_destroy(msg);
+	if (msg == NULL) {
 		continue;
 	    }
-            uuid_unparse(msg->sms.id, buf);
-	    key = octstr_format("%s", buf);
+
+	if (msg_type(msg) == sms) {
+            uuid_unparse(msg->sms.id, id);
+	    key = octstr_create(id);
 	    dict_put(msg_hash, key, msg);
 	    octstr_destroy(key);
 	    msgs++;
 	}
 	else if (msg_type(msg) == ack) {
-            uuid_unparse(msg->ack.id, buf);
-	    key = octstr_format("%s", buf);
+            uuid_unparse(msg->sms.id, id);
+            key = octstr_create(id);
 	    dmsg = dict_remove(msg_hash, key);
 	    if (dmsg != NULL) 
 		msg_destroy(dmsg);
@@ -472,7 +496,6 @@
 	    msg_dump(msg, 0);
 	    msg_destroy(msg);
 	}
-	octstr_destroy(pack);
     }
     octstr_destroy(store_file);
 
@@ -485,23 +508,31 @@
 
     keys = dict_keys(msg_hash);
     while((key = list_extract_first(keys))!=NULL) {
-	msg = dict_get(msg_hash, key);
+	msg = dict_remove(msg_hash, key);
+	octstr_destroy(key);
 
 	if (msg_type(msg) != sms) {
+	    error(0, "Found non sms message in dictionary!");
+	    msg_dump(msg, 0);
 	    msg_destroy(msg);
-	    octstr_destroy(key);
 	    continue;
 	}
 	copy = msg_duplicate(msg);
 	list_produce(sms_store, copy);
 
-	if (msg->sms.sms_type == mo)
+	if (msg->sms.sms_type == mo ||
+	    msg->sms.sms_type == report_mo) {
 	    list_produce(incoming_sms, msg);
-	if (msg->sms.sms_type == mt_push ||
-	    msg->sms.sms_type == mt_reply)
+        }
+	else if (msg->sms.sms_type == mt_push ||
+	    msg->sms.sms_type == mt_reply ||
+	    msg->sms.sms_type == report_mt) {
 	    list_produce(outgoing_sms, msg);
-	
-	octstr_destroy(key);
+        }
+	else {
+	    msg_dump(msg,0);
+            msg_destroy(msg);
+	}
     }
     list_destroy(keys, NULL);
 
@@ -514,8 +545,8 @@
     /* destroy the hash */
     dict_destroy(msg_hash);
 
-    list_unlock(sms_store);
     list_unlock(ack_store);
+    list_unlock(sms_store);
 
     return retval;
 }
@@ -526,8 +557,8 @@
 {
     int retval;
 
-    list_lock(sms_store);
     list_lock(ack_store);
+    list_lock(sms_store);
     debug("bb.store", 0, "Dumping %ld messages and %ld acks to store",
 	  list_len(sms_store), list_len(ack_store));
     mutex_lock(file_mutex);
@@ -544,15 +575,13 @@
 }
 
 
-int store_init(Octstr *fname)
+int store_init(const Octstr *fname)
 {
-    if (fname == NULL)
-        return 0; /* we are done */
-
-    if (octstr_len(fname) > (FILENAME_MAX-5))
-        panic(0, "Store file filename too long: `%s', failed to init.",
+    if (octstr_len(fname) > (FILENAME_MAX-5)) {
+        error(0, "Store file filename too long: `%s', failed to init.",
 	      octstr_get_cstr(fname));
-
+	return -1;
+    }
     filename = octstr_duplicate(fname);
     newfile = octstr_format("%s.new", octstr_get_cstr(filename));
     bakfile = octstr_format("%s.bak", octstr_get_cstr(filename));
Index: gw/bearerbox.c
===================================================================
RCS file: /home/cvs/gateway/gw/bearerbox.c,v
retrieving revision 1.150
diff -a -u -b -r1.150 bearerbox.c
--- gw/bearerbox.c	24 Nov 2003 20:08:24 -0000	1.150
+++ gw/bearerbox.c	15 Jan 2004 18:40:11 -0000
@@ -396,17 +396,11 @@
     }
 
     log = cfg_get(grp, octstr_imm("store-file"));
-    /*
-     * Store must be always initialized, even if no store file defined,
-     * because store create message id's and timestamp for all sms's
-     * handled in bearerbox! If no store file defained in config then no
-     * files will be opened and no cleanup thread started.
-     * NOTE: I believe store file must be mandatory parameter, because
-     * otherwise all queued messages in bearerbox are loss while shutdown
-     * or restart ! (alex)
-     */
+    /* initialize the store file */
+    if (log != NULL) {
     store_init(log);
-    if (log != NULL) octstr_destroy(log);
+        octstr_destroy(log);
+    }
 
     conn_config_ssl (grp);
 
Index: gw/bearerbox.h
===================================================================
RCS file: /home/cvs/gateway/gw/bearerbox.h,v
retrieving revision 1.20
diff -a -u -b -r1.20 bearerbox.h
--- gw/bearerbox.h	24 Nov 2003 20:08:24 -0000	1.20
+++ gw/bearerbox.h	15 Jan 2004 18:40:11 -0000
@@ -190,7 +190,7 @@
 
 /* initialize system. Return -1 if fname is baad (too long), otherwise
  * load data from disk */
-int store_init(Octstr *fname);
+int store_init(const Octstr *fname);
 
 /* init shutdown (system dies when all acks have been processed) */
 void store_shutdown(void);

Reply via email to