Cleaned up the code a bit.

This commit is contained in:
Gunnar Beutner 2012-09-21 09:43:06 +02:00
parent 6a15e69e88
commit 97829fbfcd
10 changed files with 70 additions and 66 deletions

View File

@ -30,7 +30,7 @@ void CheckerComponent::Start(void)
m_Endpoint->RegisterSubscription("checker"); m_Endpoint->RegisterSubscription("checker");
Service::OnCheckerChanged.connect(bind(&CheckerComponent::CheckerChangedHandler, this, _1)); Service::OnCheckerChanged.connect(bind(&CheckerComponent::CheckerChangedHandler, this, _1));
DynamicObject::OnUnregistered.connect(bind(&CheckerComponent::ServiceRemovedHandler, this, _1)); DynamicObject::OnUnregistered.connect(bind(&CheckerComponent::ObjectRemovedHandler, this, _1));
m_CheckTimer = boost::make_shared<Timer>(); m_CheckTimer = boost::make_shared<Timer>();
m_CheckTimer->SetInterval(1); m_CheckTimer->SetInterval(1);
@ -184,7 +184,7 @@ void CheckerComponent::CheckerChangedHandler(const Service::Ptr& service)
} }
} }
void CheckerComponent::ServiceRemovedHandler(const DynamicObject::Ptr& object) void CheckerComponent::ObjectRemovedHandler(const DynamicObject::Ptr& object)
{ {
Service::Ptr service = dynamic_pointer_cast<Service>(object); Service::Ptr service = dynamic_pointer_cast<Service>(object);
@ -197,3 +197,4 @@ void CheckerComponent::ServiceRemovedHandler(const DynamicObject::Ptr& object)
} }
EXPORT_COMPONENT(checker, CheckerComponent); EXPORT_COMPONENT(checker, CheckerComponent);

View File

@ -23,6 +23,9 @@
namespace icinga namespace icinga
{ {
/**
* @ingroup checker
*/
struct ServiceNextCheckExtractor struct ServiceNextCheckExtractor
{ {
typedef double result_type; typedef double result_type;
@ -71,10 +74,7 @@ private:
void AdjustCheckTimer(void); void AdjustCheckTimer(void);
void CheckerChangedHandler(const Service::Ptr& service); void CheckerChangedHandler(const Service::Ptr& service);
void ServiceRemovedHandler(const DynamicObject::Ptr& object); void ObjectRemovedHandler(const DynamicObject::Ptr& object);
//void AssignServiceRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
//void ClearServicesRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
}; };
} }

View File

@ -26,8 +26,8 @@ using namespace icinga;
*/ */
void ConvenienceComponent::Start(void) void ConvenienceComponent::Start(void)
{ {
ConfigItem::OnCommitted.connect(boost::bind(&ConvenienceComponent::HostCommittedHandler, this, _1)); ConfigItem::OnCommitted.connect(boost::bind(&ConvenienceComponent::ObjectCommittedHandler, this, _1));
ConfigItem::OnRemoved.connect(boost::bind(&ConvenienceComponent::HostRemovedHandler, this, _1)); ConfigItem::OnRemoved.connect(boost::bind(&ConvenienceComponent::ObjectRemovedHandler, this, _1));
} }
template<typename TDict> template<typename TDict>
@ -68,7 +68,7 @@ static void CopyServiceAttributes(const Host::Ptr& host, TDict serviceDesc,
Service::ResolveDependencies(host, hostchecks)); Service::ResolveDependencies(host, hostchecks));
} }
void ConvenienceComponent::HostCommittedHandler(const ConfigItem::Ptr& item) void ConvenienceComponent::ObjectCommittedHandler(const ConfigItem::Ptr& item)
{ {
if (item->GetType() != "Host") if (item->GetType() != "Host")
return; return;
@ -139,7 +139,7 @@ void ConvenienceComponent::HostCommittedHandler(const ConfigItem::Ptr& item)
host->Set("convenience_services", newServices); host->Set("convenience_services", newServices);
} }
void ConvenienceComponent::HostRemovedHandler(const ConfigItem::Ptr& item) void ConvenienceComponent::ObjectRemovedHandler(const ConfigItem::Ptr& item)
{ {
if (item->GetType() != "Host") if (item->GetType() != "Host")
return; return;
@ -161,3 +161,4 @@ void ConvenienceComponent::HostRemovedHandler(const ConfigItem::Ptr& item)
} }
EXPORT_COMPONENT(convenience, ConvenienceComponent); EXPORT_COMPONENT(convenience, ConvenienceComponent);

View File

