LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
line_ports.h
Go to the documentation of this file.
1/**
2 * @file line_ports.h
3 * @brief Host ports of the LINE service containers.
4 *
5 * Every imperialqore/ *-rest image listens on 8080 INSIDE its container; that is
6 * the image contract and does not change. What is allocated here is the HOST
7 * port each service is published on by default, so that two LINE services can
8 * run side by side. Mirrors matlab/src/io/line_ports.m,
9 * line_solver.api.io.ports and jline.io.LinePorts; the four tables must agree.
10 *
11 * The block 29200-29229 is unassigned in the IANA registry, needs no root to
12 * bind, and sits BELOW 32768, the floor of the Linux ephemeral range: RFC 6335
13 * points unregistered services at 49152-65535, but that is where operating
14 * systems draw outbound source ports from, so a fixed listener there can lose
15 * its bind to an outgoing connection, intermittently and only under load.
16 *
17 * Offsets are permanent: one is retired, never reused for another tool. Offset
18 * 0 is kept free for an aggregating gateway; offsets 5-15 are reserved for the
19 * wrapper packages that ship outside this tree.
20 *
21 * Copyright (c) 2012-2026, Imperial College London
22 * All rights reserved.
23 */
24#ifndef LINE_IO_LINE_PORTS_H
25#define LINE_IO_LINE_PORTS_H
26
27#include <cstdio>
28#include <exception>
29#include <stdexcept>
30#include <string>
31
32#include "json.hpp"
33#include "line/util/http.h"
34
35namespace line {
36namespace ports {
37
38/** First host port of the reserved block. */
39constexpr int BLOCK_FIRST = 29200;
40/** Last host port of the reserved block. */
41constexpr int BLOCK_LAST = 29229;
42/** Port every service image listens on inside its container. */
43constexpr int CONTAINER_PORT = 8080;
44/** Host port every service used before the allocation; a verified fallback only. */
45constexpr int LEGACY_PORT = 8080;
46
47/** Host port for a given offset within the LINE block. */
48inline int allocate(int offset) {
49 const int port = BLOCK_FIRST + offset;
50 if (port < BLOCK_FIRST || port > BLOCK_LAST)
51 throw std::invalid_argument("offset " + std::to_string(offset) + " maps to port " +
52 std::to_string(port) + ", outside the LINE block " +
53 std::to_string(BLOCK_FIRST) + "-" + std::to_string(BLOCK_LAST));
54 return port;
55}
56
57/** imperialqore/jmt-rest. */
58constexpr int JMT_PORT = BLOCK_FIRST + 1;
59/** imperialqore/line-lqns-rest (lqns, lqsim and qnsolver share the image). */
60constexpr int LQNS_PORT = BLOCK_FIRST + 2;
61/** imperialqore/ldes-rest. */
62constexpr int LDES_PORT = BLOCK_FIRST + 3;
63/** imperialqore/line-sage-rest. */
64constexpr int SAGE_PORT = BLOCK_FIRST + 4;
65
66/** Base URL a service published on `port` of localhost is reachable at. */
67inline std::string default_url(int port) { return "http://localhost:" + std::to_string(port); }
68
69/** Default LqnsOptions::remote_url. */
70inline std::string lqns_default_url() { return default_url(LQNS_PORT); }
71
72namespace detail {
73constexpr int PROBE_TIMEOUT_MS = 2000;
74
75inline std::string trim_slashes(std::string s) {
76 while (!s.empty() && s[s.size() - 1] == '/') s.erase(s.size() - 1);
77 return s;
78}
79} // namespace detail
80
81/** True when `<base>/api/v1/health` answers 2xx. */
82inline bool is_healthy(const std::string& base) {
83 try {
84 const http::Response r = http::get(detail::trim_slashes(base) + "/api/v1/health",
85 detail::PROBE_TIMEOUT_MS);
86 return r.status >= 200 && r.status < 300;
87 } catch (const std::exception&) {
88 return false;
89 }
90}
91
92/**
93 * True when `/api/v1/info` lists a tool named `lqns`. Identity is read, not
94 * assumed from the port: a line-sage-rest started by hand on 8080 answers
95 * /health just as an lqns-rest does.
96 */
97inline bool is_lqns_service(const std::string& base) {
98 try {
99 const http::Response r = http::get(detail::trim_slashes(base) + "/api/v1/info",
100 detail::PROBE_TIMEOUT_MS);
101 if (r.status < 200 || r.status >= 300) return false;
102 const nlohmann::json info = nlohmann::json::parse(r.body);
103 if (!info.is_object() || !info.contains("tools") || !info["tools"].is_array()) return false;
104 for (const auto& t : info["tools"])
105 if (t.is_object() && t.contains("name") && t["name"].is_string() &&
106 t["name"].get<std::string>() == "lqns")
107 return true;
108 return false;
109 } catch (const std::exception&) {
110 return false;
111 }
112}
113
114/**
115 * URL the SolverLQNS remote path posts to, given LqnsOptions::remote_url.
116 *
117 * A URL other than the default is used exactly as given. The default is used
118 * when it answers; otherwise an lqns-rest still published on the legacy port
119 * 8080, verified through /api/v1/info, is used with a warning telling the user
120 * to republish it. When neither answers the default is returned, so the
121 * request fails naming the URL the service is expected at.
122 */
123inline std::string resolve_lqns_url(const std::string& configured) {
124 const std::string url = detail::trim_slashes(configured);
125 if (url != lqns_default_url()) return configured;
126 if (is_healthy(url)) return url;
127 const std::string legacy = default_url(LEGACY_PORT);
128 if (is_lqns_service(legacy)) {
129 std::fprintf(stderr,
130 "[LINE] Warning: SolverLQNS: Found lqns-rest on the legacy port %d and using "
131 "it. The default is now %s; republish the service with \"docker run -d -p "
132 "%d:%d <image>\" or set remote_url. The %d fallback will be removed.\n",
135 return legacy;
136 }
137 return url;
138}
139
140} // namespace ports
141} // namespace line
142
143#endif // LINE_IO_LINE_PORTS_H
Minimal HTTP/1.1 client, enough to talk to a line-*-rest service.
Response get(const std::string &url, int timeoutMillis)
GET a URL.
Definition http.h:348
constexpr int LEGACY_PORT
Host port every service used before the allocation; a verified fallback only.
Definition line_ports.h:45
constexpr int SAGE_PORT
imperialqore/line-sage-rest.
Definition line_ports.h:64
constexpr int BLOCK_FIRST
First host port of the reserved block.
Definition line_ports.h:39
constexpr int BLOCK_LAST
Last host port of the reserved block.
Definition line_ports.h:41
bool is_healthy(const std::string &base)
True when <base>/api/v1/health answers 2xx.
Definition line_ports.h:82
std::string resolve_lqns_url(const std::string &configured)
URL the SolverLQNS remote path posts to, given LqnsOptions::remote_url.
Definition line_ports.h:123
constexpr int JMT_PORT
imperialqore/jmt-rest.
Definition line_ports.h:58
bool is_lqns_service(const std::string &base)
True when /api/v1/info lists a tool named lqns.
Definition line_ports.h:97
int allocate(int offset)
Host port for a given offset within the LINE block.
Definition line_ports.h:48
constexpr int LQNS_PORT
imperialqore/line-lqns-rest (lqns, lqsim and qnsolver share the image).
Definition line_ports.h:60
constexpr int CONTAINER_PORT
Port every service image listens on inside its container.
Definition line_ports.h:43
constexpr int LDES_PORT
imperialqore/ldes-rest.
Definition line_ports.h:62
std::string lqns_default_url()
Default LqnsOptions::remote_url.
Definition line_ports.h:70
std::string default_url(int port)
Base URL a service published on port of localhost is reachable at.
Definition line_ports.h:67
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
An HTTP response, with the body already de-chunked.
Definition http.h:60
std::string body
Response body, decoded.
Definition http.h:62
int status
HTTP status code.
Definition http.h:61