2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-31 15:18:09 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
double DynamicObject::m_CurrentTx = 0;
|
2013-02-19 12:17:31 +01:00
|
|
|
set<DynamicObject::WeakPtr> DynamicObject::m_ModifiedObjects;
|
2013-02-18 14:40:24 +01:00
|
|
|
boost::mutex DynamicObject::m_TransactionMutex;
|
2013-02-22 08:12:43 +01:00
|
|
|
boost::once_flag DynamicObject::m_TransactionOnce = BOOST_ONCE_INIT;
|
2013-02-18 14:40:24 +01:00
|
|
|
Timer::Ptr DynamicObject::m_TransactionTimer;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnRegistered;
|
|
|
|
signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnUnregistered;
|
2013-02-19 12:17:31 +01:00
|
|
|
signals2::signal<void (double, const set<DynamicObject::WeakPtr>&)> DynamicObject::OnTransactionClosing;
|
2013-02-27 12:44:51 +01:00
|
|
|
signals2::signal<void (double, const DynamicObject::Ptr&)> DynamicObject::OnFlushObject;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
DynamicObject::DynamicObject(const Dictionary::Ptr& serializedObject)
|
2013-03-02 09:07:47 +01:00
|
|
|
: m_ConfigTx(0), m_Registered(false)
|
2012-08-02 09:38:08 +02:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
RegisterAttribute("__name", Attribute_Config, &m_Name);
|
|
|
|
RegisterAttribute("__type", Attribute_Config, &m_Type);
|
|
|
|
RegisterAttribute("__local", Attribute_Config, &m_Local);
|
|
|
|
RegisterAttribute("__source", Attribute_Local, &m_Source);
|
|
|
|
RegisterAttribute("methods", Attribute_Config, &m_Methods);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
if (!serializedObject->Contains("configTx"))
|
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Serialized object must contain a config snapshot."));
|
2012-08-02 12:12:59 +02:00
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
/* apply config state from the config item/remote update;
|
2013-02-26 10:13:54 +01:00
|
|
|
* The DynamicType::CreateObject function takes care of restoring
|
2012-08-03 23:03:58 +02:00
|
|
|
* non-config state after the object has been fully constructed */
|
2013-03-01 12:07:52 +01:00
|
|
|
ApplyUpdate(serializedObject, Attribute_Config);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
|
|
|
boost::call_once(m_TransactionOnce, &DynamicObject::Initialize);
|
2013-02-08 23:40:28 +01:00
|
|
|
}
|
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/*
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-08 23:40:28 +01:00
|
|
|
DynamicObject::~DynamicObject(void)
|
2013-02-19 12:17:31 +01:00
|
|
|
{ }
|
2013-02-08 23:40:28 +01:00
|
|
|
|
2013-02-18 14:40:24 +01:00
|
|
|
void DynamicObject::Initialize(void)
|
|
|
|
{
|
|
|
|
/* Set up a timer to periodically create a new transaction. */
|
|
|
|
m_TransactionTimer = boost::make_shared<Timer>();
|
|
|
|
m_TransactionTimer->SetInterval(0.5);
|
|
|
|
m_TransactionTimer->OnTimerExpired.connect(boost::bind(&DynamicObject::NewTx));
|
|
|
|
m_TransactionTimer->Start();
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Dictionary::Ptr DynamicObject::BuildUpdate(double sinceTx, int attributeTypes) const
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2013-03-04 15:52:42 +01:00
|
|
|
ObjectLock olock(this);
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
DynamicObject::AttributeConstIterator it;
|
|
|
|
|
|
|
|
Dictionary::Ptr attrs = boost::make_shared<Dictionary>();
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_AttributeMutex);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-06 15:41:13 +01:00
|
|
|
for (it = m_Attributes.begin(); it != m_Attributes.end(); ++it) {
|
2013-03-04 15:52:42 +01:00
|
|
|
if (it->second.GetType() == Attribute_Transient)
|
|
|
|
continue;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
if ((it->second.GetType() & attributeTypes) == 0)
|
|
|
|
continue;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
if (it->second.GetTx() == 0)
|
|
|
|
continue;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
if (it->second.GetTx() < sinceTx && !(it->second.GetType() == Attribute_Config && m_ConfigTx >= sinceTx))
|
|
|
|
continue;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
Dictionary::Ptr attr = boost::make_shared<Dictionary>();
|
|
|
|
attr->Set("data", it->second.GetValue());
|
|
|
|
attr->Set("type", it->second.GetType());
|
|
|
|
attr->Set("tx", it->second.GetTx());
|
|
|
|
|
|
|
|
attrs->Set(it->first, attr);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
attrs->Seal();
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Dictionary::Ptr update = boost::make_shared<Dictionary>();
|
|
|
|
update->Set("attrs", attrs);
|
|
|
|
|
|
|
|
if (m_ConfigTx >= sinceTx && attributeTypes & Attribute_Config)
|
|
|
|
update->Set("configTx", m_ConfigTx);
|
|
|
|
else if (attrs->GetLength() == 0)
|
|
|
|
return Dictionary::Ptr();
|
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
update->Seal();
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
return update;
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
2012-04-20 13:49:04 +02:00
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
void DynamicObject::ApplyUpdate(const Dictionary::Ptr& serializedUpdate,
|
|
|
|
int allowedTypes)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(serializedUpdate->IsSealed());
|
2013-02-18 23:44:24 +01:00
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
Value configTxValue = serializedUpdate->Get("configTx");
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_AttributeMutex);
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
if ((allowedTypes & Attribute_Config) != 0 && !configTxValue.IsEmpty()) {
|
2013-03-06 11:03:50 +01:00
|
|
|
double configTx = configTxValue;
|
2013-02-18 23:44:24 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
if (configTx > m_ConfigTx) {
|
|
|
|
DynamicObject::AttributeIterator at;
|
2013-03-06 15:41:13 +01:00
|
|
|
for (at = m_Attributes.begin(); at != m_Attributes.end(); ++at) {
|
2013-03-04 15:52:42 +01:00
|
|
|
if ((at->second.GetType() & Attribute_Config) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
at->second.SetValue(0, Empty);
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
Dictionary::Ptr attrs = serializedUpdate->Get("attrs");
|
|
|
|
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(attrs->IsSealed());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock alock(attrs);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
Dictionary::Iterator it;
|
2013-03-06 15:41:13 +01:00
|
|
|
for (it = attrs->Begin(); it != attrs->End(); ++it) {
|
2013-02-18 23:44:24 +01:00
|
|
|
if (!it->second.IsObjectType<Dictionary>())
|
|
|
|
continue;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
Dictionary::Ptr attr = it->second;
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(attr->IsSealed());
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
int type = attr->Get("type");
|
2012-08-03 23:03:58 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
if ((type & ~allowedTypes) != 0)
|
|
|
|
continue;
|
2012-08-03 23:03:58 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
Value data = attr->Get("data");
|
|
|
|
double tx = attr->Get("tx");
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
if (type & Attribute_Config)
|
2013-03-04 15:52:42 +01:00
|
|
|
InternalRegisterAttribute(it->first, Attribute_Config);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
if (m_Attributes.find(it->first) == m_Attributes.end())
|
|
|
|
InternalRegisterAttribute(it->first, static_cast<AttributeType>(type));
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
InternalSetAttribute(it->first, data, tx, true);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
void DynamicObject::RegisterAttribute(const String& name,
|
2013-02-26 10:13:54 +01:00
|
|
|
AttributeType type, AttributeBase *boundAttribute)
|
2013-03-04 15:52:42 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-04 15:52:42 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_AttributeMutex);
|
|
|
|
|
|
|
|
InternalRegisterAttribute(name, type, boundAttribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @threadsafety Caller must hold m_AttributeMutex.
|
|
|
|
*/
|
|
|
|
void DynamicObject::InternalRegisterAttribute(const String& name,
|
|
|
|
AttributeType type, AttributeBase *boundAttribute)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
AttributeHolder attr(type, boundAttribute);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
pair<DynamicObject::AttributeIterator, bool> tt;
|
|
|
|
tt = m_Attributes.insert(make_pair(name, attr));
|
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
if (!tt.second) {
|
|
|
|
tt.first->second.SetType(type);
|
|
|
|
|
|
|
|
if (boundAttribute)
|
|
|
|
tt.first->second.Bind(boundAttribute);
|
|
|
|
}
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-03 13:19:55 +02:00
|
|
|
void DynamicObject::Set(const String& name, const Value& data)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-04 15:52:42 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_AttributeMutex);
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
InternalSetAttribute(name, data, GetCurrentTx());
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
void DynamicObject::Touch(const String& name)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(OwnsLock());
|
2013-03-04 15:52:42 +01:00
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_AttributeMutex);
|
|
|
|
|
|
|
|
AttributeIterator it = m_Attributes.find(name);
|
|
|
|
|
|
|
|
if (it == m_Attributes.end())
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Touch() called for unknown attribute: " + name));
|
|
|
|
|
|
|
|
it->second.SetTx(GetCurrentTx());
|
|
|
|
|
|
|
|
m_ModifiedAttributes.insert(name);
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_TransactionMutex);
|
|
|
|
m_ModifiedObjects.insert(GetSelf());
|
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-03 13:19:55 +02:00
|
|
|
Value DynamicObject::Get(const String& name) const
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-04 15:52:42 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_AttributeMutex);
|
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
return InternalGetAttribute(name);
|
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
2013-03-04 15:52:42 +01:00
|
|
|
* @threadsafety Caller must hold m_AttributeMutex.
|
2013-03-01 12:07:52 +01:00
|
|
|
*/
|
2012-09-21 09:43:06 +02:00
|
|
|
void DynamicObject::InternalSetAttribute(const String& name, const Value& data,
|
2013-02-08 23:40:28 +01:00
|
|
|
double tx, bool allowEditConfig)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
DynamicObject::AttributeIterator it;
|
|
|
|
it = m_Attributes.find(name);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
if (it == m_Attributes.end()) {
|
|
|
|
AttributeHolder attr(Attribute_Transient);
|
|
|
|
attr.SetValue(tx, data);
|
2013-01-25 10:12:55 +01:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
m_Attributes.insert(make_pair(name, attr));
|
|
|
|
} else {
|
|
|
|
if (!allowEditConfig && (it->second.GetType() & Attribute_Config))
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Config properties are immutable: '" + name + "'."));
|
|
|
|
|
|
|
|
it->second.SetValue(tx, data);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
if (it->second.GetType() & Attribute_Config)
|
|
|
|
m_ConfigTx = tx;
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
if (IsRegistered()) {
|
2013-02-20 19:52:25 +01:00
|
|
|
/* We can't call GetSelf() in the constructor or destructor.
|
2013-02-27 12:44:51 +01:00
|
|
|
* The Register() function will take care of adding this
|
2013-02-20 19:52:25 +01:00
|
|
|
* object to the list of modified objects later on if we can't
|
|
|
|
* do it here. */
|
2013-02-19 12:17:31 +01:00
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_TransactionMutex);
|
2013-03-04 15:52:42 +01:00
|
|
|
m_ModifiedObjects.insert(GetSelf());
|
2013-02-24 01:10:34 +01:00
|
|
|
}
|
2013-02-17 19:14:34 +01:00
|
|
|
}
|
2013-02-08 23:40:28 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
m_ModifiedAttributes.insert(name);
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
2013-03-04 15:52:42 +01:00
|
|
|
* @threadsafety Caller must hold m_AttributeMutex.
|
2013-03-01 12:07:52 +01:00
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
Value DynamicObject::InternalGetAttribute(const String& name) const
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
DynamicObject::AttributeConstIterator it;
|
|
|
|
it = m_Attributes.find(name);
|
|
|
|
|
|
|
|
if (it == m_Attributes.end())
|
2012-08-03 13:19:55 +02:00
|
|
|
return Empty;
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
return it->second.GetValue();
|
2012-04-20 14:20:25 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-12-04 08:42:24 +01:00
|
|
|
DynamicType::Ptr DynamicObject::GetType(void) const
|
2012-08-02 09:38:08 +02:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return DynamicType::GetByName(m_Type);
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String DynamicObject::GetName(void) const
|
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Name;
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
bool DynamicObject::IsLocal(void) const
|
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Local;
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-26 10:58:32 +01:00
|
|
|
bool DynamicObject::IsRegistered(void) const
|
|
|
|
{
|
2013-03-06 11:03:50 +01:00
|
|
|
ObjectLock olock(GetType());
|
2013-02-26 10:58:32 +01:00
|
|
|
return m_Registered;
|
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
void DynamicObject::SetSource(const String& value)
|
2012-07-02 19:25:33 +02:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
m_Source = value;
|
|
|
|
Touch("__source");
|
2012-07-02 19:25:33 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String DynamicObject::GetSource(void) const
|
2012-07-02 19:25:33 +02:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Source;
|
2012-07-02 19:25:33 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void DynamicObject::Register(void)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
/* Add this new object to the list of modified objects.
|
|
|
|
* We're doing this here because we can't construct
|
|
|
|
* a while WeakPtr from within the object's constructor. */
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_TransactionMutex);
|
2013-03-04 15:52:42 +01:00
|
|
|
m_ModifiedObjects.insert(GetSelf());
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
DynamicType::Ptr dtype = GetType();
|
|
|
|
dtype->RegisterObject(GetSelf());
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicObject::OnRegistrationCompleted(void)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
Start();
|
|
|
|
|
|
|
|
OnRegistered(GetSelf());
|
2012-09-28 10:39:28 +02:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
void DynamicObject::OnUnregistrationCompleted(void)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-12 13:48:37 +01:00
|
|
|
Stop();
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
OnUnregistered(GetSelf());
|
|
|
|
}
|
|
|
|
|
2012-09-28 10:39:28 +02:00
|
|
|
void DynamicObject::Start(void)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2012-09-28 10:39:28 +02:00
|
|
|
/* Nothing to do here. */
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
2013-03-12 14:01:11 +01:00
|
|
|
void DynamicObject::Stop(void)
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
|
|
|
/* Nothing to do here. */
|
|
|
|
}
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
void DynamicObject::Unregister(void)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-02-01 11:46:52 +01:00
|
|
|
DynamicType::Ptr dtype = GetType();
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (!dtype)
|
2012-07-27 16:05:02 +02:00
|
|
|
return;
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
dtype->UnregisterObject(GetSelf());
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
2013-02-19 07:26:52 +01:00
|
|
|
ScriptTask::Ptr DynamicObject::MakeMethodTask(const String& method,
|
|
|
|
const vector<Value>& arguments)
|
2012-07-14 15:59:59 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
Dictionary::Ptr methods;
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
methods = m_Methods;
|
2012-07-14 15:59:59 +02:00
|
|
|
|
2013-03-08 16:02:33 +01:00
|
|
|
if (!methods)
|
|
|
|
return ScriptTask::Ptr();
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
String funcName = methods->Get(method);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
if (funcName.IsEmpty())
|
|
|
|
return ScriptTask::Ptr();
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-15 11:19:52 +01:00
|
|
|
ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(funcName);
|
2012-07-14 15:59:59 +02:00
|
|
|
|
|
|
|
if (!func)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Function '" + funcName + "' does not exist."));
|
2012-07-14 15:59:59 +02:00
|
|
|
|
2013-02-19 07:26:52 +01:00
|
|
|
return boost::make_shared<ScriptTask>(func, arguments);
|
2012-07-14 15:59:59 +02:00
|
|
|
}
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/*
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
void DynamicObject::DumpObjects(const String& filename)
|
2012-07-24 13:13:02 +02:00
|
|
|
{
|
|
|
|
Logger::Write(LogInformation, "base", "Dumping program state to file '" + filename + "'");
|
|
|
|
|
2012-08-07 14:17:36 +02:00
|
|
|
String tempFilename = filename + ".tmp";
|
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
fstream fp;
|
|
|
|
fp.open(tempFilename.CStr(), std::ios_base::out);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
|
|
|
if (!fp)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Could not open '" + filename + "' file"));
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
StdioStream::Ptr sfp = boost::make_shared<StdioStream>(&fp, false);
|
|
|
|
sfp->Start();
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) {
|
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, type->GetObjects()) {
|
2012-10-12 10:16:03 +02:00
|
|
|
if (object->IsLocal())
|
|
|
|
continue;
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Dictionary::Ptr persistentObject = boost::make_shared<Dictionary>();
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
persistentObject->Set("type", type->GetName());
|
2012-07-27 16:05:02 +02:00
|
|
|
persistentObject->Set("name", object->GetName());
|
2012-07-24 14:18:33 +02:00
|
|
|
|
2012-08-13 10:27:49 +02:00
|
|
|
int types = Attribute_Local | Attribute_Replicated;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
/* only persist properties for replicated objects or for objects
|
|
|
|
* that are marked as persistent */
|
2012-08-02 12:12:59 +02:00
|
|
|
if (!object->GetSource().IsEmpty() /*|| object->IsPersistent()*/)
|
2012-08-02 09:38:08 +02:00
|
|
|
types |= Attribute_Config;
|
|
|
|
|
|
|
|
Dictionary::Ptr update = object->BuildUpdate(0, types);
|
|
|
|
|
|
|
|
if (!update)
|
|
|
|
continue;
|
2012-07-24 14:18:33 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
persistentObject->Set("update", update);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value value = persistentObject;
|
|
|
|
String json = value.Serialize();
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
NetString::WriteStringToStream(sfp, json);
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 14:17:36 +02:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
sfp->Close();
|
2012-11-22 12:04:32 +01:00
|
|
|
|
2012-10-17 11:35:06 +02:00
|
|
|
fp.close();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_unlink(filename.CStr());
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< errinfo_api_function("rename")
|
|
|
|
<< errinfo_errno(errno)
|
|
|
|
<< errinfo_file_name(tempFilename));
|
|
|
|
}
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/*
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
void DynamicObject::RestoreObjects(const String& filename)
|
2012-07-24 13:13:02 +02:00
|
|
|
{
|
|
|
|
Logger::Write(LogInformation, "base", "Restoring program state from file '" + filename + "'");
|
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
std::fstream fp;
|
|
|
|
fp.open(filename.CStr(), std::ios_base::in);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
StdioStream::Ptr sfp = boost::make_shared<StdioStream>(&fp, false);
|
|
|
|
sfp->Start();
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2013-01-30 10:52:52 +01:00
|
|
|
unsigned long restored = 0;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String message;
|
2012-11-23 11:02:34 +01:00
|
|
|
while (NetString::ReadStringFromStream(sfp, &message)) {
|
2012-08-05 03:10:53 +02:00
|
|
|
Dictionary::Ptr persistentObject = Value::Deserialize(message);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(persistentObject->IsSealed());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String type = persistentObject->Get("type");
|
|
|
|
String name = persistentObject->Get("name");
|
|
|
|
Dictionary::Ptr update = persistentObject->Get("update");
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-08-05 03:10:53 +02:00
|
|
|
bool hasConfig = update->Contains("configTx");
|
|
|
|
|
2012-12-04 08:42:24 +01:00
|
|
|
DynamicType::Ptr dt = DynamicType::GetByName(type);
|
|
|
|
|
|
|
|
if (!dt)
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Invalid type: " + type));
|
2012-12-04 08:42:24 +01:00
|
|
|
|
|
|
|
DynamicObject::Ptr object = dt->GetObject(name);
|
2012-08-05 03:10:53 +02:00
|
|
|
|
|
|
|
if (hasConfig && !object) {
|
2013-03-04 15:52:42 +01:00
|
|
|
object = dt->DynamicType::CreateObject(update);
|
2012-08-02 09:38:08 +02:00
|
|
|
object->Register();
|
2012-08-05 03:10:53 +02:00
|
|
|
} else if (object) {
|
|
|
|
object->ApplyUpdate(update, Attribute_All);
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
2013-01-30 10:52:52 +01:00
|
|
|
|
|
|
|
restored++;
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
2012-11-22 12:04:32 +01:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
sfp->Close();
|
2013-01-30 10:52:52 +01:00
|
|
|
|
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Restored " << restored << " objects";
|
2013-02-24 01:10:34 +01:00
|
|
|
Logger::Write(LogInformation, "base", msgbuf.str());
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2012-08-07 13:08:14 +02:00
|
|
|
void DynamicObject::DeactivateObjects(void)
|
|
|
|
{
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicType::Ptr& dt, DynamicType::GetTypes()) {
|
2013-03-01 12:07:52 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, dt->GetObjects()) {
|
2012-08-07 13:08:14 +02:00
|
|
|
object->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/*
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
double DynamicObject::GetCurrentTx(void)
|
|
|
|
{
|
2013-02-18 14:40:24 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_TransactionMutex);
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
if (m_CurrentTx == 0) {
|
|
|
|
/* Set the initial transaction ID. */
|
|
|
|
m_CurrentTx = Utility::GetTime();
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
return m_CurrentTx;
|
|
|
|
}
|
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
void DynamicObject::Flush(void)
|
|
|
|
{
|
2013-02-27 12:44:51 +01:00
|
|
|
OnFlushObject(GetCurrentTx(), GetSelf());
|
2013-02-19 23:02:08 +01:00
|
|
|
}
|
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/*
|
2013-02-18 14:40:24 +01:00
|
|
|
* @threadsafety Always. Caller must not hold any Object locks.
|
2013-02-17 19:14:34 +01:00
|
|
|
*/
|
|
|
|
void DynamicObject::NewTx(void)
|
2012-08-02 09:38:08 +02:00
|
|
|
{
|
2013-02-18 14:40:24 +01:00
|
|
|
double tx;
|
2013-02-19 12:17:31 +01:00
|
|
|
set<DynamicObject::WeakPtr> objects;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
{
|
2013-02-18 14:40:24 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_TransactionMutex);
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-02-18 14:40:24 +01:00
|
|
|
tx = m_CurrentTx;
|
2013-02-17 19:14:34 +01:00
|
|
|
m_ModifiedObjects.swap(objects);
|
2013-02-18 14:40:24 +01:00
|
|
|
m_CurrentTx = Utility::GetTime();
|
2013-02-08 23:40:28 +01:00
|
|
|
}
|
|
|
|
|
2013-02-19 12:17:31 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::WeakPtr& wobject, objects) {
|
|
|
|
DynamicObject::Ptr object = wobject.lock();
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (!object || !object->IsRegistered())
|
2013-02-19 12:17:31 +01:00
|
|
|
continue;
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
set<String, string_iless> attrs;
|
2012-08-03 23:03:58 +02:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(object);
|
|
|
|
attrs.swap(object->m_ModifiedAttributes);
|
|
|
|
}
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
BOOST_FOREACH(const String& attr, attrs) {
|
|
|
|
object->OnAttributeChanged(attr);
|
|
|
|
}
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
2013-02-14 14:58:26 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
OnTransactionClosing(tx, objects);
|
2013-02-26 10:58:32 +01:00
|
|
|
}
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
void DynamicObject::OnAttributeChanged(const String&)
|
2013-03-02 09:07:47 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
}
|
2012-08-07 21:02:12 +02:00
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/*
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2012-12-04 08:42:24 +01:00
|
|
|
DynamicObject::Ptr DynamicObject::GetObject(const String& type, const String& name)
|
|
|
|
{
|
|
|
|
DynamicType::Ptr dtype = DynamicType::GetByName(type);
|
2013-03-01 12:07:52 +01:00
|
|
|
return dtype->GetObject(name);
|
2012-12-04 08:42:24 +01:00
|
|
|
}
|
2013-02-02 14:28:11 +01:00
|
|
|
|
|
|
|
const DynamicObject::AttributeMap& DynamicObject::GetAttributes(void) const
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-02-02 14:28:11 +01:00
|
|
|
return m_Attributes;
|
2013-02-02 20:00:02 +01:00
|
|
|
}
|