|
| 1 | +// Copyright 2020 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 | +// A server that starts, and then stops after 60 seconds. |
| 16 | +// This example really does nothing other than show how to integrate |
| 17 | +// the C++ SDK. |
| 18 | + |
| 19 | +#include <agones/sdk.h> |
| 20 | +#include <grpc++/grpc++.h> |
| 21 | +#include <iostream> |
| 22 | +#include <thread> |
| 23 | + |
| 24 | +// send health check pings |
| 25 | +void DoHealth(std::shared_ptr<agones::SDK> sdk) { |
| 26 | + bool ok = sdk->Health(); |
| 27 | + std::cout << "Health ping " << (ok ? "sent" : "failed") << "\n" << std::flush; |
| 28 | +} |
| 29 | + |
| 30 | +// watch GameServer Updates |
| 31 | +void WatchUpdates(std::shared_ptr<agones::SDK> sdk) { |
| 32 | + std::cout << "Starting to watch GameServer updates...\n" << std::flush; |
| 33 | + sdk->WatchGameServer([](const agones::dev::sdk::GameServer& gameserver) { |
| 34 | + std::cout << "GameServer Update:\n" // |
| 35 | + << "\tname: " << gameserver.object_meta().name() << "\n" // |
| 36 | + << "\tstate: " << gameserver.status().state() << "\n" |
| 37 | + << std::flush; |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +int main() { |
| 42 | + std::cout << "C++ Game Server has started!\n" |
| 43 | + << "Getting the instance of the SDK.\n" << std::flush; |
| 44 | + auto sdk = std::make_shared<agones::SDK>(); |
| 45 | + |
| 46 | + std::cout << "Attempting to connect...\n" << std::flush; |
| 47 | + if (!sdk->Connect()) { |
| 48 | + std::cerr << "Exiting!\n"; |
| 49 | + return -1; |
| 50 | + } |
| 51 | + std::cout << "...handshake complete.\n" << std::flush; |
| 52 | + |
| 53 | + DoHealth(sdk); |
| 54 | + std::thread watch(WatchUpdates, sdk); |
| 55 | + |
| 56 | + std::cout << "Marking server as ready...\n" << std::flush; |
| 57 | + grpc::Status status = sdk->Ready(); |
| 58 | + if (!status.ok()) { |
| 59 | + std::cerr << "Could not run Ready(): " << status.error_message() |
| 60 | + << ". Exiting!\n"; |
| 61 | + return -1; |
| 62 | + } |
| 63 | + std::cout << "...marked Ready\n" << std::flush; |
| 64 | + |
| 65 | + status = sdk->Allocate(); |
| 66 | + if (!status.ok()) { |
| 67 | + std::cerr << "Could not run Allocate(): " << status.error_message() |
| 68 | + << ". Exiting!\n"; |
| 69 | + return -1; |
| 70 | + } |
| 71 | + std::cout << "...marked Allocated\n" << std::flush; |
| 72 | + |
| 73 | + std::chrono::seconds sec(1); |
| 74 | + status = sdk->Reserve(sec); |
| 75 | + if (!status.ok()) { |
| 76 | + std::cerr << "Could not run Reserve(): " << status.error_message() |
| 77 | + << ". Exiting!\n"; |
| 78 | + return -1; |
| 79 | + } |
| 80 | + std::cout << "...marked Reserved\n" << std::flush; |
| 81 | + |
| 82 | + std::cout << "Getting GameServer details...\n" << std::flush; |
| 83 | + agones::dev::sdk::GameServer gameserver; |
| 84 | + status = sdk->GameServer(&gameserver); |
| 85 | + |
| 86 | + if (!status.ok()) { |
| 87 | + std::cerr << "Could not run GameServer(): " << status.error_message() |
| 88 | + << ". Exiting!\n"; |
| 89 | + return -1; |
| 90 | + } |
| 91 | + |
| 92 | + std::cout << "GameServer name: " << gameserver.object_meta().name() << "\n" |
| 93 | + << std::flush; |
| 94 | + |
| 95 | + std::cout << "Setting a label\n" << std::flush; |
| 96 | + status = sdk->SetLabel( |
| 97 | + "test-label", |
| 98 | + std::to_string(gameserver.object_meta().creation_timestamp())); |
| 99 | + if (!status.ok()) { |
| 100 | + std::cerr << "Could not run SetLabel(): " << status.error_message() |
| 101 | + << ". Exiting!\n"; |
| 102 | + return -1; |
| 103 | + } |
| 104 | + |
| 105 | + std::cout << "Setting an annotation\n" << std::flush; |
| 106 | + status = |
| 107 | + sdk->SetAnnotation("test-annotation", gameserver.object_meta().uid()); |
| 108 | + if (!status.ok()) { |
| 109 | + std::cerr << "Could not run SetAnnotation(): " << status.error_message() |
| 110 | + << ". Exiting!\n"; |
| 111 | + return -1; |
| 112 | + } |
| 113 | + |
| 114 | + for (int i = 0; i < 2; i++) { |
| 115 | + int time = i * 10; |
| 116 | + std::cout << "Running for " + std::to_string(time) + " seconds !\n" |
| 117 | + << std::flush; |
| 118 | + |
| 119 | + std::this_thread::sleep_for(std::chrono::seconds(10)); |
| 120 | + |
| 121 | + if (i == 1) { |
| 122 | + std::cout << "Shutting down after 10 seconds...\n" << std::flush; |
| 123 | + grpc::Status status = sdk->Shutdown(); |
| 124 | + if (!status.ok()) { |
| 125 | + std::cerr << "Could not run Shutdown():" << status.error_message() |
| 126 | + << ". Exiting!\n"; |
| 127 | + return -1; |
| 128 | + } |
| 129 | + std::cout << "...marked for Shutdown\n" << std::flush; |
| 130 | + } |
| 131 | + } |
| 132 | + watch.join(); |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
0 commit comments