-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
202 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,173 @@ | ||
#include "jsonkvPackage.h" | ||
#include "KVServer.h" | ||
#include <string.h> | ||
#include <algorithm> | ||
#include "ErrorCode.h" | ||
|
||
using namespace nlohmann; | ||
using namespace ADCS; | ||
|
||
jsonkvPacket::jsonkvPacket(const char* pData, unsigned int nDataLen, int clientSocket) : IPacket(pData, nDataLen, clientSocket) | ||
{ | ||
m_type = PackageType::KV; | ||
|
||
try | ||
{ | ||
m_json_request = json::parse(m_data.data); | ||
} | ||
catch(std::invalid_argument arg) | ||
{ | ||
printf("parser json error: %s", arg.std::exception::what()); | ||
// paser json error | ||
} | ||
catch(...) | ||
{ } | ||
} | ||
|
||
jsonkvPacket::~jsonkvPacket() | ||
{ | ||
|
||
} | ||
|
||
void jsonkvPacket::Process() | ||
{ | ||
if( m_json_request.is_array()) | ||
{ | ||
for(size_t i = 0; i < m_json_request.size(); i++) | ||
{ | ||
m_json_result.push_back(__process_one_operation__(m_json_request[i])); | ||
} | ||
} | ||
else if(m_json_request.is_object()) | ||
{ | ||
m_json_result = __process_one_operation__(m_json_request); | ||
} | ||
} | ||
|
||
json jsonkvPacket::__process_one_operation__(json jrequest) | ||
{ | ||
json jresult; | ||
jresult["jsonkv"] = "1.0"; | ||
jresult["result"]["value"] = "0"; | ||
jresult["result"]["code"] = "0"; | ||
jresult["result"]["message"] = "success"; | ||
jresult["id"] = jrequest["id"]; | ||
|
||
// check the version of package | ||
if( jrequest["jsonkv"] != "1.0" ) | ||
{ | ||
jresult["result"]["code"] = "800"; | ||
jresult["result"]["message"] = "Unsupported protocol"; | ||
} | ||
|
||
CKVServer* kvserver = CKVServer::GetInstance(); | ||
if( !kvserver) | ||
{ | ||
jresult["result"]["code"] = "1024"; | ||
jresult["result"]["message"] = "Internal Server Error"; | ||
return jresult; | ||
} | ||
|
||
std::string kvOperator = jrequest["operate"]; | ||
std::transform(kvOperator.begin(),kvOperator.end(),kvOperator.begin(), ::tolower); | ||
if( kvOperator == "put") | ||
{ | ||
ErrorCode::KVStore code = kvserver->SetValue(m_clientsocket, jrequest["key"], jrequest["value"]); | ||
if( code == ErrorCode::KVStore::Success ) | ||
{ | ||
jresult["result"]["message"] = "put success"; | ||
} | ||
else | ||
{ | ||
jresult["result"]["code"] = std::to_string((int)code); | ||
jresult["result"]["message"] = ErrorCode::Explain((unsigned int)code); | ||
} | ||
} | ||
else if(kvOperator == "get") | ||
{ | ||
std::string strValue = ""; | ||
ErrorCode::KVStore code = kvserver->GetValue(jrequest["key"], strValue); | ||
if( code == ErrorCode::KVStore::Success ) | ||
{ | ||
jresult["result"]["value"] = strValue; | ||
jresult["result"]["message"] = "get success"; | ||
} | ||
else | ||
{ | ||
jresult["result"]["code"] = std::to_string((int)code); | ||
jresult["result"]["message"] = ErrorCode::Explain((unsigned int)code); | ||
} | ||
} | ||
else if(kvOperator == "delete") | ||
{ | ||
ErrorCode::KVStore code = kvserver->Delete(m_clientsocket, jrequest["key"]); | ||
if( code == ErrorCode::KVStore::Success ) | ||
{ | ||
jresult["result"]["message"] = "Delete Success"; | ||
} | ||
else | ||
{ | ||
jresult["result"]["code"] = std::to_string((int)code); | ||
jresult["result"]["message"] = ErrorCode::Explain((unsigned int)code); | ||
} | ||
} | ||
else | ||
{ | ||
jresult["result"]["code"] = "900"; | ||
jresult["result"]["message"] = "Invalid Operator!"; | ||
} | ||
|
||
return jresult; | ||
} | ||
|
||
bool jsonkvPacket::IsValid() const | ||
{ | ||
if(!IPacket::IsValid()) return false; | ||
|
||
if(m_json_request.is_array()) | ||
{ | ||
for(size_t i = 0; i < m_json_request.size(); i++) | ||
{ | ||
auto j = m_json_request[i]; | ||
if(j["jsonkv"] == "1.0") | ||
return true; | ||
} | ||
} | ||
else if( m_json_request.is_object()) | ||
{ | ||
if(m_json_request["jsonkv"] == "1.0") | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool jsonkvPacket::GetResult(char*& pStreamData, unsigned long& ulDataLen) | ||
{ | ||
std::string strResult = m_json_result.dump(); | ||
|
||
ulDataLen = strResult.length(); | ||
pStreamData = new char[ulDataLen]; | ||
if(pStreamData == nullptr) | ||
{ | ||
ulDataLen = 0; | ||
return false; | ||
} | ||
memcpy(pStreamData, strResult.c_str(), ulDataLen); | ||
|
||
return true; | ||
} | ||
#include "jsonkvPackage.h" | ||
#include "KVServer.h" | ||
#include <string.h> | ||
#include <algorithm> | ||
#include "ErrorCode.h" | ||
#include<iostream> | ||
using namespace std; | ||
using namespace nlohmann; | ||
using namespace ADCS; | ||
|
||
jsonkvPacket::jsonkvPacket(const char* pData, unsigned int nDataLen, int clientSocket) : IPacket(pData, nDataLen, clientSocket) | ||
{ | ||
m_type = PackageType::KV; | ||
|
||
try | ||
{ | ||
m_json_request = json::parse(m_data.data); | ||
} | ||
catch(std::invalid_argument arg) | ||
{ | ||
printf("parser json error: %s", arg.std::exception::what()); | ||
// paser json error | ||
} | ||
catch(...) | ||
{ } | ||
} | ||
jsonkvPacket(std::string pData) | ||
{ | ||
try | ||
{ | ||
m_json_request = json::parse(pData); | ||
} | ||
catch(std::invalid_argument arg) | ||
{ | ||
printf("parser json error: %s", arg.std::exception::what()); | ||
// paser json error | ||
} | ||
catch(...) | ||
{ } | ||
} | ||
jsonkvPacket::~jsonkvPacket() | ||
{ | ||
|
||
} | ||
|
||
void jsonkvPacket::Process() | ||
{ | ||
if( m_json_request.is_array()) | ||
{ | ||
for(size_t i = 0; i < m_json_request.size(); i++) | ||
{ | ||
m_json_result.push_back(__process_one_operation__(m_json_request[i])); | ||
} | ||
} | ||
else if(m_json_request.is_object()) | ||
{ | ||
m_json_result = __process_one_operation__(m_json_request); | ||
} | ||
} | ||
|
||
json jsonkvPacket::__process_one_operation__(json jrequest) | ||
{ | ||
json jresult; | ||
jresult["jsonkv"] = "1.0"; | ||
jresult["result"]["value"] = "0"; | ||
jresult["result"]["code"] = "0"; | ||
jresult["result"]["message"] = "success"; | ||
jresult["id"] = jrequest["id"]; | ||
|
||
// check the version of package | ||
if( jrequest["jsonkv"] != "1.0" ) | ||
{ | ||
jresult["result"]["code"] = "800"; | ||
jresult["result"]["message"] = "Unsupported protocol"; | ||
} | ||
|
||
CKVServer* kvserver = CKVServer::GetInstance(); | ||
if( !kvserver) | ||
{ | ||
jresult["result"]["code"] = "1024"; | ||
jresult["result"]["message"] = "Internal Server Error"; | ||
return jresult; | ||
} | ||
|
||
std::string kvOperator = jrequest["operate"]; | ||
std::transform(kvOperator.begin(),kvOperator.end(),kvOperator.begin(), ::tolower); | ||
if( kvOperator == "put") | ||
{ | ||
ErrorCode::KVStore code = kvserver->SetValue(m_clientsocket, jrequest["key"], jrequest["value"]); | ||
if( code == ErrorCode::KVStore::Success ) | ||
{ | ||
jresult["result"]["message"] = "put success"; | ||
} | ||
else | ||
{ | ||
jresult["result"]["code"] = std::to_string((int)code); | ||
jresult["result"]["message"] = ErrorCode::Explain((unsigned int)code); | ||
} | ||
} | ||
else if(kvOperator == "get") | ||
{ | ||
std::string strValue = ""; | ||
ErrorCode::KVStore code = kvserver->GetValue(jrequest["key"], strValue); | ||
if( code == ErrorCode::KVStore::Success ) | ||
{ | ||
jresult["result"]["value"] = strValue; | ||
jresult["result"]["message"] = "get success"; | ||
} | ||
else | ||
{ | ||
jresult["result"]["code"] = std::to_string((int)code); | ||
jresult["result"]["message"] = ErrorCode::Explain((unsigned int)code); | ||
} | ||
} | ||
else if(kvOperator == "delete") | ||
{ | ||
ErrorCode::KVStore code = kvserver->Delete(m_clientsocket, jrequest["key"]); | ||
if( code == ErrorCode::KVStore::Success ) | ||
{ | ||
jresult["result"]["message"] = "Delete Success"; | ||
} | ||
else | ||
{ | ||
jresult["result"]["code"] = std::to_string((int)code); | ||
jresult["result"]["message"] = ErrorCode::Explain((unsigned int)code); | ||
} | ||
} | ||
else | ||
{ | ||
jresult["result"]["code"] = "900"; | ||
jresult["result"]["message"] = "Invalid Operator!"; | ||
} | ||
|
||
return jresult; | ||
} | ||
|
||
bool jsonkvPacket::IsValid() const | ||
{ | ||
if(!IPacket::IsValid()) return false; | ||
|
||
if(m_json_request.is_array()) | ||
{ | ||
for(size_t i = 0; i < m_json_request.size(); i++) | ||
{ | ||
auto j = m_json_request[i]; | ||
if(j["jsonkv"] == "1.0") | ||
return true; | ||
} | ||
} | ||
else if( m_json_request.is_object()) | ||
{ | ||
if(m_json_request["jsonkv"] == "1.0") | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool jsonkvPacket::GetResult(char*& pStreamData, unsigned long& ulDataLen) | ||
{ | ||
std::string strResult = m_json_result.dump(); | ||
|
||
ulDataLen = strResult.length(); | ||
pStreamData = new char[ulDataLen]; | ||
if(pStreamData == nullptr) | ||
{ | ||
ulDataLen = 0; | ||
return false; | ||
} | ||
memcpy(pStreamData, strResult.c_str(), ulDataLen); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
|
||
#ifndef _ADCS__jsonkvPackage_H_ | ||
#define _ADCS__jsonkvPackage_H_ | ||
|
||
#include "PackageInterface.h" | ||
#include "json.hpp" | ||
|
||
class jsonkvPacket : public IPacket | ||
{ | ||
public: | ||
jsonkvPacket(const char* pData, unsigned int nDataLen, int clientSocket); | ||
|
||
virtual ~jsonkvPacket(); | ||
virtual void Process(); | ||
virtual bool IsValid() const; | ||
|
||
protected: | ||
virtual bool GetResult(char*& pStreamData, unsigned long& ulDataLen); | ||
|
||
private: | ||
nlohmann::json m_json_request; | ||
nlohmann::json m_json_result; | ||
|
||
nlohmann::json __process_one_operation__(nlohmann::json jrequest); | ||
}; | ||
|
||
#endif | ||
|
||
#ifndef _ADCS__jsonkvPackage_H_ | ||
#define _ADCS__jsonkvPackage_H_ | ||
|
||
#include "PackageInterface.h" | ||
#include "json.hpp" | ||
#include<iostream> | ||
#include<string> | ||
using namespace std; | ||
class jsonkvPacket : public IPacket | ||
{ | ||
public: | ||
jsonkvPacket(const char* pData, unsigned int nDataLen, int clientSocket); | ||
jsonkvPacket(std::string pData); | ||
virtual ~jsonkvPacket(); | ||
virtual void Process(); | ||
virtual bool IsValid() const; | ||
|
||
protected: | ||
virtual bool GetResult(char*& pStreamData, unsigned long& ulDataLen); | ||
|
||
private: | ||
nlohmann::json m_json_request; | ||
nlohmann::json m_json_result; | ||
|
||
nlohmann::json __process_one_operation__(nlohmann::json jrequest); | ||
}; | ||
|
||
#endif |