[asterisk-scf-commits] asterisk-scf/integration/ice-util-cpp.git branch "basecomponent" updated.
Commits to the Asterisk SCF project code repositories
asterisk-scf-commits at lists.digium.com
Wed Jul 13 15:48:17 CDT 2011
branch "basecomponent" has been updated
via 454a11c6eafa4a139938aa322a7ae92519861f5d (commit)
from 5ed8071cafb4b82a0052b3b35adb9e33a9adf6eb (commit)
Summary of changes:
include/AsteriskSCF/Component/Component.h | 74 +++++++++++++-
include/AsteriskSCF/Component/TestContext.h | 11 +-
.../Discovery/LocatorRegistrationWrapper.h | 10 +-
include/AsteriskSCF/Helpers/PropertyHelper.h | 5 +-
.../AsteriskSCF/Replication/ReplicationContext.h | 2 +-
src/Component/Component.cpp | 105 +++++++++++++++++---
src/Component/TestContext.cpp | 39 +++-----
test/PropertyHelper/PropertyHelperTest.cpp | 4 +-
8 files changed, 187 insertions(+), 63 deletions(-)
- Log -----------------------------------------------------------------
commit 454a11c6eafa4a139938aa322a7ae92519861f5d
Author: Ken Hunt <ken.hunt at digium.com>
Date: Wed Jul 13 15:47:50 2011 -0500
Various updates from making things work.
diff --git a/include/AsteriskSCF/Component/Component.h b/include/AsteriskSCF/Component/Component.h
index df3c44b..d37c5ad 100644
--- a/include/AsteriskSCF/Component/Component.h
+++ b/include/AsteriskSCF/Component/Component.h
@@ -59,12 +59,9 @@ public:
protected:
- void registerPrimaryService(const AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr& service);
- void registerBackplaneService(const AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr& service);
-
/////////////////////////////////////////////////////////////////////
// Notification for some of
- // the component events.
+ // the component events.
virtual void onPreInitialize() {}
virtual void onPostInitialize() {}
virtual void onSuspend() {}
@@ -155,7 +152,7 @@ protected:
/**
* Create all services to be associated with the primary service adapter.
- * Note: This is a required override.
+ * Note: This is a REQUIRED override.
*/
virtual void createPrimaryServices() = 0;
@@ -168,16 +165,76 @@ protected:
/**
* A required override for the component to locate and cache any
* proxies to remote services that it needs to operate.
+ * Note: This is a REQUIRED override.
*/
virtual void findRemoteServices() = 0;
+
+ /////////////////////////////////////////////////////////////////////
+ // Remote service registration operations.
+ // Components are usually discovered via the Service Locator,
+ // but some components may need to push proxies directly to other
+ // components. These operations assume you should only be registered
+ // when you're active.
+
+ /**
+ * Register this component's proxies with some service
+ * (other than the ServiceLocator).
+ */
+ virtual void registerWithRemoteServices() {}
+
+ /**
+ * Deregister this component's proxies from service
+ * (other than the ServiceLocator).
+ */
+ virtual void deregisterFromRemoteServices() {}
+
/////////////////////////////////////////////////////////////////////
- // Service locator interfaces
+ // Service locator operations
+
+ /**
+ * Helper that caches the service references for the primary adapter, and invokes
+ * the register method of the registration wrapper.
+ */
+ bool registerPrimaryService(const AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr& service);
+
+ /**
+ * Helper that caches the service references for the backplane adapter, and invokes
+ * the register method of the registration wrapper.
+ */
+ bool registerBackplaneService(const AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr& service);
+
+ /**
+ * Helper that wraps a service for registration by making some assumptions that
+ * are probably correct for most cases.
+ */
+ AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr
+ wrapServiceForRegistration(const Ice::ObjectPrx& proxy,
+ const std::string& category,
+ const std::string& service,
+ const std::string& id = "");
+
+ /**
+ * The derived component must override this operation, and is expected
+ * to register all services associated with the primary adapter.
+ * This operation is called after the initialize() operation, so the adapters
+ * exist.
+ * If the derived component calls registerPrimaryService(LocatorRegistrationWrapperPtr& service)
+ * for each component, there should be no need to override unregisterPrimaryServices(),
+ * suspendPrimaryServices(), or unsuspendPrimaryServices().
+ */
virtual void registerPrimaryServices() = 0;
void unregisterPrimaryServices();
void suspendPrimaryServices();
void unsuspendPrimaryServices();
+ /**
+ * The derived component should override this operation only if it wishes to create
+ * additional backplane services.
+ * If the derived component calls registerBackplaneService(LocatorRegistrationWrapperPtr& service)
+ * for each component, there should be no need to override unregisterBackplaneServices(),
+ * suspendBackplaneServices(), or unsuspendBackplaneServices().
+ */
virtual void registerBackplaneServices();
void unregisterBackplaneServices();
void suspendBackplaneServices();
@@ -197,6 +254,11 @@ protected:
const Ice::ObjectAdapterPtr& getBackplaneAdapter() const {return mBackplaneAdapter;}
std::string getServiceLocatorManagementProperty() const;
+ // Allow setting an alternative communicator.
+ // This should only be done in onPreInitialize() override, or
+ // something may be using the IceBox-provided communicator.
+ void setCommunicator(const Ice::CommunicatorPtr& communicator) {mCommunicator = communicator;}
+
// State data
AsteriskSCF::System::Logging::Logger mLogger;
diff --git a/include/AsteriskSCF/Component/TestContext.h b/include/AsteriskSCF/Component/TestContext.h
index 15564fd..ff8576d 100644
--- a/include/AsteriskSCF/Component/TestContext.h
+++ b/include/AsteriskSCF/Component/TestContext.h
@@ -23,9 +23,7 @@ namespace AsteriskSCF
{
namespace Component
{
-class TestContextPriv;
-
-class InvalidTestMode : public std::exception
+class ASTERISK_SCF_ICEBOX_EXPORT InvalidTestMode : public std::exception
{
public:
InvalidTestMode(const std::string& mode)
@@ -46,7 +44,7 @@ private:
* This class provides the component's classes with the context
* needed for testing.
*/
-class TestContext
+class ASTERISK_SCF_ICEBOX_EXPORT TestContext
{
public:
TestContext() ;
@@ -60,7 +58,7 @@ public:
/**
* Gets the paramters to for the test mode.
- * If the mode hasn't been set, will throw InvalidTestMode.
+ * If the mode hasn't been set, throws InvalidTestMode.
*/
AsteriskSCF::System::Component::V1::ComponentTestParamSeq
getTestModeParams(const std::string& mode);
@@ -87,7 +85,8 @@ public:
void clearAllTestModes();
private:
- boost::shared_ptr<TestContextPriv> mImpl;
+ std::map<std::string, AsteriskSCF::System::Component::V1::ComponentTestParamSeq> mTestModeMap;
+ boost::shared_mutex mLock;
};
typedef boost::shared_ptr<TestContext> TestContextPtr;
diff --git a/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h b/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
index 95edc18..aff7c60 100644
--- a/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
+++ b/include/AsteriskSCF/Discovery/LocatorRegistrationWrapper.h
@@ -27,13 +27,13 @@ namespace Discovery
/**
*
- * Helper template for services that need to be registered with the locator service. If the service may not
+ * Helper class for services that need to be registered with the locator service. If the service is not
* immediately available, the RegisterThread can be used to continue to try and register the service.
*
* TODO: It might be handy to add some "termination conditions" so it doesn't simply wait forever.
*
**/
-class LocatorRegistrationWrapper : public IceUtil::Shared
+class ASTERISK_SCF_ICEBOX_EXPORT LocatorRegistrationWrapper : public IceUtil::Shared
{
public:
@@ -137,13 +137,13 @@ private:
AsteriskSCF::Core::Discovery::V1::ServiceManagementPrx mServiceManagement;
std::string mComparatorGUID;
};
-typedef IceUtil::Handle<LocatorRegistrationWrapper> LocatorRegistrationWrapperPtr;
+typedef ASTERISK_SCF_ICEBOX_EXPORT IceUtil::Handle<LocatorRegistrationWrapper> LocatorRegistrationWrapperPtr;
/**
* This thread takes care of registering the registration wrapper instance if the service discovery components are
* unavailable during startup.
**/
-class RegisterThread : public IceUtil::Thread
+class ASTERISK_SCF_ICEBOX_EXPORT RegisterThread : public IceUtil::Thread
{
public:
RegisterThread(const LocatorRegistrationWrapperPtr& registration,
@@ -196,7 +196,7 @@ private:
IceUtil::Time mRetryInterval;
bool mStopped;
};
-typedef IceUtil::Handle<RegisterThread> RegisterThreadPtr;
+typedef ASTERISK_SCF_ICEBOX_EXPORT IceUtil::Handle<RegisterThread> RegisterThreadPtr;
} /* End of namespace Discovery */
diff --git a/include/AsteriskSCF/Helpers/PropertyHelper.h b/include/AsteriskSCF/Helpers/PropertyHelper.h
index 6beb4c2..fd92045 100644
--- a/include/AsteriskSCF/Helpers/PropertyHelper.h
+++ b/include/AsteriskSCF/Helpers/PropertyHelper.h
@@ -42,7 +42,8 @@ inline std::string propGetSet(const Ice::PropertiesPtr& p, const std::string& pr
}
/**
- * A method to convert a string to a boolean value.
+ * A method to convert a string to a boolean value. Useful for
+ * converting string property values.
*
* Values that result in 'true':
* - "true", "True", "TRUE" or other case variants of "true"
@@ -68,7 +69,7 @@ inline bool stringToBool(const std::string& valueStr)
* @return The boolean value of the property with name 'propertyName', or
* 'defaultvalue' if it the named property isn't found in 'properties'.
*/
-inline bool getBooleanPropertyWithDefault(const Ice::PropertiesPtr& properties, const std::string& propertyName, bool defaultValue)
+inline bool getBooleanPropertyValueWithDefault(const Ice::PropertiesPtr& properties, const std::string& propertyName, bool defaultValue)
{
std::string defaultStr("no");
if (defaultValue)
diff --git a/include/AsteriskSCF/Replication/ReplicationContext.h b/include/AsteriskSCF/Replication/ReplicationContext.h
index c41485e..c57abb8 100644
--- a/include/AsteriskSCF/Replication/ReplicationContext.h
+++ b/include/AsteriskSCF/Replication/ReplicationContext.h
@@ -71,7 +71,7 @@ private:
boost::shared_mutex mLock;
ReplicationStateType mState;
};
-typedef boost::shared_ptr<ReplicationContext> ReplicationContextPtr;
+typedef ASTERISK_SCF_ICEBOX_EXPORT boost::shared_ptr<ReplicationContext> ReplicationContextPtr;
} // namespace Replication
} // namespace AsteriskSCF
diff --git a/src/Component/Component.cpp b/src/Component/Component.cpp
index 92fdbef..a829059 100644
--- a/src/Component/Component.cpp
+++ b/src/Component/Component.cpp
@@ -39,7 +39,6 @@ namespace AsteriskSCF
{
namespace Component
{
-
static const string ServiceLocatorManagementPropertyName("LocatorServiceManagement.Proxy");
static const string ServiceLocatorPropertyName("LocatorService.Proxy");
@@ -269,6 +268,7 @@ void Component::activated()
{
mReplicationContext->setState(ACTIVE_IN_REPLICA_GROUP);
stopListeningToStateReplicators();
+ registerWithRemoteServices();
// Notify subclasses
onActivated();
@@ -285,6 +285,7 @@ void Component::standby()
{
mReplicationContext->setState(STANDBY_IN_REPLICA_GROUP);
listenToStateReplicators();
+ deregisterFromRemoteServices();
// Notify subclasses
onStandby();
@@ -296,38 +297,66 @@ void Component::standby()
}
/**
+ * Helper function to wrap a service in a registration wrapper.
+ */
+LocatorRegistrationWrapperPtr Component::wrapServiceForRegistration(const Ice::ObjectPrx& proxy,
+ const string& category,
+ const string& service,
+ const string& id)
+{
+ string guid = mName + "." + category;
+
+ // Configure how other components look this component up.
+ ServiceLocatorParamsPtr params = new ServiceLocatorParams;
+ params->category = category;
+ params->service = service;
+ params->id = getName();
+
+ LocatorRegistrationWrapperPtr wrapper =
+ new LocatorRegistrationWrapper(mCommunicator, getServiceLocatorManagementProperty(), proxy,
+ guid, params);
+
+ return wrapper;
+}
+
+/**
* In the override of registerServices(), this operation should
* be called for each primary interface registered with the service locator.
*/
-void Component::registerPrimaryService(const AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr& service)
+bool Component::registerPrimaryService(const AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr& service)
{
+ bool result(false);
try
{
mPrimaryServices.push_back(service);
- service->registerService();
+ result = service->registerService();
}
catch(const Ice::Exception& e)
{
mLogger(Error) << BOOST_CURRENT_FUNCTION << e.what();
throw;
}
+
+ return result;
}
/**
* This operation should be called for each backplane interface registered with the service locator.
*/
-void Component::registerBackplaneService(const AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr& service)
+bool Component::registerBackplaneService(const AsteriskSCF::Discovery::LocatorRegistrationWrapperPtr& service)
{
+ bool result(false);
try
{
mBackplaneServices.push_back(service);
- service->registerService();
+ result = service->registerService();
}
catch(const Ice::Exception& e)
{
mLogger(Error) << BOOST_CURRENT_FUNCTION << e.what();
throw;
}
+ return result;
}
/**
@@ -568,7 +597,7 @@ void Component::initReplicationContext()
{
try
{
- bool standalone = AsteriskSCF::getBooleanPropertyWithDefault(
+ bool standalone = AsteriskSCF::getBooleanPropertyValueWithDefault(
mCommunicator->getProperties(), mName + ".Standalone", false);
ReplicationStateType state(STANDBY_IN_REPLICA_GROUP);
@@ -584,9 +613,8 @@ void Component::initReplicationContext()
// place, non-standalone instances will need to be made active via
// the Replica interface. But for now, we default to active unless
// the soon-to-be obsolete Standby property is set.
- string standbyProp =
- mCommunicator->getProperties()->getPropertyWithDefault(mName + ".Standby", "no");
- if (boost::iequals(standbyProp,"no") || boost::iequals(standbyProp,"false"))
+ if (AsteriskSCF::getBooleanPropertyValueWithDefault(mCommunicator->getProperties(),
+ mName + ".Standby", false))
{
state = ACTIVE_IN_REPLICA_GROUP;
}
@@ -647,9 +675,10 @@ void Component::createBackplaneServices()
try
{
// Check to see if we're configured to publish a test interface.
- string testProp = mCommunicator->getProperties()->getPropertyWithDefault(
- mName + ".ComponentTest", "no");
- mPublishTestInterface = boost::iequals(testProp,"yes") || boost::iequals(testProp,"true");
+ mPublishTestInterface = AsteriskSCF::getBooleanPropertyValueWithDefault(
+ mCommunicator->getProperties(),
+ mName + ".ComponentTest",
+ false);
if (mPublishTestInterface)
{
@@ -776,7 +805,7 @@ void Component::initialize()
}
catch(const ::Ice::Exception &e)
{
- mLogger(Error) << "Problems in " << mName << BOOST_CURRENT_FUNCTION << e.what();
+ mLogger(Error) << mName << ", " << BOOST_CURRENT_FUNCTION << " : " << e.what();
throw e;
}
}
@@ -808,6 +837,12 @@ void Component::start(const string& name,
try
{
+ // Active mode?
+ if (mReplicationContext->isActive())
+ {
+ registerWithRemoteServices();
+ }
+
// Plug into the Asterisk SCF discovery system so that the interfaces we provide
// can be located.
registerBackplaneServices();
@@ -824,7 +859,7 @@ void Component::start(const string& name,
}
catch(const Ice::Exception& e)
{
- mLogger(Error) << BOOST_CURRENT_FUNCTION << e.what();
+ mLogger(Error) << mName << ", " << BOOST_CURRENT_FUNCTION << e.what();
throw;
}
@@ -842,7 +877,14 @@ void Component::resumed()
// Standby mode?
if (mReplicationContext->getState() == STANDBY_IN_REPLICA_GROUP)
{
- listenToStateReplicators();
+ try
+ {
+ listenToStateReplicators();
+ }
+ catch(const std::exception& e)
+ {
+ mLogger(Error) << mName << ", " << BOOST_CURRENT_FUNCTION << e.what();
+ }
}
// Allow servants on this adapter to process requests.
@@ -850,6 +892,18 @@ void Component::resumed()
// Re-enable lookups of our interfaces in the ServiceLocator.
unsuspendPrimaryServices();
+
+ if (mReplicationContext->isActive())
+ {
+ try
+ {
+ registerWithRemoteServices();
+ }
+ catch(const std::exception& e)
+ {
+ mLogger(Error) << mName << ", " << BOOST_CURRENT_FUNCTION << e.what();
+ }
+ }
}
// Notify subclasses
@@ -876,7 +930,26 @@ void Component::suspendService(bool shuttingDown)
// Standby mode?
if (mReplicationContext->getState() == STANDBY_IN_REPLICA_GROUP)
{
- stopListeningToStateReplicators();
+ try
+ {
+ stopListeningToStateReplicators();
+ }
+ catch(const std::exception& e)
+ {
+ mLogger(Error) << mName << ", " << BOOST_CURRENT_FUNCTION << e.what();
+ }
+ }
+
+ if (mReplicationContext->isActive())
+ {
+ try
+ {
+ deregisterFromRemoteServices();
+ }
+ catch(const std::exception& e)
+ {
+ mLogger(Error) << mName << ", " << BOOST_CURRENT_FUNCTION << e.what();
+ }
}
// Place the service adapter in holding state.
diff --git a/src/Component/TestContext.cpp b/src/Component/TestContext.cpp
index c4213f1..3390ed4 100644
--- a/src/Component/TestContext.cpp
+++ b/src/Component/TestContext.cpp
@@ -26,18 +26,7 @@ namespace AsteriskSCF
namespace Component
{
-class TestContextPriv
-{
-public:
- TestContextPriv()
- {
- }
-
- map<string, ComponentTestParamSeq> mTestModeMap;
- boost::shared_mutex mLock;
-};
-
-TestContext::TestContext() : mImpl(new TestContextPriv())
+TestContext::TestContext()
{
}
@@ -48,8 +37,8 @@ TestContext::TestContext() : mImpl(new TestContextPriv())
*/
bool TestContext::hasTestMode(const std::string& mode)
{
- boost::shared_lock<boost::shared_mutex> lock(mImpl->mLock);
- return (mImpl->mTestModeMap.find(mode) != mImpl->mTestModeMap.end());
+ boost::shared_lock<boost::shared_mutex> lock(mLock);
+ return (mTestModeMap.find(mode) != mTestModeMap.end());
}
/**
@@ -57,10 +46,10 @@ bool TestContext::hasTestMode(const std::string& mode)
*/
ComponentTestParamSeq TestContext::getTestModeParams(const std::string& mode)
{
- boost::shared_lock<boost::shared_mutex> lock(mImpl->mLock);
+ boost::shared_lock<boost::shared_mutex> lock(mLock);
- map<string, ComponentTestParamSeq>::iterator i = mImpl->mTestModeMap.find(mode);
- if (i == mImpl->mTestModeMap.end())
+ map<string, ComponentTestParamSeq>::iterator i = mTestModeMap.find(mode);
+ if (i == mTestModeMap.end())
{
throw InvalidTestMode(mode);
}
@@ -73,11 +62,11 @@ ComponentTestParamSeq TestContext::getTestModeParams(const std::string& mode)
*/
void TestContext::setTestMode(const std::string& mode)
{
- boost::unique_lock<boost::shared_mutex> lock(mImpl->mLock);
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
ComponentTestParamSeq emptyParams;
- mImpl->mTestModeMap[mode] = emptyParams;
+ mTestModeMap[mode] = emptyParams;
}
/**
@@ -86,9 +75,9 @@ void TestContext::setTestMode(const std::string& mode)
void TestContext::setTestMode(const std::string& mode,
const ComponentTestParamSeq& params)
{
- boost::unique_lock<boost::shared_mutex> lock(mImpl->mLock);
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
- mImpl->mTestModeMap[mode] = params;
+ mTestModeMap[mode] = params;
}
/**
@@ -96,8 +85,8 @@ void TestContext::setTestMode(const std::string& mode,
*/
void TestContext::clearTestMode(const std::string& mode)
{
- boost::unique_lock<boost::shared_mutex> lock(mImpl->mLock);
- mImpl->mTestModeMap.erase(mode);
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
+ mTestModeMap.erase(mode);
}
/**
@@ -105,8 +94,8 @@ void TestContext::clearTestMode(const std::string& mode)
*/
void TestContext::clearAllTestModes()
{
- boost::unique_lock<boost::shared_mutex> lock(mImpl->mLock);
- mImpl->mTestModeMap.clear();
+ boost::unique_lock<boost::shared_mutex> lock(mLock);
+ mTestModeMap.clear();
}
} // end Component
diff --git a/test/PropertyHelper/PropertyHelperTest.cpp b/test/PropertyHelper/PropertyHelperTest.cpp
index dd96286..2f5e85c 100644
--- a/test/PropertyHelper/PropertyHelperTest.cpp
+++ b/test/PropertyHelper/PropertyHelperTest.cpp
@@ -102,13 +102,13 @@ static void runPropertyHelperTest()
BOOST_CHECK(stringToBool("1"));
BOOST_CHECK(!stringToBool("0"));
- bool bval = getBooleanPropertyWithDefault(p, "test.boolprop", false);
+ bool bval = getBooleanPropertyValueWithDefault(p, "test.boolprop", false);
BOOST_CHECK(!bval);
result = propGetSet(p, "test.boolprop", "True");
BOOST_CHECK(result == "True");
- bval = getBooleanPropertyWithDefault(p, "test.boolprop", false);
+ bval = getBooleanPropertyValueWithDefault(p, "test.boolprop", false);
BOOST_CHECK(bval);
}
using namespace boost::unit_test;
-----------------------------------------------------------------------
--
asterisk-scf/integration/ice-util-cpp.git
More information about the asterisk-scf-commits
mailing list