2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-jsonrpc.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
void ConnectionManager::RegisterServer(JsonRpcServer::Ptr server)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
m_Servers.push_front(server);
|
|
|
|
server->OnNewClient.bind(bind_weak(&ConnectionManager::NewClientHandler, shared_from_this()));
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
void ConnectionManager::UnregisterServer(JsonRpcServer::Ptr server)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
m_Servers.remove(server);
|
|
|
|
// TODO: unbind event
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
void ConnectionManager::RegisterClient(JsonRpcClient::Ptr client)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
m_Clients.push_front(client);
|
|
|
|
client->OnNewMessage.bind(bind_weak(&ConnectionManager::NewMessageHandler, shared_from_this()));
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
void ConnectionManager::UnregisterClient(JsonRpcClient::Ptr client)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
m_Clients.remove(client);
|
|
|
|
// TODO: unbind event
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
int ConnectionManager::NewClientHandler(NewClientEventArgs::Ptr ncea)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
JsonRpcClient::Ptr client = static_pointer_cast<JsonRpcClient>(ncea->Client);
|
2012-03-31 16:01:31 +02:00
|
|
|
RegisterClient(client);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
int ConnectionManager::CloseClientHandler(EventArgs::Ptr ea)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-31 16:01:31 +02:00
|
|
|
UnregisterClient(static_pointer_cast<JsonRpcClient>(ea->Source));
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
int ConnectionManager::NewMessageHandler(NewMessageEventArgs::Ptr nmea)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
JsonRpcMessage::Ptr request = nmea->Message;
|
|
|
|
JsonRpcClient::Ptr client = static_pointer_cast<JsonRpcClient>(nmea->Source);
|
2012-03-29 20:03:29 +02:00
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
map<string, event<NewMessageEventArgs::Ptr> >::iterator i;
|
2012-03-29 20:03:29 +02:00
|
|
|
i = m_Methods.find(request->GetMethod());
|
|
|
|
|
|
|
|
if (i == m_Methods.end()) {
|
2012-04-02 20:50:35 +02:00
|
|
|
JsonRpcMessage::Ptr response = new_object<JsonRpcMessage>();
|
2012-03-29 20:03:29 +02:00
|
|
|
response->SetVersion("2.0");
|
|
|
|
response->SetError("Unknown method.");
|
|
|
|
response->SetID(request->GetID());
|
|
|
|
Netstring::WriteJSONToFIFO(client->GetSendQueue(), response->GetJSON());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i->second(nmea);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-29 20:03:29 +02:00
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
void ConnectionManager::RegisterMethod(string method, function<int (NewMessageEventArgs::Ptr)> callback)
|
2012-03-29 20:03:29 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
map<string, event<NewMessageEventArgs::Ptr> >::iterator i;
|
2012-03-29 20:03:29 +02:00
|
|
|
i = m_Methods.find(method);
|
|
|
|
|
|
|
|
if (i == m_Methods.end()) {
|
2012-04-02 20:50:35 +02:00
|
|
|
m_Methods[method] = event<NewMessageEventArgs::Ptr>();
|
2012-03-29 20:03:29 +02:00
|
|
|
i = m_Methods.find(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
i->second.bind(callback);
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
void ConnectionManager::UnregisterMethod(string method, function<int (NewMessageEventArgs::Ptr)> function)
|
2012-03-29 20:03:29 +02:00
|
|
|
{
|
|
|
|
// TODO: implement
|
|
|
|
}
|
2012-03-31 16:01:31 +02:00
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
void ConnectionManager::SendMessage(JsonRpcMessage::Ptr message)
|
2012-03-31 16:01:31 +02:00
|
|
|
{
|
|
|
|
/* TODO: filter messages based on event subscriptions */
|
2012-04-02 20:50:35 +02:00
|
|
|
for (list<JsonRpcClient::Ptr>::iterator i = m_Clients.begin(); i != m_Clients.end(); i++)
|
2012-03-31 16:01:31 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
JsonRpcClient::Ptr client = *i;
|
2012-03-31 16:01:31 +02:00
|
|
|
client->SendMessage(message);
|
|
|
|
}
|
|
|
|
}
|