Add async-client support; refine http message dump format.
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
file(GLOB SRCS *.cc *.h)
|
||||
|
||||
add_executable(soap_calc_server ${SRCS})
|
||||
|
||||
target_link_libraries(soap_calc_server webcc pugixml ${Boost_LIBRARIES})
|
||||
target_link_libraries(soap_calc_server "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -1,94 +0,0 @@
|
||||
#include "calc_service.h"
|
||||
|
||||
// Sleep several seconds for the client to test timeout control.
|
||||
#define SLEEP_FOR_TIMEOUT_TEST 0
|
||||
|
||||
#include "boost/lexical_cast.hpp"
|
||||
|
||||
#if SLEEP_FOR_TIMEOUT_TEST
|
||||
#include "boost/thread/thread.hpp"
|
||||
#endif
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/soap_request.h"
|
||||
#include "webcc/soap_response.h"
|
||||
|
||||
#if SLEEP_FOR_TIMEOUT_TEST
|
||||
static const int kSleepSeconds = 3;
|
||||
#endif
|
||||
|
||||
bool CalcService::Handle(const webcc::SoapRequest& soap_request,
|
||||
webcc::SoapResponse* soap_response) {
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
if (!GetParameters(soap_request, &x, &y)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& op = soap_request.operation();
|
||||
|
||||
LOG_INFO("Soap operation '%s': %.2f, %.2f", op.c_str(), x, y);
|
||||
|
||||
std::function<double(double, double)> calc;
|
||||
|
||||
if (op == "add") {
|
||||
calc = [](double x, double y) { return x + y; };
|
||||
|
||||
} else if (op == "subtract") {
|
||||
calc = [](double x, double y) { return x - y; };
|
||||
|
||||
} else if (op == "multiply") {
|
||||
calc = [](double x, double y) { return x * y; };
|
||||
|
||||
} else if (op == "divide") {
|
||||
calc = [](double x, double y) { return x / y; };
|
||||
|
||||
if (y == 0.0) {
|
||||
LOG_ERRO("Cannot divide by 0.");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
LOG_ERRO("Operation '%s' is not supported.", op.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!calc) {
|
||||
return false;
|
||||
}
|
||||
|
||||
double result = calc(x, y);
|
||||
|
||||
soap_response->set_soapenv_ns(webcc::kSoapEnvNamespace);
|
||||
soap_response->set_service_ns({
|
||||
"cal",
|
||||
"http://www.example.com/calculator/"
|
||||
});
|
||||
|
||||
soap_response->set_operation(soap_request.operation());
|
||||
|
||||
soap_response->set_result_name("Result");
|
||||
soap_response->set_result(std::to_string(result));
|
||||
|
||||
#if SLEEP_FOR_TIMEOUT_TEST
|
||||
LOG_INFO("Sleep %d seconds for the client to test timeout control.",
|
||||
kSleepSeconds);
|
||||
boost::this_thread::sleep_for(boost::chrono::seconds(kSleepSeconds));
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CalcService::GetParameters(const webcc::SoapRequest& soap_request,
|
||||
double* x,
|
||||
double* y) {
|
||||
try {
|
||||
*x = boost::lexical_cast<double>(soap_request.GetParameter("x"));
|
||||
*y = boost::lexical_cast<double>(soap_request.GetParameter("y"));
|
||||
|
||||
} catch (boost::bad_lexical_cast& e) {
|
||||
LOG_ERRO("Parameter cast error: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
#ifndef CALC_SERVICE_H_
|
||||
#define CALC_SERVICE_H_
|
||||
|
||||
#include "webcc/soap_service.h"
|
||||
|
||||
class CalcService : public webcc::SoapService {
|
||||
public:
|
||||
CalcService() = default;
|
||||
~CalcService() override = default;
|
||||
|
||||
bool Handle(const webcc::SoapRequest& soap_request,
|
||||
webcc::SoapResponse* soap_response) override;
|
||||
|
||||
private:
|
||||
bool GetParameters(const webcc::SoapRequest& soap_request,
|
||||
double* x,
|
||||
double* y);
|
||||
};
|
||||
|
||||
#endif // CALC_SERVICE_H_
|
||||
@@ -1,37 +0,0 @@
|
||||
#include <iostream>
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/soap_server.h"
|
||||
#include "calc_service.h"
|
||||
|
||||
static void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <port>" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " 8080" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 2) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::INFO, 0);
|
||||
|
||||
unsigned short port = std::atoi(argv[1]);
|
||||
|
||||
std::size_t workers = 2;
|
||||
|
||||
try {
|
||||
webcc::SoapServer server(port, workers);
|
||||
|
||||
server.Bind(std::make_shared<CalcService>(), "/calculator");
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user