icinga2/lib/base/dynamicobject.cpp

393 lines
9.8 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2013-09-25 07:43:57 +02:00
* Copyright (C) 2012-2013 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. *
******************************************************************************/
2013-03-16 21:18:53 +01:00
#include "base/dynamicobject.h"
#include "base/dynamictype.h"
#include "base/netstring.h"
#include "base/registry.h"
#include "base/stdiostream.h"
2013-08-28 08:18:58 +02:00
#include "base/debug.h"
2013-03-16 21:18:53 +01:00
#include "base/objectlock.h"
#include "base/logger_fwd.h"
#include "base/exception.h"
2013-03-18 11:02:18 +01:00
#include "base/timer.h"
2013-03-25 18:36:15 +01:00
#include "base/scriptfunction.h"
2013-03-16 21:18:53 +01:00
#include <fstream>
#include <boost/make_shared.hpp>
#include <boost/tuple/tuple.hpp>
2013-03-16 21:18:53 +01:00
#include <boost/foreach.hpp>
2013-03-18 17:04:22 +01:00
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/exception/errinfo_file_name.hpp>
using namespace icinga;
boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnStarted;
boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnStopped;
boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnStateChanged;
boost::signals2::signal<void (const DynamicObject::Ptr&, const String&, bool)> DynamicObject::OnAuthorityChanged;
DynamicObject::DynamicObject(void)
: m_Active(false)
{ }
DynamicObject::~DynamicObject(void)
2013-02-19 12:17:31 +01:00
{ }
Dictionary::Ptr DynamicObject::Serialize(int attributeTypes) const
2013-02-18 14:40:24 +01:00
{
Dictionary::Ptr update = boost::make_shared<Dictionary>();
ASSERT(!OwnsLock());
2013-03-04 15:52:42 +01:00
ObjectLock olock(this);
InternalSerialize(update, attributeTypes);
/* Make sure our own InternalSerialize() method was called. */
ASSERT(update->Contains("__marker"));
update->Remove("__marker");
2013-02-26 10:13:54 +01:00
return update;
}
void DynamicObject::Deserialize(const Dictionary::Ptr& update, int attributeTypes)
{
ASSERT(!OwnsLock());
2013-03-04 15:52:42 +01:00
{
ObjectLock olock(this);
InternalDeserialize(update, attributeTypes);
}
}
void DynamicObject::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
{
if (attributeTypes & Attribute_Config) {
bag->Set("__name", m_Name);
bag->Set("__type", m_Type);
bag->Set("methods", m_Methods);
bag->Set("custom", m_Custom);
2013-09-12 10:03:48 +02:00
bag->Set("authorities", m_Authorities);
2013-09-17 13:18:26 +02:00
bag->Set("domains", m_Domains);
2013-03-04 15:52:42 +01:00
}
2013-09-17 13:18:26 +02:00
if (attributeTypes & Attribute_State)
bag->Set("extensions", m_Extensions);
2013-03-04 15:52:42 +01:00
/* This attribute is used by Serialize() to check that this
* method was called. */
bag->Set("__marker", 1);
2012-08-03 13:19:55 +02:00
}
void DynamicObject::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
{
if (attributeTypes & Attribute_Config) {
m_Name = bag->Get("__name");
m_Type = bag->Get("__type");
m_Methods = bag->Get("methods");
m_Custom = bag->Get("custom");
2013-09-12 10:03:48 +02:00
m_Authorities = bag->Get("authorities");
2013-09-17 13:18:26 +02:00
m_Domains = bag->Get("domains");
2013-02-17 19:14:34 +01:00
}
2013-09-17 13:18:26 +02:00
if (attributeTypes & Attribute_State)
m_Extensions = bag->Get("extensions");
}
DynamicType::Ptr DynamicObject::GetType(void) const
{
2013-02-26 10:13:54 +01:00
return DynamicType::GetByName(m_Type);
}
String DynamicObject::GetName(void) const
{
2013-02-26 10:13:54 +01:00
return m_Name;
}
bool DynamicObject::IsActive(void) const
{
return m_Active;
}
2013-09-12 10:03:48 +02:00
Array::Ptr DynamicObject::GetAuthorities(void) const
{
return m_Authorities;
}
void DynamicObject::SetAuthority(const String& type, bool value)
2013-09-12 10:03:48 +02:00
{
ASSERT(!OwnsLock());
{
ObjectLock olock(this);
if (!m_Authority)
m_Authority = boost::make_shared<Dictionary>();
bool old_value = HasAuthority(type);
if (old_value == value)
return;
m_Authority->Set(type, value);
}
OnAuthorityChanged(GetSelf(), type, value);
}
bool DynamicObject::HasAuthority(const String& type) const
{
if (!m_Authority)
return true;
return m_Authority->Get(type);
2013-09-12 10:03:48 +02:00
}
2013-09-17 13:18:26 +02:00
Array::Ptr DynamicObject::GetDomains(void) const
{
return m_Domains;
}
void DynamicObject::SetPrivileges(const String& instance, int privs)
{
m_Privileges[instance] = privs;
}
bool DynamicObject::HasPrivileges(const String& instance, int privs) const
{
if (privs == 0)
return true;
2013-09-17 13:18:26 +02:00
std::map<String, int>::const_iterator it;
it = m_Privileges.find(instance);
if (it == m_Privileges.end())
return false;
return (it->second & privs) == privs;
}
void DynamicObject::SetExtension(const String& key, const Object::Ptr& object)
{
Dictionary::Ptr extensions = m_Extensions;
if (!extensions) {
extensions = boost::make_shared<Dictionary>();
m_Extensions = extensions;
}
extensions->Set(key, object);
}
Object::Ptr DynamicObject::GetExtension(const String& key)
{
Dictionary::Ptr extensions = m_Extensions;
if (!extensions)
return Object::Ptr();
return extensions->Get(key);
}
void DynamicObject::ClearExtension(const String& key)
{
Dictionary::Ptr extensions = m_Extensions;
if (!extensions)
return;
extensions->Remove(key);
}
void DynamicObject::Register(void)
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
2013-03-04 15:52:42 +01:00
DynamicType::Ptr dtype = GetType();
dtype->RegisterObject(GetSelf());
}
void DynamicObject::Start(void)
{
ASSERT(!OwnsLock());
2013-03-01 12:07:52 +01:00
ASSERT(!m_Active);
m_Active = true;
OnStarted(GetSelf());
}
void DynamicObject::Stop(void)
{
ASSERT(!OwnsLock());
2013-03-02 09:07:47 +01:00
ASSERT(m_Active);
m_Active = false;
OnStopped(GetSelf());
}
void DynamicObject::OnConfigLoaded(void)
{
/* Nothing to do here. */
}
void DynamicObject::OnStateLoaded(void)
{
/* Nothing to do here. */
}
2013-03-25 18:36:15 +01:00
Value DynamicObject::InvokeMethod(const String& method,
2013-03-16 21:18:53 +01:00
const std::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)
2013-03-25 18:36:15 +01:00
BOOST_THROW_EXCEPTION(std::invalid_argument("Method '" + method + "' does not exist."));
2013-03-08 16:02:33 +01:00
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())
2013-03-25 18:36:15 +01:00
BOOST_THROW_EXCEPTION(std::invalid_argument("Method '" + method + "' does not exist."));
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-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::invalid_argument("Function '" + funcName + "' does not exist."));
2012-07-14 15:59:59 +02:00
2013-03-25 18:36:15 +01:00
return func->Invoke(arguments);
2012-07-14 15:59:59 +02:00
}
2012-07-24 13:13:02 +02:00
void DynamicObject::DumpObjects(const String& filename, int attributeTypes)
2012-07-24 13:13:02 +02:00
{
2013-03-16 21:18:53 +01:00
Log(LogInformation, "base", "Dumping program state to file '" + filename + "'");
2012-07-24 13:13:02 +02:00
String tempFilename = filename + ".tmp";
2013-03-16 21:18:53 +01:00
std::fstream fp;
fp.open(tempFilename.CStr(), std::ios_base::out);
2012-07-24 13:13:02 +02:00
if (!fp)
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + filename + "' file"));
2012-07-24 13:13:02 +02:00
StdioStream::Ptr sfp = boost::make_shared<StdioStream>(&fp, false);
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()) {
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());
persistentObject->Set("name", object->GetName());
Dictionary::Ptr update = object->Serialize(attributeTypes);
if (!update)
continue;
persistentObject->Set("update", update);
2012-07-24 13:13:02 +02:00
Value value = persistentObject;
String json = value.Serialize();
2012-07-24 13:13:02 +02:00
NetString::WriteStringToStream(sfp, json);
2012-07-24 13:13:02 +02:00
}
}
sfp->Close();
2012-11-22 12:04:32 +01: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()
2013-03-18 17:04:22 +01:00
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
2013-03-11 13:45:08 +01:00
}
2012-07-24 13:13:02 +02:00
}
void DynamicObject::RestoreObjects(const String& filename, int attributeTypes)
2012-07-24 13:13:02 +02:00
{
2013-03-16 21:18:53 +01:00
Log(LogInformation, "base", "Restoring program state from file '" + filename + "'");
2012-07-24 13:13:02 +02:00
std::fstream fp;
fp.open(filename.CStr(), std::ios_base::in);
2012-07-24 13:13:02 +02:00
StdioStream::Ptr sfp = boost::make_shared<StdioStream>(&fp, false);
2012-07-24 13:13:02 +02:00
unsigned long restored = 0;
String message;
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
String type = persistentObject->Get("type");
String name = persistentObject->Get("name");
Dictionary::Ptr update = persistentObject->Get("update");
2012-07-24 13:13:02 +02:00
DynamicType::Ptr dt = DynamicType::GetByName(type);
if (!dt)
continue;
DynamicObject::Ptr object = dt->GetObject(name);
2012-08-05 03:10:53 +02:00
if (object) {
ASSERT(!object->IsActive());
2013-08-29 16:09:11 +02:00
Log(LogDebug, "base", "Restoring object '" + name + "' of type '" + type + "'.");
object->Deserialize(update, attributeTypes);
2013-08-29 19:25:34 +02:00
object->OnStateLoaded();
}
restored++;
2012-07-24 13:13:02 +02:00
}
2012-11-22 12:04:32 +01:00
sfp->Close();
2013-03-16 21:18:53 +01:00
std::ostringstream msgbuf;
msgbuf << "Restored " << restored << " objects";
2013-03-16 21:18:53 +01:00
Log(LogInformation, "base", msgbuf.str());
2012-07-24 13:13:02 +02:00
}
void DynamicObject::StopObjects(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()) {
if (object->IsActive())
object->Stop();
}
}
}
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);
}
Dictionary::Ptr DynamicObject::GetCustom(void) const
{
return m_Custom;
}