Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

dlr_mem.c

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * gw/dlr_mem.c
00059  *
00060  * Implementation of handling delivery reports (DLRs)
00061  * in memory
00062  *
00063  * Andreas Fink <andreas@fink.org>, 18.08.2001
00064  * Stipe Tolj <stolj@wapme.de>, 22.03.2002
00065  * Alexander Malysh <a.malysh@centrium.de> 2003
00066  */
00067 
00068 #include "gwlib/gwlib.h"
00069 #include "dlr_p.h"
00070 
00071 /*
00072  * This is the global list where all messages being sent out are being kept track
00073  * of his list is looked up once a delivery report comes in
00074  */
00075 static List *dlr_waiting_list;
00076 static RWLock rwlock;
00077 
00078 /*
00079  * Destroy dlr_waiting_list.
00080  */
00081 static void dlr_mem_shutdown()
00082 {
00083     gw_rwlock_wrlock(&rwlock);
00084     gwlist_destroy(dlr_waiting_list, (gwlist_item_destructor_t *)dlr_entry_destroy);
00085     gw_rwlock_unlock(&rwlock);
00086     gw_rwlock_destroy(&rwlock);
00087 }
00088 
00089 /*
00090  * Get count of dlr messages waiting.
00091  */
00092 static long dlr_mem_messages(void)
00093 {
00094     return gwlist_len(dlr_waiting_list);
00095 }
00096 
00097 static void dlr_mem_flush(void)
00098 {
00099     long i;
00100     long len;
00101 
00102     gw_rwlock_wrlock(&rwlock);
00103     len = gwlist_len(dlr_waiting_list);
00104     for (i=0; i < len; i++)
00105         gwlist_delete(dlr_waiting_list, i, 1);
00106     gw_rwlock_unlock(&rwlock);
00107 }
00108 
00109 /*
00110  * add struct dlr_entry to list
00111  */
00112 static void dlr_mem_add(struct dlr_entry *dlr)
00113 {
00114     gw_rwlock_wrlock(&rwlock);
00115     gwlist_append(dlr_waiting_list,dlr);
00116     gw_rwlock_unlock(&rwlock);
00117 }
00118 
00119 /*
00120  * Private compare function
00121  * Return 0 if entry match and 1 if not.
00122  */
00123 static int dlr_mem_entry_match(struct dlr_entry *dlr, const Octstr *smsc, const Octstr *ts, const Octstr *dst)
00124 {
00125     /* XXX: check destination addr too, because e.g. for UCP is not enough to check only
00126      *          smsc and timestamp (timestamp is even without milliseconds)
00127      */
00128     if(octstr_compare(dlr->smsc,smsc) == 0 && octstr_compare(dlr->timestamp,ts) == 0)
00129         return 0;
00130 
00131     return 1;
00132 }
00133 
00134 /*
00135  * Find matching entry and return copy of it, otherwise NULL
00136  */
00137 static struct dlr_entry *dlr_mem_get(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
00138 {
00139     long i;
00140     long len;
00141     struct dlr_entry *dlr = NULL, *ret = NULL;
00142 
00143     gw_rwlock_rdlock(&rwlock);
00144     len = gwlist_len(dlr_waiting_list);
00145     for (i=0; i < len; i++) {
00146         dlr = gwlist_get(dlr_waiting_list, i);
00147 
00148         if (dlr_mem_entry_match(dlr, smsc, ts, dst) == 0) {
00149             ret = dlr_entry_duplicate(dlr);
00150             break;
00151         }
00152     }
00153     gw_rwlock_unlock(&rwlock);
00154 
00155     /* we couldnt find a matching entry */
00156     return ret;
00157 }
00158 
00159 /*
00160  * Remove matching entry
00161  */
00162 static void dlr_mem_remove(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
00163 {
00164     long i;
00165     long len;
00166     struct dlr_entry *dlr = NULL;
00167 
00168     gw_rwlock_wrlock(&rwlock);
00169     len = gwlist_len(dlr_waiting_list);
00170     for (i=0; i < len; i++) {
00171         dlr = gwlist_get(dlr_waiting_list, i);
00172 
00173         if (dlr_mem_entry_match(dlr, smsc, ts, dst) == 0) {
00174             gwlist_delete(dlr_waiting_list, i, 1);
00175             dlr_entry_destroy(dlr);
00176             break;
00177         }
00178     }
00179     gw_rwlock_unlock(&rwlock);
00180 }
00181 
00182 static struct dlr_storage  handles = {
00183     .type = "internal",
00184     .dlr_add = dlr_mem_add,
00185     .dlr_get = dlr_mem_get,
00186     .dlr_remove = dlr_mem_remove,
00187     .dlr_shutdown = dlr_mem_shutdown,
00188     .dlr_messages = dlr_mem_messages,
00189     .dlr_flush = dlr_mem_flush
00190 };
00191 
00192 /*
00193  * Initialize dlr_waiting_list and return out storage handles.
00194  */
00195 struct dlr_storage *dlr_init_mem(Cfg *cfg)
00196 {
00197     dlr_waiting_list = gwlist_create();
00198     gw_rwlock_init_static(&rwlock);
00199 
00200     return &handles;
00201 }
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.