Add async-client support; refine http message dump format.

This commit is contained in:
Adam Gu
2018-06-04 12:03:22 +08:00
parent 79665c75ba
commit 9bf45e6ecb
48 changed files with 798 additions and 402 deletions
+7
View File
@@ -0,0 +1,7 @@
file(GLOB SRCS *.cc *.h)
add_executable(soap_calc_client ${SRCS})
target_link_libraries(soap_calc_client webcc pugixml ${Boost_LIBRARIES})
target_link_libraries(soap_calc_client "${CMAKE_THREAD_LIBS_INIT}")
+89
View File
@@ -0,0 +1,89 @@
#include "calc_client.h"
#include <iostream>
#include "boost/lexical_cast.hpp"
#include "webcc/logger.h"
// Set to 0 to test our own calculator server created with webcc.
#define ACCESS_PARASOFT 0
CalcClient::CalcClient() {
Init();
}
bool CalcClient::Add(double x, double y, double* result) {
return Calc("add", "x", "y", x, y, result);
}
bool CalcClient::Subtract(double x, double y, double* result) {
return Calc("subtract", "x", "y", x, y, result);
}
bool CalcClient::Multiply(double x, double y, double* result) {
return Calc("multiply", "x", "y", x, y, 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
}
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";
port_ = ""; // Default to "80".
service_ns_ = { "cal", "http://www.parasoft.com/wsdl/calculator/" };
result_name_ = "Result";
#else
url_ = "/calculator";
host_ = "localhost";
port_ = "8080";
service_ns_ = { "ser", "http://www.example.com/calculator/" };
result_name_ = "Result";
#endif
}
bool CalcClient::Calc(const std::string& operation,
const std::string& x_name,
const std::string& y_name,
double x,
double y,
double* result) {
// Prepare parameters.
std::vector<webcc::Parameter> parameters{
{ x_name, x },
{ y_name, y }
};
// Make the call.
std::string result_str;
webcc::Error error = Call(operation, std::move(parameters), &result_str);
// Error handling if any.
if (error != webcc::kNoError) {
LOG_ERRO("Operation '%s' failed: %s",
operation.c_str(),
webcc::DescribeError(error));
return false;
}
// Convert the result from string to double.
try {
*result = boost::lexical_cast<double>(result_str);
} catch (boost::bad_lexical_cast&) {
return false;
}
return true;
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef CALC_CLIENT_H_
#define CALC_CLIENT_H_
#include <string>
#include "webcc/soap_client.h"
class CalcClient : public webcc::SoapClient {
public:
CalcClient();
bool Add(double x, double y, double* result);
bool Subtract(double x, double y, double* result);
bool Multiply(double x, double y, double* result);
bool Divide(double x, double y, double* result);
// For testing purpose.
bool NotExist(double x, double y, double* result);
protected:
void Init();
// A more concrete wrapper to make a call.
bool Calc(const std::string& operation,
const std::string& x_name,
const std::string& y_name,
double x,
double y,
double* result);
};
#endif // CALC_CLIENT_H_
+250
View File
@@ -0,0 +1,250 @@
<?xml version="1.0" encoding="US-ASCII"?>
<!--generated by GLUE Standard 4.1.2 on Fri Nov 21 13:50:48 PST 2003-->
<wsdl:definitions name="Calculator"
targetNamespace="http://www.parasoft.com/wsdl/calculator/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tme="http://www.themindelectric.com/"
xmlns:tns="http://www.parasoft.com/wsdl/calculator/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xsd:schema
elementFormDefault="qualified"
targetNamespace="http://www.parasoft.com/wsdl/calculator/">
<xsd:element
name="add">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="x" type="xsd:float"/>
<xsd:element name="y"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="addResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="Result"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="divide">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="numerator" type="xsd:float"/>
<xsd:element
name="denominator"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="divideResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="Result"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="multiply">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="x" type="xsd:float"/>
<xsd:element name="y"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="multiplyResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="Result"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="subtract">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="x" type="xsd:float"/>
<xsd:element name="y"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element
name="subtractResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="Result"
type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message
name="add0In">
<wsdl:part element="tns:add"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="add0Out">
<wsdl:part element="tns:addResponse"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="divide1In">
<wsdl:part element="tns:divide"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="divide1Out">
<wsdl:part element="tns:divideResponse"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="multiply2In">
<wsdl:part element="tns:multiply"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="multiply2Out">
<wsdl:part element="tns:multiplyResponse"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="subtract3In">
<wsdl:part element="tns:subtract"
name="parameters"/>
</wsdl:message>
<wsdl:message
name="subtract3Out">
<wsdl:part element="tns:subtractResponse"
name="parameters"/>
</wsdl:message>
<wsdl:portType
name="ICalculator">
<wsdl:operation name="add"
parameterOrder="x y">
<wsdl:input message="tns:add0In"
name="add0In"/>
<wsdl:output message="tns:add0Out"
name="add0Out"/>
</wsdl:operation>
<wsdl:operation name="divide"
parameterOrder="numerator denominator">
<wsdl:input
message="tns:divide1In" name="divide1In"/>
<wsdl:output
message="tns:divide1Out"
name="divide1Out"/>
</wsdl:operation>
<wsdl:operation
name="multiply" parameterOrder="x y">
<wsdl:input
message="tns:multiply2In" name="multiply2In"/>
<wsdl:output
message="tns:multiply2Out"
name="multiply2Out"/>
</wsdl:operation>
<wsdl:operation
name="subtract" parameterOrder="x y">
<wsdl:input
message="tns:subtract3In" name="subtract3In"/>
<wsdl:output
message="tns:subtract3Out"
name="subtract3Out"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding
name="ICalculator" type="tns:ICalculator">
<soap:binding
style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation
name="add">
<soap:operation soapAction="add"
style="document"/>
<wsdl:input name="add0In">
<soap:body
use="literal"/>
</wsdl:input>
<wsdl:output
name="add0Out">
<soap:body
use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation
name="divide">
<soap:operation soapAction="divide"
style="document"/>
<wsdl:input name="divide1In">
<soap:body
use="literal"/>
</wsdl:input>
<wsdl:output
name="divide1Out">
<soap:body
use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation
name="multiply">
<soap:operation soapAction="multiply"
style="document"/>
<wsdl:input name="multiply2In">
<soap:body
use="literal"/>
</wsdl:input>
<wsdl:output
name="multiply2Out">
<soap:body
use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation
name="subtract">
<soap:operation soapAction="subtract"
style="document"/>
<wsdl:input name="subtract3In">
<soap:body
use="literal"/>
</wsdl:input>
<wsdl:output
name="subtract3Out">
<soap:body
use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service
name="Calculator">
<wsdl:documentation>instance of class webtool.soap.examples.calculator.Calculator</wsdl:documentation>
<wsdl:port
binding="tns:ICalculator" name="ICalculator">
<soap:address
location="http://ws1.parasoft.com/glue/calculator"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
+36
View File
@@ -0,0 +1,36 @@
#include <iostream>
#include "webcc/logger.h"
#include "calc_client.h"
int main() {
LOG_INIT(webcc::VERB, 0);
CalcClient calc;
double x = 1.0;
double y = 2.0;
double result = 0.0;
if (calc.Add(x, y, &result)) {
printf("add: %.1f\n", result);
}
if (calc.Subtract(x, y, &result)) {
printf("subtract: %.1f\n", result);
}
if (calc.Multiply(x, y, &result)) {
printf("multiply: %.1f\n", result);
}
if (calc.Divide(x, y, &result)) {
printf("divide: %.1f\n", result);
}
// Try to call a non-existing operation.
if (calc.NotExist(x, y, &result)) {
printf("notexist: %.1f\n", result);
}
return 0;
}
+6
View File
@@ -0,0 +1,6 @@
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}")
+94
View File
@@ -0,0 +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) {
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;
}
+20
View File
@@ -0,0 +1,20 @@
#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_
+39
View File
@@ -0,0 +1,39 @@
#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;
}