r3351 - in trunk: libminisip libminisip/include libminisip/include/libminisip/config libminisip/source/subsystem_config libmnetutil/include/libmnetutil libmnetutil/source

mikaelsv at minisip.org mikaelsv at minisip.org
Mon Jul 2 19:06:09 CEST 2007


Author: mikaelsv
Date: 2007-07-02 19:06:08 +0200 (Mon, 02 Jul 2007)
New Revision: 3351

Added:
   trunk/libminisip/include/libminisip/config/CacheManager.h
   trunk/libminisip/source/subsystem_config/CacheManager.cxx
Modified:
   trunk/libminisip/Makefile.am
   trunk/libminisip/include/Makefile.am
   trunk/libmnetutil/include/libmnetutil/DirectorySet.h
   trunk/libmnetutil/source/DirectorySet.cxx
Log:

* Updated DirectorySet with some "constness" and a better version of
  findItems called findItemsPrioritized.

* Added CacheManager to libminisip (I put it there due to future
  dependecies to both libmcrypto and libmnetutil). The purpose of this
  class will be to govern all cachable items, e.g. certificates and
  information about where to locate LDAP server. The point of having
  a cache manager is that cache clean-up can be done very easily.



Modified: trunk/libminisip/Makefile.am
===================================================================
--- trunk/libminisip/Makefile.am	2007-06-30 19:57:13 UTC (rev 3350)
+++ trunk/libminisip/Makefile.am	2007-07-02 17:06:08 UTC (rev 3351)
@@ -416,6 +416,7 @@
 ldappb_la_LIBADD = libminisip.la $(MUTIL_LIBS) $(MNETUTIL_LIBS)
 endif
 
+libcachemanager_src = 	source/subsystem_config/CacheManager.cxx
 
 libminisip_la_SOURCES = source/Minisip.cxx \
 			source/MinisipExceptions.cxx \
@@ -435,7 +436,8 @@
 			$(libzrtp_src) \
 			$(libsdp_src) \
 			$(libsoundcard_src) \
-			$(libspaudio_src)
+			$(libspaudio_src) \
+			$(libcachemanager_src)
 
 libminisip_la_LIBADD += $(libsoundcard_libadd)
 

Modified: trunk/libminisip/include/Makefile.am
===================================================================
--- trunk/libminisip/include/Makefile.am	2007-06-30 19:57:13 UTC (rev 3350)
+++ trunk/libminisip/include/Makefile.am	2007-07-02 17:06:08 UTC (rev 3351)
@@ -108,6 +108,7 @@
 			libminisip/config/ConfBackend.h \
 			libminisip/config/UserConfig.h \
 			libminisip/config/OnlineConfBackend.h \
+			libminisip/config/CacheManager.h \
 			libminisip/contacts/ContactDb.h \
 			libminisip/contacts/PhoneBook.h \
 			libminisip/ipprovider/SimpleIpProvider.h \

