1616// This example really does nothing other than show how to integrate
1717// the C++ SDK.
1818
19- #include < iostream>
20- #include < thread>
2119#include < agones/sdk.h>
2220#include < grpc++/grpc++.h>
21+ #include < iostream>
22+ #include < thread>
2323
2424// send health check pings
25- void doHealth (agones::SDK *sdk) {
26- while (true ) {
27- if (!sdk->Health ()) {
28- std::cout << " Health ping failed" << std::endl;
29- } else {
30- std::cout << " Health ping sent" << std::endl;
31- }
32- std::this_thread::sleep_for (std::chrono::seconds (2 ));
33- }
25+ void DoHealth (std::shared_ptr<agones::SDK > sdk) {
26+ while (true ) {
27+ bool ok = sdk->Health ();
28+ std::cout << " Health ping " << (ok ? " sent" : " failed" ) << " \n "
29+ << std::flush;
30+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
31+ }
3432}
3533
3634// watch GameServer Updates
37- void watchUpdates (agones::SDK *sdk) {
38- std::cout << " Starting to watch GameServer updates..." << std::endl;
39- sdk->WatchGameServer ([](stable::agones::dev::sdk::GameServer gameserver){
40- std::cout << " GameServer Update, name: " << gameserver.object_meta ().name () << std::endl;
41- std::cout << " GameServer Update, state: " << gameserver.status ().state () << std::endl;
42- });
35+ void WatchUpdates (std::shared_ptr<agones::SDK > sdk) {
36+ std::cout << " Starting to watch GameServer updates...\n " << std::flush;
37+ sdk->WatchGameServer ([](agones::dev::sdk::GameServer gameserver) {
38+ std::cout << " GameServer Update:\n " //
39+ << " \t name: " << gameserver.object_meta ().name () << " \n " //
40+ << " \t state: " << gameserver.status ().state () << " \n "
41+ << std::flush;
42+ });
4343}
4444
4545int main () {
46- std::cout << " C++ Game Server has started!" << std::endl;
47-
48- std::cout << " Getting the instance of the SDK!" << std::endl;
49- agones::SDK *sdk = new agones::SDK ();
50-
51- std::cout << " Attempting to connect..." << std::endl;
52- bool connected = sdk->Connect ();
53- if (!connected) {
54- std::cout << " Could not connect to the sidecar. Exiting!" << std::endl;
55- return -1 ;
56- }
57- std::cout << " ...handshake complete." << std::endl;
58-
59- std::thread health (doHealth, sdk);
60- std::thread watch (watchUpdates, sdk);
61-
62- std::cout << " Setting a label" << std::endl;
63- grpc::Status status = sdk->SetLabel (" test-label" , " test-value" );
64- if (!status.ok ()) {
65- std::cout << " Could not run SetLabel(): " + status.error_message () + " . Exiting!" << std::endl;
66- return -1 ;
67- }
68-
69- std::cout << " Setting an annotation" << std::endl;
70- status = sdk->SetAnnotation (" test-annotation" , " test value" );
71- if (!status.ok ()) {
72- std::cout << " Could not run SetAnnotation(): " + status.error_message () + " . Exiting!" << std::endl;
46+ std::cout << " C++ Game Server has started!\n "
47+ << " Getting the instance of the SDK.\n "
48+ << std::flush;
49+ auto sdk = std::make_shared<agones::SDK >();
50+
51+ std::cout << " Attempting to connect...\n " << std::flush;
52+ if (!sdk->Connect ()) {
53+ std::cerr << " Could not connect to the sidecar. Exiting!\n " ;
54+ return -1 ;
55+ }
56+ std::cout << " ...handshake complete.\n " << std::flush;
57+
58+ std::thread health (DoHealth, sdk);
59+ std::thread watch (WatchUpdates, sdk);
60+
61+ std::cout << " Setting a label\n " << std::flush;
62+ grpc::Status status = sdk->SetLabel (" test-label" , " test-value" );
63+ if (!status.ok ()) {
64+ std::cerr << " Could not run SetLabel(): " << status.error_message ()
65+ << " . Exiting!\n " ;
66+ return -1 ;
67+ }
68+
69+ std::cout << " Setting an annotation\n " << std::flush;
70+ status = sdk->SetAnnotation (" test-annotation" , " test value" );
71+ if (!status.ok ()) {
72+ std::cerr << " Could not run SetAnnotation(): " << status.error_message ()
73+ << " . Exiting!\n " ;
74+ return -1 ;
75+ }
76+
77+ std::cout << " Marking server as ready...\n " << std::flush;
78+ status = sdk->Ready ();
79+ if (!status.ok ()) {
80+ std::cerr << " Could not run Ready(): " << status.error_message ()
81+ << " . Exiting!\n " ;
82+ return -1 ;
83+ }
84+ std::cout << " ...marked Ready\n " << std::flush;
85+
86+ std::cout << " Getting GameServer details...\n " << std::flush;
87+ agones::dev::sdk::GameServer gameserver;
88+ status = sdk->GameServer (&gameserver);
89+
90+ if (!status.ok ()) {
91+ std::cerr << " Could not run GameServer(): " << status.error_message ()
92+ << " . Exiting!\n " ;
93+ return -1 ;
94+ }
95+
96+ std::cout << " GameServer name: " << gameserver.object_meta ().name () << " \n "
97+ << std::flush;
98+
99+ for (int i = 0 ; i < 10 ; i++) {
100+ int time = i * 10 ;
101+ std::cout << " Running for " + std::to_string (time) + " seconds !\n "
102+ << std::flush;
103+
104+ std::this_thread::sleep_for (std::chrono::seconds (10 ));
105+
106+ if (i == 5 ) {
107+ std::cout << " Shutting down after 60 seconds...\n " << std::flush;
108+ grpc::Status status = sdk->Shutdown ();
109+ if (!status.ok ()) {
110+ std::cerr << " Could not run Shutdown():" << status.error_message ()
111+ << " . Exiting!\n " ;
73112 return -1 ;
113+ }
114+ std::cout << " ...marked for Shutdown\n " << std::flush;
74115 }
116+ }
75117
76- std::cout << " Marking server as ready..." << std::endl;
77- status = sdk->Ready ();
78- if (!status.ok ()) {
79- std::cout << " Could not run Ready(): " + status.error_message () + " . Exiting!" << std::endl;
80- return -1 ;
81- }
82-
83- std::cout << " ...marked Ready" << std::endl;
84-
85- std::cout << " Getting GameServer details..." << std::endl;
86- stable::agones::dev::sdk::GameServer gameserver;
87- status = sdk->GameServer (&gameserver);
88-
89- if (!status.ok ()) {
90- std::cout << " Could not run GameServer(): " + status.error_message () + " . Exiting!" << std::endl;
91- return -1 ;
92- }
93-
94- std::cout << " GameServer name: " << gameserver.object_meta ().name () << std::endl;
95-
96- for (int i = 0 ; i < 10 ; i++) {
97- int time = i*10 ;
98- std::cout << " Running for " + std::to_string (time) + " seconds !" << std::endl;
99-
100- std::this_thread::sleep_for (std::chrono::seconds (10 ));
101-
102- if (i == 5 ) {
103- std::cout << " Shutting down after 60 seconds..." << std::endl;
104- grpc::Status status = sdk->Shutdown ();
105- if (!status.ok ()) {
106- std::cout << " Could not run Shutdown():" + status.error_message () + " . Exiting!" << std::endl;
107- return -1 ;
108- }
109- std::cout << " ...marked for Shutdown" << std::endl;
110- }
111- }
112-
113- delete sdk;
114-
115- return 0 ;
118+ return 0 ;
116119}
0 commit comments