icinga2/components/livestatus/component.cpp

84 lines
3.2 KiB
C++
Raw Normal View History

2013-03-10 03:09:01 +01: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 *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2013-03-17 20:19:29 +01:00
#include "livestatus/component.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/logger_fwd.h"
2013-03-17 20:19:29 +01:00
#include "base/tcpsocket.h"
#include "base/application.h"
2013-03-16 21:18:53 +01:00
#include <boost/smart_ptr/make_shared.hpp>
2013-03-10 03:09:01 +01:00
using namespace icinga;
using namespace livestatus;
2013-03-10 03:09:01 +01:00
REGISTER_TYPE(LivestatusComponent);
LivestatusComponent::LivestatusComponent(const Dictionary::Ptr& serializedUpdate)
: DynamicObject(serializedUpdate)
{
RegisterAttribute("socket_path", Attribute_Config, &m_SocketPath);
}
2013-03-10 03:09:01 +01:00
/**
* Starts the component.
*/
void LivestatusComponent::Start(void)
{
2013-03-11 13:45:08 +01:00
//#ifndef _WIN32
// UnixSocket::Ptr socket = boost::make_shared<UnixSocket>();
// socket->Bind(GetSocketPath());
//#else /* _WIN32 */
2013-03-10 05:10:51 +01:00
TcpSocket::Ptr socket = boost::make_shared<TcpSocket>();
2013-03-11 13:45:08 +01:00
socket->Bind("6558", AF_INET);
//#endif /* _WIN32 */
2013-03-10 05:10:51 +01:00
socket->OnNewClient.connect(boost::bind(&LivestatusComponent::NewClientHandler, this, _2));
2013-03-10 03:09:01 +01:00
socket->Listen();
socket->Start();
m_Listener = socket;
}
String LivestatusComponent::GetSocketPath(void) const
{
Value socketPath = m_SocketPath;
2013-03-10 03:09:01 +01:00
if (socketPath.IsEmpty())
return Application::GetLocalStateDir() + "/run/icinga2/livestatus";
else
return socketPath;
}
void LivestatusComponent::NewClientHandler(const Socket::Ptr& client)
{
2013-03-16 21:18:53 +01:00
Log(LogInformation, "livestatus", "Client connected");
2013-03-10 03:09:01 +01:00
LivestatusConnection::Ptr lconnection = boost::make_shared<LivestatusConnection>(client);
lconnection->OnClosed.connect(boost::bind(&LivestatusComponent::ClientClosedHandler, this, _1));
m_Connections.insert(lconnection);
client->Start();
}
void LivestatusComponent::ClientClosedHandler(const Connection::Ptr& connection)
{
LivestatusConnection::Ptr lconnection = static_pointer_cast<LivestatusConnection>(connection);
2013-03-16 21:18:53 +01:00
Log(LogInformation, "livestatus", "Client disconnected");
2013-03-10 03:09:01 +01:00
m_Connections.erase(lconnection);
}