Cleaned up JSON-RPC API.

This commit is contained in:
Gunnar Beutner 2012-03-31 16:01:31 +02:00
parent 4388f20d64
commit 3158767ac6
6 changed files with 65 additions and 21 deletions

View File

@ -2,25 +2,25 @@
using namespace icinga;
void ConnectionManager::BindServer(JsonRpcServer::RefType server)
void ConnectionManager::RegisterServer(JsonRpcServer::RefType server)
{
m_Servers.push_front(server);
server->OnNewClient.bind(bind_weak(&ConnectionManager::NewClientHandler, shared_from_this()));
}
void ConnectionManager::UnbindServer(JsonRpcServer::RefType server)
void ConnectionManager::UnregisterServer(JsonRpcServer::RefType server)
{
m_Servers.remove(server);
// TODO: unbind event
}
void ConnectionManager::BindClient(JsonRpcClient::RefType client)
void ConnectionManager::RegisterClient(JsonRpcClient::RefType client)
{
m_Clients.push_front(client);
client->OnNewMessage.bind(bind_weak(&ConnectionManager::NewMessageHandler, shared_from_this()));
}
void ConnectionManager::UnbindClient(JsonRpcClient::RefType client)
void ConnectionManager::UnregisterClient(JsonRpcClient::RefType client)
{
m_Clients.remove(client);
// TODO: unbind event
@ -29,14 +29,14 @@ void ConnectionManager::UnbindClient(JsonRpcClient::RefType client)
int ConnectionManager::NewClientHandler(NewClientEventArgs::RefType ncea)
{
JsonRpcClient::RefType client = static_pointer_cast<JsonRpcClient>(ncea->Client);
BindClient(client);
RegisterClient(client);
return 0;
}
int ConnectionManager::CloseClientHandler(EventArgs::RefType ea)
{
UnbindClient(static_pointer_cast<JsonRpcClient>(ea->Source));
UnregisterClient(static_pointer_cast<JsonRpcClient>(ea->Source));
return 0;
}
@ -81,3 +81,13 @@ void ConnectionManager::UnregisterMethod(string method, function<int (NewMessage
{
// TODO: implement
}
void ConnectionManager::SendMessage(JsonRpcMessage::RefType message)
{
/* TODO: filter messages based on event subscriptions */
for (list<JsonRpcClient::RefType>::iterator i = m_Clients.begin(); i != m_Clients.end(); i++)
{
JsonRpcClient::RefType client = *i;
client->SendMessage(message);
}
}

View File

@ -20,16 +20,18 @@ public:
typedef shared_ptr<ConnectionManager> RefType;
typedef weak_ptr<ConnectionManager> WeakRefType;
void BindServer(JsonRpcServer::RefType server);
void UnbindServer(JsonRpcServer::RefType server);
void RegisterServer(JsonRpcServer::RefType server);
void UnregisterServer(JsonRpcServer::RefType server);
void BindClient(JsonRpcClient::RefType client);
void UnbindClient(JsonRpcClient::RefType client);
void RegisterClient(JsonRpcClient::RefType client);
void UnregisterClient(JsonRpcClient::RefType client);
void RegisterMethod(string method, function<int (NewMessageEventArgs::RefType)> function);
void UnregisterMethod(string method, function<int (NewMessageEventArgs::RefType)> function);
void SendMessage(JsonRpcMessage::RefType message);
};
}
#endif /* I2_CONNECTIONMANAGER_H */
#endif /* I2_CONNECTIONMANAGER_H */

View File

@ -11,4 +11,4 @@
#include "jsonrpcserver.h"
#include "connectionmanager.h"
#endif /* I2_JSONRPC_H */
#endif /* I2_JSONRPC_H */

View File

@ -30,19 +30,19 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{8DD52FAC-ECEE-48C2-B266-E7C47ED485F8}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>i2jsonrpc</RootNamespace>
<RootNamespace>icinga</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
@ -55,12 +55,12 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>$(MSBuildProjectDirectory)\..\base;$(IncludePath)</IncludePath>
<LibraryPath>..\Debug;$(LibraryPath)</LibraryPath>
<IncludePath>$(ProjectDir)\..\base;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IncludePath>$(MSBuildProjectDirectory)\..\base;$(IncludePath)</IncludePath>
<LibraryPath>..\Release;$(LibraryPath)</LibraryPath>
<IncludePath>$(ProjectDir)\..\base;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@ -75,7 +75,8 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<Lib>
<AdditionalDependencies>..\Debug\base.lib</AdditionalDependencies>
<AdditionalDependencies>
</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -95,7 +96,8 @@
<OptimizeReferences>true</OptimizeReferences>
</Link>
<Lib>
<AdditionalDependencies>..\Release\base.lib</AdditionalDependencies>
<AdditionalDependencies>
</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -113,6 +113,32 @@ cJSON *JsonRpcMessage::GetParams(void)
return object;
}
void JsonRpcMessage::SetParam(const string& name, const string& value)
{
}
cJSON *JsonRpcMessage::GetParam(const string& name)
{
cJSON *params = GetFieldObject("params");
if (params == NULL)
return NULL;
return cJSON_GetObjectItem(params, name.c_str());
}
bool JsonRpcMessage::GetParamString(const string name, string *value)
{
cJSON *param = GetParam(name);
if (param == NULL || param->type != cJSON_String)
return false;
*value = param->valuestring;
return true;
}
void JsonRpcMessage::ClearResult(void)
{
SetFieldObject("result", NULL);

View File

@ -40,6 +40,10 @@ public:
void ClearParams(void);
cJSON *GetParams(void);
void SetParam(const string& name, const string& value);
cJSON *GetParam(const string& name);
bool GetParamString(const string name, string *value);
void ClearResult();
cJSON *GetResult(void);