Added: trunk/libminisip/include/libminisip/config/CacheManager.h
===================================================================
--- trunk/libminisip/include/libminisip/config/CacheManager.h	                        (rev 0)
+++ trunk/libminisip/include/libminisip/config/CacheManager.h	2007-07-02 17:06:08 UTC (rev 3351)
@@ -0,0 +1,96 @@
+/*
+ Copyright (C) 2004-2007 The Minisip Team
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+/* Copyright (C) 2007
+ *
+ * Authors: Mikael Svensson
+ */
+
+#ifndef _CACHEMANAGER_H_
+#define _CACHEMANAGER_H_
+
+#include <libminisip/libminisip_config.h>
+
+#include <libmutil/MemObject.h>
+#include <libmutil/CacheItem.h>
+#include <libmnetutil/DirectorySet.h>
+#include <libmnetutil/DirectorySetItem.h>
+
+#include <string>
+#include <map>
+
+#define CACHEMANAGER_CERTSET_ROOTCAS "rootcas"
+#define CACHEMANAGER_DIRSET_MAIN "main"
+
+class LIBMINISIP_API CacheManager : public MObject {
+	public:
+		//CacheManager();
+
+		//MRef<CertificateSetItem*> findCertificate();
+
+		/**
+		 * Returns a directory set item responsible for a particular domain.
+		 *
+		 * Note that even though several cached directories may match the given domain name only
+		 * the first directory will be returned. Use DirectorySet directly if you need a list
+		 * of all matching directories and not just any matching.
+		 *
+		 * @param	defaultSet	Specifies which set to scan. All sets are scanned if this
+		 * 				parameter is left out or set to an empty string.
+		 */
+		MRef<DirectorySetItem*> findDirectory(const std::string domain, const std::string defaultSet = "");
+		//MRef<CrlSetItem*> findCrl();
+
+		MRef<DirectorySet*> getDirectorySet(std::string key);
+
+		/**
+		 * Adds a directory item to a directory set in the cache.
+		 *
+		 * @param	dirItem		The item to add.
+		 * @param	setKey		The name of the directory set to which the item should be added.
+		 * 				If the string is empty the item will be added to a new directory
+		 * 				set with a random name.
+		 * @returns	The name of the directory set to which the item was added.
+		 */
+		std::string addDirectory(MRef<DirectorySetItem*> dirItem, std::string setKey);
+
+		/**
+		 * Wrapper for addDirectory() that automatically creates an LDAP directory set item
+		 * and adds it to a directory set in the cache.
+		 *
+		 * @param	url		URL speciying the location of the LDAP directory
+		 * @param	subTree		Specifies which part of the DNS tree that the directory
+		 * 				is responsible for.
+		 * @param	setKey		The name of the directory set to which the item should be added.
+		 * 				If the string is empty the item will be added to a new directory
+		 * 				set with a random name.
+		 * @returns	The name of the directory set to which the item was added.
+		 */
+		std::string addDirectoryLdap(std::string url, std::string subTree, const std::string setKey);
+
+		//void purgeCache();
+		//void removeFromCache(MRef<CacheItem*> item);
+	private:
+		std::string getNewDirectoryKey() const;
+
+		std::map<std::string, MRef<DirectorySet*> > directorySets;
+
+
+};
+
+#endif

Added: trunk/libminisip/source/subsystem_config/CacheManager.cxx
===================================================================
--- trunk/libminisip/source/subsystem_config/CacheManager.cxx	                        (rev 0)
+++ trunk/libminisip/source/subsystem_config/CacheManager.cxx	2007-07-02 17:06:08 UTC (rev 3351)
@@ -0,0 +1,84 @@
+/*
+ Copyright (C) 2007 the Minisip Team
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+/* Copyright (C) 2007
+ *
+ * Authors: Mikael Svensson
+*/
+
+#include <config.h>
+#include <libminisip/config/CacheManager.h>
+#include <libmutil/stringutils.h>
+
+#include <list>
+
+MRef<DirectorySetItem*> CacheManager::findDirectory(const std::string domain, const std::string defaultSet) {
+	std::vector<MRef<DirectorySetItem*> > res;
+	if (defaultSet.length() == 0) {
+		// Scan all directory sets
+		//std::map<const std::string, MRef<DirectorySet*> >::iterator i = directorySets.begin();
+		//while (i != directorySets.end()) {
+		for (std::map<const std::string, MRef<DirectorySet*> >::iterator i = directorySets.begin(); i != directorySets.end(); i++) {
+			res = i->second->findItemsPrioritized(domain);
+			if (!res.empty())
+				return res.front();
+		}
+
+	} else {
+		// Scan only one directory set, the one mentioned in the function parameters.
+		if (directorySets.find(defaultSet) != directorySets.end()) {
+			res = directorySets[defaultSet]->findItemsPrioritized(domain);
+			if (!res.empty())
+				return res.front();
+		}
+	}
+	// Return empty item if no result found
+	return MRef<DirectorySetItem*>();
+}
+
+MRef<DirectorySet*> CacheManager::getDirectorySet(std::string key) {
+	if (directorySets.find(key) != directorySets.end())
+		return directorySets[key];
+	return MRef<DirectorySet*>();
+}
+
+std::string CacheManager::addDirectory(const MRef<DirectorySetItem*> dirItem, std::string setKey) {
+	if (0 == setKey.length()) {
+		setKey = getNewDirectoryKey();
+	}
+	if (directorySets.find(setKey) == directorySets.end()) {
+		directorySets[setKey] = MRef<DirectorySet*>(new DirectorySet());
+	}
+	directorySets[setKey]->addItem(dirItem);
+	return setKey;
+}
+
+std::string CacheManager::addDirectoryLdap(std::string url, std::string subTree, const std::string setKey) {
+	return addDirectory(MRef<DirectorySetItem*>(new DirectorySetItem(url, subTree)), setKey != "" ? setKey : getNewDirectoryKey());
+}
+
+//void CacheManager::purgeCache();
+//void CacheManager::removeFromCache(MRef<CacheItem*> item);
+
+std::string CacheManager::getNewDirectoryKey() const {
+	std::string newName = "dirset";
+	int num = 1;
+	while (directorySets.find(newName + itoa(num)) != directorySets.end())
+		num++;
+	return newName;
+}

