Commit 0c193196 by Amir Aharon

Merge branch 'Feature-RestEvpp' into 'develop'

Feature rest evpp

See merge request !4
parents 15ae02bf 680fe12e
......@@ -37,8 +37,18 @@ build
bin
lib
tmp
cmake-build-debug
_CPack_Packages
CMakeFiles
CMakeCache.txt
cmake_install.cmake
install_manifest.txt
dist
target
*.cmake
GPATH
GTAGS
GSYMS
GRTAGS
typescript
{
"variant": {
"label": "Debug",
"keywordSettings": {
"buildType": "debug"
},
"description": "Emit debug information without performing optimizations"
},
"activeEnvironments": []
}
\ No newline at end of file
......@@ -21,7 +21,10 @@
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
]
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17"
},
{
"name": "Linux",
......@@ -34,8 +37,13 @@
"/usr/lib/gcc/x86_64-linux-gnu/4.9/include-fixed",
"/usr/include/x86_64-linux-gnu",
"/usr/include",
"${workspaceRoot}/src"
"${workspaceRoot}/../3party",
"${workspaceRoot}/src",
"/usr/include/c++/5",
"/usr/include/x86_64-linux-gnu/c++/5",
"${workspaceRoot}/../3party/cereal/include"
],
"compileCommands": "./compile_commands.json",
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
......@@ -48,11 +56,15 @@
"/usr/lib/gcc/x86_64-linux-gnu/4.9/include-fixed",
"/usr/include/x86_64-linux-gnu",
"/usr/include",
"${workspaceRoot}"
"${workspaceRoot}/../3party",
"${workspaceRoot}/src",
"/usr/include/c++/5"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
"cStandard": "c11",
"cppStandard": "c++17"
},
{
"name": "Win32",
......@@ -72,8 +84,11 @@
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 3
"version": 4
}
\ No newline at end of file
......@@ -10,7 +10,24 @@
"preLaunchTask": "start-gdbserver",
"miDebuggerPath": "/usr/bin/gdb",
"targetArchitecture": "x64",
"program": "${workspaceRoot}/bin/test_Microservice",
"program": "${workspaceRoot}/build/bin/test_Microservice",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"sourceFileMap":{
"/home/develop/project": "${workspaceRoot}"
},
"environment": []
},
{
"name": "C++ Client Launch (GDBSERVER)",
"type": "cppdbg",
"request": "launch",
"miDebuggerServerAddress": "localhost:2000",
"preLaunchTask": "start-gdbserver-client",
"miDebuggerPath": "/usr/bin/gdb",
"targetArchitecture": "x64",
"program": "${workspaceRoot}/build/bin/test_MicroserviceClient",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
......@@ -27,7 +44,7 @@
// "preLaunchTask": "start-gdbserver",
"miDebuggerPath": "/usr/bin/gdb",
"targetArchitecture": "x64",
"program": "${workspaceRoot}/bin/test_Microservice",
"program": "${workspaceRoot}/build/bin/test_Microservice",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
......
{
"files.associations": {
"array": "cpp",
"*.tcc": "cpp",
"functional": "cpp",
"istream": "cpp",
"tuple": "cpp",
"utility": "cpp",
"bitset": "cpp",
"chrono": "cpp"
}
}
\ No newline at end of file
......@@ -24,6 +24,11 @@
{
"label": "build",
"type": "shell",
// Make this the default build command.
"group": {
"kind": "build",
"isDefault": true
},
"command": "docker exec -it devenv make",
"problemMatcher": [
"$gcc"
......@@ -32,13 +37,56 @@
{
"label": "clean",
"type": "shell",
"command": "docker exec -it devenv make clean"
"command": "docker exec -it devenv make clean",
"problemMatcher": [
"$gcc"
]
},
{
"label": "make package",
"type": "shell",
"command": "docker exec -it devenv make package",
},
{
"label": "start-gdbserver",
"type": "shell",
// not using -it so that it can be a preLaunchTask
"command": "docker exec -d devenv gdbserver :2000 bin/test_Microservice",
"command": "docker exec -d devenv gdbserver :2000 build/bin/test_Microservice",
"problemMatcher": []
},
{
"label": "start-gdbserver-client",
"type": "shell",
// not using -it so that it can be a preLaunchTask
"command": "docker exec -d devenv gdbserver :2000 build/bin/test_MicroserviceClient",
"problemMatcher": []
},
{
"label": "build develop image",
"type": "shell",
// not using -it so that it can be a preLaunchTask
"command": "docker build -f Dockerfile.develop -t municipalitybank.com:5050/ipgallery.common.cpp/microservice/develop .",
"problemMatcher": []
},
{
"label": "run app",
"type": "shell",
// not using -it so that it can be a preLaunchTask
"command": "docker exec -d devenv build/bin/test_Microservice",
"problemMatcher": []
},
{
"label": "run client test",
"type": "shell",
// not using -it so that it can be a preLaunchTask
"command": "docker exec -d devenv build/bin/test_MicroserviceClient",
"problemMatcher": []
},
{
"label": "stop app",
"type": "shell",
// not using -it so that it can be a preLaunchTask
"command": "docker exec -d devenv pkill test_Micro",
"problemMatcher": []
}
]
......
......@@ -2,31 +2,32 @@ cmake_minimum_required(VERSION 2.8.12)
project(Microservice)
# version stuff
set (Microservice_VERSION_MAJOR 1)
set (Microservice_VERSION_MINOR 4)
set (Microservice_VERSION_PATCH 2)
set (Microservice_VERSION_MINOR 5)
set (Microservice_VERSION_PATCH 0)
set(Microservice_VERSION_STRING ${Microservice_VERSION_MAJOR}.${Microservice_VERSION_MINOR}.${Microservice_VERSION_PATCH})
# type build flags
#set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BINARY_DIR build)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-O0")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# 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
-lcrypto -llog4cpp -lglog -lzmqpp -lzmq )
-lcrypto -llog4cpp -lglog -lzmqpp -lzmq -levpp -levent)
# 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
# -lcrypto -lRabbitmq -lrabbitmq -llog4cpp -lglog -lzmqpp -lzmq )
link_directories( ../3party/lib )
link_directories( ../internals/lib )
#link_directories( ../internals/lib )
# h files locations
include_directories(src)
......@@ -40,12 +41,12 @@ include_directories(SYSTEM ../3party/rabbitmq)
include_directories(SYSTEM ../3party/flatbuffers/include)
include_directories(SYSTEM ../3party/poco-1.7.8/Foundation/include)
include_directories(SYSTEM ../3party/evpp/build-release/include)
include_directories(SYSTEM ../internals/include/Rabbitmq)
# include_directories(SYSTEM ../internals/include/Rabbitmq)
include_directories(SYSTEM /usr/include/Poco)
include_directories(SYSTEM /usr/include/hiredis)
# recursive search files cpp files
file(GLOB_RECURSE SOURCES "src/*.cpp" src/Microservice_BaseRestResponse.h src/common/Microservice_RestResponse.h src/common/Microservice_MsgQContext.h src/handlers/Microservice_Reactor.cpp src/handlers/Microservice_Reactor.h src/common/Microservice_PubSubContext.h src/handlers/Microservice_MsgQHandler.h src/handlers/Microservice_PubSubHandler.h src/impl/servers/Microservice_IMsgQueueServerZmqImpl.cpp src/impl/servers/Microservice_IMsgQueueServerZmqImpl.h src/impl/Microservice_IMsgArchiverCerealImpls.h src/utils/ServerFactory.cpp src/utils/ServerFactory.h src/utils/ClientFactory.cpp src/utils/ClientFactory.h src/impl/clients/MSZMQClientImpl.cpp src/impl/clients/MSZMQClientImpl.h src/impl/clients/MSIPubSubClientImpl.cpp src/impl/clients/MSIPubSubClientImpl.h)
file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.h")
set (3PARTY_SOURCES ../3party/civetweb/src/civetweb.c ../3party/civetweb/src/CivetServer.cpp ../3party/civetweb/src/md5.inl ../3party/civetweb/src/handle_form.inl)
# remove RMQ for now
......@@ -62,9 +63,10 @@ list(REMOVE_ITEM SOURCES "${RMQHandler_file_path}"
"${RMQServer_file_path}"
"${RMQClient_file_path}")
message("${SOURCES}")
#Generate the shared library from the sources
add_library(Microservice SHARED ${SOURCES} ${3PARTY_SOURCES} src/impl/servers/Microservice_IRestServerEvppImpl.cpp src/impl/servers/Microservice_IRestServerEvppImpl.h src/impl/servers/Microservice_IRestServerZmqImpl.cpp src/impl/servers/Microservice_IRestServerZmqImpl.h src/common/Microservice_Iface.cpp src/impl/clients/MSICommandClientZmqImpl.cpp src/impl/clients/MSICommandClientZmqImpl.h src/impl/Microservice_ICacheClientPocoImpl.h src/handlers/Microservice_TestHandler.cpp src/handlers/Microservice_TestHandler.h)
add_library(Microservice SHARED ${SOURCES} ${3PARTY_SOURCES})
#add_library(Microservice SHARED ${SOURCES} ${3PARTY_SOURCES} src/impl/servers/Microservice_IRestServerEvppImpl.cpp src/impl/servers/Microservice_IRestServerEvppImpl.h src/impl/servers/Microservice_IRestServerZmqImpl.cpp src/impl/servers/Microservice_IRestServerZmqImpl.h src/common/Microservice_Iface.cpp src/impl/clients/MSICommandClientZmqImpl.cpp src/impl/clients/MSICommandClientZmqImpl.h src/impl/Microservice_ICacheClientPocoImpl.h src/handlers/Microservice_TestHandler.cpp src/handlers/Microservice_TestHandler.h)
target_link_libraries(Microservice ${PROJECT_LINK_LIBS} )
set_target_properties(Microservice PROPERTIES VERSION ${Microservice_VERSION_STRING}
SOVERSION ${Microservice_VERSION_MAJOR})
......@@ -125,8 +127,8 @@ install(FILES ${INSTALL_FILES} DESTINATION include/microservice/utils)
# 3party libs
INSTALL( DIRECTORY ../3party/lib DESTINATION lib/3party )
# internal libs
file (GLOB INSTALL_FILES "../internals/lib/libRabbitmq.*")
install(FILES ${INSTALL_FILES} DESTINATION lib/internals)
#file (GLOB INSTALL_FILES "../internals/lib/libRabbitmq.*")
#install(FILES ${INSTALL_FILES} DESTINATION lib/internals)
#include 3party files
INSTALL( DIRECTORY ../3party/cereal-1.2.1/include DESTINATION include/3party/cereal-1.2.1 )
......@@ -135,8 +137,9 @@ INSTALL( DIRECTORY ../3party/cppmetrics-0.1.1-Linux/include DESTINATION include/
file (GLOB INSTALL_FILES "../3party/mongoose/mongoose.*")
install(FILES ${INSTALL_FILES} DESTINATION include/3party/mongoose)
INSTALL( DIRECTORY ../3party/cpprest/Release/include DESTINATION include/3party/cpprest )
INSTALL( DIRECTORY ../3party/evpp/build-release/include DESTINATION include/3party/evpp )
INSTALL( DIRECTORY ../3party/rabbitmq DESTINATION include/3party )
INSTALL( DIRECTORY ../internals/include/Rabbitmq DESTINATION include/internals )
#INSTALL( DIRECTORY ../internals/include/Rabbitmq DESTINATION include/internals )
INSTALL( DIRECTORY /usr/include/hiredis DESTINATION include/3party )
INSTALL( DIRECTORY ../3party/flatbuffers/include DESTINATION include/3party/flatbuffers )
......@@ -163,5 +166,5 @@ include(CPack)
#
install(TARGETS Microservice DESTINATION lib)
set(PUBLISH_FILE ${CPACK_PACKAGE_FILE_NAME}-${CPACK_SYSTEM_NAME}.tar.gz)
set(PUBLISH_FILE target/${CPACK_PACKAGE_FILE_NAME}-${CPACK_SYSTEM_NAME}.tar.gz)
add_custom_target(publish COMMAND gradle uploadArchives -Pcversion=${Microservice_VERSION_STRING} -Ppublish_file=${PUBLISH_FILE})
\ No newline at end of file
#FROM ubuntu:14.04
#FROM ubuntu-debootstrap:14.04
FROM municipalitybank.com:5050/mcx/devops/dev-cpp
FROM municipalitybank.com:5050/ipgallery.common.cpp/microservice/develop
USER root
COPY ./doc/install-dependencies.sh /usr/
RUN chmod +x /usr/install-dependencies.sh
RUN apt-get update
RUN /usr/install-dependencies.sh
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
USER develop
RUN mkdir /home/develop/dist
COPY ./target/*.tar.gz /home/develop/dist/
RUN cd /home/develop/dist && tar xzf *.tar.gz
# setting for gcc >= 4.9.2
#RUN rm /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#COPY ./tmp/libstdc++.so.6 /usr/lib/x86_64-linux-gnu/libstdc++.so.6
......@@ -20,12 +15,6 @@ RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
#RUN echo kernel.shmmax=49249672960 >> /etc/sysctl.conf
#RUN echo kernel.shmall=10485760 >> /etc/sysctl.conf
# app
# COPY ./tmp/* /usr/lib/x86_64-linux-gnu/
# COPY ./bin/test_MicroserviceClient /usr/
# WORKDIR /usr
USER develop
EXPOSE 50010
......
#FROM ubuntu:14.04
#FROM ubuntu-debootstrap:14.04
FROM municipalitybank.com:5050/mcx/devops/dev-cpp
USER root
COPY ./doc/install-dependencies.sh /usr/
RUN chmod +x /usr/install-dependencies.sh
RUN apt-get update
RUN /usr/install-dependencies.sh
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# setting for gcc >= 4.9.2
#RUN rm /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#COPY ./tmp/libstdc++.so.6 /usr/lib/x86_64-linux-gnu/libstdc++.so.6
# setting for shared-memory
#RUN echo kernel.shmmax=49249672960 >> /etc/sysctl.conf
#RUN echo kernel.shmall=10485760 >> /etc/sysctl.conf
# app
# COPY ./tmp/* /usr/lib/x86_64-linux-gnu/
# COPY ./bin/test_MicroserviceClient /usr/
# WORKDIR /usr
USER develop
EXPOSE 50010
# ENTRYPOINT ["/bin/bash"]
## C++ Microservice Framework
## VERSIONS:
# 1.5.0
- add new rest server Evpp
- add new Evpp http client, currently NOT TO BE USED!
- script/build_microservice_docker.sh: add a script to build the microservice develop image as base for other c++ apps
- script/upload_pckage.sh: add a script to upload Microservice package to archiva as artifect
# 1.4.2
- moving to municipalitybank.com cloud
# 1.4.1
......
......@@ -12,3 +12,8 @@ services:
- "./:/home/develop/project"
- "./../3party:/home/develop/3party"
# command: bash
networks:
- devnet
networks:
devnet:
driver: bridge
\ No newline at end of file
......@@ -6,4 +6,4 @@
# Created on May 8, 2016, 9:59:18 AM
#
sudo apt-get install -y libhiredis0.10 libhiredis-dev libzmq3 libzmq3-dev liblog4cpp5 liblog4cpp5-dev \
libgoogle-glog-dev libboost-all-dev libssl-dev uuid-dev libzmqpp-dev libmhash-dev
\ No newline at end of file
libgoogle-glog-dev libboost-all-dev libssl-dev uuid-dev libzmqpp-dev libmhash-dev libevent-dev
\ No newline at end of file
base-cpp-microservice-test:
environment:
log4cpp.console: yes
graphite_hostport: 172.16.1.244:2015
image: 172.16.1.132:5000/ipgallery/base-cpp-microservice-test
version: '2'
services:
##### dev-microservice
dev-microservice:
image: municipalitybank.com:5050/ipgallery.common.cpp/microservice/develop
ports:
- "2000:2000"
- "50010:50010"
privileged: true
container_name: devenv
volumes:
- "./:/home/develop/project"
- "./../3party:/home/develop/3party"
command: tail -f /dev/null
networks:
- devnet
networks:
devnet:
driver: bridge
\ No newline at end of file
#!/bin/sh
# building the final docker microservice framework image
if [ -z "$1" ]
then
echo "Usage $0 [version]"
exit
fi
echo "building the base image..."
docker build -f Dockerfile.develop -t municipalitybank.com:5050/ipgallery.common.cpp/microservice/develop .
echo "run build env"
#docker-compose -f develop-compose.yml run -d --service-ports --name devenv dev-microservice bash &
docker-compose up -d dev-microservice
# wait for container tro start
sleep 5
echo "cmake generate"
docker exec -it devenv cmake .
echo "clean"
docker exec -it devenv make clean
echo "build"
# set to exit on fail
set -e
docker exec -it devenv make
echo "packaging..."
# make package
docker exec -it devenv make package
# # copy 3party libs to tmp inside project
# rm -rf ./tmp
# mkdir tmp
# docker exec -it cp `ldd build/lib/libMicroservice.so | grep 3partry | awk '{print $3}'` ./tmp/
# now copy everything to target
echo "copy and extract package.."
rm -rf ./target
mkdir target
mv Microservice-$1-Linux* ./target/
echo "build image..."
docker build -t municipalitybank.com:5050/ipgallery.common.cpp/microservice:$1 .
# closing container
docker-compose down
#!/bin/sh
if [ -z "$1" ] || [ -z "$2" ]
then
echo "Usage $0 [version] [file]"
exit
fi
gradle uploadArchives -Pcversion=$1 -Ppublish_file=$2
......@@ -59,6 +59,11 @@ namespace nsMicroservice_Constants
static const int EXIT_MSG_LEN = strlen(EXIT_MSG);
static const int REQUEST_MSG_INITIAL_SIZE = 1024;
static const char *const NOT_IMPLEMENTED = "Not Implemented";
static const std::string STD_STRING_CONTENT_TYPE_JSON = std::string(CONTENT_TYPE_JSON);
static const std::string STD_STRING_HEADER_CONTENT_TYPE = std::string(HEADER_CONTENT_TYPE);
static const std::string STD_STRING_CONTENT_TYPE_TEXT = std::string("text/html");
static const std::string RCID_HEADER = std::string("X-RCID");
static const char* FAILED_BUILD_URI = "Failed to build uri";
}
/*
......
#include "MSICommandClientEvppImpl.h"
#include <evpp/httpc/request.h>
#include <evpp/httpc/conn.h>
#include <utils/CommonUtils.h>
#include <mutex>
#include <condition_variable>
#include <common/Microservice_RestResponse.h>
#include <error/en.h>
static const char *COMMAND_TIMEOUT_PARAM = "command.timeout";
static const double DEFAULT_COMMAND_TIMEOUT = 5.0;
static const char *HTTP_SCHEME = "http://";
static const char *HTTPS_SCHEME = "https://";
static const int HTTP_SCHEME_LEN = strlen(HTTP_SCHEME);
static const int HTTPS_SCHEME_LEN = strlen(HTTPS_SCHEME);
static const char *NULL_REST_RESPONSE_OBJECT = "null rest response object passed";
static const char *FAILED_BUILD_URL = "Failed to build url";
static const char* CONNECTION_FAILED_OR_REQUEST_TIMEOUT = "Connection or request timeout";
MSICommandClientEvppImpl::MSICommandClientEvppImpl() : requestTimeout_(DEFAULT_COMMAND_TIMEOUT)
{
auto reqTO = std::getenv(COMMAND_TIMEOUT_PARAM);
char *endptr;
if (reqTO)
{
requestTimeout_ = strtod(reqTO, &endptr);
}
loopThread_.Start(true);
}
MSICommandClientEvppImpl::~MSICommandClientEvppImpl()
{
loopThread_.Stop(true);
}
MSRetStat MSICommandClientEvppImpl::Create(MSCommandParams *p_cmd_params, cMicroservice_BaseRestResponse *p_response)
{
MSRetStat retstat;
auto cmdDataPtr = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params, p_response, nullptr, &retstat, &create_counters_));
HandleCommandAndCallback(cmdDataPtr, cMicroservice_Enums::eCrudMethod::eCreate);
return retstat;
;
}
MSRetStat MSICommandClientEvppImpl::Read(MSCommandParams *p_cmd_params, cMicroservice_BaseRestResponse *p_response)
{
MSRetStat retstat;
auto cmdDataPtr = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params, p_response, nullptr, &retstat, &create_counters_));
HandleCommandAndCallback(cmdDataPtr, cMicroservice_Enums::eCrudMethod::eRead);
return retstat;
;
}
MSRetStat MSICommandClientEvppImpl::Update(MSCommandParams *p_cmd_params, cMicroservice_BaseRestResponse *p_response)
{
MSRetStat retstat;
auto cmdDataPtr = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params, p_response, nullptr, &retstat, &create_counters_));
HandleCommandAndCallback(cmdDataPtr, cMicroservice_Enums::eCrudMethod::eUpdate);
return retstat;
;
}
MSRetStat MSICommandClientEvppImpl::Delete(MSCommandParams *p_cmd_params, cMicroservice_BaseRestResponse *p_response)
{
MSRetStat retstat;
auto cmdDataPtr = std::make_shared<HandleCommandData>(HandleCommandData(p_cmd_params, p_response, nullptr, &retstat, &create_counters_));
HandleCommandAndCallback(cmdDataPtr, cMicroservice_Enums::eCrudMethod::eDelete);
return retstat;
;
}
bool MSICommandClientEvppImpl::BuildUrl(MSCommandParams *p_cmd_params, std::string &url)
{
//std::string unencoded_url;
if (p_cmd_params == nullptr)
return false;
const char* entity = p_cmd_params->GetEntity().c_str();
if (strncmp(entity, HTTP_SCHEME, HTTP_SCHEME_LEN) != 0 &&
strncmp(entity, HTTPS_SCHEME, HTTPS_SCHEME_LEN) != 0)
url.append(HTTP_SCHEME); //.append(entity);
url.append(entity);
// params
if (!p_cmd_params->GetParams().empty())
{
for (auto param : p_cmd_params->GetParams())
{
url.append(1, '/').append(param.c_str());
}
}
else if (!p_cmd_params->GetParamsString().empty())
{
url.append(1, '/').append(p_cmd_params->GetParamsString().c_str());
}
// query params
if (!p_cmd_params->GetRequestParams().empty())
{
url.append(1, '?').append(CommonUtils::urlencode(p_cmd_params->GetRequestParams()));
}
/*
* encode it
*/
//url = CommonUtils::urlencode(unencoded_url);
return true;
}
/**
* Creating the reqeust and callback, activating the request and analyzing results
*/
void MSICommandClientEvppImpl::HandleCommandAndCallback(ICommandClient::HttpCommandDataPtr &cmdDataPtr,
cMicroservice_Enums::eCrudMethod crudMethod)
{
std::mutex m;
auto sharedCv = std::make_shared<std::condition_variable>();
evpp::httpc::RequestPtr requestPtr = nullptr;
auto f = [&m,this,cmdDataPtr,sharedCv](const std::shared_ptr<evpp::httpc::Response> &responsePtr) {
//std::cout << "http_code=" << response->http_code() << " [" << responsePtr->body().ToString() << "]";
//std::string header = response->FindHeader("Connection");
cmdDataPtr->p_retstat->Reset();
cmdDataPtr->p_response->Reset();
std::unique_lock<std::mutex> lk(m);
register int resp_code = responsePtr->http_code();
if (resp_code == 0){
this->handleRequestFailed(cmdDataPtr,CONNECTION_FAILED_OR_REQUEST_TIMEOUT);
} else if (resp_code >= 200 && resp_code <= 206){
this->handleRequestSuccess(cmdDataPtr,responsePtr);
} else {
this->handleRequestFailed(cmdDataPtr,nsMicroservice_Constants::REQUEST_ERROR);
}
lk.unlock();
cmdDataPtr->finished = true;
if (!cmdDataPtr->p_cmd_params->IsAsync_())
sharedCv->notify_all();
};
std::string url;
if (BuildUrl(cmdDataPtr->p_cmd_params, url))
{
switch (crudMethod)
{
case cMicroservice_Enums::eCrudMethod::eCreate:
requestPtr = std::make_shared<evpp::httpc::Request>(evpp::httpc::PostRequest(loopThread_.loop(),
url,
cmdDataPtr->p_cmd_params->GetContent().c_str(),
evpp::Duration(requestTimeout_)));
break;
case cMicroservice_Enums::eCrudMethod::eRead:
requestPtr = std::make_shared<evpp::httpc::Request>(evpp::httpc::GetRequest(loopThread_.loop(),
url,
evpp::Duration(requestTimeout_)));
break;
}
//std::shared_ptr<evpp::httpc::GetRequest> r(new evpp::httpc::GetRequest(t.loop(), uri, evpp::Duration(requestTimeout_)));
if (requestPtr)
{
requestPtr->Execute(f);
if (!cmdDataPtr->p_cmd_params->IsAsync_())
{
std::unique_lock<std::mutex> lk(m);
sharedCv->wait_for(lk, std::chrono::seconds((int)(requestTimeout_ * 2)));
if (!cmdDataPtr->finished) {
/**
* handle to error
*/
p_logger_->warning("%s, failed on timeout, Cmnd Id: %u", __PRETTY_FUNCTION__, cmdDataPtr->p_cmd_params->GetCommandId());
handleRequestFailed(cmdDataPtr,CONNECTION_FAILED_OR_REQUEST_TIMEOUT);
}
}
} else {
cmdDataPtr->p_retstat->SetError("Unsupported method");
}
}else {
cmdDataPtr->p_retstat->SetError(nsMicroservice_Constants::FAILED_BUILD_URI);
}
}
void MSICommandClientEvppImpl::handleRequestFailed(const ICommandClient::HttpCommandDataPtr &cmdDataPtr,const char* error)
{
cmdDataPtr->p_retstat->SetError(error);
cmdDataPtr->p_command_counters->failed++;
cmdDataPtr->p_response->SetError(cmdDataPtr->p_retstat->GetError());
}
void MSICommandClientEvppImpl::handleRequestSuccess(const ICommandClient::HttpCommandDataPtr &cmdDataPtr,
const std::shared_ptr<evpp::httpc::Response> &responsePtr)
{
rapidjson::Document& doc = cmdDataPtr->p_response->GetObjectNode();
const char* respBody = responsePtr->body().data();
if(!doc.Parse<0>(respBody).HasParseError()) {
cmdDataPtr->p_command_counters->succeed++;
// delegate ?
if (cmdDataPtr->p_response->GetTypeHash() == Microservice_RestResponse::TYPE_HASH){
Microservice_RestResponse* p_rr = (Microservice_RestResponse*)cmdDataPtr->p_response;
p_rr->setResponse_code(responsePtr->http_code());
p_rr->setCommandId(cmdDataPtr->p_cmd_params->GetCommandId());
}
} else {
handleRequestFailed(cmdDataPtr,rapidjson::GetParseError_En(doc.GetParseError()));
}
}
#ifndef MSICOMMANDCLIENTEVPPIMPL_H
#define MSICOMMANDCLIENTEVPPIMPL_H
#include "common/Microservice_Iface.h"
#include <evpp/event_loop_thread.h>
#include <evpp/httpc/response.h>
using namespace nsMicroservice_Iface;
class MSICommandClientEvppImpl : public ICommandClient
{
private:
evpp::EventLoopThread loopThread_;
double requestTimeout_;
/**
* 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 cmdDataPtr
*/
void HandleCommandAndCallback(ICommandClient::HttpCommandDataPtr &cmdDataPtr,cMicroservice_Enums::eCrudMethod crudMethod);
void handleRequestFailed(const ICommandClient::HttpCommandDataPtr &cmdDataPtr,const char* error);
void handleRequestSuccess(const ICommandClient::HttpCommandDataPtr &cmdDataPtr,
const std::shared_ptr<evpp::httpc::Response> &responsePtr);
public:
MSICommandClientEvppImpl();
virtual ~MSICommandClientEvppImpl();
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;
};
#endif // MSICOMMANDCLIENTEVPPIMPL_H
\ No newline at end of file
......@@ -14,7 +14,7 @@
#include <error/en.h>
#include "MSICommandClientZmqImpl.h"
static const char* FAILED_BUILD_URI = "Failed to build uri";
const auto CRUD_METHOD_CREATE = common::context::CrudMethod_Create;
static const std::string CREATE_METHOD_STRING = common::context::EnumNameCrudMethod(CRUD_METHOD_CREATE);
......@@ -248,7 +248,7 @@ void MSICommandClientZmqImpl::HandleCommand(ICommandClient::HttpCommandDataPtr &
p_requestWorkParams_->p_clientSend_->send_raw((const char *) p_builder->GetBufferPointer(), p_builder->GetSize(),zmqpp::socket::dont_wait);
} else {
cmdDataPtr->p_retstat->SetError(FAILED_BUILD_URI);
cmdDataPtr->p_retstat->SetError(nsMicroservice_Constants::FAILED_BUILD_URI);
}
}
......
#include "Microservice_IRestServerEvppImpl.h"
#include <signal.h>
#include <handlers/Microservice_Reactor.h>
#include <evhttp.h>
#include <utils/CommonUtils.h>
static const int SLEEP_INTERVAL = 1; // IN MICROSEC
static int s_sig_num = 0;
const char* Microservice_IRequestRestEvppImpl::GetMethod(){
auto type = connParams_.ctx_->req()->type;
const char *method;
switch (type) {
case EVHTTP_REQ_GET:
method = "GET";
break;
case EVHTTP_REQ_POST:
method = "POST";
break;
case EVHTTP_REQ_HEAD:
method = "HEAD";
break;
case EVHTTP_REQ_PUT:
method = "PUT";
break;
case EVHTTP_REQ_DELETE:
method = "DELETE";
break;
case EVHTTP_REQ_OPTIONS:
method = "OPTIONS";
break;
case EVHTTP_REQ_TRACE:
method = "TRACE";
break;
case EVHTTP_REQ_CONNECT:
method = "CONNECT";
break;
case EVHTTP_REQ_PATCH:
method = "PATCH";
break;
default:
method = nullptr;
break;
}
return (method);
}
void DefaultHandler(evpp::EventLoop* loop,
const evpp::http::ContextPtr& ctx,
const evpp::http::HTTPSendResponseCallback& cb) {
std::stringstream oss;
oss << "Nothing to see in " << "\n"
<< "uri = " << ctx->uri() << "\n"
<< ".. move along" << "\n";
ctx->set_response_http_code(404);
ctx->AddResponseHeader(nsMicroservice_Constants::STD_STRING_HEADER_CONTENT_TYPE,
nsMicroservice_Constants::STD_STRING_CONTENT_TYPE_TEXT);
ctx->AddResponseHeader("Server", "ipgms");
cb(oss.str());
}
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler);
s_sig_num = sig_num;
}
Microservice_IRestServerEvppImpl::Microservice_IRestServerEvppImpl(cMicroservice_RestServerParams* p_param)
:p_server_(nullptr),p_runThread_(nullptr) {
serverType_.assign(getType());
p_param_ = p_param;
}
bool Microservice_IRestServerEvppImpl::build(std::string& appName, const std::map<std::string,nsMicroservice_Iface::IHandler*>& msHandlersMap,
nsMicroservice_Iface::ILogger* pc_Logger,
nsMicroservice_Iface::IPubSub* pc_PubSub,
nsMicroservice_Iface::IMetricsFactory* p_metrics_factory){
bool result = false;
if (p_param_)
{
appName_.assign(appName);
p_logger_ = pc_Logger;
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
result = true;
}
return result;
}
void Microservice_IRestServerEvppImpl::registerService(nsMicroservice_Iface::IServiceDiscovery* pc_ServiceDiscovery,
std::string& id){
if (pc_ServiceDiscovery != nullptr)
pc_ServiceDiscovery->registerService(appName_, id, p_param_->getHost(), p_param_->getPort());
}
void Microservice_IRestServerEvppImpl::run(){
p_runThread_ = new std::thread(std::bind([this](){
p_server_->Start();
while (!p_server_->IsStopped() && s_sig_num == 0) {
usleep(SLEEP_INTERVAL);
}
// stopping if not stopped already
if (!p_server_->IsStopped())
p_server_->Stop();
}));
}
void Microservice_IRestServerEvppImpl::stop(){
if (p_runThread_){
s_sig_num = 1;
p_runThread_->join();
}
}
bool Microservice_IRestServerEvppImpl::init(){
p_server_ = new evpp::http::Server(p_param_->getWorkerThreadsNum());
p_server_->SetThreadDispatchPolicy(evpp::ThreadDispatchPolicy::kIPAddressHashing);
p_server_->RegisterDefaultHandler([this](evpp::EventLoop* loop,
const evpp::http::ContextPtr& ctx,
const evpp::http::HTTPSendResponseCallback& cb) {
EvppConnParams connParams(ctx,cb);
this->HandleRequest(connParams);
});
// p_server_->RegisterHandler("/test",
// [this](evpp::EventLoop* loop,
// const evpp::http::ContextPtr& ctx,
// const evpp::http::HTTPSendResponseCallback& cb) {
// std::stringstream oss;
// oss << "testing 1..2..3.." << "\n";
// ctx->set_response_http_code(200);
// ctx->AddResponseHeader(nsMicroservice_Constants::STD_STRING_HEADER_CONTENT_TYPE,
// nsMicroservice_Constants::STD_STRING_CONTENT_TYPE_TEXT);
// ctx->AddResponseHeader("Server", "ipgms");
// cb(oss.str());
// });
p_server_->Init(p_param_->getPort());
return true;
}
void Microservice_IRestServerEvppImpl::HandleRequest(EvppConnParams &connParams){
const char* pba_Uri = connParams.ctx_->uri().c_str();
if (pba_Uri[0] == '/')
{
const char* pba_NextSlash = strchr(pba_Uri + 1, '/');
if (pba_NextSlash)
{
std::string apiContextPath(pba_Uri,(int)(pba_NextSlash - pba_Uri));
std::string key(nsMicroservice_Iface::IRestServer::TYPE);
key.append(nsMicroservice_Constants::TYPE_PREFIX_SEPERATOR).append(apiContextPath);
if(p_reactor_)
HandleNewRequest(connParams, key, apiContextPath);
else
SendGeneralError(connParams,500,"Missing Reactor body!");
}
else
SendNotImplemented(connParams);
}
else
SendNotImplemented(connParams);
}
void Microservice_IRestServerEvppImpl::SendGeneralError(EvppConnParams &connParams,
int respCode,
const char *error){
connParams.ctx_->set_response_http_code(respCode);
std::string errorString = std::string(error);
connParams.ctx_->AddResponseHeader("Content-Type", "text/html");
connParams.ctx_->AddResponseHeader("Server", "ipgms");
connParams.cb_(errorString);
}
void Microservice_IRestServerEvppImpl::HandleNewRequest(EvppConnParams &connParams,
std::string key,
std::string& apiContextPath){
MSRetStat retStat;
Microservice_IRequestRestEvppImpl evppReqRestImpl(connParams);
Microservice_IResponseRestEvppImpl evppRespRestImpl(connParams);
cMicroservice_RequestContext ctx(this,
&evppRespRestImpl,
&evppReqRestImpl);
retStat = ParseRequest(connParams, ctx, apiContextPath);
if (retStat.IsSuccess())
p_reactor_->Delegate(key, &ctx);
else
SendGeneralError(connParams, 500, "Failed in parsing...kus restek! yored lekafa..");
}
void Microservice_IRestServerEvppImpl::SendNotImplemented(EvppConnParams &connParams){
SendGeneralError(connParams,501,nsMicroservice_Constants::NOT_IMPLEMENTED);
}
MSRetStat Microservice_IRestServerEvppImpl::ParseRequest(EvppConnParams &connParams,
cMicroservice_RequestContext& ctx,
std::string& apiContextPath){
/*
* getting params
*/
auto p_request = ((Microservice_IRequestRestEvppImpl*)ctx.mpti_Request);
char* buff = p_request->GetBuffer();
const auto uriLen = connParams.ctx_->uri().length();
memcpy(buff, connParams.ctx_->uri().c_str(),
(uriLen < nsMicroservice_Constants::MAX_URI_LENGTH) ? uriLen : nsMicroservice_Constants::MAX_URI_LENGTH - 1);
buff[uriLen] = CNULL;
char* pba_ParamsStr = &buff[apiContextPath.length()];
char* pba_token = strtok(pba_ParamsStr,nsMicroservice_Constants::SLASH_SEPERATOR);
while(pba_token)
{
ctx.mc_Params.push_back(pba_token);
pba_token = strtok(NULL,nsMicroservice_Constants::SLASH_SEPERATOR);
}
/*
* getting query parameters
*/
GetQueryParams(ctx);
/*
* Log request
*/
LogRequest(p_request);
/**
* get crud method
*/
ctx.crudMethod = GetCrudMethod(p_request);
return MSRetStat();
}
void Microservice_IRestServerEvppImpl::LogRequest(Microservice_IRequestRestEvppImpl* p_request) {
//return connParams_.ctx_->req()->type;
if (p_logger_->getLevel() == cMicroservice_Enums::eLogLevel::eDebug) {
std::string str("Received request: ");
str.append(p_request->GetMethod());
str.append(", uri: ").append(p_request->GetRelativePath());
if (p_request->GetQueryString())
str.append(", query string: ").append(p_request->GetQueryString());
p_logger_->debug(str);
}
}
void
Microservice_IRestServerEvppImpl::GetQueryParams(cMicroservice_RequestContext &ctx) {
/*
* getting query parameters
*/
if (!ctx.mpti_Request->GetQueryString())
return;
// reusing the buffer in the request
auto p_request = ((Microservice_IRequestRestEvppImpl*)ctx.mpti_Request);
char* buff = p_request->GetBuffer();
*buff = CNULL;
DequeStringMap* pc_queryParams = &ctx.mc_QueryParameters;
const auto queryLen = strlen(p_request->GetQueryString());
memcpy(buff, p_request->GetQueryString(),
(queryLen < nsMicroservice_Constants::MAX_URI_LENGTH) ? queryLen : nsMicroservice_Constants::MAX_URI_LENGTH - 1);
buff[queryLen] = CNULL;
CommonUtils::BuildQueryParams(buff,pc_queryParams);
}
eCrudMethod Microservice_IRestServerEvppImpl::GetCrudMethod(Microservice_IRequestRestEvppImpl* p_request) {
auto iter = _microservice_RestCrudMap.find(std::string(p_request->GetMethod()));
if (iter != _microservice_RestCrudMap.end())
return iter->second;
return eCrudMethod::eMaxMethods;
}
#ifndef MICROSERVICE_IRESTSERVEREVPPIMPL_H
#define MICROSERVICE_IRESTSERVEREVPPIMPL_H
#include "common/Microservice_Iface.h"
#include <common/Microservice_RequestContext.h>
#include "../../params/Microservice_Params.h"
#include <thread>
#include <evpp/http/http_server.h>
struct EvppConnParams {
const evpp::http::ContextPtr& ctx_;
const evpp::http::HTTPSendResponseCallback& cb_;
EvppConnParams(const evpp::http::ContextPtr& ctx, const evpp::http::HTTPSendResponseCallback& cb) :
ctx_(ctx),cb_(cb) {}
};
class Microservice_IResponseRestEvppImpl: public nsMicroservice_Iface::IResponse
{
// for cloning
// Microservice_IResponseRestEvppImpl() :
// // p_respConnection_(p_respConnection),
// // respBuilder_(nsMicroservice_Constants::REQUEST_MSG_INITIAL_SIZE),
// rcid_(0)
// {}
//Evpppp::socket* p_respConnection_;
EvppConnParams connParams_;
std::uint64_t rcid_;
public:
Microservice_IResponseRestEvppImpl(EvppConnParams connParams) : connParams_(connParams), rcid_(0) {}
Microservice_IResponseRestEvppImpl(EvppConnParams connParams, std::uint64_t rcid) : connParams_(connParams), rcid_(rcid) {}
void Send(const char* response) override {
std::string strResponse = std::string(response);
connParams_.ctx_->AddResponseHeader(nsMicroservice_Constants::STD_STRING_HEADER_CONTENT_TYPE,
nsMicroservice_Constants::STD_STRING_CONTENT_TYPE_JSON);
if (rcid_ > 0)
connParams_.ctx_->AddResponseHeader(nsMicroservice_Constants::RCID_HEADER,std::to_string(rcid_));
connParams_.cb_(strResponse);
}
void Reset() override { /*p_respConnection_ = nullptr; */}
// void setParams(/*Evpppp::socket *p_respConnection, */std::uint64_t rcid) {
// //p_respConnection_ = p_respConnection;
// rcid_ = rcid;
// }
virtual nsMicroservice_Iface::IResponse *clone() override {
return new Microservice_IResponseRestEvppImpl(connParams_,rcid_);
}
};
class Microservice_IRequestRestEvppImpl: public nsMicroservice_Iface::IRequest {
public:
Microservice_IRequestRestEvppImpl(EvppConnParams connParams): connParams_(connParams) {
queryString_ = ParseQueryString();
}
const char *ParseQueryString() {
// if (p_restMsg_)
// return p_restMsg_->queryString()->c_str();
auto uri = connParams_.ctx_->original_uri();
const char* start = strchr(const_cast<char*>(uri), '?');
if (start) {
return (start+1);
}
return nullptr;
}
const char *GetQueryString() override {
return queryString_;
}
const char *GetRelativePath() override {
// if (p_restMsg_)
// return p_restMsg_->url()->c_str();
return connParams_.ctx_->uri().c_str();
}
const char *GetContent() override {
// if (p_restMsg_)
// return p_restMsg_->content()->c_str();
return connParams_.ctx_->body().data();
}
void Reset() override {
// p_restMsg_ = nullptr;
}
const char* GetMethod();
char* GetBuffer() { return buff_; }
private:
EvppConnParams connParams_;
const char* queryString_;
char buff_[nsMicroservice_Constants::MAX_URI_LENGTH];
};
class Microservice_IRestServerEvppImpl : public nsMicroservice_Iface::IRestServer , public nsMicroservice_Iface::IContainer {
public:
Microservice_IRestServerEvppImpl(cMicroservice_RestServerParams* p_param);
Microservice_IRestServerEvppImpl(const Microservice_IRestServerEvppImpl& orig) {}
virtual ~Microservice_IRestServerEvppImpl() {}
bool build(std::string& appName, const std::map<std::string,nsMicroservice_Iface::IHandler*>& msHandlersMap,
nsMicroservice_Iface::ILogger* pc_Logger,
nsMicroservice_Iface::IPubSub* pc_PubSub,
nsMicroservice_Iface::IMetricsFactory* p_metrics_factory) override;
void registerService(nsMicroservice_Iface::IServiceDiscovery* pc_ServiceDiscovery, std::string& id) override;
void run() override;
void stop() override;
virtual bool init() override;
void HandleRequest(EvppConnParams &connParams);
void SendNotImplemented(EvppConnParams &connParams);
private:
evpp::http::Server* p_server_;
std::thread* p_runThread_;
nsMicroservice_Iface::ILogger* p_logger_;
std::string serverType_;
cMicroservice_RestServerParams* p_param_;
std::string appName_;
// consts
static constexpr uint32_t SLEEP_INTERVAL = 1; //useconds
void SendGeneralError(EvppConnParams &connParams,
int respCode,
const char *error);
void HandleNewRequest(EvppConnParams &connParams,
std::string key,
std::string& apiContextPath);
MSRetStat ParseRequest(EvppConnParams &connParams,
cMicroservice_RequestContext& ctx,
std::string& apiContextPath);
void LogRequest(Microservice_IRequestRestEvppImpl* p_request);
void GetQueryParams(cMicroservice_RequestContext &ctx);
eCrudMethod GetCrudMethod(Microservice_IRequestRestEvppImpl* p_request);
};
#endif // MICROSERVICE_IRESTSERVEREVPPIMPL_H
......@@ -8,6 +8,7 @@
#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,
......@@ -16,6 +17,17 @@ ClientFactory::createHttpImplMsClient(std::string serviceName, std::string host,
new cMicroservice_BaseClientParams(serviceName,cacheEnabled,cacheTimeout,metricsEnabled,host,port,cacheHost));
}
cMicroservice_Client* ClientFactory::createEvppCommandImpl(std::string serviceName,
std::string host,
int port,
bool cacheEnabled,
int cacheTimeout,
bool metricsEnabled,
std::string cacheHost){
return new cMicroservice_Client(new MSICommandClientEvppImpl(),
new cMicroservice_BaseClientParams(serviceName,cacheEnabled,cacheTimeout,metricsEnabled,host,port,cacheHost));
}
cMicroservice_Client *ClientFactory::createZmqMsgQImp(std::string serviceName, std::string host,
int port, Microservice_ZMQServerParams::eProtocol protocol,
bool cacheEnabled, int cacheTimeout, bool metricsEnabled,
......
......@@ -26,6 +26,13 @@ public:
int cacheTimeout = 0,
bool metricsEnabled = false,
std::string cacheHost = "");
static cMicroservice_Client* createEvppCommandImpl(std::string serviceName,
std::string host = "",
int port = 0,
bool cacheEnabled = false,
int cacheTimeout = 0,
bool metricsEnabled = false,
std::string cacheHost = "");
static cMicroservice_Client* createRMQImplMsClient();
static cMicroservice_Client* createZmqMsgQImp(std::string serviceName,
std::string host,
......
......@@ -38,3 +38,27 @@ void CommonUtils::BuildQueryParams(char *buffer, DequeStringMap *p_queryParams)
pba_token = strtok(NULL, nsMicroservice_Constants::AND_SEPERATOR);
}
}
std::string CommonUtils::urlencode(const std::string& url)
{
//RFC 3986 section 2.3 Unreserved Characters (January 2005)
const std::string unreserved = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~";
std::string escaped="";
for(size_t i=0; i<url.length(); i++)
{
if (unreserved.find_first_of(url[i]) != std::string::npos)
{
escaped.push_back(url[i]);
}
else
{
escaped.append("%");
char buf[3];
sprintf(buf, "%.2X", url[i]);
escaped.append(buf);
}
}
return escaped;
}
\ No newline at end of file
......@@ -59,6 +59,8 @@ public:
const auto start = std::chrono::high_resolution_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::nanoseconds>(start).count();
}
static std::string urlencode(const std::string& url);
};
......
......@@ -7,12 +7,18 @@
#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));
}
Microservice_IRestServerEvppImpl*
ServerFactory::createIRestServerEvppImpl(std::string host, int port, int workerThreadsNum){
return new Microservice_IRestServerEvppImpl(new cMicroservice_RestServerParams(port,host,workerThreadsNum));
}
Microservice_IMsgQueueServerZmqImpl *
ServerFactory::createIMsgQueueServerZmqImpl(std::string host, int port, Microservice_ZMQServerParams::eProtocol protocol) {
return new Microservice_IMsgQueueServerZmqImpl(Microservice_ZMQServerParams(host,port,protocol));
......
......@@ -12,6 +12,7 @@ class cMicroservice_IRestServerCivetWebImpl;
class Microservice_IMsgQueueServerZmqImpl;
//class cMicroservice_IRestServerRMQImpl;
class Microservice_IRestServerZmqImpl;
class Microservice_IRestServerEvppImpl;
/**
* factory to create different servers
......@@ -22,6 +23,10 @@ public:
static cMicroservice_IRestServerCivetWebImpl* createIRestServerCivetWebImpl(std::string host,
int port,
int workerThreadsNum);
static Microservice_IRestServerEvppImpl* createIRestServerEvppImpl(std::string host,
int port,
int workerThreadsNum);
static Microservice_IMsgQueueServerZmqImpl* createIMsgQueueServerZmqImpl(std::string host,
int port,
Microservice_ZMQServerParams::eProtocol aProtocol);
......
......@@ -24,6 +24,14 @@
#include <impl/servers/Microservice_IRestServerZmqImpl.h>
#include <common/Microservice_RestResponse.h>
#include <impl/MsgArchiverJsonImpl.h>
#include <evpp/event_loop_thread.h>
#include <evpp/httpc/request.h>
#include <evpp/httpc/conn.h>
#include <evpp/httpc/response.h>
#include <memory>
static const char *const PUBSUBHOST = "zmqpubsub";
......@@ -157,7 +165,7 @@ void pubsubtest(cMicroservice_Client *p_Client) {
// for convenience
using json = nlohmann::json;
static const int ITERATIONS = 1000000;
static const int ITERATIONS = 10000;
static const char *const JSON_CONTENT = "{\n"
......@@ -428,11 +436,41 @@ void SendZmqRestRequests(const Microservice_App &msApp, cMicroservice_Client *p_
}
}
#include <atomic>
void runEvppClientTest(){
std::atomic_int counter(1);
bool responsed = false;
evpp::EventLoopThread t;
t.Start(true);
std::string uri = "http://localhost:50010/_mon/_stat";
for (int i = 0; i < ITERATIONS; i++)
{
std::shared_ptr<evpp::httpc::GetRequest> r(new evpp::httpc::GetRequest(t.loop(), uri, evpp::Duration(3.0)));
auto f = [&counter, &responsed, r](const std::shared_ptr<evpp::httpc::Response> &response) {
std::cout << "http_code=" << response->http_code() << " [" << response->body().ToString() << "]";
//std::string header = response->FindHeader("Connection");
register int count = counter++;
if (count >= ITERATIONS)
responsed = true;
};
r->Execute(f);
}
while (!responsed) {
usleep(1);
}
t.Stop(true);
}
int main(int argc, char *argv[])
{
auto duration = CommonUtils::measureFunc<>(runEvppClientTest);
std::cout <<" Testing " << ITERATIONS << " with map serialization json took: " << duration << "msec" << '\n';
//runRestZmqTest();
// testCaches();
testJsons();
// testJsons();
......
......@@ -12,6 +12,7 @@
#include <params/Microservice_Params.h>
#include <document.h> //rapidjson
#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>
......@@ -108,8 +109,8 @@ public:
rpj_Doc.AddMember(rapidjson::StringRef(it->first.c_str()),rapidjson::StringRef(dequeIt->c_str()),rpj_Alloc);
}
}
// ReadSync(pc_reqCtx);
ReadAsync2(pc_reqCtx);
ReadSync(pc_reqCtx);
// ReadAsync2(pc_reqCtx);
//this->WriteObjectToResponse(pc_reqCtx,rpj_Doc);
// add metric
long value = rand() % 1000 + 1;
......@@ -123,10 +124,11 @@ public:
MSCommandParams cmd_params;
cmd_params
.WithEntity("http://172.16.1.132:5000/v1")
.WithParamsString("search")
.WithRequestParams("q=base")
.WithEntity("http://localhost:50010")
.WithParamsString("_mon/_stat")
//.WithRequestParams("q=base")
.WithHeadersMap(&headers);
MSRetStat retstat = p_rest_client_->Read(&cmd_params, &rest_response);
if(retstat.IsSuccess())
WriteObjectToResponse(pc_reqCtx, rest_response);
......@@ -379,10 +381,12 @@ int main(int argc, char *argv[])
.withMonitoring() // need to add reload
.withPubSub(NULL)
.withServiceDiscovery(NULL)
.addClient(ClientFactory::createHttpImplMsClient("other-service", "localhost", 32010, true, 10, false,"localhost:6379")) //new cMicroservice_Client(new MSICommandClientHttpImpl(),&clientParams))
.addClient(ClientFactory::createEvppCommandImpl("other-service", "localhost", 32010, true, 10, false,"localhost:6379"))
// .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("", 50010, 1))// .addRestServer(new cMicroservice_IRestServerCivetWebImpl(new cMicroservice_RestServerParams(50010,"",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"))
.addHandler("/zmq",new MSMsgQHandler())
......
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