@ -32,9 +32,8 @@ public:
virtual void Start(void); virtual void Start(void);
private: private:
void HostAddedHandler(const ConfigItem::Ptr& item); void ObjectCommittedHandler(const ConfigItem::Ptr& item);
void HostCommittedHandler(const ConfigItem::Ptr& item); void ObjectRemovedHandler(const ConfigItem::Ptr& item);
void HostRemovedHandler(const ConfigItem::Ptr& item);
}; };
} }

View File

@ -28,7 +28,8 @@ void DemoComponent::Start(void)
{ {
m_Endpoint = Endpoint::MakeEndpoint("demo", true); m_Endpoint = Endpoint::MakeEndpoint("demo", true);
m_Endpoint->RegisterTopicHandler("demo::HelloWorld", m_Endpoint->RegisterTopicHandler("demo::HelloWorld",
boost::bind(&DemoComponent::HelloWorldRequestHandler, this, _2, _3)); boost::bind(&DemoComponent::HelloWorldRequestHandler, this, _2,
_3));
m_DemoTimer = boost::make_shared<Timer>(); m_DemoTimer = boost::make_shared<Timer>();
m_DemoTimer->SetInterval(5); m_DemoTimer->SetInterval(5);
@ -51,20 +52,25 @@ void DemoComponent::Stop(void)
*/ */
void DemoComponent::DemoTimerHandler(void) void DemoComponent::DemoTimerHandler(void)
{ {
Logger::Write(LogInformation, "demo", "Sending multicast 'hello world' message."); Logger::Write(LogInformation, "demo", "Sending multicast 'hello"
" world' message.");
RequestMessage request; RequestMessage request;
request.SetMethod("demo::HelloWorld"); request.SetMethod("demo::HelloWorld");
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, request); EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
request);
} }
/** /**
* Processes demo::HelloWorld messages. * Processes demo::HelloWorld messages.
*/ */
void DemoComponent::HelloWorldRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request) void DemoComponent::HelloWorldRequestHandler(const Endpoint::Ptr& sender,
const RequestMessage& request)
{ {
Logger::Write(LogInformation, "demo", "Got 'hello world' from address=" + sender->GetAddress() + ", identity=" + sender->GetName()); Logger::Write(LogInformation, "demo", "Got 'hello world' from"
" address=" + sender->GetAddress() + ", identity=" +
sender->GetName());
} }
EXPORT_COMPONENT(demo, DemoComponent); EXPORT_COMPONENT(demo, DemoComponent);

View File

@ -85,15 +85,18 @@ Dictionary::Ptr DynamicObject::BuildUpdate(double sinceTx, int attributeTypes) c
return update; return update;
} }
void DynamicObject::ApplyUpdate(const Dictionary::Ptr& serializedUpdate, int allowedTypes) void DynamicObject::ApplyUpdate(const Dictionary::Ptr& serializedUpdate,
int allowedTypes)
{ {
InternalApplyUpdate(serializedUpdate, allowedTypes, false); InternalApplyUpdate(serializedUpdate, allowedTypes, false);
} }
void DynamicObject::InternalApplyUpdate(const Dictionary::Ptr& serializedUpdate, int allowedTypes, bool suppressEvents) void DynamicObject::InternalApplyUpdate(const Dictionary::Ptr& serializedUpdate,
int allowedTypes, bool suppressEvents)
{ {
double configTx = 0; double configTx = 0;
if ((allowedTypes & Attribute_Config) != 0 && serializedUpdate->Contains("configTx")) { if ((allowedTypes & Attribute_Config) != 0 &&
serializedUpdate->Contains("configTx")) {
configTx = serializedUpdate->Get("configTx"); configTx = serializedUpdate->Get("configTx");
if (configTx > m_ConfigTx) if (configTx > m_ConfigTx)
@ -127,7 +130,8 @@ void DynamicObject::InternalApplyUpdate(const Dictionary::Ptr& serializedUpdate,
} }
} }
void DynamicObject::RegisterAttribute(const String& name, DynamicAttributeType type) void DynamicObject::RegisterAttribute(const String& name,
DynamicAttributeType type)
{ {
DynamicAttribute attr; DynamicAttribute attr;
attr.Type = type; attr.Type = type;
@ -155,7 +159,8 @@ Value DynamicObject::Get(const String& name) const
return InternalGetAttribute(name); return InternalGetAttribute(name);
} }
void DynamicObject::InternalSetAttribute(const String& name, const Value& data, double tx, bool suppressEvent) void DynamicObject::InternalSetAttribute(const String& name, const Value& data,
double tx, bool suppressEvent)
{ {
DynamicAttribute attr; DynamicAttribute attr;
attr.Type = Attribute_Transient; attr.Type = Attribute_Transient;
@ -412,7 +417,8 @@ void DynamicObject::RestoreObjects(const String& filename)
std::ifstream fp; std::ifstream fp;
fp.open(filename.CStr()); fp.open(filename.CStr());
/* TODO: Fix this horrible mess. */ /* TODO: Fix this horrible mess by implementing a class that provides
* IOQueue functionality for files. */
FIFO::Ptr fifo = boost::make_shared<FIFO>(); FIFO::Ptr fifo = boost::make_shared<FIFO>();
while (fp) { while (fp) {
char buffer[1024]; char buffer[1024];

View File

@ -145,10 +145,12 @@ String Utility::GetCertificateCN(const shared_ptr<X509>& certificate)
{ {
char buffer[256]; char buffer[256];
int rc = X509_NAME_get_text_by_NID(X509_get_subject_name(certificate.get()), NID_commonName, buffer, sizeof(buffer)); int rc = X509_NAME_get_text_by_NID(X509_get_subject_name(certificate.get()),
NID_commonName, buffer, sizeof(buffer));
if (rc == -1) if (rc == -1)
throw_exception(OpenSSLException("X509 certificate has no CN attribute", ERR_get_error())); throw_exception(OpenSSLException("X509 certificate has no CN"
" attribute", ERR_get_error()));
return buffer; return buffer;
} }
@ -165,14 +167,17 @@ shared_ptr<X509> Utility::GetX509Certificate(String pemfile)
BIO *fpcert = BIO_new(BIO_s_file()); BIO *fpcert = BIO_new(BIO_s_file());
if (fpcert == NULL) if (fpcert == NULL)
throw_exception(OpenSSLException("BIO_new failed", ERR_get_error())); throw_exception(OpenSSLException("BIO_new failed",
ERR_get_error()));
if (BIO_read_filename(fpcert, pemfile.CStr()) < 0) if (BIO_read_filename(fpcert, pemfile.CStr()) < 0)
throw_exception(OpenSSLException("BIO_read_filename failed", ERR_get_error())); throw_exception(OpenSSLException("BIO_read_filename failed",
ERR_get_error()));
cert = PEM_read_bio_X509_AUX(fpcert, NULL, NULL, NULL); cert = PEM_read_bio_X509_AUX(fpcert, NULL, NULL, NULL);
if (cert == NULL) if (cert == NULL)
throw_exception(OpenSSLException("PEM_read_bio_X509_AUX failed", ERR_get_error())); throw_exception(OpenSSLException("PEM_read_bio_X509_AUX failed",
ERR_get_error()));
BIO_free(fpcert); BIO_free(fpcert);
@ -210,7 +215,8 @@ String Utility::DirName(const String& path)
#else /* _WIN32 */ #else /* _WIN32 */
if (!PathRemoveFileSpec(dir)) { if (!PathRemoveFileSpec(dir)) {
free(dir); free(dir);
throw_exception(Win32Exception("PathRemoveFileSpec() failed", GetLastError())); throw_exception(Win32Exception("PathRemoveFileSpec() failed",
GetLastError()));
} }
result = dir; result = dir;

View File

@ -258,25 +258,9 @@ void EndpointManager::SendMulticastMessage(const Endpoint::Ptr& sender,
} }
} }
/**
* Calls the specified callback function for each registered endpoint.
*
* @param callback The callback function.
*/
//void EndpointManager::ForEachEndpoint(function<void (const EndpointManager::Ptr&, const Endpoint::Ptr&)> callback)
//{
// map<String, Endpoint::Ptr>::iterator prev, i;
// for (i = m_Endpoints.begin(); i != m_Endpoints.end(); ) {
// prev = i;
// i++;
//
// callback(GetSelf(), prev->second);
// }
//}
void EndpointManager::SendAPIMessage(const Endpoint::Ptr& sender, const Endpoint::Ptr& recipient, void EndpointManager::SendAPIMessage(const Endpoint::Ptr& sender, const Endpoint::Ptr& recipient,
RequestMessage& message, RequestMessage& message,
function<void(const EndpointManager::Ptr&, const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> callback, double timeout) const EndpointManager::APICallback& callback, double timeout)
{ {
m_NextMessageID++; m_NextMessageID++;
@ -355,7 +339,8 @@ void EndpointManager::RequestTimerHandler(void)
map<String, PendingRequest>::iterator it; map<String, PendingRequest>::iterator it;
for (it = m_Requests.begin(); it != m_Requests.end(); it++) { for (it = m_Requests.begin(); it != m_Requests.end(); it++) {
if (it->second.HasTimedOut()) { if (it->second.HasTimedOut()) {
it->second.Callback(GetSelf(), Endpoint::Ptr(), it->second.Request, ResponseMessage(), true); it->second.Callback(GetSelf(), Endpoint::Ptr(),
it->second.Request, ResponseMessage(), true);
m_Requests.erase(it); m_Requests.erase(it);
@ -364,7 +349,8 @@ void EndpointManager::RequestTimerHandler(void)
} }
} }
void EndpointManager::ProcessResponseMessage(const Endpoint::Ptr& sender, const ResponseMessage& message) void EndpointManager::ProcessResponseMessage(const Endpoint::Ptr& sender,
const ResponseMessage& message)
{ {
String id; String id;
if (!message.GetID(&id)) if (!message.GetID(&id))
@ -381,16 +367,6 @@ void EndpointManager::ProcessResponseMessage(const Endpoint::Ptr& sender, const
m_Requests.erase(it); m_Requests.erase(it);
} }
//EndpointManager::Iterator EndpointManager::Begin(void)
//{
// return m_Endpoints.begin();
//}
//EndpointManager::Iterator EndpointManager::End(void)
//{
// return m_Endpoints.end();
//}
EndpointManager::Ptr EndpointManager::GetInstance(void) EndpointManager::Ptr EndpointManager::GetInstance(void)
{ {
static EndpointManager::Ptr instance; static EndpointManager::Ptr instance;

View File

@ -51,8 +51,10 @@ public:
void SendAnycastMessage(const Endpoint::Ptr& sender, const RequestMessage& message); void SendAnycastMessage(const Endpoint::Ptr& sender, const RequestMessage& message);
void SendMulticastMessage(const Endpoint::Ptr& sender, const RequestMessage& message); void SendMulticastMessage(const Endpoint::Ptr& sender, const RequestMessage& message);
typedef function<void(const EndpointManager::Ptr&, const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> APICallback;
void SendAPIMessage(const Endpoint::Ptr& sender, const Endpoint::Ptr& recipient, RequestMessage& message, void SendAPIMessage(const Endpoint::Ptr& sender, const Endpoint::Ptr& recipient, RequestMessage& message,
function<void(const EndpointManager::Ptr&, const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> callback, double timeout = 30); const APICallback& callback, double timeout = 30);
void ProcessResponseMessage(const Endpoint::Ptr& sender, const ResponseMessage& message); void ProcessResponseMessage(const Endpoint::Ptr& sender, const ResponseMessage& message);

View File

@ -30,7 +30,8 @@ using namespace icinga;
JsonRpcClient::JsonRpcClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext) JsonRpcClient::JsonRpcClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
: TlsClient(role, sslContext) : TlsClient(role, sslContext)
{ {
OnDataAvailable.connect(boost::bind(&JsonRpcClient::DataAvailableHandler, this)); OnDataAvailable.connect(boost::bind(&JsonRpcClient::DataAvailableHandler,
this));
} }
/** /**
@ -59,12 +60,16 @@ void JsonRpcClient::DataAvailableHandler(void)
try { try {
Value value = Value::Deserialize(jsonString); Value value = Value::Deserialize(jsonString);
if (!value.IsObjectType<Dictionary>()) if (!value.IsObjectType<Dictionary>()) {
throw_exception(invalid_argument("JSON-RPC message must be a dictionary.")); throw_exception(invalid_argument("JSON-RPC"
" message must be a dictionary."));
}
OnNewMessage(GetSelf(), MessagePart(value)); OnNewMessage(GetSelf(), MessagePart(value));
} catch (const exception& ex) { } catch (const exception& ex) {
Logger::Write(LogCritical, "jsonrpc", "Exception while processing message from JSON-RPC client: " + String(ex.what())); Logger::Write(LogCritical, "remoting", "Exception"
" while processing message from JSON-RPC client: " +
String(ex.what()));
} }
} }
} }
@ -77,9 +82,11 @@ void JsonRpcClient::DataAvailableHandler(void)
* @param sslContext SSL context for the TLS connection. * @param sslContext SSL context for the TLS connection.
* @returns A new JSON-RPC client. * @returns A new JSON-RPC client.
*/ */
JsonRpcClient::Ptr icinga::JsonRpcClientFactory(SOCKET fd, TcpClientRole role, shared_ptr<SSL_CTX> sslContext) JsonRpcClient::Ptr icinga::JsonRpcClientFactory(SOCKET fd, TcpClientRole role,
shared_ptr<SSL_CTX> sslContext)
{ {
JsonRpcClient::Ptr client = boost::make_shared<JsonRpcClient>(role, sslContext); JsonRpcClient::Ptr client = boost::make_shared<JsonRpcClient>(role,
sslContext);
client->SetFD(fd); client->SetFD(fd);
return client; return client;
} }