icinga2/lib/base/dynamicobject.cpp

358 lines
8.6 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright (C) 2012-2015 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. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "base/dynamicobject.hpp"
#include "base/dynamictype.hpp"
#include "base/serializer.hpp"
#include "base/netstring.hpp"
2014-10-26 19:59:49 +01:00
#include "base/json.hpp"
2014-05-25 16:23:35 +02:00
#include "base/stdiostream.hpp"
#include "base/debug.hpp"
#include "base/objectlock.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
2014-05-25 16:23:35 +02:00
#include "base/exception.hpp"
#include "base/function.hpp"
2014-05-25 16:23:35 +02:00
#include "base/initialize.hpp"
#include "base/workqueue.hpp"
#include "base/context.hpp"
#include "base/application.hpp"
2013-03-16 21:18:53 +01:00
#include <fstream>
#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;
REGISTER_TYPE(DynamicObject);
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::OnPaused;
boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnResumed;
boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnStateChanged;
DynamicObject::DynamicObject(void)
2013-10-26 09:41:45 +02:00
{ }
DynamicType::Ptr DynamicObject::GetType(void) const
{
return DynamicType::GetByName(GetTypeNameV());
}
DebugInfo DynamicObject::GetDebugInfo(void) const
{
return m_DebugInfo;
}
void DynamicObject::SetDebugInfo(const DebugInfo& di)
{
m_DebugInfo = di;
}
bool DynamicObject::IsActive(void) const
{
2013-10-26 09:41:45 +02:00
return GetActive();
2013-09-12 10:03:48 +02:00
}
bool DynamicObject::IsPaused(void) const
{
return GetPaused();
}
void DynamicObject::SetExtension(const String& key, const Value& value)
{
2013-10-26 09:41:45 +02:00
Dictionary::Ptr extensions = GetExtensions();
if (!extensions) {
extensions = new Dictionary();
2013-10-26 09:41:45 +02:00
SetExtensions(extensions);
}
extensions->Set(key, value);
}
Value DynamicObject::GetExtension(const String& key)
{
2013-10-26 09:41:45 +02:00
Dictionary::Ptr extensions = GetExtensions();
if (!extensions)
return Empty;
return extensions->Get(key);
}
void DynamicObject::ClearExtension(const String& key)
{
2013-10-26 09:41:45 +02:00
Dictionary::Ptr extensions = GetExtensions();
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(this);
}
void DynamicObject::Start(void)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
2013-03-01 12:07:52 +01:00
SetStartCalled(true);
}
void DynamicObject::Activate(void)
{
CONTEXT("Activating object '" + GetName() + "' of type '" + GetType()->GetName() + "'");
ASSERT(!OwnsLock());
Start();
ASSERT(GetStartCalled());
{
ObjectLock olock(this);
ASSERT(!IsActive());
SetActive(true);
}
OnStarted(this);
SetAuthority(true);
}
void DynamicObject::Stop(void)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
SetStopCalled(true);
}
void DynamicObject::Deactivate(void)
{
CONTEXT("Deactivating object '" + GetName() + "' of type '" + GetType()->GetName() + "'");
ASSERT(!OwnsLock());
SetAuthority(false);
{
ObjectLock olock(this);
if (!IsActive())
return;
SetActive(false);
}
Stop();
2013-03-02 09:07:47 +01:00
ASSERT(GetStopCalled());
OnStopped(this);
}
void DynamicObject::OnConfigLoaded(void)
{
/* Nothing to do here. */
}
void DynamicObject::OnAllConfigLoaded(void)
{
/* Nothing to do here. */
}
void DynamicObject::OnStateLoaded(void)
{
/* Nothing to do here. */
}
void DynamicObject::Pause(void)
{
SetPauseCalled(true);
}
void DynamicObject::Resume(void)
{
SetResumeCalled(true);
}
void DynamicObject::SetAuthority(bool authority)
{
if (authority && GetPaused()) {
SetResumeCalled(false);
Resume();
ASSERT(GetResumeCalled());
SetPaused(false);
OnResumed(this);
} else if (!authority && !GetPaused()) {
SetPauseCalled(false);
Pause();
ASSERT(GetPauseCalled());
SetPaused(true);
OnPaused(this);
}
}
void DynamicObject::DumpObjects(const String& filename, int attributeTypes)
2012-07-24 13:13:02 +02:00
{
2014-10-19 17:52:17 +02:00
Log(LogInformation, "DynamicObject")
<< "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)
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));
2012-07-24 13:13:02 +02:00
StdioStream::Ptr sfp = new 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 = new 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 = Serialize(object, attributeTypes);
if (!update)
continue;
persistentObject->Set("update", update);
2012-07-24 13:13:02 +02:00
2014-10-26 19:59:49 +01:00
String json = JsonEncode(persistentObject);
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::RestoreObject(const String& message, int attributeTypes)
{
2014-10-26 19:59:49 +01:00
Dictionary::Ptr persistentObject = JsonDecode(message);
String type = persistentObject->Get("type");
DynamicType::Ptr dt = DynamicType::GetByName(type);
if (!dt)
return;
String name = persistentObject->Get("name");
DynamicObject::Ptr object = dt->GetObject(name);
if (!object)
return;
ASSERT(!object->IsActive());
2014-12-19 12:19:28 +01:00
#ifdef I2_DEBUG
2014-10-19 17:52:17 +02:00
Log(LogDebug, "DynamicObject")
<< "Restoring object '" << name << "' of type '" << type << "'.";
2014-12-19 12:19:28 +01:00
#endif /* I2_DEBUG */
Dictionary::Ptr update = persistentObject->Get("update");
Deserialize(object, update, false, attributeTypes);
object->OnStateLoaded();
object->SetStateLoaded(true);
}
void DynamicObject::RestoreObjects(const String& filename, int attributeTypes)
2012-07-24 13:13:02 +02:00
{
2014-10-19 17:52:17 +02:00
Log(LogInformation, "DynamicObject")
<< "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 = new StdioStream (&fp, false);
2012-07-24 13:13:02 +02:00
unsigned long restored = 0;
WorkQueue upq(25000, Application::GetConcurrency());
String message;
StreamReadContext src;
while (NetString::ReadStringFromStream(sfp, &message, src) == StatusNewItem) {
upq.Enqueue(boost::bind(&DynamicObject::RestoreObject, message, attributeTypes));
restored++;
2012-07-24 13:13:02 +02:00
}
2012-11-22 12:04:32 +01:00
sfp->Close();
upq.Join();
unsigned long no_state = 0;
BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) {
BOOST_FOREACH(const DynamicObject::Ptr& object, type->GetObjects()) {
if (!object->GetStateLoaded()) {
object->OnStateLoaded();
object->SetStateLoaded(true);
no_state++;
}
}
}
2014-10-19 17:52:17 +02:00
Log(LogInformation, "DynamicObject")
<< "Restored " << restored << " objects. Loaded " << no_state << " new objects without state.";
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()) {
object->Deactivate();
}
}
}
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);
}