Commit 67e80a1c by Amir Aharon

first run after removing cpprest and civetweb

parent 5faab7bf
......@@ -16,7 +16,8 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${BUILD_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BUILD_DIR}/bin)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# linked libs and their locations
# libcpprest is here for pplx tasks
# linked libs and their locations
set ( PROJECT_LINK_LIBS -lPocoFoundation -ljson -lhiredis -lcpprest -lcppmetrics -lboost_random -lboost_timer -lboost_chrono
-lboost_system -lboost_thread -lboost_date_time -lboost_regex -lboost_filesystem -lpthread
-lboost_random -lboost_chrono -lboost_system -lboost_thread -lssl
......
......@@ -11,7 +11,7 @@
#include <map>
#include <deque>
//#include <mongoose.h>
#include <civetweb.h>
// #include <civetweb.h>
#include <stdlib.h>
#include <stringbuffer.h> //rapidjson string
......
......@@ -18,15 +18,15 @@
#ifndef MICROSERVICES_ILOGGERSPDLOGIMPL_H
#define MICROSERVICES_ILOGGERSPDLOGIMPL_H
#include "common/Microservice_Iface.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
class Microservice_ILoggerSpdlogImpl: public nsMicroservice_Iface::ILogger
{
private:
std::shared_ptr<logger> mpc_Logger;
std::shared_ptr<spdlog::logger> mpc_Logger;
public:
Microservice_ILoggerSpdlogImpl();
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: MSICommandClientHttpImpl.cpp
* Author: amir
*
* Created on May 8, 2016, 4:08 PM
*/
#include "MSICommandClientHttpImpl.h"
#include "Microservice_BaseRestResponse.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/base_uri.h>
#include <common/Microservice_RestResponse.h>
#include <error/en.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
static const char* HTTP_SCHEME = U("http://");
static const int HTTP_SCHEME_LEN = strlen(HTTP_SCHEME);
static const char* NULL_REST_RESPONSE_OBJECT = "null rest response object passed";
static const char* FAILED_BUILD_URL = "Failed to build url";
//#define LOG_ERROR(str) if(p_logger_) p_logger_->error(str);
MSICommandClientHttpImpl::MSICommandClientHttpImpl()
{
}
MSICommandClientHttpImpl::MSICommandClientHttpImpl(const MSICommandClientHttpImpl& orig) {
}
MSICommandClientHttpImpl::~MSICommandClientHttpImpl() {
}
bool MSICommandClientHttpImpl::BuildUrl(MSCommandParams* p_cmd_params, std::string& url) {
std::string unencoded_url;
if(p_cmd_params == nullptr)
return false;
auto entity = p_cmd_params->GetEntity().c_str();
if(strncmp(entity,HTTP_SCHEME,HTTP_SCHEME_LEN) != 0)
unencoded_url.append(HTTP_SCHEME);//.append(entity);
unencoded_url.append(entity);
// params
if(!p_cmd_params->GetParams().empty())
{
for(auto param : p_cmd_params->GetParams())
{
unencoded_url.append(1,'/') .append(param.c_str());
}
}
else if(!p_cmd_params->GetParamsString().empty())
{
unencoded_url.append(1,'/') .append(p_cmd_params->GetParamsString().c_str());
}
// query params
if(!p_cmd_params->GetParamsString().empty())
{
unencoded_url.append(1,'?').append(p_cmd_params->GetRequestParams());
}
/*
* encode it
*/
url = web::uri::encode_uri(unencoded_url);
return true;
}
void MSICommandClientHttpImpl::HandleCommand(HttpCommandDataPtr& cmdDataPtr){
std::string url;
if(cmdDataPtr->p_response == nullptr)
{
cmdDataPtr->p_retstat->SetError(NULL_REST_RESPONSE_OBJECT);
return;
}
try
{
if(BuildUrl(cmdDataPtr->p_cmd_params,url))
{
cmdDataPtr->p_response->Reset();
pplx::task<http_response> request_task = createRequestTask(cmdDataPtr, url);
if(cmdDataPtr->p_cmd_params->IsAsync_())
{
request_task.then([this,cmdDataPtr](http_response resp){
if(resp.status_code() == status_codes::OK)
{
cmdDataPtr->p_command_counters->succeed++;
}
else
{
std::stringstream ss;
ss << resp.status_code() << " - " << resp.reason_phrase();
cmdDataPtr->p_retstat->SetError(ss.str().c_str());
LOGGER_ERROR(p_logger_,ss.str());
cmdDataPtr->p_command_counters->failed++;
}
});
}
else
{
http_response resp = request_task.get();
if(resp.status_code() == status_codes::OK)
{
utility::string_t content = resp.extract_string().get();
if(!content.empty())
{
rapidjson::Document& doc = cmdDataPtr->p_response->GetObjectNode();
if(!doc.Parse<0>(content.c_str()).HasParseError())
{
cmdDataPtr->p_command_counters->succeed++;
// delegate ?
if (cmdDataPtr->p_response->GetTypeHash() == Microservice_RestResponse::TYPE_HASH)
DelegateRestResponse(cmdDataPtr->p_response,resp);
}
else
{
cmdDataPtr->p_retstat->SetError(GetParseError_En(doc.GetParseError()));
cmdDataPtr->p_command_counters->failed++;
cmdDataPtr->p_response->SetError(cmdDataPtr->p_retstat->GetError());
}
}
}
else
{
std::stringstream ss;
ss << resp.status_code() << " - " << resp.reason_phrase();
cmdDataPtr->p_retstat->SetError(ss.str().c_str());
LOGGER_ERROR(p_logger_,ss.str());
cmdDataPtr->p_command_counters->failed++;
cmdDataPtr->p_response->SetError(cmdDataPtr->p_retstat->GetError());
// delegate ?
if (cmdDataPtr->p_response->GetTypeHash() == Microservice_RestResponse::TYPE_HASH)
DelegateRestResponse(cmdDataPtr->p_response,resp);
}
}
}
else
{
cmdDataPtr->p_retstat->SetError(FAILED_BUILD_URL);
}
}
catch (web::http::http_exception exp)
{
cmdDataPtr->p_retstat->SetError(exp.what());
LOGGER_ERROR(p_logger_,exp.what());
cmdDataPtr->p_command_counters->failed++;
}
}
/**
* creating the request task
* adding all the cmd data params
* @param cmdDataPtr
* @param url
* @return request task to perform
*/
pplx::task<http_response>
MSICommandClientHttpImpl::createRequestTask(const MSICommandClientHttpImpl::HttpCommandDataPtr &cmdDataPtr,
const std::string &url) const {
http_client client(url);
//config.set_timeout<std::chrono::seconds>(std::chrono::seconds(2));
http_request request(*cmdDataPtr->p_mtd);
// http_client_config config = client.client_config();
// config.set_timeout<std::chrono::seconds>(std::chrono::seconds(2));
// headers
request.headers().add(header_names::accept, "*/*");
auto& headers = cmdDataPtr->p_cmd_params->GetHeadersMap();
if (!headers.empty()){
for (auto header : headers){
request.headers().add(header.first,header.second);
}
}
// content
if(!cmdDataPtr->p_cmd_params->GetContent().empty())
request.set_body(cmdDataPtr->p_cmd_params->GetContent(),"application/json");
else
request.headers().add(header_names::content_length,"0");
//auto request_task = client.request(*cmdDataPtr->p_mtd);
//auto request_task =
return client.request(request);//request_task;
}
MSRetStat MSICommandClientHttpImpl::Create(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat;
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::POST),&retstat,&create_counters_));
HandleCommand(cmd_data);
return retstat;
}
MSRetStat MSICommandClientHttpImpl::Read(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat;
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::GET),&retstat,&read_counters_));
HandleCommand(cmd_data);
return retstat;
}
MSRetStat MSICommandClientHttpImpl::Update(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat;
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::PUT),&retstat,&update_counters_));
HandleCommand(cmd_data);
return retstat;
}
MSRetStat MSICommandClientHttpImpl::Delete(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) {
MSRetStat retstat;
auto cmd_data = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params,p_response,&(methods::DEL),&retstat,&delete_counters_));
HandleCommand(cmd_data);
return retstat;
}
void MSICommandClientHttpImpl::DelegateRestResponse(cMicroservice_BaseRestResponse *pResponse,
web::http::http_response &response) {
Microservice_RestResponse* p_RestResponse = (Microservice_RestResponse*)pResponse;
p_RestResponse->setResponse_code(response.status_code());
auto& headersMap = p_RestResponse->getHeaderMap();
for(auto header : response.headers())
{
std::string key = header.first;
p_RestResponse->addHeader(key,header.second);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: MSICommandClientHttpImpl.h
* Author: amir
*
* Created on May 8, 2016, 4:08 PM
*/
#ifndef MSICOMMANDCLIENTHTTPIMPL_H
#define MSICOMMANDCLIENTHTTPIMPL_H
#include "common/Microservice_Iface.h"
#include <atomic>
#include <cpprest/http_msg.h>
using namespace nsMicroservice_Iface;
class MSICommandClientHttpImpl : public ICommandClient {
public:
// struct CommandCounters
// {
// std::atomic_int succeed;
// std::atomic_int failed;
// CommandCounters(int succeed, int failed) :
// succeed(succeed), failed(failed) {
// }
// CommandCounters():
// succeed(0), failed(0) {
// }
// };
// struct HandleCommandData
// {
// MSCommandParams* p_cmd_params;
// cMicroservice_BaseRestResponse* p_response;
// const std::string* p_mtd;
// MSRetStat* p_retstat;
// CommandCounters* p_command_counters;
//
// HandleCommandData(MSCommandParams* p_cmd_params,
// cMicroservice_BaseRestResponse* p_response,
// const std::string* p_mtd,
// MSRetStat* p_retstat,
// CommandCounters* p_command_counters) :
// p_cmd_params(p_cmd_params), p_response(p_response), p_mtd(p_mtd), p_retstat(p_retstat), p_command_counters(p_command_counters) {
// }
//
// };
// typedef std::shared_ptr<HandleCommandData> HttpCommandDataPtr;
MSICommandClientHttpImpl();
MSICommandClientHttpImpl(const MSICommandClientHttpImpl& orig);
virtual ~MSICommandClientHttpImpl();
MSRetStat Create(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) override;
MSRetStat Read(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) override;
MSRetStat Update(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) override;
MSRetStat Delete(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) override;
// bool supportAsync() override {
// return false;
// }
private:
/**
* building url from the command params
* @param p_cmd_params
* @param url
* @return
*/
bool BuildUrl(MSCommandParams* p_cmd_params,std::string& url );
/**
* handle all the command flow
* @param p_cmd_data
*/
void HandleCommand(HttpCommandDataPtr& cmdDataPtr);
void DelegateRestResponse(cMicroservice_BaseRestResponse *pResponse, web::http::http_response &response);
pplx::task<web::http::http_response> createRequestTask(const HttpCommandDataPtr &cmdDataPtr, const std::string &url) const;
};
#endif /* MSICOMMANDCLIENTHTTPIMPL_H */
......@@ -5,7 +5,7 @@
*/
/*
* File: MSICommandClientHttpImpl.cpp
* File: MSICommandClientRMQImpl.cpp
* Author: amir
*
* Created on May 8, 2016, 4:08 PM
......
......@@ -5,7 +5,7 @@
*/
/*
* File: MSICommandClientHttpImpl.h
* File: MSICommandClientRMQImpl.h
* Author: amir
*
* Created on May 8, 2016, 4:08 PM
......
......@@ -16,7 +16,7 @@ static const int HIGH_WATER_MARK = 1000;
#include <zmqpp/context.hpp>
#include <common/RestMsg_generated.h>
#include <common/RestResponse_generated.h>
#include <common/Microservice_RequestContext.h>
class Microservice_IResponseRestZmqImpl: public nsMicroservice_Iface::IResponse
{
......
......@@ -4,19 +4,11 @@
#include "ClientFactory.h"
#include <Microservice_Client.h>
#include <impl/clients/MSICommandClientHttpImpl.h>
#include <impl/clients/MSZMQClientImpl.h>
#include <impl/clients/MSIPubSubClientImpl.h>
#include <impl/clients/MSICommandClientZmqImpl.h>
#include <impl/clients/MSICommandClientEvppImpl.h>
cMicroservice_Client *
ClientFactory::createHttpImplMsClient(std::string serviceName, std::string host, int port, bool cacheEnabled,
int cacheTimeout, bool metricsEnabled, std::string cacheHost) {
return new cMicroservice_Client(new MSICommandClientHttpImpl(),
new cMicroservice_BaseClientParams(serviceName,cacheEnabled,cacheTimeout,metricsEnabled,host,port,cacheHost));
}
cMicroservice_Client* ClientFactory::createEvppCommandImpl(std::string serviceName,
std::string host,
int port,
......
......@@ -6,14 +6,12 @@
#define MICROSERVICE_CLIENTFACTORY_H
#include <impl/clients/MSICommandClientHttpImpl.h>
#include <impl/clients/MSICommandClientRMQImpl.h>
/**
* specific clients factory
*/
class MSICommandClientHttpImpl;
class MSICommandClientRMQImpl;
class cMicroservice_Client;
......
......@@ -3,16 +3,16 @@
//
#include "ServerFactory.h"
#include <impl/servers/Microservice_IRestServerCivetWebImpl.h>
// #include <impl/servers/Microservice_IRestServerCivetWebImpl.h>
#include <impl/servers/Microservice_IMsgQueueServerZmqImpl.h>
//#include <impl/servers/Microservice_IRestServerRMQImpl.h>
#include <impl/servers/Microservice_IRestServerZmqImpl.h>
#include <impl/servers/Microservice_IRestServerEvppImpl.h>
cMicroservice_IRestServerCivetWebImpl *
ServerFactory::createIRestServerCivetWebImpl(std::string host, int port, int workerThreadsNum) {
return new cMicroservice_IRestServerCivetWebImpl(new cMicroservice_RestServerParams(port,host,workerThreadsNum));
}
// cMicroservice_IRestServerCivetWebImpl *
// ServerFactory::createIRestServerCivetWebImpl(std::string host, int port, int workerThreadsNum) {
// return new cMicroservice_IRestServerCivetWebImpl(new cMicroservice_RestServerParams(port,host,workerThreadsNum));
// }
Microservice_IRestServerEvppImpl*
ServerFactory::createIRestServerEvppImpl(std::string host, int port, int workerThreadsNum){
......
......@@ -8,7 +8,7 @@
#include <string>
#include <params/Microservice_Params.h>
class cMicroservice_IRestServerCivetWebImpl;
// class cMicroservice_IRestServerCivetWebImpl;
class Microservice_IMsgQueueServerZmqImpl;
//class cMicroservice_IRestServerRMQImpl;
class Microservice_IRestServerZmqImpl;
......@@ -20,9 +20,9 @@ class Microservice_IRestServerEvppImpl;
class ServerFactory {
public:
static cMicroservice_IRestServerCivetWebImpl* createIRestServerCivetWebImpl(std::string host,
int port,
int workerThreadsNum);
// static cMicroservice_IRestServerCivetWebImpl* createIRestServerCivetWebImpl(std::string host,
// int port,
// int workerThreadsNum);
static Microservice_IRestServerEvppImpl* createIRestServerEvppImpl(std::string host,
int port,
int workerThreadsNum);
......
......@@ -6,10 +6,7 @@
*/
#include <stdlib.h>
#include <Microservice_App.h>
#include <impl/servers/Microservice_IRestServerCivetWebImpl.h>
// #include <impl/servers/Microservice_IRestServerRMQImpl.h>
#include <impl/clients/MSICommandClientHttpImpl.h>
// #include <impl/clients/MSICommandClientRMQImpl.h>
// #include <impl/servers/Microservice_IRestServerCivetWebImpl.h>
#include <utils/ClientFactory.h>
......@@ -376,7 +373,7 @@ void runRestZmqTest(){
.withPubSub(NULL)
.withServiceDiscovery(NULL)
.addClient(ClientFactory::createZmqCommandImpl(appName,"clientApp", 0,"serverApp", 0,Microservice_ZMQServerParams::eProtocol::eIpc))
.addServer(ServerFactory::createIRestServerCivetWebImpl("", 50010, 1))
// .addServer(ServerFactory::createIRestServerCivetWebImpl("", 50010, 1))
.addServer(ServerFactory::createIRestServerZmqImpl("serverApp",0,Microservice_ZMQServerParams::eProtocol::eIpc))
.addTest("SendZmqRestRequests", [&msApp,&appName](std::stringstream& output,DequeStringMap& queryParams) -> MSRetStat {
auto p_zmqClient = msApp.GetMSClient(appName);
......
......@@ -11,12 +11,11 @@
#include <Microservice_Client.h>
#include <params/Microservice_Params.h>
#include <document.h> //rapidjson
#include <impl/servers/Microservice_IRestServerCivetWebImpl.h>
// #include <impl/servers/Microservice_IRestServerCivetWebImpl.h>
#include <impl/servers/Microservice_IRestServerEvppImpl.h>
#include <impl/servers/Microservice_IMsgQueueServerZmqImpl.h>
// #include <impl/servers/Microservice_IRestServerRMQImpl.h>
#include <impl/Microservice_ICacheClientRedisImpl.h>
#include <impl/clients/MSICommandClientHttpImpl.h>
#include <Microservice_BaseRestResponse.h>
#include <params/MSCommandParams.h>
#include <common/MSTypes.h>
......@@ -422,7 +421,7 @@ int main(int argc, char *argv[])
// .addClient(ClientFactory::createHttpImplMsClient("other-service", "localhost", 32010, true, 10, false,"localhost:6379"))
.addClient(ClientFactory::createZmqMsgQImp("zmq-service", msApp.name(), 0,
Microservice_ZMQServerParams::eProtocol::eIpc))
.addServer(ServerFactory::createIRestServerCivetWebImpl("", 50020, 1))
// .addServer(ServerFactory::createIRestServerCivetWebImpl("", 50020, 1))
.addServer(ServerFactory::createIRestServerEvppImpl("", 50010, 8))
.addServer(ServerFactory::createIMsgQueueServerZmqImpl(msApp.name(), 0, Microservice_ZMQServerParams::eProtocol::eIpc))
.addHandler("/xxx",(Microservice_RestHandler*)new cMicroserviceHandler("hello"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment