nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
consul.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_CONSUL_H_
2#define NANOMMO_SERVER_CONSUL_H_
3
4#include <cstdint>
5#include <string>
6#include <utility>
7#include <vector>
8
9#include "absl/status/status.h"
10#include "absl/status/statusor.h"
11#include "protocol/nanommo.pb.h"
12
13namespace nanommo {
14
15/// A world, as it registers with Consul.
17 std::string name;
18 std::string public_host;
19 uint16_t port = 0;
20 uint16_t metrics_port = 0;
21 int players = 0;
22 /// The ClientHash of the world's content.
23 uint64_t content = 0;
24 /// When the instance started, in seconds since the epoch.
25 int64_t started = 0;
26};
27
28/// A world instance as players reach it, and as the servers do.
30 std::string public_host;
31 uint16_t port = 0;
32 /// Its own address, on Consul's network.
33 std::string address;
34};
35
36/// A world runs as two instances for a moment, while one hands its players
37/// over to the other, so each instance registers under a name of its own, its
38/// world's and its port's.
39std::string InstanceId(const WorldRegistration& world);
40
41class Consul {
42 public:
43 static constexpr uint16_t kPort = 8500;
44
45 explicit Consul(std::string host) : host_(std::move(host)) {}
46
47 /// Registers the world, or registers it again with its players. Its health
48 /// check starts as passing, and keeps its status when it's registered again.
49 absl::Status Register(const WorldRegistration& world) const;
50
51 /// Passes the instance's health check, which turns critical ten seconds after
52 /// the last pass. NotFound when Consul doesn't know the instance.
53 absl::Status Pass(const std::string& instance) const;
54
55 absl::Status Deregister(const std::string& instance) const;
56
57 /// The worlds that pass their health checks, by name, with the host and
58 /// port players reach them at: each world's newest instance.
59 absl::StatusOr<std::vector<protocol::WorldListing>> Worlds() const;
60
61 /// Each world instance that passes its health check, by the host and port
62 /// players reach it at, with its own address on Consul's network: what the
63 /// gateway may connect a page to, and where.
64 absl::StatusOr<std::vector<WorldInstance>> Instances() const;
65
66 private:
67 absl::StatusOr<std::string> OwnAddress() const;
68 absl::Status Put(const std::string& path, const std::string& body) const;
69 absl::StatusOr<std::string> Get(const std::string& path) const;
70
71 std::string host_;
72};
73
74} // namespace nanommo
75
76#endif // NANOMMO_SERVER_CONSUL_H_
absl::Status Pass(const std::string &instance) const
Definition consul.cc:106
absl::Status Register(const WorldRegistration &world) const
Definition consul.cc:80
absl::StatusOr< std::vector< protocol::WorldListing > > Worlds() const
Definition consul.cc:114
absl::Status Deregister(const std::string &instance) const
Definition consul.cc:110
Consul(std::string host)
Definition consul.h:45
static constexpr uint16_t kPort
Definition consul.h:43
absl::StatusOr< std::vector< WorldInstance > > Instances() const
Definition consul.cc:168
Definition chat.cc:9
std::string InstanceId(const WorldRegistration &world)
Definition consul.cc:76
A world instance as players reach it, and as the servers do.
Definition consul.h:29
std::string address
Its own address, on Consul's network.
Definition consul.h:33
std::string public_host
Definition consul.h:30
A world, as it registers with Consul.
Definition consul.h:16
std::string public_host
Definition consul.h:18
uint64_t content
The ClientHash of the world's content.
Definition consul.h:23
int64_t started
When the instance started, in seconds since the epoch.
Definition consul.h:25