Modified: trunk/libmnetutil/include/libmnetutil/DirectorySet.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/DirectorySet.h	2007-06-30 19:57:13 UTC (rev 3350)
+++ trunk/libmnetutil/include/libmnetutil/DirectorySet.h	2007-07-02 17:06:08 UTC (rev 3351)
@@ -95,7 +95,7 @@
 		static DirectorySet *create();
 
 		DirectorySet* clone();
-		void addLdap (std::string url, std::string subTree);
+		void addLdap (const std::string url, const std::string subTree);
 
 		/**
 		 * Returns \em reference to the list of all directory items in the set
@@ -112,6 +112,7 @@
 		 * subTree = "ssvl.kth.se" and you want to retrieve both of them: then you just set the
 		 * first function parameter to "kth.se" and the second to "true".
 		 *
+		 * @deprecated	I see no use for this function. findItemsPrioritized() provides a more useful functionality.
 		 * @note	This function does \em not return a reference variable, which getItems() do.
 		 * 		However, even though it returns a new list, each of the list items still refer
 		 * 		to the exact same items that are stored in the set (i.e. items are not cloned
@@ -124,7 +125,30 @@
 		 * 					If false: function returns only items where there is and
 		 * 					exact matching of \p subTree.
 		 */
-		std::list<MRef<DirectorySetItem*> > findItems(const std::string subTree, bool endsWithIsEnough = true);
+		std::list<MRef<DirectorySetItem*> > findItems(const std::string subTree, const bool endsWithIsEnough = true);
+
+		/**
+		 * Returns a list of directory items where the first item in the list represents the
+		 * directory most likely to maintain information about users in \p domain.
+		 *
+		 * The function works like this:
+		 * - First and foremost all directory items are scanned and those that have some
+		 *   part of their domain name in common with \p domain make it to the next step.
+		 *
+		 *   A directory URL \c i have "something in common" with \p domain if the string
+		 *   \p domain ends with the string \c i. This means that if we are looking for
+		 *   directories with information about alice at ssvl.kth.se and there is a cached
+		 *   directory item for "kth.se", then that cached item should be returned. If,
+		 *   however, we are looking for "alice at kth.se" it would not be OK to return a
+		 *   cached directory item for "ssvl.kth.se".
+		 *
+		 * - The second phase is simply a sorting of the result from phase 1: the longest
+		 *   match should be at the beginning of the list ("ssvl.kth.se" outranks "kth.se"
+		 *   as it is more probable that the former has information about "alice at ssvl.kth.se"
+		 *   than the latter).
+		 */
+		std::vector<MRef<DirectorySetItem*> > findItemsPrioritized(const std::string domain);
+
 		MRef<DirectorySetItem*> getNext();
 
 		void initIndex();
@@ -133,12 +157,12 @@
 
 		void remove (MRef<DirectorySetItem*> removedItem);
 
-		void addItem (MRef<DirectorySetItem*> item);
+		void addItem (const MRef<DirectorySetItem*> item);
 
 		/**
 		 * Create directory item refering to an LDAP directory
 		 */
