Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dyyer authored Nov 20, 2016
1 parent 42ffd74 commit f4b03f5
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 186 deletions.
332 changes: 173 additions & 159 deletions ServerCPP/src/jsonkvPackage.cpp
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;
}
56 changes: 29 additions & 27 deletions ServerCPP/src/jsonkvPackage.h
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

0 comments on commit f4b03f5

Please sign in to comment.