forked from lizan/service-control-client-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I6bccc8f52b3e920334a850aff1c9a19862207fd2
- Loading branch information
1 parent
d8ac404
commit 8d0c759
Showing
4 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
licenses(["notice"]) | ||
|
||
cc_library( | ||
name = "http_transport", | ||
srcs = [ | ||
"transport/http_transport.cc", | ||
], | ||
hdrs = [ | ||
"transport/http_transport.h", | ||
], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@//:service_control_client_lib", | ||
"@//third_party/config:servicecontrol", | ||
], | ||
) | ||
|
||
cc_binary( | ||
name = "http_sample", | ||
srcs = [ | ||
"transport/http_sample.cc", | ||
], | ||
visibility = ["//visibility:public"], | ||
linkopts = [ | ||
"-lcurl", | ||
], | ||
deps = [ | ||
":http_transport", | ||
"@//:service_control_client_lib", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* Copyright 2016 Google Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
#include <fstream> | ||
#include <future> | ||
#include <iostream> | ||
#include "google/api/servicecontrol/v1/service_controller.pb.h" | ||
#include "google/protobuf/stubs/logging.h" | ||
#include "google/protobuf/stubs/logging.h" | ||
#include "google/protobuf/stubs/status.h" | ||
#include "google/protobuf/text_format.h" | ||
#include "include/service_control_client.h" | ||
#include "sample/transport/http_transport.h" | ||
|
||
using ::google::api::servicecontrol::v1::CheckRequest; | ||
using ::google::api::servicecontrol::v1::CheckResponse; | ||
using ::google::api::servicecontrol::v1::ReportRequest; | ||
using ::google::api::servicecontrol::v1::ReportResponse; | ||
using ::google::service_control_client::CheckAggregationOptions; | ||
using ::google::service_control_client::ReportAggregationOptions; | ||
using ::google::service_control_client::ServiceControlClient; | ||
using ::google::service_control_client::ServiceControlClientOptions; | ||
using ::google::service_control_client::TransportDoneFunc; | ||
using ::google::service_control_client::sample::transport::LibCurlTransport; | ||
using ::google::protobuf::util::Status; | ||
using ::google::protobuf::TextFormat; | ||
|
||
const char kCheckRequest[] = R"( | ||
service_name: "echo-dot-esp-load-test.appspot.com" | ||
operation { | ||
operation_id: "abced" | ||
operation_name: "EchoGetMessageAuthed" | ||
consumer_id: "project:esp-load-test" | ||
start_time: { | ||
seconds: 1000 | ||
nanos: 2000 | ||
} | ||
end_time: { | ||
seconds: 3000 | ||
nanos: 4000 | ||
} | ||
} | ||
)"; | ||
|
||
const char kReportRequest[] = R"( | ||
service_name: "echo-dot-esp-load-test.appspot.com" | ||
operations: { | ||
operation_id: "operation-1" | ||
operation_name: "EchoGetMessageAuthed" | ||
consumer_id: "project:esp-load-test" | ||
start_time { | ||
seconds: 1000 | ||
nanos: 2000 | ||
} | ||
end_time { | ||
seconds: 3000 | ||
nanos: 4000 | ||
} | ||
} | ||
)"; | ||
|
||
void print_usage() { | ||
fprintf(stderr, | ||
"Missing Argument.\n" | ||
"Usage: http_sample server_url service_name auth_token.\n" | ||
"auth_token can be obtained by one of the followings: \n" | ||
"1) Fetching from GCP metadata server if this code is running " | ||
"inside a Google Cloud Platform VM. \n" | ||
"2) Generating every one hour with service account secret file.\n" | ||
" a) Create service account and download client-secret-file: \n" | ||
" Google Cloud Console -> IAM & ADMIN -> Service Accounts \n" | ||
" -> Create Service Account (with editor role)\n" | ||
" b) Generate auth token: \n" | ||
" gen-auth-token.sh\n" | ||
" -s client-secret-file\n" | ||
" -a " | ||
"https://servicecontrol.googleapis.com/" | ||
"google.api.servicecontrol.v1.ServiceController\n"); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
if (argc <= 3) { | ||
print_usage(); | ||
return 1; | ||
} | ||
|
||
std::string server_url = argv[1]; | ||
std::string service_name = argv[2]; | ||
std::string auth_token = argv[3]; | ||
|
||
CheckRequest check_request; | ||
ReportRequest report_request; | ||
CheckResponse check_response; | ||
ReportResponse report_response; | ||
::google::protobuf::TextFormat::ParseFromString(kCheckRequest, | ||
&check_request); | ||
::google::protobuf::TextFormat::ParseFromString(kReportRequest, | ||
&report_request); | ||
|
||
// Initialize the sample transport. | ||
LibCurlTransport* transport = | ||
new LibCurlTransport(server_url, service_name, auth_token); | ||
|
||
// Initialize service control client. | ||
std::unique_ptr<ServiceControlClient> client; | ||
// Both check cache and report cache has been disabled. So each check/report | ||
// call at client will use the transport to call remote. | ||
ServiceControlClientOptions options( | ||
CheckAggregationOptions(-1 /*entries*/, 1000 /* refresh_interval_ms */, | ||
1000 /*flush_interval_ms*/), | ||
ReportAggregationOptions(-1 /*entries*/, 1000 /* refresh_interval_ms */)); | ||
|
||
options.check_transport = [transport](const CheckRequest& check_request, | ||
CheckResponse* check_response, | ||
TransportDoneFunc on_done) { | ||
transport->Check(check_request, check_response, on_done); | ||
}; | ||
options.report_transport = [transport](const ReportRequest& report_request, | ||
ReportResponse* report_response, | ||
TransportDoneFunc on_done) { | ||
transport->Report(report_request, report_response, on_done); | ||
}; | ||
|
||
client = CreateServiceControlClient(service_name, options); | ||
|
||
// Call Check. | ||
std::promise<Status> check_promise_status; | ||
std::future<Status> check_future_status = check_promise_status.get_future(); | ||
client->Check(check_request, &check_response, [&check_promise_status]( | ||
const Status& status) { | ||
std::cout << "status is :" << status.ToString() << std::endl; | ||
std::promise<Status> moved_promise(std::move(check_promise_status)); | ||
moved_promise.set_value(status); | ||
}); | ||
|
||
// Call Report. | ||
std::promise<Status> report_promise_status; | ||
std::future<Status> report_future_status = report_promise_status.get_future(); | ||
client->Report(report_request, &report_response, [&report_promise_status]( | ||
const Status& status) { | ||
std::cout << "status is :" << status.ToString() << std::endl; | ||
std::promise<Status> moved_promise(std::move(report_promise_status)); | ||
moved_promise.set_value(status); | ||
}); | ||
|
||
check_future_status.wait(); | ||
report_future_status.wait(); | ||
|
||
std::cout << "check response is:" << check_response.DebugString() | ||
<< std::endl; | ||
std::cout << "report response is:" << report_response.DebugString() | ||
<< std::endl; | ||
|
||
delete transport; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* Copyright 2016 Google Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
#include "sample/transport/http_transport.h" | ||
#include <curl/curl.h> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
#include <thread> | ||
|
||
using ::google::api::servicecontrol::v1::CheckRequest; | ||
using ::google::api::servicecontrol::v1::CheckResponse; | ||
using ::google::api::servicecontrol::v1::ReportRequest; | ||
using ::google::api::servicecontrol::v1::ReportResponse; | ||
|
||
using ::google::protobuf::util::error::Code; | ||
using ::google::protobuf::util::Status; | ||
|
||
namespace google { | ||
namespace service_control_client { | ||
namespace sample { | ||
namespace transport { | ||
namespace { | ||
|
||
Status ConvertHttpCodeToStatus(const long &http_code) { | ||
Status status; | ||
std::cout << "http_code is: " << http_code << std::endl; | ||
switch (http_code) { | ||
case 400: | ||
status = Status(Code::INVALID_ARGUMENT, std::string("Bad Request.")); | ||
case 403: | ||
status = | ||
Status(Code::PERMISSION_DENIED, std::string("Permission Denied.")); | ||
case 404: | ||
status = Status(Code::NOT_FOUND, std::string("Not Found.")); | ||
case 409: | ||
status = Status(Code::ABORTED, std::string("Conflict.")); | ||
case 416: | ||
status = Status(Code::OUT_OF_RANGE, | ||
std::string("Requested Range Not Satisfiable.")); | ||
case 429: | ||
status = | ||
Status(Code::RESOURCE_EXHAUSTED, std::string("Too Many Requests.")); | ||
case 499: | ||
status = Status(Code::CANCELLED, std::string("Client Closed Request.")); | ||
case 504: | ||
status = Status(Code::DEADLINE_EXCEEDED, std::string("Gateway Timeout.")); | ||
case 501: | ||
status = Status(Code::UNIMPLEMENTED, std::string("Not Implemented.")); | ||
case 503: | ||
status = Status(Code::UNAVAILABLE, std::string("Service Unavailable.")); | ||
case 401: | ||
status = Status(Code::UNAUTHENTICATED, std::string("Unauthorized.")); | ||
default: { | ||
if (http_code >= 200 && http_code < 300) { | ||
status = Status(Code::OK, std::string("OK.")); | ||
|
||
} else if (http_code >= 400 && http_code < 500) { | ||
status = | ||
Status(Code::FAILED_PRECONDITION, std::string("Client Error.")); | ||
} else if (http_code >= 500 && http_code < 600) { | ||
status = Status(Code::INTERNAL, std::string("Server Error.")); | ||
} else | ||
status = Status(Code::UNKNOWN, std::string("Unknown Error.")); | ||
} | ||
} | ||
return status; | ||
} | ||
|
||
static size_t ResultBodyCallback(void *data, size_t size, size_t nmemb, | ||
void *instance) { | ||
size_t data_len = size * nmemb; | ||
|
||
if (data_len > 0) { | ||
((std::string *)instance)->append((char *)data, size * nmemb); | ||
} | ||
return data_len; | ||
} | ||
|
||
Status SendHttp(const std::string &url, const std::string &auth_header, | ||
const std::string &request_body, std::string *response_body) { | ||
CURL *curl = curl_easy_init(); | ||
curl_easy_setopt(curl, CURLOPT_URL, url.data()); | ||
|
||
struct curl_slist *list = NULL; | ||
list = curl_slist_append(list, "Content-Type: application/x-protobuf"); | ||
list = curl_slist_append(list, "X-GFE-SSL: yes"); | ||
|
||
list = curl_slist_append(list, auth_header.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); | ||
|
||
// 0L will not include headers in response body. 1L will include headers in | ||
// response body. | ||
curl_easy_setopt(curl, CURLOPT_HEADER, 0L); | ||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); | ||
|
||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); | ||
curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); | ||
curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, | ||
"ALL:!aNULL:!LOW:!EXPORT:!SSLv2"); | ||
|
||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_body.data()); | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ResultBodyCallback); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response_body); | ||
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); | ||
|
||
CURLcode res = curl_easy_perform(curl); | ||
Status status = Status::OK; | ||
if (res != CURLE_OK) { | ||
std::cout << "curl easy_perform() failed." << std::endl; | ||
long http_code = 0; | ||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); | ||
Status status = ConvertHttpCodeToStatus(http_code); | ||
} | ||
|
||
curl_easy_cleanup(curl); | ||
return status; | ||
} | ||
|
||
} // namespace | ||
|
||
void LibCurlTransport::Check( | ||
const ::google::api::servicecontrol::v1::CheckRequest &request, | ||
::google::api::servicecontrol::v1::CheckResponse *response, | ||
TransportDoneFunc on_done) { | ||
std::string *request_body = new std::string(); | ||
request.SerializeToString(request_body); | ||
|
||
std::thread t([request_body, response, on_done, this]() { | ||
std::string response_body; | ||
Status status = SendHttp(this->check_url_, this->auth_token_header_, | ||
*request_body, &response_body); | ||
delete request_body; | ||
if (status.ok()) { | ||
if (!response->ParseFromString(response_body)) { | ||
status = Status(Code::INVALID_ARGUMENT, | ||
std::string("Cannot parse response to proto.")); | ||
} else { | ||
std::cout << "response parsed from string: " << response->DebugString() | ||
<< std::endl; | ||
} | ||
} | ||
on_done(status); | ||
|
||
}); | ||
t.detach(); | ||
} | ||
|
||
void LibCurlTransport::Report( | ||
const ::google::api::servicecontrol::v1::ReportRequest &request, | ||
::google::api::servicecontrol::v1::ReportResponse *response, | ||
TransportDoneFunc on_done) { | ||
std::string *request_body = new std::string(); | ||
request.SerializeToString(request_body); | ||
|
||
std::thread t([request_body, response, on_done, this]() { | ||
std::string response_body; | ||
Status status = SendHttp(this->report_url_, this->auth_token_header_, | ||
*request_body, &response_body); | ||
delete request_body; | ||
if (status.ok()) { | ||
if (!response->ParseFromString(response_body)) { | ||
status = Status(Code::INVALID_ARGUMENT, | ||
std::string("Cannot parse response to proto.")); | ||
} else { | ||
std::cout << "response parsed from string: " << response->DebugString() | ||
<< std::endl; | ||
} | ||
} | ||
on_done(status); | ||
}); | ||
t.detach(); | ||
} | ||
|
||
} // namespace transport | ||
} // namespace sample | ||
} // namespace service_control_client | ||
} // namespace google |
Oops, something went wrong.