Skip to content

Commit

Permalink
Merge pull request #292 from Waqar144/size-empty
Browse files Browse the repository at this point in the history
Use empty() to check for emptyness
  • Loading branch information
cinemast committed May 20, 2021
2 parents e63b74f + 2a7b472 commit 78a0e36
Show file tree
Hide file tree
Showing 25 changed files with 119 additions and 138 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ build/*
*.xcworkspace
xcuserdata

#Qt-creator noise
build-*

# svn & cvs
.svn
CVS
Expand Down
3 changes: 2 additions & 1 deletion src/examples/serialportserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ using namespace std;

class SampleServer : public AbstractServer<SampleServer> {
public:
SampleServer(LinuxSerialPortServer &server) : AbstractServer<SampleServer>(server) {
SampleServer(LinuxSerialPortServer &server)
: AbstractServer<SampleServer>(server) {
this->bindAndAddMethod(Procedure("sayHello", PARAMS_BY_NAME, JSON_STRING,
"name", JSON_STRING, NULL),
&SampleServer::sayHello);
Expand Down
2 changes: 1 addition & 1 deletion src/examples/stubclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main() {
cout << "Compare: " << c.isEqual("Peter", "peter") << endl;
cout << "Build object: " << c.buildObject("Peter", 1990) << endl;

c.sayHello(""); //expects a server error
c.sayHello(""); // expects a server error

} catch (JsonRpcException &e) {
cerr << e.what() << endl;
Expand Down
60 changes: 26 additions & 34 deletions src/examples/stubserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MyStubServer : public AbstractStubServer {
virtual std::string sayHello(const std::string &name);
virtual int addNumbers(int param1, int param2);
virtual double addNumbers2(double param1, double param2);
virtual Json::Value calculate(const Json::Value& args);
virtual Json::Value calculate(const Json::Value &args);
virtual bool isEqual(const std::string &str1, const std::string &str2);
virtual Json::Value buildObject(const std::string &name, int age);
virtual std::string methodWithoutParameters();
Expand All @@ -43,7 +43,6 @@ string MyStubServer::sayHello(const string &name) {

int MyStubServer::addNumbers(int param1, int param2) { return param1 + param2; }


double MyStubServer::addNumbers2(double param1, double param2) {
return param1 + param2;
}
Expand All @@ -52,43 +51,36 @@ bool MyStubServer::isEqual(const string &str1, const string &str2) {
return str1 == str2;
}

Json::Value MyStubServer::calculate(const Json::Value& args) {
Json::Value MyStubServer::calculate(const Json::Value &args) {
Json::Value result;
if((args.isMember("arg1") && args["arg1"].isInt()) &&
(args.isMember("arg2") && args["arg2"].isInt()) &&
(args.isMember("operator") && args["operator"].isString()))
{
if ((args.isMember("arg1") && args["arg1"].isInt()) &&
(args.isMember("arg2") && args["arg2"].isInt()) &&
(args.isMember("operator") && args["operator"].isString())) {
int calculated = 0;

switch(args["operator"].asString()[0])
{
case '+':
{
calculated = args["arg1"].asInt() + args["arg2"].asInt();
break;
}
case '-':
{
calculated = args["arg1"].asInt() - args["arg2"].asInt();
break;
}
case '*':
{
calculated = args["arg1"].asInt() * args["arg2"].asInt();
break;
}
case '/':
{
if(args["arg2"].asInt() != 0)
{
calculated = args["arg1"].asInt() / args["arg2"].asInt();
}
break;
switch (args["operator"].asString()[0]) {
case '+': {
calculated = args["arg1"].asInt() + args["arg2"].asInt();
break;
}
case '-': {
calculated = args["arg1"].asInt() - args["arg2"].asInt();
break;
}
case '*': {
calculated = args["arg1"].asInt() * args["arg2"].asInt();
break;
}
case '/': {
if (args["arg2"].asInt() != 0) {
calculated = args["arg1"].asInt() / args["arg2"].asInt();
}
default:
break;
break;
}

default:
break;
}

result.append(calculated);
}

Expand Down
4 changes: 2 additions & 2 deletions src/jsonrpccpp/client/batchcall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int BatchCall::addCall(const string &methodname, const Json::Value &params,
call[RpcProtocolClient::KEY_PROTOCOL_VERSION] = "2.0";
call[RpcProtocolClient::KEY_PROCEDURE_NAME] = methodname;

if(params.isNull() || params.size() > 0)
if (params.isNull() || !params.empty())
call[RpcProtocolClient::KEY_PARAMETER] = params;

if (!isNotification) {
Expand All @@ -39,7 +39,7 @@ string BatchCall::toString(bool fast) const {
if (fast) {
Json::StreamWriterBuilder wbuilder;
wbuilder["indentation"] = "";
result = Json::writeString(wbuilder,this->result);
result = Json::writeString(wbuilder, this->result);
} else {
Json::StreamWriterBuilder wbuilder;
result = Json::writeString(wbuilder, this->result);
Expand Down
13 changes: 8 additions & 5 deletions src/jsonrpccpp/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

using namespace jsonrpc;

Client::Client(IClientConnector &connector, clientVersion_t version, bool omitEndingLineFeed)
Client::Client(IClientConnector &connector, clientVersion_t version,
bool omitEndingLineFeed)
: connector(connector) {
this->protocol = new RpcProtocolClient(version, omitEndingLineFeed);
}
Expand All @@ -36,12 +37,14 @@ void Client::CallProcedures(const BatchCall &calls, BatchResponse &result) {

try {
if (!reader.parse(response, tmpresult) || !tmpresult.isArray()) {
throw JsonRpcException(Errors::ERROR_CLIENT_INVALID_RESPONSE, "Array expected.");
throw JsonRpcException(Errors::ERROR_CLIENT_INVALID_RESPONSE,
"Array expected.");
}
} catch (const Json::Exception& e) {
throw JsonRpcException(Errors::ERROR_RPC_JSON_PARSE_ERROR, Errors::GetErrorMessage(Errors::ERROR_RPC_JSON_PARSE_ERROR), response);
} catch (const Json::Exception &e) {
throw JsonRpcException(
Errors::ERROR_RPC_JSON_PARSE_ERROR,
Errors::GetErrorMessage(Errors::ERROR_RPC_JSON_PARSE_ERROR), response);
}


for (unsigned int i = 0; i < tmpresult.size(); i++) {
if (tmpresult[i].isObject()) {
Expand Down
10 changes: 4 additions & 6 deletions src/jsonrpccpp/client/connectors/linuxserialportclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <iostream>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <iostream>
#include <termios.h>
#include <unistd.h>

using namespace jsonrpc;
using namespace std;

LinuxSerialPortClient::LinuxSerialPortClient(const std::string& deviceName)
LinuxSerialPortClient::LinuxSerialPortClient(const std::string &deviceName)
: deviceName(deviceName) {}

LinuxSerialPortClient::~LinuxSerialPortClient() {}

void LinuxSerialPortClient::SendRPCMessage(const std::string &message,
std::string &result) {
std::string &result) {
int serial_fd = this->Connect();

StreamWriter writer;
Expand All @@ -53,7 +52,6 @@ int LinuxSerialPortClient::Connect() {
int serial_fd = open(deviceName.c_str(), O_RDWR);

return serial_fd;

}

int LinuxSerialPortClient::Connect(const string &deviceName) {
Expand Down
4 changes: 1 addition & 3 deletions src/jsonrpccpp/client/connectors/tcpsocketclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ TcpSocketClient::TcpSocketClient(const std::string &ipToConnect,
#endif
}

TcpSocketClient::~TcpSocketClient() {
delete this->realSocket;
}
TcpSocketClient::~TcpSocketClient() { delete this->realSocket; }

void TcpSocketClient::SendRPCMessage(const std::string &message,
std::string &result) {
Expand Down
4 changes: 2 additions & 2 deletions src/jsonrpccpp/client/connectors/windowstcpsocketclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ SOCKET WindowsTcpSocketClient::Connect() throw(JsonRpcException) {
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
char port[6];
itoa(this->port, port, 10);
char port[6];
itoa(this->port, port, 10);
DWORD retval =
getaddrinfo(this->hostToConnect.c_str(), port, &hints, &result);
if (retval != 0)
Expand Down
8 changes: 5 additions & 3 deletions src/jsonrpccpp/client/rpcprotocolclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const std::string RpcProtocolClient::KEY_ERROR_CODE = "code";
const std::string RpcProtocolClient::KEY_ERROR_MESSAGE = "message";
const std::string RpcProtocolClient::KEY_ERROR_DATA = "data";

RpcProtocolClient::RpcProtocolClient(clientVersion_t version, bool omitEndingLineFeed)
RpcProtocolClient::RpcProtocolClient(clientVersion_t version,
bool omitEndingLineFeed)
: version(version), omitEndingLineFeed(omitEndingLineFeed) {}

void RpcProtocolClient::BuildRequest(const std::string &method,
Expand All @@ -46,9 +47,10 @@ void RpcProtocolClient::HandleResponse(const std::string &response,
if (reader.parse(response, value)) {
this->HandleResponse(value, result);
} else {
throw JsonRpcException(Errors::ERROR_RPC_JSON_PARSE_ERROR, " " + response);
throw JsonRpcException(Errors::ERROR_RPC_JSON_PARSE_ERROR,
" " + response);
}
} catch (Json::Exception& e) {
} catch (Json::Exception &e) {
throw JsonRpcException(Errors::ERROR_RPC_JSON_PARSE_ERROR, " " + response);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/jsonrpccpp/common/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ JsonRpcException::JsonRpcException(int code)

JsonRpcException::JsonRpcException(int code, const std::string &message)
: code(code), message(Errors::GetErrorMessage(code)) {
if (this->message != "")
if (!this->message.empty())
this->message = this->message + ": ";
this->message = this->message + message;
this->setWhatMessage();
Expand All @@ -27,7 +27,7 @@ JsonRpcException::JsonRpcException(int code, const std::string &message)
JsonRpcException::JsonRpcException(int code, const std::string &message,
const Json::Value &data)
: code(code), message(Errors::GetErrorMessage(code)), data(data) {
if (this->message != "")
if (!this->message.empty())
this->message = this->message + ": ";
this->message = this->message + message;
this->setWhatMessage();
Expand Down
2 changes: 1 addition & 1 deletion src/jsonrpccpp/common/specificationparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ SpecificationParser::GetProceduresFromString(const string &content) {
}
void SpecificationParser::GetProcedure(Json::Value &signature,
Procedure &result) {
if (signature.isObject() && GetProcedureName(signature) != "") {
if (signature.isObject() && !GetProcedureName(signature).empty()) {
result.SetProcedureName(GetProcedureName(signature));
if (signature.isMember(KEY_SPEC_RETURN_TYPE)) {
result.SetProcedureType(RPC_METHOD);
Expand Down
3 changes: 2 additions & 1 deletion src/jsonrpccpp/common/streamreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ bool StreamReader::Read(std::string &target, int fd, char delimiter) {
} else {
target.append(buffer, static_cast<size_t>(bytesRead));
}
} while (memchr(buffer, delimiter, bytesRead) == NULL);//(target.find(delimiter) == string::npos && bytesRead > 0);
} while (memchr(buffer, delimiter, bytesRead) ==
NULL); //(target.find(delimiter) == string::npos && bytesRead > 0);

target.pop_back();
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/jsonrpccpp/server/abstractprotocolhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void AbstractProtocolHandler::HandleRequest(const std::string &request,
}

if (resp != Json::nullValue)
retValue = Json::writeString(wbuilder,resp);
retValue = Json::writeString(wbuilder, resp);
}

void AbstractProtocolHandler::ProcessRequest(const Json::Value &request,
Expand Down
17 changes: 9 additions & 8 deletions src/jsonrpccpp/server/connectors/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct mhd_coninfo {
HttpServer::HttpServer(int port, const std::string &sslcert,
const std::string &sslkey, int threads)
: AbstractServerConnector(), port(port), threads(threads), running(false),
path_sslcert(sslcert), path_sslkey(sslkey), daemon(NULL), bindlocalhost(false) {}
path_sslcert(sslcert), path_sslkey(sslkey), daemon(NULL),
bindlocalhost(false) {}

HttpServer::~HttpServer() {}

Expand All @@ -44,7 +45,7 @@ IClientConnectionHandler *HttpServer::GetHandler(const std::string &url) {
return NULL;
}

HttpServer& HttpServer::BindLocalhost() {
HttpServer &HttpServer::BindLocalhost() {
this->bindlocalhost = true;
return *this;
}
Expand Down Expand Up @@ -76,10 +77,11 @@ bool HttpServer::StartListening() {
loopback_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

this->daemon = MHD_start_daemon(
mhd_flags, this->port, NULL, NULL, HttpServer::callback, this,
MHD_OPTION_THREAD_POOL_SIZE, this->threads, MHD_OPTION_SOCK_ADDR, (struct sockaddr *)(&(this->loopback_addr)), MHD_OPTION_END);
mhd_flags, this->port, NULL, NULL, HttpServer::callback, this,
MHD_OPTION_THREAD_POOL_SIZE, this->threads, MHD_OPTION_SOCK_ADDR,
(struct sockaddr *)(&(this->loopback_addr)), MHD_OPTION_END);

} else if (this->path_sslcert != "" && this->path_sslkey != "") {
} else if (!this->path_sslcert.empty() && !this->path_sslkey.empty()) {
try {
SpecificationParser::GetFileContent(this->path_sslcert, this->sslcert);
SpecificationParser::GetFileContent(this->path_sslkey, this->sslkey);
Expand Down Expand Up @@ -193,9 +195,8 @@ int HttpServer::callback(void *cls, MHD_Connection *connection, const char *url,
client_connection->server->SendResponse("Not allowed HTTP Method",
client_connection);
}

if (client_connection != nullptr)
{

if (client_connection != nullptr) {
delete client_connection;
}
*con_cls = NULL;
Expand Down
13 changes: 4 additions & 9 deletions src/jsonrpccpp/server/connectors/linuxserialportserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ using namespace std;
#define READ_TIMEOUT 0.001 // Set timeout in seconds

LinuxSerialPortServer::LinuxSerialPortServer(const std::string &deviceName,
size_t threads)
: AbstractThreadedServer(threads), deviceName(deviceName), reader(DEFAULT_BUFFER_SIZE) {}
size_t threads)
: AbstractThreadedServer(threads), deviceName(deviceName),
reader(DEFAULT_BUFFER_SIZE) {}

LinuxSerialPortServer::~LinuxSerialPortServer() {
close(this->serial_fd);
}
LinuxSerialPortServer::~LinuxSerialPortServer() { close(this->serial_fd); }

bool LinuxSerialPortServer::InitializeListener() {

serial_fd = open(deviceName.c_str(), O_RDWR);

return serial_fd >= 0;

}

int LinuxSerialPortServer::CheckForConnection() {
Expand All @@ -55,7 +53,6 @@ int LinuxSerialPortServer::CheckForConnection() {
timeout.tv_usec = (suseconds_t)(READ_TIMEOUT * 1000000);
// Wait for something to read
return select(serial_fd + 1, &read_fds, nullptr, nullptr, &timeout);

}

void LinuxSerialPortServer::HandleConnection(int connection) {
Expand All @@ -66,5 +63,3 @@ void LinuxSerialPortServer::HandleConnection(int connection) {
response.append(1, DEFAULT_DELIMITER_CHAR);
writer.Write(response, serial_fd);
}


4 changes: 1 addition & 3 deletions src/jsonrpccpp/server/connectors/tcpsocketserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ TcpSocketServer::TcpSocketServer(const std::string &ipToBind,
#endif
}

TcpSocketServer::~TcpSocketServer() {
delete this->realSocket;
}
TcpSocketServer::~TcpSocketServer() { delete this->realSocket; }

bool TcpSocketServer::StartListening() {
if (this->realSocket != NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/jsonrpccpp/server/rpcprotocolserverv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void RpcProtocolServerV2::HandleSingleRequest(const Json::Value &req,
}
void RpcProtocolServerV2::HandleBatchRequest(const Json::Value &req,
Json::Value &response) {
if (req.size() == 0)
if (req.empty())
this->WrapError(Json::nullValue, Errors::ERROR_RPC_INVALID_REQUEST,
Errors::GetErrorMessage(Errors::ERROR_RPC_INVALID_REQUEST),
response);
Expand Down

0 comments on commit 78a0e36

Please sign in to comment.