agones.dev/agones@v1.53.0/sdks/cpp/src/agones/sdk.cc (about) 1 // Copyright 2017 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "agones/sdk.h" 16 17 #include <grpcpp/grpcpp.h> 18 19 #include <utility> 20 21 namespace agones { 22 23 struct SDK::SDKImpl { 24 std::string host_; 25 std::shared_ptr<grpc::Channel> channel_; 26 std::unique_ptr<agones::dev::sdk::SDK::Stub> stub_; 27 std::unique_ptr<grpc::ClientWriter<agones::dev::sdk::Empty>> health_; 28 std::unique_ptr<grpc::ClientContext> health_context_; 29 }; 30 31 SDK::SDK() : pimpl_{std::make_unique<SDKImpl>()} { 32 const char* port = std::getenv("AGONES_SDK_GRPC_PORT"); 33 pimpl_->host_ = std::string("localhost:") + (port ? port : "9357"); 34 pimpl_->channel_ = 35 grpc::CreateChannel(pimpl_->host_, grpc::InsecureChannelCredentials()); 36 } 37 38 SDK::~SDK() {} 39 40 bool SDK::Connect() { 41 if (!pimpl_->channel_->WaitForConnected( 42 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), 43 gpr_time_from_seconds(30, GPR_TIMESPAN)))) { 44 std::cerr << "Could not connect to the sidecar at " << pimpl_->host_ 45 << ".\n"; 46 return false; 47 } 48 49 pimpl_->stub_ = agones::dev::sdk::SDK::NewStub(pimpl_->channel_); 50 51 // Make the health connection. 52 agones::dev::sdk::Empty response; 53 pimpl_->health_context_ = 54 std::unique_ptr<grpc::ClientContext>(new grpc::ClientContext); 55 pimpl_->health_ = pimpl_->stub_->Health(&*pimpl_->health_context_, &response); 56 57 return true; 58 } 59 60 grpc::Status SDK::Ready() { 61 grpc::ClientContext context; 62 context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), 63 gpr_time_from_seconds(30, GPR_TIMESPAN))); 64 agones::dev::sdk::Empty request; 65 agones::dev::sdk::Empty response; 66 67 return pimpl_->stub_->Ready(&context, request, &response); 68 } 69 70 grpc::Status SDK::Allocate() { 71 grpc::ClientContext context; 72 context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), 73 gpr_time_from_seconds(30, GPR_TIMESPAN))); 74 agones::dev::sdk::Empty request; 75 agones::dev::sdk::Empty response; 76 77 return pimpl_->stub_->Allocate(&context, request, &response); 78 } 79 80 grpc::Status SDK::Reserve(std::chrono::seconds seconds) { 81 grpc::ClientContext context; 82 context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), 83 gpr_time_from_seconds(30, GPR_TIMESPAN))); 84 85 agones::dev::sdk::Duration request; 86 request.set_seconds(seconds.count()); 87 88 agones::dev::sdk::Empty response; 89 90 return pimpl_->stub_->Reserve(&context, request, &response); 91 } 92 93 bool SDK::Health() { 94 agones::dev::sdk::Empty request; 95 return pimpl_->health_->Write(request); 96 } 97 98 grpc::Status SDK::GameServer(agones::dev::sdk::GameServer* response) { 99 grpc::ClientContext context; 100 context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), 101 gpr_time_from_seconds(30, GPR_TIMESPAN))); 102 agones::dev::sdk::Empty request; 103 104 return pimpl_->stub_->GetGameServer(&context, request, response); 105 } 106 107 grpc::Status SDK::WatchGameServer( 108 const std::function<void(const agones::dev::sdk::GameServer&)>& callback) { 109 grpc::ClientContext context; 110 agones::dev::sdk::Empty request; 111 agones::dev::sdk::GameServer gameServer; 112 113 std::unique_ptr<grpc::ClientReader<agones::dev::sdk::GameServer>> reader = 114 pimpl_->stub_->WatchGameServer(&context, request); 115 while (reader->Read(&gameServer)) { 116 callback(gameServer); 117 } 118 return reader->Finish(); 119 } 120 121 grpc::Status SDK::Shutdown() { 122 grpc::ClientContext context; 123 context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), 124 gpr_time_from_seconds(30, GPR_TIMESPAN))); 125 agones::dev::sdk::Empty request; 126 agones::dev::sdk::Empty response; 127 128 return pimpl_->stub_->Shutdown(&context, request, &response); 129 } 130 131 grpc::Status SDK::SetLabel(std::string key, std::string value) { 132 grpc::ClientContext context; 133 context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), 134 gpr_time_from_seconds(30, GPR_TIMESPAN))); 135 136 agones::dev::sdk::KeyValue request; 137 request.set_key(std::move(key)); 138 request.set_value(std::move(value)); 139 140 agones::dev::sdk::Empty response; 141 142 return pimpl_->stub_->SetLabel(&context, request, &response); 143 } 144 145 grpc::Status SDK::SetAnnotation(std::string key, std::string value) { 146 grpc::ClientContext context; 147 context.set_deadline(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), 148 gpr_time_from_seconds(30, GPR_TIMESPAN))); 149 150 agones::dev::sdk::KeyValue request; 151 request.set_key(std::move(key)); 152 request.set_value(std::move(value)); 153 154 agones::dev::sdk::Empty response; 155 156 return pimpl_->stub_->SetAnnotation(&context, request, &response); 157 } 158 } // namespace agones