Update readme
This commit is contained in:
@@ -31,7 +31,7 @@ public:
|
||||
http_request.Build();
|
||||
|
||||
webcc::HttpClient http_client;
|
||||
webcc::Error error = http_client.SendRequest(http_request, http_response);
|
||||
webcc::Error error = http_client.MakeRequest(http_request, http_response);
|
||||
|
||||
return error == webcc::kNoError;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#include <iostream>
|
||||
#include "boost/lexical_cast.hpp"
|
||||
|
||||
// Set to 0 to test our own calculator server created with webcc.
|
||||
#define ACCESS_PARASOFT 0
|
||||
|
||||
CalcClient::CalcClient() {
|
||||
Init();
|
||||
}
|
||||
@@ -19,13 +22,22 @@ bool CalcClient::Multiply(double x, double y, double* result) {
|
||||
}
|
||||
|
||||
bool CalcClient::Divide(double x, double y, double* result) {
|
||||
// ParaSoft's Calculator Service uses different parameter names for Divide.
|
||||
#if ACCESS_PARASOFT
|
||||
return Calc("divide", "numerator", "denominator", x, y, result);
|
||||
#else
|
||||
return Calc("divide", "x", "y", x, y, result);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Set to 0 to test our own calculator server created with webcc.
|
||||
#define ACCESS_PARASOFT 0
|
||||
bool CalcClient::NotExist(double x, double y, double* result) {
|
||||
return Calc("notexist", "x", "y", x, y, result);
|
||||
}
|
||||
|
||||
void CalcClient::Init() {
|
||||
// Override the default timeout.
|
||||
timeout_seconds_ = 5;
|
||||
|
||||
#if ACCESS_PARASOFT
|
||||
url_ = "/glue/calculator";
|
||||
host_ = "ws1.parasoft.com";
|
||||
|
||||
@@ -16,6 +16,9 @@ public:
|
||||
|
||||
bool Divide(double x, double y, double* result);
|
||||
|
||||
// For testing purpose.
|
||||
bool NotExist(double x, double y, double* result);
|
||||
|
||||
protected:
|
||||
void Init();
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ int main() {
|
||||
printf("add: %.1f\n", result);
|
||||
}
|
||||
|
||||
#if 0
|
||||
if (calc.Subtract(x, y, &result)) {
|
||||
printf("subtract: %.1f\n", result);
|
||||
}
|
||||
@@ -24,7 +23,11 @@ int main() {
|
||||
if (calc.Divide(x, y, &result)) {
|
||||
printf("divide: %.1f\n", result);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Try to call a non-existing operation.
|
||||
if (calc.NotExist(x, y, &result)) {
|
||||
printf("notexist: %.1f\n", result);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,36 +1,94 @@
|
||||
#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) {
|
||||
try {
|
||||
if (soap_request.operation() == "add") {
|
||||
double x = boost::lexical_cast<double>(soap_request.GetParameter("x"));
|
||||
double y = boost::lexical_cast<double>(soap_request.GetParameter("y"));
|
||||
|
||||
double result = 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));
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
// NOT_IMPLEMENTED
|
||||
}
|
||||
} catch (boost::bad_lexical_cast&) {
|
||||
// BAD_REQUEST
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
if (!GetParameters(soap_request, &x, &y)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ public:
|
||||
|
||||
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,4 +1,5 @@
|
||||
#include <iostream>
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/soap_server.h"
|
||||
#include "calc_service.h"
|
||||
|
||||
@@ -14,6 +15,8 @@ int main(int argc, char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::VERB, 0);
|
||||
|
||||
unsigned short port = std::atoi(argv[1]);
|
||||
|
||||
std::size_t workers = 2;
|
||||
|
||||
Reference in New Issue
Block a user