Rename ErrorCode to Error; refine class SoapClient.

master
Adam Gu 8 years ago
parent 091f06e3a6
commit dc030207df

@ -18,8 +18,8 @@ const std::string kTextXmlUtf8 = "text/xml; charset=utf-8";
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
const char* GetErrorMessage(ErrorCode error_code) { const char* GetErrorMessage(Error error) {
switch (error_code) { switch (error) {
case kHostResolveError: case kHostResolveError:
return "Cannot resolve the host."; return "Cannot resolve the host.";

@ -35,7 +35,8 @@ const std::size_t kInvalidLength = std::string::npos;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
enum ErrorCode { // Error codes.
enum Error {
kNoError = 0, // OK kNoError = 0, // OK
kHostResolveError, kHostResolveError,
@ -59,7 +60,7 @@ enum ErrorCode {
}; };
// Return a descriptive message for the given error code. // Return a descriptive message for the given error code.
const char* GetErrorMessage(ErrorCode error_code); const char* GetErrorMessage(Error error);
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

@ -50,7 +50,7 @@ void Connection::HandleRead(boost::system::error_code ec,
return; return;
} }
ErrorCode error = request_parser_.Parse(buffer_.data(), bytes_transferred); Error error = request_parser_.Parse(buffer_.data(), bytes_transferred);
if (error != kNoError) { if (error != kNoError) {
// Bad request. // Bad request.

@ -56,8 +56,8 @@ HttpClient::HttpClient()
: timeout_seconds_(15) { : timeout_seconds_(15) {
} }
ErrorCode HttpClient::SendRequest(const HttpRequest& request, Error HttpClient::SendRequest(const HttpRequest& request,
HttpResponse* response) { HttpResponse* response) {
assert(response != NULL); assert(response != NULL);
using boost::asio::ip::tcp; using boost::asio::ip::tcp;
@ -71,6 +71,7 @@ ErrorCode HttpClient::SendRequest(const HttpRequest& request,
port = "80"; port = "80";
} }
// TODO: IPv4 or both IPv4 and IPv6
boost::system::error_code ec; boost::system::error_code ec;
tcp::resolver::results_type endpoints = tcp::resolver::results_type endpoints =
resolver.resolve(/*tcp::v4(), */request.host(), port, ec); resolver.resolve(/*tcp::v4(), */request.host(), port, ec);
@ -130,7 +131,7 @@ ErrorCode HttpClient::SendRequest(const HttpRequest& request,
// Parse the response piece just read. // Parse the response piece just read.
// If the content has been fully received, next time flag "finished_" // If the content has been fully received, next time flag "finished_"
// will be set. // will be set.
ErrorCode error = parser.Parse(buffer_.data(), length); Error error = parser.Parse(buffer_.data(), length);
if (error != kNoError) { if (error != kNoError) {
return error; return error;

@ -25,8 +25,8 @@ public:
} }
// Send an HTTP request, wait until the response is received. // Send an HTTP request, wait until the response is received.
ErrorCode SendRequest(const HttpRequest& request, Error SendRequest(const HttpRequest& request,
HttpResponse* response); HttpResponse* response);
private: private:
boost::asio::io_context io_context_; boost::asio::io_context io_context_;

@ -18,7 +18,7 @@ void HttpParser::Reset() {
// TODO: Reset parsing state. // TODO: Reset parsing state.
} }
ErrorCode HttpParser::Parse(const char* data, size_t len) { Error HttpParser::Parse(const char* data, size_t len) {
if (header_parsed_) { if (header_parsed_) {
// Add the data to the content. // Add the data to the content.
message_->AppendContent(data, len); message_->AppendContent(data, len);
@ -50,7 +50,7 @@ ErrorCode HttpParser::Parse(const char* data, size_t len) {
if (!start_line_parsed_) { if (!start_line_parsed_) {
start_line_parsed_ = true; start_line_parsed_ = true;
ErrorCode error = ParseStartLine(line); Error error = ParseStartLine(line);
if (error != kNoError) { if (error != kNoError) {
return error; return error;
} }

@ -21,11 +21,11 @@ public:
// Reset parsing state. // Reset parsing state.
void Reset(); void Reset();
ErrorCode Parse(const char* data, size_t len); Error Parse(const char* data, size_t len);
protected: protected:
// Parse HTTP start line. // Parse HTTP start line.
virtual ErrorCode ParseStartLine(const std::string& line) = 0; virtual Error ParseStartLine(const std::string& line) = 0;
void ParseContentLength(const std::string& line); void ParseContentLength(const std::string& line);
@ -33,7 +33,7 @@ protected:
// The result HTTP message. // The result HTTP message.
HttpMessage* message_; HttpMessage* message_;
ErrorCode error_; Error error_;
// Data waiting to be parsed. // Data waiting to be parsed.
std::string pending_data_; std::string pending_data_;

@ -12,7 +12,7 @@ HttpRequestParser::HttpRequestParser(HttpRequest* request)
, request_(request) { , request_(request) {
} }
ErrorCode HttpRequestParser::ParseStartLine(const std::string& line) { Error HttpRequestParser::ParseStartLine(const std::string& line) {
// Example: POST / HTTP/1.1 // Example: POST / HTTP/1.1
std::vector<std::string> strs; std::vector<std::string> strs;

@ -12,7 +12,7 @@ public:
explicit HttpRequestParser(HttpRequest* request); explicit HttpRequestParser(HttpRequest* request);
private: private:
ErrorCode ParseStartLine(const std::string& line) override; Error ParseStartLine(const std::string& line) override;
private: private:
HttpRequest* request_; HttpRequest* request_;

@ -10,7 +10,7 @@ HttpResponseParser::HttpResponseParser(HttpResponse* response)
} }
// TODO: Use split. // TODO: Use split.
ErrorCode HttpResponseParser::ParseStartLine(const std::string& line) { Error HttpResponseParser::ParseStartLine(const std::string& line) {
size_t off = 0; size_t off = 0;
size_t pos = line.find(' '); size_t pos = line.find(' ');

@ -13,7 +13,7 @@ public:
private: private:
// Parse HTTP start line; E.g., "HTTP/1.1 200 OK". // Parse HTTP start line; E.g., "HTTP/1.1 200 OK".
ErrorCode ParseStartLine(const std::string& line) override; Error ParseStartLine(const std::string& line) override;
private: private:
// The result response message. // The result response message.

@ -13,63 +13,57 @@
namespace csoap { namespace csoap {
bool SoapClient::Call(const std::string& operation, Error SoapClient::Call(const std::string& operation,
const csoap::Parameter* parameters, const Parameter* parameters,
size_t count, std::size_t count,
std::string* result) { std::string* result) {
assert(!url_.empty() && assert(!url_.empty() &&
!host_.empty() && !host_.empty() &&
!result_name_.empty() && !result_name_.empty() &&
service_ns_.IsValid()); service_ns_.IsValid());
csoap::SoapRequest soap_request; SoapRequest soap_request;
soap_request.set_soapenv_ns(kSoapEnvNamespace); soap_request.set_soapenv_ns(kSoapEnvNamespace); // TODO: Configurable
soap_request.set_service_ns(service_ns_); soap_request.set_service_ns(service_ns_);
soap_request.set_operation(operation); soap_request.set_operation(operation);
for (size_t i = 0; i < count; ++i) { for (std::size_t i = 0; i < count; ++i) {
soap_request.AddParameter(parameters[i]); soap_request.AddParameter(parameters[i]);
} }
std::string http_request_body; std::string http_request_body;
soap_request.ToXml(&http_request_body); soap_request.ToXml(&http_request_body);
csoap::HttpRequest http_request; HttpRequest http_request;
http_request.set_version(csoap::kHttpV11); http_request.set_version(kHttpV11); // TODO
http_request.set_url(url_); http_request.set_url(url_);
http_request.set_content_length(http_request_body.size()); http_request.set_content_length(http_request_body.size());
http_request.set_content(http_request_body); // TODO: move http_request.set_content(http_request_body); // TODO: move
http_request.set_host(host_, port_); http_request.set_host(host_, port_);
http_request.set_soap_action(operation); http_request.set_soap_action(operation);
csoap::HttpResponse http_response; HttpResponse http_response;
csoap::HttpClient http_client; HttpClient http_client;
csoap::ErrorCode ec = http_client.SendRequest(http_request, &http_response); Error error = http_client.SendRequest(http_request, &http_response);
if (ec != csoap::kNoError) {
std::cerr << csoap::GetErrorMessage(ec) << std::endl;
if (ec == csoap::kHttpStatusError) { if (error != kNoError) {
//std::cerr << "\t" return error;
// << http_response.status() << ", "
// << http_response.reason() << std::endl;
}
return false;
} }
csoap::SoapResponse soap_response; SoapResponse soap_response;
soap_response.set_result_name(result_name_); soap_response.set_result_name(result_name_);
if (soap_response.FromXml(http_response.content())) { if (!soap_response.FromXml(http_response.content())) {
*result = soap_response.result(); return kXmlError; // TODO: Some SOAP error?
return true;
} }
return false; *result = soap_response.result();
return kNoError;
} }
} // namespace csoap } // namespace csoap

@ -19,10 +19,10 @@ protected:
} }
// A generic wrapper to make a call. // A generic wrapper to make a call.
bool Call(const std::string& operation, Error Call(const std::string& operation,
const csoap::Parameter* parameters, const Parameter* parameters,
size_t count, std::size_t count,
std::string* result); std::string* result);
protected: protected:
// Request URL. // Request URL.

@ -55,7 +55,11 @@ bool CalculatorClient::Calc(const std::string& operation,
}; };
std::string result_str; std::string result_str;
if (!Call(operation, parameters, 2, &result_str)) { csoap::Error error = Call(operation, parameters, 2, &result_str);
if (error != csoap::kNoError) {
std::cerr << "Error: " << error;
std::cerr << ", " << csoap::GetErrorMessage(error) << std::endl;
return false; return false;
} }

@ -9,7 +9,7 @@ CsdmClient::CsdmClient() {
} }
bool CsdmClient::GetVersion(std::string* result_xml) { bool CsdmClient::GetVersion(std::string* result_xml) {
return Call("getVersion", NULL, 0, result_xml); return Call0("getVersion", result_xml);
} }
void CsdmClient::Init() { void CsdmClient::Init() {
@ -20,7 +20,12 @@ void CsdmClient::Init() {
host_ = "127.0.0.1"; host_ = "127.0.0.1";
port_ = "52540"; port_ = "52540";
result_name_ = "Result"; result_name_ = "return";
}
bool CsdmClient::Call0(const std::string& operation,
std::string* result_xml) {
return CallEx(operation, NULL, 0, result_xml);
} }
bool CsdmClient::Call1(const std::string& operation, bool CsdmClient::Call1(const std::string& operation,
@ -31,5 +36,27 @@ bool CsdmClient::Call1(const std::string& operation,
{ name, value }, { name, value },
}; };
return Call(operation, parameters, 1, result_xml); return CallEx(operation, parameters, 1, result_xml);
}
bool CsdmClient::Call2(const std::string& operation,
const std::string& name1,
const std::string& value1,
const std::string& name2,
const std::string& value2,
std::string* result_xml) {
csoap::Parameter parameters[] = {
{ name1, value1 },
{ name2, value2 },
};
return CallEx(operation, parameters, 2, result_xml);
}
bool CsdmClient::CallEx(const std::string& operation,
const csoap::Parameter* parameters,
std::size_t count,
std::string* result) {
csoap::Error error = Call(operation, parameters, count, result);
return error == csoap::kNoError;
} }

@ -15,11 +15,29 @@ public:
protected: protected:
void Init(); void Init();
// Call CSDM API with NO parameter.
bool Call0(const std::string& operation,
std::string* result_xml);
// Call CSDM API with one parameter. // Call CSDM API with one parameter.
bool Call1(const std::string& operation, bool Call1(const std::string& operation,
const std::string& name, const std::string& name,
const std::string& value, const std::string& value,
std::string* result_xml); std::string* result_xml);
// Call CSDM API with two parameters.
bool Call2(const std::string& operation,
const std::string& name1,
const std::string& value1,
const std::string& name2,
const std::string& value2,
std::string* result_xml);
// A wrapper of CSoapClient::Call().
bool CallEx(const std::string& operation,
const csoap::Parameter* parameters,
std::size_t count,
std::string* result);
}; };
#endif // CSDM_CLIENT_H_ #endif // CSDM_CLIENT_H_

Loading…
Cancel
Save