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;
|
|
|
|
set<DynamicObject::Ptr> DynamicObject::m_ModifiedObjects;
|
|
|
|
|
|
|
|
boost::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnRegistered;
|
|
|
|
boost::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnUnregistered;
|
|
|
|
boost::signal<void (const set<DynamicObject::Ptr>&)> DynamicObject::OnTransactionClosing;
|
|
|
|
|
|
|
|
DynamicObject::DynamicObject(const Dictionary::Ptr& serializedObject)
|
|
|
|
: m_ConfigTx(0)
|
|
|
|
{
|
|
|
|
RegisterAttribute("__name", Attribute_Config);
|
|
|
|
RegisterAttribute("__type", Attribute_Config);
|
|
|
|
RegisterAttribute("__local", Attribute_Config);
|
|
|
|
RegisterAttribute("__abstract", Attribute_Config);
|
|
|
|
RegisterAttribute("__source", Attribute_Local);
|
|
|
|
RegisterAttribute("methods", Attribute_Config);
|
|
|
|
|
2012-08-02 12:12:59 +02:00
|
|
|
if (!serializedObject->Contains("configTx"))
|
2012-08-14 09:51:11 +02:00
|
|
|
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;
|
|
|
|
* The DynamicObject::Create function takes care of restoring
|
|
|
|
* non-config state after the object has been fully constructed */
|
|
|
|
InternalApplyUpdate(serializedObject, Attribute_Config, true);
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
2012-06-12 09:36:18 +02:00
|
|
|
|
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
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
DynamicObject::AttributeConstIterator it;
|
|
|
|
|
|
|
|
Dictionary::Ptr attrs = boost::make_shared<Dictionary>();
|
|
|
|
|
|
|
|
for (it = m_Attributes.begin(); it != m_Attributes.end(); it++) {
|
|
|
|
if (it->second.Type == Attribute_Transient)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((it->second.Type & attributeTypes) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (it->second.Tx == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (it->second.Tx < sinceTx && !(it->second.Type == Attribute_Config && m_ConfigTx >= sinceTx))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dictionary::Ptr attr = boost::make_shared<Dictionary>();
|
|
|
|
attr->Set("data", it->second.Data);
|
|
|
|
attr->Set("type", it->second.Type);
|
|
|
|
attr->Set("tx", it->second.Tx);
|
|
|
|
|
|
|
|
attrs->Set(it->first, attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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-08-03 23:03:58 +02:00
|
|
|
{
|
|
|
|
InternalApplyUpdate(serializedUpdate, allowedTypes, false);
|
|
|
|
}
|
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
void DynamicObject::InternalApplyUpdate(const Dictionary::Ptr& serializedUpdate,
|
|
|
|
int allowedTypes, bool suppressEvents)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
double configTx = 0;
|
2012-09-21 09:43:06 +02:00
|
|
|
if ((allowedTypes & Attribute_Config) != 0 &&
|
|
|
|
serializedUpdate->Contains("configTx")) {
|
2012-08-02 09:38:08 +02:00
|
|
|
configTx = serializedUpdate->Get("configTx");
|
|
|
|
|
|
|
|
if (configTx > m_ConfigTx)
|
|
|
|
ClearAttributesByType(Attribute_Config);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr attrs = serializedUpdate->Get("attrs");
|
|
|
|
|
|
|
|
Dictionary::Iterator it;
|
|
|
|
for (it = attrs->Begin(); it != attrs->End(); it++) {
|
|
|
|
if (!it->second.IsObjectType<Dictionary>())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dictionary::Ptr attr = it->second;
|
|
|
|
|
|
|
|
int type = attr->Get("type");
|
2012-08-03 23:03:58 +02:00
|
|
|
|
|
|
|
if ((type & ~allowedTypes) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value data = attr->Get("data");
|
2012-08-02 09:38:08 +02:00
|
|
|
double tx = attr->Get("tx");
|
|
|
|
|
|
|
|
if (type & Attribute_Config)
|
|
|
|
RegisterAttribute(it->first, Attribute_Config);
|
|
|
|
|
|
|
|
if (!HasAttribute(it->first))
|
|
|
|
RegisterAttribute(it->first, static_cast<DynamicAttributeType>(type));
|
|
|
|
|
|
|
|
InternalSetAttribute(it->first, data, tx, suppressEvents);
|
|
|
|
}
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
void DynamicObject::RegisterAttribute(const String& name,
|
|
|
|
DynamicAttributeType type)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
DynamicAttribute attr;
|
|
|
|
attr.Type = type;
|
|
|
|
attr.Tx = 0;
|
|
|
|
|
|
|
|
pair<DynamicObject::AttributeIterator, bool> tt;
|
|
|
|
tt = m_Attributes.insert(make_pair(name, attr));
|
|
|
|
|
|
|
|
if (!tt.second)
|
|
|
|
tt.first->second.Type = type;
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
InternalSetAttribute(name, data, GetCurrentTx());
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
void DynamicObject::Touch(const String& name)
|
|
|
|
{
|
|
|
|
InternalSetAttribute(name, InternalGetAttribute(name), GetCurrentTx());
|
|
|
|
}
|
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
Value DynamicObject::Get(const String& name) const
|
|
|
|
{
|
|
|
|
return InternalGetAttribute(name);
|
|
|
|
}
|
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
void DynamicObject::InternalSetAttribute(const String& name, const Value& data,
|
|
|
|
double tx, bool suppressEvent)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
DynamicAttribute attr;
|
|
|
|
attr.Type = Attribute_Transient;
|
|
|
|
attr.Data = data;
|
|
|
|
attr.Tx = tx;
|
|
|
|
|
|
|
|
pair<DynamicObject::AttributeIterator, bool> tt;
|
|
|
|
tt = m_Attributes.insert(make_pair(name, attr));
|
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
Value oldValue;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!tt.second && tx >= tt.first->second.Tx) {
|
2012-08-03 23:03:58 +02:00
|
|
|
oldValue = tt.first->second.Data;
|
2012-08-02 09:38:08 +02:00
|
|
|
tt.first->second.Data = data;
|
|
|
|
tt.first->second.Tx = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tt.first->second.Type & Attribute_Config)
|
|
|
|
m_ConfigTx = tx;
|
|
|
|
|
|
|
|
if (!suppressEvent) {
|
|
|
|
m_ModifiedObjects.insert(GetSelf());
|
2012-08-03 23:03:58 +02:00
|
|
|
OnAttributeChanged(name, oldValue);
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Value DynamicObject::InternalGetAttribute(const String& name) const
|
2012-03-31 15:18:09 +02: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
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
return it->second.Data;
|
2012-04-20 14:20:25 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
bool DynamicObject::HasAttribute(const String& name) const
|
2012-07-02 14:38:37 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
return (m_Attributes.find(name) != m_Attributes.end());
|
2012-07-02 14:38:37 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void DynamicObject::ClearAttributesByType(DynamicAttributeType type)
|
2012-07-02 14:38:37 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
DynamicObject::AttributeIterator prev, at;
|
|
|
|
for (at = m_Attributes.begin(); at != m_Attributes.end(); ) {
|
|
|
|
if (at->second.Type == type) {
|
|
|
|
prev = at;
|
|
|
|
at++;
|
|
|
|
m_Attributes.erase(prev);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
at++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String DynamicObject::GetType(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("__type");
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String DynamicObject::GetName(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("__name");
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DynamicObject::IsLocal(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("__local");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (value != 0);
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DynamicObject::IsAbstract(void) const
|
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("__abstract");
|
|
|
|
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (value != 0);
|
2012-07-02 14:38:37 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void DynamicObject::SetSource(const String& value)
|
2012-07-02 19:25:33 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Set("__source", value);
|
2012-07-02 19:25:33 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String DynamicObject::GetSource(void) const
|
2012-07-02 19:25:33 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
return Get("__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
|
|
|
{
|
2012-07-10 13:00:53 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr dobj = GetObject(GetType(), GetName());
|
|
|
|
DynamicObject::Ptr self = GetSelf();
|
2012-06-12 09:36:18 +02:00
|
|
|
assert(!dobj || dobj == self);
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
pair<DynamicObject::TypeMap::iterator, bool> ti;
|
|
|
|
ti = GetAllObjects().insert(make_pair(GetType(), DynamicObject::NameMap()));
|
2012-07-27 16:05:02 +02:00
|
|
|
ti.first->second.insert(make_pair(GetName(), GetSelf()));
|
2012-07-02 19:25:33 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
OnRegistered(GetSelf());
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
void DynamicObject::Unregister(void)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2012-07-10 13:00:53 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::TypeMap::iterator tt;
|
2012-07-27 16:05:02 +02:00
|
|
|
tt = GetAllObjects().find(GetType());
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (tt == GetAllObjects().end())
|
|
|
|
return;
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::NameMap::iterator nt = tt->second.find(GetName());
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (nt == tt->second.end())
|
|
|
|
return;
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
tt->second.erase(nt);
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
OnUnregistered(GetSelf());
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
DynamicObject::Ptr DynamicObject::GetObject(const String& type, const String& name)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::TypeMap::iterator tt;
|
2012-07-27 16:05:02 +02:00
|
|
|
tt = GetAllObjects().find(type);
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (tt == GetAllObjects().end())
|
2012-07-30 10:17:29 +02:00
|
|
|
return DynamicObject::Ptr();
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::NameMap::iterator nt = tt->second.find(name);
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (nt == tt->second.end())
|
2012-07-30 10:17:29 +02:00
|
|
|
return DynamicObject::Ptr();
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
return nt->second;
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
pair<DynamicObject::TypeMap::iterator, DynamicObject::TypeMap::iterator> DynamicObject::GetTypes(void)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2012-07-27 16:05:02 +02:00
|
|
|
return make_pair(GetAllObjects().begin(), GetAllObjects().end());
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
pair<DynamicObject::NameMap::iterator, DynamicObject::NameMap::iterator> DynamicObject::GetObjects(const String& type)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
pair<DynamicObject::TypeMap::iterator, bool> ti;
|
|
|
|
ti = GetAllObjects().insert(make_pair(type, DynamicObject::NameMap()));
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
return make_pair(ti.first->second.begin(), ti.first->second.end());
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
2012-07-14 12:44:37 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
ScriptTask::Ptr DynamicObject::InvokeMethod(const String& method,
|
|
|
|
const vector<Value>& arguments, ScriptTask::CompletionCallback callback)
|
2012-07-14 15:59:59 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
Value value = Get("methods");
|
|
|
|
|
|
|
|
if (!value.IsObjectType<Dictionary>())
|
|
|
|
return ScriptTask::Ptr();
|
|
|
|
|
|
|
|
Dictionary::Ptr methods = value;
|
|
|
|
if (!methods->Contains(method))
|
2012-07-14 15:59:59 +02:00
|
|
|
return ScriptTask::Ptr();
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String funcName = methods->Get(method);
|
|
|
|
|
2012-07-14 15:59:59 +02:00
|
|
|
ScriptFunction::Ptr func = ScriptFunction::GetByName(funcName);
|
|
|
|
|
|
|
|
if (!func)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(invalid_argument("Function '" + funcName + "' does not exist."));
|
2012-07-14 15:59:59 +02:00
|
|
|
|
2012-07-15 17:15:49 +02:00
|
|
|
ScriptTask::Ptr task = boost::make_shared<ScriptTask>(func, arguments);
|
|
|
|
task->Start(callback);
|
2012-07-14 15:59:59 +02:00
|
|
|
|
|
|
|
return task;
|
|
|
|
}
|
2012-07-24 13:13:02 +02:00
|
|
|
|
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-07-24 13:13:02 +02:00
|
|
|
ofstream fp;
|
2012-08-07 14:17:36 +02:00
|
|
|
fp.open(tempFilename.CStr());
|
2012-07-24 13:13:02 +02:00
|
|
|
|
|
|
|
if (!fp)
|
2012-07-26 11:42:57 +02:00
|
|
|
throw_exception(runtime_error("Could not open '" + filename + "' file"));
|
2012-07-24 13:13:02 +02:00
|
|
|
|
|
|
|
FIFO::Ptr fifo = boost::make_shared<FIFO>();
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::TypeMap::iterator tt;
|
2012-07-27 16:05:02 +02:00
|
|
|
for (tt = GetAllObjects().begin(); tt != GetAllObjects().end(); tt++) {
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::NameMap::iterator nt;
|
2012-07-27 16:05:02 +02:00
|
|
|
for (nt = tt->second.begin(); nt != tt->second.end(); nt++) {
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr object = nt->second;
|
2012-07-27 16:05:02 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr persistentObject = boost::make_shared<Dictionary>();
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
persistentObject->Set("type", object->GetType());
|
|
|
|
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-08-02 09:38:08 +02:00
|
|
|
/* This is quite ugly, unfortunatelly NetString requires an IOQueue object */
|
|
|
|
NetString::WriteStringToIOQueue(fifo.get(), json);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
size_t count;
|
|
|
|
while ((count = fifo->GetAvailableBytes()) > 0) {
|
|
|
|
char buffer[1024];
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (count > sizeof(buffer))
|
|
|
|
count = sizeof(buffer);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
fifo->Read(buffer, count);
|
|
|
|
fp.write(buffer, count);
|
|
|
|
}
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 14:17:36 +02:00
|
|
|
|
2012-08-14 09:51:11 +02:00
|
|
|
if (rename(tempFilename.CStr(), filename.CStr()) < 0)
|
|
|
|
throw_exception(PosixException("rename() failed", errno));
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
|
|
|
|
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 + "'");
|
|
|
|
|
|
|
|
std::ifstream fp;
|
2012-08-02 09:38:08 +02:00
|
|
|
fp.open(filename.CStr());
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
/* TODO: Fix this horrible mess by implementing a class that provides
|
|
|
|
* IOQueue functionality for files. */
|
2012-07-24 13:13:02 +02:00
|
|
|
FIFO::Ptr fifo = boost::make_shared<FIFO>();
|
|
|
|
while (fp) {
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
fp.read(buffer, sizeof(buffer));
|
|
|
|
fifo->Write(buffer, fp.gcount());
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String message;
|
|
|
|
while (NetString::ReadStringFromIOQueue(fifo.get(), &message)) {
|
2012-08-05 03:10:53 +02:00
|
|
|
Dictionary::Ptr persistentObject = Value::Deserialize(message);
|
2012-07-24 13:13:02 +02: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");
|
|
|
|
|
|
|
|
DynamicObject::Ptr object = GetObject(type, name);
|
|
|
|
|
|
|
|
if (hasConfig && !object) {
|
|
|
|
object = Create(type, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2012-08-07 13:08:14 +02:00
|
|
|
void DynamicObject::DeactivateObjects(void)
|
|
|
|
{
|
|
|
|
DynamicObject::TypeMap::iterator tt;
|
|
|
|
for (tt = GetAllObjects().begin(); tt != GetAllObjects().end(); tt++) {
|
|
|
|
DynamicObject::NameMap::iterator nt;
|
|
|
|
|
|
|
|
while ((nt = tt->second.begin()) != tt->second.end()) {
|
|
|
|
DynamicObject::Ptr object = nt->second;
|
|
|
|
|
|
|
|
object->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::TypeMap& DynamicObject::GetAllObjects(void)
|
2012-07-27 16:05:02 +02:00
|
|
|
{
|
|
|
|
static TypeMap objects;
|
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::ClassMap& DynamicObject::GetClasses(void)
|
2012-07-27 16:05:02 +02:00
|
|
|
{
|
|
|
|
static ClassMap classes;
|
|
|
|
return classes;
|
|
|
|
}
|
|
|
|
|
2012-08-14 12:51:51 +02:00
|
|
|
bool DynamicObject::ClassExists(const String& name)
|
|
|
|
{
|
|
|
|
return (GetClasses().find(name) != GetClasses().end());
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void DynamicObject::RegisterClass(const String& type, DynamicObject::Factory factory)
|
2012-07-27 16:05:02 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
if (GetObjects(type).first != GetObjects(type).second)
|
|
|
|
throw_exception(runtime_error("Cannot register class for type '" +
|
|
|
|
type + "': Objects of this type already exist."));
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
GetClasses()[type] = factory;
|
2012-07-27 16:05:02 +02:00
|
|
|
}
|
|
|
|
|
2012-08-04 10:23:18 +02:00
|
|
|
DynamicObject::Ptr DynamicObject::Create(const String& type, const Dictionary::Ptr& serializedUpdate)
|
2012-07-27 16:05:02 +02:00
|
|
|
{
|
2012-08-03 23:03:58 +02:00
|
|
|
DynamicObject::ClassMap::iterator ct;
|
|
|
|
ct = GetClasses().find(type);
|
|
|
|
|
|
|
|
DynamicObject::Ptr obj;
|
|
|
|
if (ct != GetClasses().end()) {
|
2012-08-04 10:23:18 +02:00
|
|
|
obj = ct->second(serializedUpdate);
|
2012-08-03 23:03:58 +02:00
|
|
|
} else {
|
2012-08-04 10:23:18 +02:00
|
|
|
obj = boost::make_shared<DynamicObject>(serializedUpdate);
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
Logger::Write(LogCritical, "base", "Creating generic DynamicObject for type '" + type + "'");
|
|
|
|
}
|
|
|
|
|
2012-08-04 10:23:18 +02:00
|
|
|
/* apply the object's non-config attributes */
|
|
|
|
obj->ApplyUpdate(serializedUpdate, Attribute_All & ~Attribute_Config);
|
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
return obj;
|
2012-07-27 16:05:02 +02:00
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
double DynamicObject::GetCurrentTx(void)
|
|
|
|
{
|
|
|
|
assert(m_CurrentTx != 0);
|
|
|
|
|
|
|
|
return m_CurrentTx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicObject::BeginTx(void)
|
|
|
|
{
|
|
|
|
m_CurrentTx = Utility::GetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicObject::FinishTx(void)
|
|
|
|
{
|
|
|
|
OnTransactionClosing(m_ModifiedObjects);
|
|
|
|
m_ModifiedObjects.clear();
|
|
|
|
|
|
|
|
m_CurrentTx = 0;
|
|
|
|
}
|
2012-08-03 23:03:58 +02:00
|
|
|
|
2012-08-07 21:02:12 +02:00
|
|
|
void DynamicObject::OnAttributeChanged(const String&, const Value&)
|
2012-08-04 14:13:04 +02:00
|
|
|
{ }
|
2012-08-07 21:02:12 +02:00
|
|
|
|