[asterisk-scf-commits] asterisk-scf/integration/ice-util-cpp.git branch "retry_deux" updated.
Commits to the Asterisk SCF project code repositories
asterisk-scf-commits at lists.digium.com
Wed Feb 1 12:41:44 CST 2012
branch "retry_deux" has been updated
via 4cf6a1608548d00b6cdd06b683648b38aa44c64d (commit)
via bc87a3370215789d0db7783e4a4d0c847fd68c56 (commit)
via 341b917fb666edc302c8ace05a5f402645628347 (commit)
via ddcd69645b91e06540d0b44dfe6f3da459db5542 (commit)
from 1cc46335e9e0efd2a0baea66c2e002267c12da56 (commit)
Summary of changes:
.../Candidates.h => Helpers/OperationContext.h} | 21 ++--
.../AsteriskSCF/Helpers/OperationContextCache.h | 72 ++++++++
src/Helpers/CMakeLists.txt | 1 +
src/Helpers/OperationContextCache.cpp | 171 ++++++++++++++++++++
.../LocatorRegistrationTest.cpp | 9 +
5 files changed, 263 insertions(+), 11 deletions(-)
copy include/AsteriskSCF/{NAT/Candidates.h => Helpers/OperationContext.h} (60%)
create mode 100644 include/AsteriskSCF/Helpers/OperationContextCache.h
create mode 100644 src/Helpers/OperationContextCache.cpp
- Log -----------------------------------------------------------------
commit 4cf6a1608548d00b6cdd06b683648b38aa44c64d
Author: Ken Hunt <ken.hunt at digium.com>
Date: Wed Feb 1 12:41:06 2012 -0600
Added cache utility for OperationContext objects.
diff --git a/include/AsteriskSCF/Helpers/OperationContextCache.h b/include/AsteriskSCF/Helpers/OperationContextCache.h
new file mode 100644
index 0000000..6bbbb00
--- /dev/null
+++ b/include/AsteriskSCF/Helpers/OperationContextCache.h
@@ -0,0 +1,72 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2012, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+#pragma once
+
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/shared_ptr.hpp>
+#include <IceUtil/Timer.h>
+
+#include <AsteriskSCF/System/OperationsIf.h>
+
+namespace AsteriskSCF
+{
+namespace Helpers
+{
+
+class OperationContextCachEntry;
+typedef boost::shared_ptr<OperationContextCachEntry> OperationContextCachEntryPtr;
+
+/**
+ * Utiltity class that provides a queryable cache of OperationContext objects.
+ */
+class OperationContextCache : public IceUtil::Shared
+{
+public:
+ /**
+ * ctor
+ * @param ttlMinutes The time-to-live for the OperationContexts being cached.
+ * Entries will remain in the cache for at least the provided value, but can
+ * be longer.
+ */
+ OperationContextCache(int ttlMinutes);
+ ~OperationContextCache();
+
+ /**
+ * Caches the specified context if it isnt' already in the cache.
+ * @return true The context was added, which means it wasn't already in the cache.
+ * @note Make sure you don't confuse the return value of this operation with the return
+ * value of the 'contains' operation.
+ */
+ bool addOperationContext(const AsteriskSCF::System::V1::OperationContextPtr& operationContext);
+
+ /**
+ * Tests if the specified context is in the cache.
+ */
+ bool contains(const AsteriskSCF::System::V1::OperationContextPtr& operationContext);
+
+ void prune();
+
+private:
+ boost::shared_mutex mLock;
+ IceUtil::TimerTaskPtr mTimerTask;
+ IceUtil::TimerPtr mTimer;
+ IceUtil::Time mTTL;
+ std::map<std::string, OperationContextCachEntryPtr> mCache;
+};
+typedef IceUtil::Handle<OperationContextCache> OperationContextCachePtr;
+
+} // Helpers
+} // AsteriskSCF
diff --git a/src/Helpers/CMakeLists.txt b/src/Helpers/CMakeLists.txt
index 61dfeb4..fe359ef 100644
--- a/src/Helpers/CMakeLists.txt
+++ b/src/Helpers/CMakeLists.txt
@@ -1 +1,2 @@
astscf_component_add_files(ASTSCFIceUtilCpp Network.cpp)
+astscf_component_add_files(ASTSCFIceUtilCpp OperationContextCache.cpp)
diff --git a/src/Helpers/OperationContextCache.cpp b/src/Helpers/OperationContextCache.cpp
new file mode 100644
index 0000000..981c885
--- /dev/null
+++ b/src/Helpers/OperationContextCache.cpp
@@ -0,0 +1,171 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2012, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+#include <boost/thread/locks.hpp>
+
+#include <AsteriskSCF/Helpers/OperationContextCache.h>
+
+using namespace AsteriskSCF::System::V1;
+
+namespace AsteriskSCF
+{
+namespace Helpers
+{
+
+/**
+ * Wrapper to hold OperationContext with a timestamp in the cache.
+ */
+class OperationContextCachEntry : IceUtil::Shared
+{
+public:
+ OperationContextCachEntry(const OperationContextPtr& context, const IceUtil::Time& ttl) :
+ mContext(context),
+ mTimestamp(IceUtil::Time::now()),
+ mTTL(ttl)
+ {
+ }
+
+ OperationContextPtr getContext()
+ {
+ return mContext;
+ }
+
+ /**
+ * Compares this entries timestamp to the passed in time.
+ * Return true if the difference is greater than the TTL.
+ */
+ bool isStale(const IceUtil::Time& now)
+ {
+ if (now - mTimestamp > mTTL)
+ {
+ return true;
+ }
+ return false;
+ }
+
+private:
+ OperationContextPtr mContext;
+ IceUtil::Time mTimestamp;
+ IceUtil::Time mTTL;
+};
+
+/**
+ * A TimerTask to instigate the pruning of the cache.
+ */
+class OperationContextPruner : public IceUtil::TimerTask
+{
+public:
+ OperationContextPruner(const OperationContextCachePtr& cache)
+ {
+ }
+
+ /**
+ * Override for the TimerTask interface.
+ */
+ void runTimerTask()
+ {
+ mCache->prune();
+ }
+
+private:
+ OperationContextCachePtr mCache;
+};
+typedef IceUtil::Handle<OperationContextPruner> OperationContextPrunerPtr;
+
+/**
+ * Constructor.
+ * @param ttlMinutes The time to live for the cache, specified in minutes.
+ * This is a minimum time for an OperationContext to be cached. They
+ * may be cached longer.
+ */
+OperationContextCache::OperationContextCache(int ttlMinutes)
+ : mTTL(IceUtil::Time::seconds(ttlMinutes*60)),
+ mTimer(new IceUtil::Timer),
+ mTimerTask(new OperationContextPruner(this))
+{
+ mTimer->scheduleRepeated(mTimerTask, mTTL);
+}
+
+OperationContextCache::~OperationContextCache()
+{
+ mTimer->cancel(mTimerTask);
+}
+
+/**
+ * Caches the specified context if it isnt' already in the cache.
+ * @return true The context was added, which means it wasn't already in the cache.
+ * @note Make sure you don't confuse the return value of this operation with the return
+ * value of the 'contains' operation.
+ */
+bool OperationContextCache::addOperationContext(const AsteriskSCF::System::V1::OperationContextPtr& operationContext)
+{
+ if (contains(operationContext))
+ {
+ return false;
+ }
+
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
+
+ OperationContextCachEntryPtr entry(new OperationContextCachEntry(operationContext, mTTL));
+ mCache[operationContext->id] = entry;
+
+ return true;
+}
+
+/**
+ * Tests if the specified context is in the cache.
+ * @param operationContext
+ */
+bool OperationContextCache::contains(const AsteriskSCF::System::V1::OperationContextPtr& operationContext)
+{
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
+
+ std::map<std::string, OperationContextCachEntryPtr>::iterator entry = mCache.find(operationContext->id);
+ if (entry != mCache.end())
+ {
+ return false;
+ }
+ return true;
+}
+
+/**
+ * Drop entries that are older than the TTL.
+ */
+void OperationContextCache::prune()
+{
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
+
+ std::map<std::string, OperationContextCachEntryPtr>::const_iterator cacheIter;
+
+ IceUtil::Time now(IceUtil::Time::now());
+ std::vector<std::string> purgeList;
+
+ for(cacheIter = mCache.begin(); cacheIter != mCache.end(); cacheIter++)
+ {
+ if (cacheIter->second->isStale(now))
+ {
+ purgeList.push_back(cacheIter->first);
+ }
+ }
+
+ std::vector<std::string>::const_iterator purgeIter;
+ for (purgeIter = purgeList.begin(); purgeIter != purgeList.end(); purgeIter++)
+ {
+ mCache.erase(*purgeIter);
+ }
+}
+
+} // namespace Helpers
+} // namespace AsteriskSCF
commit bc87a3370215789d0db7783e4a4d0c847fd68c56
Merge: 1cc4633 341b917
Author: Ken Hunt <ken.hunt at digium.com>
Date: Tue Jan 31 16:20:47 2012 -0600
Merge branch 'operation-context-propagation' of git.asterisk.org:asterisk-scf/integration/ice-util-cpp into retry_deux
-----------------------------------------------------------------------
--
asterisk-scf/integration/ice-util-cpp.git
More information about the asterisk-scf-commits
mailing list