2012-04-06 08:56:52 +02:00
|
|
|
#include "i2-icinga.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
2012-04-16 16:27:41 +02:00
|
|
|
|
2012-04-23 13:45:41 +02:00
|
|
|
string JsonRpcEndpoint::GetAddress(void) const
|
|
|
|
{
|
2012-04-26 16:45:00 +02:00
|
|
|
if (!m_Client)
|
|
|
|
return "<disconnected endpoint>";
|
|
|
|
|
|
|
|
return m_Client->GetPeerAddress();
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
2012-04-16 16:27:41 +02:00
|
|
|
JsonRpcClient::Ptr JsonRpcEndpoint::GetClient(void)
|
|
|
|
{
|
|
|
|
return m_Client;
|
|
|
|
}
|
|
|
|
|
2012-04-23 13:45:41 +02:00
|
|
|
void JsonRpcEndpoint::AddAllowedMethodSinkPrefix(string method)
|
|
|
|
{
|
|
|
|
m_AllowedMethodSinkPrefixes.insert(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonRpcEndpoint::RemoveAllowedMethodSinkPrefix(string method)
|
|
|
|
{
|
|
|
|
m_AllowedMethodSinkPrefixes.erase(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonRpcEndpoint::IsAllowedMethodSink(string method) const
|
|
|
|
{
|
2012-04-23 14:01:31 +02:00
|
|
|
set<string>::iterator i;
|
|
|
|
for (i = m_AllowedMethodSinkPrefixes.begin(); i != m_AllowedMethodSinkPrefixes.end(); i++) {
|
|
|
|
if (method.compare(0, method.length(), method) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonRpcEndpoint::AddAllowedMethodSourcePrefix(string method)
|
|
|
|
{
|
|
|
|
m_AllowedMethodSourcePrefixes.insert(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonRpcEndpoint::RemoveAllowedMethodSourcePrefix(string method)
|
|
|
|
{
|
|
|
|
m_AllowedMethodSourcePrefixes.erase(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonRpcEndpoint::IsAllowedMethodSource(string method) const
|
|
|
|
{
|
2012-04-23 14:01:31 +02:00
|
|
|
set<string>::iterator i;
|
|
|
|
for (i = m_AllowedMethodSourcePrefixes.begin(); i != m_AllowedMethodSourcePrefixes.end(); i++) {
|
|
|
|
if (method.compare(0, method.length(), method) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
void JsonRpcEndpoint::Connect(string host, unsigned short port, shared_ptr<SSL_CTX> sslContext)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-04-26 21:33:23 +02:00
|
|
|
m_PeerHostname = host;
|
|
|
|
m_PeerPort = port;
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
JsonRpcClient::Ptr client = make_shared<JsonRpcClient>(RoleOutbound, sslContext);
|
2012-04-18 15:22:25 +02:00
|
|
|
client->Connect(host, port);
|
|
|
|
client->Start();
|
|
|
|
|
|
|
|
SetClient(client);
|
|
|
|
}
|
|
|
|
|
2012-04-16 16:27:41 +02:00
|
|
|
void JsonRpcEndpoint::SetClient(JsonRpcClient::Ptr client)
|
|
|
|
{
|
|
|
|
m_Client = client;
|
2012-04-18 15:22:25 +02:00
|
|
|
client->OnNewMessage += bind_weak(&JsonRpcEndpoint::NewMessageHandler, shared_from_this());
|
|
|
|
client->OnClosed += bind_weak(&JsonRpcEndpoint::ClientClosedHandler, shared_from_this());
|
|
|
|
client->OnError += bind_weak(&JsonRpcEndpoint::ClientErrorHandler, shared_from_this());
|
2012-04-24 15:56:48 +02:00
|
|
|
client->OnVerifyCertificate += bind_weak(&JsonRpcEndpoint::VerifyCertificateHandler, shared_from_this());
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonRpcEndpoint::IsLocal(void) const
|
|
|
|
{
|
|
|
|
return false;
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonRpcEndpoint::IsConnected(void) const
|
|
|
|
{
|
2012-04-19 12:20:03 +02:00
|
|
|
return (bool)m_Client;
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& message)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
if (IsConnected()) {
|
|
|
|
string id;
|
|
|
|
if (message.GetID(&id))
|
|
|
|
// TODO: remove calls after a certain timeout (and notify callers?)
|
|
|
|
m_PendingCalls[id] = sender;
|
|
|
|
|
2012-04-16 16:27:41 +02:00
|
|
|
m_Client->SendMessage(message);
|
2012-04-19 12:16:52 +02:00
|
|
|
} else {
|
|
|
|
// TODO: persist the event
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
void JsonRpcEndpoint::ProcessResponse(Endpoint::Ptr sender, const JsonRpcResponse& message)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
|
|
|
if (IsConnected())
|
|
|
|
m_Client->SendMessage(message);
|
|
|
|
}
|
2012-04-18 15:22:25 +02:00
|
|
|
|
|
|
|
int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea)
|
|
|
|
{
|
|
|
|
const Message& message = nmea.Message;
|
|
|
|
Endpoint::Ptr sender = static_pointer_cast<Endpoint>(shared_from_this());
|
|
|
|
|
|
|
|
string method;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (message.GetPropertyString("method", &method)) {
|
2012-04-23 13:45:41 +02:00
|
|
|
if (!IsAllowedMethodSource(method))
|
|
|
|
return 0;
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest request = message;
|
|
|
|
|
|
|
|
string id;
|
|
|
|
if (request.GetID(&id))
|
|
|
|
GetEndpointManager()->SendAnycastRequest(sender, request, false);
|
|
|
|
else
|
|
|
|
GetEndpointManager()->SendMulticastRequest(sender, request, false);
|
|
|
|
} else {
|
2012-04-20 10:38:11 +02:00
|
|
|
JsonRpcResponse response = message;
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
// TODO: deal with response messages
|
|
|
|
throw NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int JsonRpcEndpoint::ClientClosedHandler(const EventArgs& ea)
|
|
|
|
{
|
2012-04-26 12:58:20 +02:00
|
|
|
string address = GetAddress();
|
2012-04-26 16:45:00 +02:00
|
|
|
Application::Log("Lost connection to endpoint: " + address);
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
m_PendingCalls.clear();
|
|
|
|
|
2012-04-26 21:33:23 +02:00
|
|
|
if (m_PeerHostname != string()) {
|
2012-04-18 15:22:25 +02:00
|
|
|
Timer::Ptr timer = make_shared<Timer>();
|
|
|
|
timer->SetInterval(30);
|
|
|
|
timer->SetUserArgs(ea);
|
|
|
|
timer->OnTimerExpired += bind_weak(&JsonRpcEndpoint::ClientReconnectHandler, shared_from_this());
|
|
|
|
timer->Start();
|
|
|
|
m_ReconnectTimer = timer;
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-04-26 16:45:00 +02:00
|
|
|
Application::Log("Spawned reconnect timer (30 seconds)");
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-04-19 12:16:52 +02:00
|
|
|
// TODO: _only_ clear non-persistent method sources/sinks
|
|
|
|
// unregister ourselves if no persistent sources/sinks are left (use a timer for that, once we have a TTL property for the methods)
|
|
|
|
ClearMethodSinks();
|
|
|
|
ClearMethodSources();
|
|
|
|
|
2012-04-20 16:44:32 +02:00
|
|
|
if (CountMethodSinks() == 0 && !m_ReconnectTimer)
|
2012-04-19 12:16:52 +02:00
|
|
|
GetEndpointManager()->UnregisterEndpoint(static_pointer_cast<Endpoint>(shared_from_this()));
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
m_Client.reset();
|
|
|
|
|
|
|
|
// TODO: persist events, etc., for now we just disable the endpoint
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int JsonRpcEndpoint::ClientErrorHandler(const SocketErrorEventArgs& ea)
|
|
|
|
{
|
|
|
|
cerr << "Error occured for JSON-RPC socket: Code=" << ea.Code << "; Message=" << ea.Message << endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int JsonRpcEndpoint::ClientReconnectHandler(const TimerEventArgs& ea)
|
|
|
|
{
|
|
|
|
JsonRpcClient::Ptr client = static_pointer_cast<JsonRpcClient>(ea.UserArgs.Source);
|
|
|
|
Timer::Ptr timer = static_pointer_cast<Timer>(ea.Source);
|
|
|
|
|
2012-04-26 21:33:23 +02:00
|
|
|
GetEndpointManager()->AddConnection(m_PeerHostname, m_PeerPort);
|
2012-04-18 15:22:25 +02:00
|
|
|
|
|
|
|
timer->Stop();
|
|
|
|
m_ReconnectTimer.reset();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-24 15:56:48 +02:00
|
|
|
|
|
|
|
int JsonRpcEndpoint::VerifyCertificateHandler(const VerifyCertificateEventArgs& ea)
|
|
|
|
{
|
2012-04-24 16:27:23 +02:00
|
|
|
if (ea.Certificate && ea.ValidCertificate) {
|
|
|
|
string identity = Utility::GetCertificateCN(ea.Certificate);
|
|
|
|
|
|
|
|
if (GetIdentity().empty() && !identity.empty())
|
|
|
|
SetIdentity(identity);
|
|
|
|
}
|
2012-04-24 15:56:48 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|