-		MRef<DirectorySetItem*> createItemLdap(std::string url, std::string subTree);
+		MRef<DirectorySetItem*> createItemLdap(const std::string url, const std::string subTree);
 
 	private:
 		std::list<MRef<DirectorySetItem*> >::iterator itemsIndex;

Modified: trunk/libmnetutil/source/DirectorySet.cxx
===================================================================
--- trunk/libmnetutil/source/DirectorySet.cxx	2007-06-30 19:57:13 UTC (rev 3350)
+++ trunk/libmnetutil/source/DirectorySet.cxx	2007-07-02 17:06:08 UTC (rev 3351)
@@ -30,6 +30,8 @@
 #include <libmnetutil/DirectorySet.h>
 #include <libmutil/stringutils.h>
 
+#include <algorithm>
+
 DirectorySet::DirectorySet(){
 	itemsIndex = items.begin();
 }
@@ -41,7 +43,7 @@
 	items.clear();
 }
 
-DirectorySet* DirectorySet::clone(){
+DirectorySet* DirectorySet::clone() {
 	DirectorySet * db = create();
 
 	lock();
@@ -68,7 +70,7 @@
 	mLock.unlock();
 }
 
-void DirectorySet::addItem( MRef<DirectorySetItem*> item ){
+void DirectorySet::addItem(const MRef<DirectorySetItem*> item ){
 	items.push_back( item );
 	itemsIndex = items.begin();
 }
@@ -98,16 +100,15 @@
 	initIndex();
 }
 
-std::list<MRef<DirectorySetItem*> > &DirectorySet::getItems(){
+std::list<MRef<DirectorySetItem*> > &DirectorySet::getItems() {
 	return items;
 }
-std::list<MRef<DirectorySetItem*> > DirectorySet::findItems(const std::string subTree, bool endsWithIsEnough) {
+std::list<MRef<DirectorySetItem*> > DirectorySet::findItems(const std::string subTree, const bool endsWithIsEnough) {
 	std::list<MRef<DirectorySetItem*> > res;
 	initIndex();
 
 	while( itemsIndex != items.end() ){
 		MRef<DirectorySetItem*> item = *itemsIndex;
-		bool addThis = false;
 
 		if ((endsWithIsEnough && stringEndsWith(item->getSubTree(), subTree)) || item->getSubTree() == subTree)
 			res.push_back(item);
@@ -118,12 +119,50 @@
 	return res;
 }
 
+/**
+ * This function is placed outside of DirectorySet class since I couldn't
+ * get the sort function to use it when it was a part of a class.
+ *
+ * @author	Mikael Svensson
+ */
+bool directorySetItemUrlComparator(MRef<DirectorySetItem*> a, MRef<DirectorySetItem*> b) {
+	return (a->getSubTree().length() > b->getSubTree().length());
+}
 
+std::vector<MRef<DirectorySetItem*> > DirectorySet::findItemsPrioritized(const std::string domain) {
+
+	std::vector<MRef<DirectorySetItem*> > res;
+	/*
+	PHASE 1: Picking out candidate directory server.
+	*/
+	initIndex();
+
+	while( itemsIndex != items.end() ){
+		MRef<DirectorySetItem*> item = *itemsIndex;
+		if (stringEndsWith(domain, item->getSubTree()))
+			res.push_back(item);
+
+		itemsIndex++;
+	}
+
+	/*
+	PHASE 2: Sorting the candidates. Longest URL first.
+	*/
+	sort(res.begin(), res.end(), directorySetItemUrlComparator);
+
+	/*
+	CONCLUDE =)
+	*/
+	initIndex();
+	return res;
+}
+
+
 void DirectorySet::initIndex(){
 	itemsIndex = items.begin();
 }
 
-MRef<DirectorySetItem*> DirectorySet::getNext(){
+MRef<DirectorySetItem*> DirectorySet::getNext() {
 	MRef<DirectorySetItem*> tmp;
 
 	if( itemsIndex == items.end() ){



More information about the Minisip-devel mailing list