From 3158767ac668f05372c6a26156a46ea058d437a9 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 31 Mar 2012 16:01:31 +0200 Subject: [PATCH] Cleaned up JSON-RPC API. --- jsonrpc/connectionmanager.cpp | 22 ++++++++++++++++------ jsonrpc/connectionmanager.h | 12 +++++++----- jsonrpc/i2-jsonrpc.h | 2 +- jsonrpc/jsonrpc.vcxproj | 20 +++++++++++--------- jsonrpc/jsonrpcmessage.cpp | 26 ++++++++++++++++++++++++++ jsonrpc/jsonrpcmessage.h | 4 ++++ 6 files changed, 65 insertions(+), 21 deletions(-) diff --git a/jsonrpc/connectionmanager.cpp b/jsonrpc/connectionmanager.cpp index 3d23299c9..5d1e336fc 100644 --- a/jsonrpc/connectionmanager.cpp +++ b/jsonrpc/connectionmanager.cpp @@ -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(ncea->Client); - BindClient(client); + RegisterClient(client); return 0; } int ConnectionManager::CloseClientHandler(EventArgs::RefType ea) { - UnbindClient(static_pointer_cast(ea->Source)); + UnregisterClient(static_pointer_cast(ea->Source)); return 0; } @@ -81,3 +81,13 @@ void ConnectionManager::UnregisterMethod(string method, function::iterator i = m_Clients.begin(); i != m_Clients.end(); i++) + { + JsonRpcClient::RefType client = *i; + client->SendMessage(message); + } +} diff --git a/jsonrpc/connectionmanager.h b/jsonrpc/connectionmanager.h index 13e8ca03f..f92b802b8 100644 --- a/jsonrpc/connectionmanager.h +++ b/jsonrpc/connectionmanager.h @@ -20,16 +20,18 @@ public: typedef shared_ptr RefType; typedef weak_ptr 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 function); void UnregisterMethod(string method, function function); + + void SendMessage(JsonRpcMessage::RefType message); }; } -#endif /* I2_CONNECTIONMANAGER_H */ \ No newline at end of file +#endif /* I2_CONNECTIONMANAGER_H */ diff --git a/jsonrpc/i2-jsonrpc.h b/jsonrpc/i2-jsonrpc.h index 127cffd8d..a25d58ce0 100644 --- a/jsonrpc/i2-jsonrpc.h +++ b/jsonrpc/i2-jsonrpc.h @@ -11,4 +11,4 @@ #include "jsonrpcserver.h" #include "connectionmanager.h" -#endif /* I2_JSONRPC_H */ \ No newline at end of file +#endif /* I2_JSONRPC_H */ diff --git a/jsonrpc/jsonrpc.vcxproj b/jsonrpc/jsonrpc.vcxproj index 9b1f7f626..ccb112e08 100644 --- a/jsonrpc/jsonrpc.vcxproj +++ b/jsonrpc/jsonrpc.vcxproj @@ -30,19 +30,19 @@ {8DD52FAC-ECEE-48C2-B266-E7C47ED485F8} Win32Proj - i2jsonrpc + icinga StaticLibrary true - Unicode + MultiByte StaticLibrary false true - Unicode + MultiByte @@ -55,12 +55,12 @@ - $(MSBuildProjectDirectory)\..\base;$(IncludePath) - ..\Debug;$(LibraryPath) + $(ProjectDir)\..\base;$(IncludePath) + $(OutDir);$(LibraryPath) - $(MSBuildProjectDirectory)\..\base;$(IncludePath) - ..\Release;$(LibraryPath) + $(ProjectDir)\..\base;$(IncludePath) + $(OutDir);$(LibraryPath) @@ -75,7 +75,8 @@ true - ..\Debug\base.lib + + @@ -95,7 +96,8 @@ true - ..\Release\base.lib + + diff --git a/jsonrpc/jsonrpcmessage.cpp b/jsonrpc/jsonrpcmessage.cpp index 81a7d8945..a5fbeb55e 100644 --- a/jsonrpc/jsonrpcmessage.cpp +++ b/jsonrpc/jsonrpcmessage.cpp @@ -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); diff --git a/jsonrpc/jsonrpcmessage.h b/jsonrpc/jsonrpcmessage.h index ca5f6d7c0..1f63d553c 100644 --- a/jsonrpc/jsonrpcmessage.h +++ b/jsonrpc/jsonrpcmessage.h @@ -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);