55int main(
int argc,
char* argv[]) {
56 std::cout <<
"nanommo server: version " << nanommo::kVersion << std::endl;
57 constexpr int kTicksPerSecond = 20;
58 const absl::StatusOr<std::chrono::nanoseconds> interval =
61 std::cerr <<
"nanommo server: " << interval.status() <<
"\n";
64 const std::optional<uint16_t> port =
65 argc > 1 ? ParsePort(argv[1]) : uint16_t{7777};
68 <<
"nanommo server: the port should be a number from 1 to 65535, not "
72 const std::string metrics_setting =
74 const std::optional<uint16_t> metrics_port = ParsePort(metrics_setting);
76 std::cerr <<
"nanommo server: NANOMMO_METRICS_PORT should be a number from "
78 << metrics_setting <<
"\n";
83 if (name.empty() || name.size() > 16 ||
84 !std::ranges::all_of(name, absl::ascii_isalnum)) {
85 std::cerr <<
"nanommo server: NANOMMO_WORLD should be up to 16 letters "
91 const std::string seed_setting =
nanommo::Setting(
"NANOMMO_WORLD_SEED",
"1");
92 if (!absl::SimpleAtoi(seed_setting, &seed)) {
93 std::cerr <<
"nanommo server: NANOMMO_WORLD_SEED should be a whole number "
94 "from 0 to 4294967295, not "
95 << seed_setting <<
"\n";
98 const absl::StatusOr<nanommo::Content> content =
101 std::cerr <<
"nanommo server: " << content.status() <<
"\n";
109 std::cerr <<
"nanommo server: " << tls.status() <<
"\n";
116 std::optional<prometheus::Exposer> exposer;
118 exposer.emplace(
"0.0.0.0:" + std::to_string(*metrics_port));
119 }
catch (
const std::exception& error) {
120 std::cerr <<
"nanommo server: couldn't serve metrics on port "
121 << *metrics_port <<
" (" << error.what()
122 <<
"). Is another server running here? Another world needs a "
123 "port of its own, and NANOMMO_METRICS_PORT too\n";
126 exposer->RegisterCollectable(metrics.
registry());
128 absl::StatusOr<std::unique_ptr<nanommo::Database>> database_or =
132 if (!database_or.ok()) {
133 std::cerr <<
"nanommo server: " << database_or.status() <<
"\n";
136 const std::unique_ptr<nanommo::Database> database = *std::move(database_or);
137 absl::StatusOr<std::unique_ptr<nanommo::Redis>> redis_or =
140 if (!redis_or.ok()) {
141 std::cerr <<
"nanommo server: " << redis_or.status() <<
"\n";
144 const std::unique_ptr<nanommo::Redis> redis = *std::move(redis_or);
153 absl::StatusOr<std::unique_ptr<nanommo::Server>> server_or =
155 metrics, tokens, players, guilds, events,
157 if (!server_or.ok()) {
158 std::cerr <<
"nanommo server: " << server_or.status() <<
"\n";
161 const std::unique_ptr<nanommo::Server> server = *std::move(server_or);
162 std::cout <<
"nanommo server: " << name <<
", listening on port " << *port
163 <<
", " << kTicksPerSecond <<
" ticks/s, "
164 << absl::FormatDuration(absl::FromChrono(*interval)) <<
" per tick"
165 <<
", world seed " << seed << std::endl;
167 std::cout <<
"nanommo server: content "
168 << absl::StrCat(absl::Hex(content_hash, absl::kZeroPad16))
170 std::cout <<
"nanommo server: metrics on port " << *metrics_port
171 <<
", at /metrics" << std::endl;
178 .metrics_port = *metrics_port,
179 .content = content_hash,
180 .started = absl::ToUnixSeconds(absl::Now()),
185 const std::string collector =
nanommo::Setting(
"NANOMMO_OTLP_ENDPOINT",
"");
186 if (!collector.empty()) {
189 std::cout <<
"nanommo server: sending spans to " << collector << std::endl;
192 asio::thread_pool consul_thread(1);
194 std::atomic<bool> waiting =
false;
195 std::optional<int> listed;
197 constexpr std::chrono::seconds kHeartbeat{2};
198 asio::steady_timer heartbeat(io);
202 auto beat = [&](
this auto self, std::error_code error) ->
void {
206 registration.
players = server->players();
207 if (!waiting.exchange(
true)) {
209 consul_thread, [&consul, &listed, &said, &waiting, registration] {
210 absl::Status status = absl::OkStatus();
211 if (listed != registration.
players) {
212 status = consul.
Register(registration);
226 const std::string news = status.ok()
227 ?
"registered with Consul as " +
233 std::cout <<
"nanommo server: " << news << std::endl;
238 heartbeat.expires_after(kHeartbeat);
239 heartbeat.async_wait(self);
244 constexpr std::chrono::seconds kHandOverTime{10};
245 asio::steady_timer leaving(io);
246 std::chrono::steady_clock::time_point deadline;
247 auto wait = [&](
this auto self, std::error_code) ->
void {
248 if (server->players() == 0 || std::chrono::steady_clock::now() > deadline) {
252 leaving.expires_after(std::chrono::milliseconds(100));
253 leaving.async_wait(self);
257 asio::signal_set signals(io, SIGINT, SIGTERM);
258 signals.async_wait([&](std::error_code,
int) {
260 consul_thread.join();
261 const absl::Status deregistered = consul.
Deregister(instance);
262 if (!deregistered.ok()) {
263 std::cerr <<
"nanommo server: " << deregistered <<
"\n";
267 const absl::StatusOr<std::vector<nanommo::protocol::WorldListing>> worlds =
269 const nanommo::protocol::WorldListing* next =
nullptr;
271 const auto found = std::ranges::find(
272 *worlds, name, &nanommo::protocol::WorldListing::name);
273 next = found != worlds->end() ? &*found :
nullptr;
275 if (next ==
nullptr || server->players() == 0) {
280 server->HandOver(next->host(),
static_cast<uint16_t
>(next->port()));
281 std::cout <<
"nanommo server: handing " << told
282 << (told == 1 ?
" player" :
" players") <<
" over to "
283 << next->host() <<
":" << next->port() << std::endl;
284 deadline = std::chrono::steady_clock::now() + kHandOverTime;
289 std::cout <<
"nanommo server: stopped" << std::endl;
static absl::StatusOr< std::unique_ptr< Server > > Create(asio::io_context &io, uint16_t port, asio::ssl::context &tls, std::chrono::nanoseconds interval, std::string name, uint32_t seed, const Content &content, Metrics &metrics, Tokens &tokens, Players &players, Guilds &guilds, Events &events, Storyteller &storyteller)