Rename ErrorCode to Error; refine class SoapClient.
This commit is contained in:
+2
-2
@@ -18,8 +18,8 @@ const std::string kTextXmlUtf8 = "text/xml; charset=utf-8";
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const char* GetErrorMessage(ErrorCode error_code) {
|
||||
switch (error_code) {
|
||||
const char* GetErrorMessage(Error error) {
|
||||
switch (error) {
|
||||
case kHostResolveError:
|
||||
return "Cannot resolve the host.";
|
||||
|
||||
|
||||
+3
-2
@@ -35,7 +35,8 @@ const std::size_t kInvalidLength = std::string::npos;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
enum ErrorCode {
|
||||
// Error codes.
|
||||
enum Error {
|
||||
kNoError = 0, // OK
|
||||
|
||||
kHostResolveError,
|
||||
@@ -59,7 +60,7 @@ enum ErrorCode {
|
||||
};
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
ErrorCode error = request_parser_.Parse(buffer_.data(), bytes_transferred);
|
||||
Error error = request_parser_.Parse(buffer_.data(), bytes_transferred);
|
||||
|
||||
if (error != kNoError) {
|
||||
// Bad request.
|
||||
|
||||
@@ -56,7 +56,7 @@ HttpClient::HttpClient()
|
||||
: timeout_seconds_(15) {
|
||||
}
|
||||
|
||||
ErrorCode HttpClient::SendRequest(const HttpRequest& request,
|
||||
Error HttpClient::SendRequest(const HttpRequest& request,
|
||||
HttpResponse* response) {
|
||||
assert(response != NULL);
|
||||
|
||||
@@ -71,6 +71,7 @@ ErrorCode HttpClient::SendRequest(const HttpRequest& request,
|
||||
port = "80";
|
||||
}
|
||||
|
||||
// TODO: IPv4 or both IPv4 and IPv6
|
||||
boost::system::error_code ec;
|
||||
tcp::resolver::results_type endpoints =
|
||||
resolver.resolve(/*tcp::v4(), */request.host(), port, ec);
|
||||
@@ -130,7 +131,7 @@ ErrorCode HttpClient::SendRequest(const HttpRequest& request,
|
||||
// Parse the response piece just read.
|
||||
// If the content has been fully received, next time flag "finished_"
|
||||
// will be set.
|
||||
ErrorCode error = parser.Parse(buffer_.data(), length);
|
||||
Error error = parser.Parse(buffer_.data(), length);
|
||||
|
||||
if (error != kNoError) {
|
||||
return error;
|
||||
|
||||
@@ -25,7 +25,7 @@ public:
|
||||
}
|
||||
|
||||
// Send an HTTP request, wait until the response is received.
|
||||
ErrorCode SendRequest(const HttpRequest& request,
|
||||
Error SendRequest(const HttpRequest& request,
|
||||
HttpResponse* response);
|
||||
|
||||
private:
|
||||
|
||||
@@ -18,7 +18,7 @@ void HttpParser::Reset() {
|
||||
// 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_) {
|
||||
// Add the data to the content.
|
||||
message_->AppendContent(data, len);
|
||||
@@ -50,7 +50,7 @@ ErrorCode HttpParser::Parse(const char* data, size_t len) {
|
||||
|
||||
if (!start_line_parsed_) {
|
||||
start_line_parsed_ = true;
|
||||
ErrorCode error = ParseStartLine(line);
|
||||
Error error = ParseStartLine(line);
|
||||
if (error != kNoError) {
|
||||
return error;
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@ public:
|
||||
// Reset parsing state.
|
||||
void Reset();
|
||||
|
||||
ErrorCode Parse(const char* data, size_t len);
|
||||
Error Parse(const char* data, size_t len);
|
||||
|
||||
protected:
|
||||
// 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);
|
||||
|
||||
@@ -33,7 +33,7 @@ protected:
|
||||
// The result HTTP message.
|
||||
HttpMessage* message_;
|
||||
|
||||
ErrorCode error_;
|
||||
Error error_;
|
||||
|
||||
// Data waiting to be parsed.
|
||||
std::string pending_data_;
|
||||
|
||||
@@ -12,7 +12,7 @@ HttpRequestParser::HttpRequestParser(HttpRequest* request)
|
||||
, request_(request) {
|
||||
}
|
||||
|
||||
ErrorCode HttpRequestParser::ParseStartLine(const std::string& line) {
|
||||
Error HttpRequestParser::ParseStartLine(const std::string& line) {
|
||||
// Example: POST / HTTP/1.1
|
||||
|
||||
std::vector<std::string> strs;
|
||||
|
||||
@@ -12,7 +12,7 @@ public:
|
||||
explicit HttpRequestParser(HttpRequest* request);
|
||||
|
||||
private:
|
||||
ErrorCode ParseStartLine(const std::string& line) override;
|
||||
Error ParseStartLine(const std::string& line) override;
|
||||
|
||||
private:
|
||||
HttpRequest* request_;
|
||||
|
||||
@@ -10,7 +10,7 @@ HttpResponseParser::HttpResponseParser(HttpResponse* response)
|
||||
}
|
||||
|
||||
// TODO: Use split.
|
||||
ErrorCode HttpResponseParser::ParseStartLine(const std::string& line) {
|
||||
Error HttpResponseParser::ParseStartLine(const std::string& line) {
|
||||
size_t off = 0;
|
||||
|
||||
size_t pos = line.find(' ');
|
||||
|
||||
@@ -13,7 +13,7 @@ public:
|
||||
|
||||
private:
|
||||
// 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:
|
||||
// The result response message.
|
||||
|
||||
+19
-25
@@ -13,63 +13,57 @@
|
||||
|
||||
namespace csoap {
|
||||
|
||||
bool SoapClient::Call(const std::string& operation,
|
||||
const csoap::Parameter* parameters,
|
||||
size_t count,
|
||||
Error SoapClient::Call(const std::string& operation,
|
||||
const Parameter* parameters,
|
||||
std::size_t count,
|
||||
std::string* result) {
|
||||
assert(!url_.empty() &&
|
||||
!host_.empty() &&
|
||||
!result_name_.empty() &&
|
||||
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_operation(operation);
|
||||
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
soap_request.AddParameter(parameters[i]);
|
||||
}
|
||||
|
||||
std::string 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_content_length(http_request_body.size());
|
||||
http_request.set_content(http_request_body); // TODO: move
|
||||
http_request.set_host(host_, port_);
|
||||
http_request.set_soap_action(operation);
|
||||
|
||||
csoap::HttpResponse http_response;
|
||||
HttpResponse http_response;
|
||||
|
||||
csoap::HttpClient http_client;
|
||||
csoap::ErrorCode ec = http_client.SendRequest(http_request, &http_response);
|
||||
if (ec != csoap::kNoError) {
|
||||
std::cerr << csoap::GetErrorMessage(ec) << std::endl;
|
||||
HttpClient http_client;
|
||||
Error error = http_client.SendRequest(http_request, &http_response);
|
||||
|
||||
if (ec == csoap::kHttpStatusError) {
|
||||
//std::cerr << "\t"
|
||||
// << http_response.status() << ", "
|
||||
// << http_response.reason() << std::endl;
|
||||
if (error != kNoError) {
|
||||
return error;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
csoap::SoapResponse soap_response;
|
||||
SoapResponse soap_response;
|
||||
soap_response.set_result_name(result_name_);
|
||||
|
||||
if (soap_response.FromXml(http_response.content())) {
|
||||
*result = soap_response.result();
|
||||
return true;
|
||||
if (!soap_response.FromXml(http_response.content())) {
|
||||
return kXmlError; // TODO: Some SOAP error?
|
||||
}
|
||||
|
||||
return false;
|
||||
*result = soap_response.result();
|
||||
|
||||
return kNoError;
|
||||
}
|
||||
|
||||
} // namespace csoap
|
||||
|
||||
@@ -19,9 +19,9 @@ protected:
|
||||
}
|
||||
|
||||
// A generic wrapper to make a call.
|
||||
bool Call(const std::string& operation,
|
||||
const csoap::Parameter* parameters,
|
||||
size_t count,
|
||||
Error Call(const std::string& operation,
|
||||
const Parameter* parameters,
|
||||
std::size_t count,
|
||||
std::string* result);
|
||||
|
||||
protected:
|
||||
|
||||
@@ -55,7 +55,11 @@ bool CalculatorClient::Calc(const std::string& operation,
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ CsdmClient::CsdmClient() {
|
||||
}
|
||||
|
||||
bool CsdmClient::GetVersion(std::string* result_xml) {
|
||||
return Call("getVersion", NULL, 0, result_xml);
|
||||
return Call0("getVersion", result_xml);
|
||||
}
|
||||
|
||||
void CsdmClient::Init() {
|
||||
@@ -20,7 +20,12 @@ void CsdmClient::Init() {
|
||||
host_ = "127.0.0.1";
|
||||
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,
|
||||
@@ -31,5 +36,27 @@ bool CsdmClient::Call1(const std::string& operation,
|
||||
{ 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:
|
||||
void Init();
|
||||
|
||||
// Call CSDM API with NO parameter.
|
||||
bool Call0(const std::string& operation,
|
||||
std::string* result_xml);
|
||||
|
||||
// Call CSDM API with one parameter.
|
||||
bool Call1(const std::string& operation,
|
||||
const std::string& name,
|
||||
const std::string& value,
|
||||
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_
|
||||
|
||||
Reference in New Issue
Block a user