LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Toggle main menu visibility
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
35
namespace
line
{
36
namespace
ports
{
37
38
/** First host port of the reserved block. */
39
constexpr
int
BLOCK_FIRST
= 29200;
40
/** Last host port of the reserved block. */
41
constexpr
int
BLOCK_LAST
= 29229;
42
/** Port every service image listens on inside its container. */
43
constexpr
int
CONTAINER_PORT
= 8080;
44
/** Host port every service used before the allocation; a verified fallback only. */
45
constexpr
int
LEGACY_PORT
= 8080;
46
47
/** Host port for a given offset within the LINE block. */
48
inline
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. */
58
constexpr
int
JMT_PORT
=
BLOCK_FIRST
+ 1;
59
/** imperialqore/line-lqns-rest (lqns, lqsim and qnsolver share the image). */
60
constexpr
int
LQNS_PORT
=
BLOCK_FIRST
+ 2;
61
/** imperialqore/ldes-rest. */
62
constexpr
int
LDES_PORT
=
BLOCK_FIRST
+ 3;
63
/** imperialqore/line-sage-rest. */
64
constexpr
int
SAGE_PORT
=
BLOCK_FIRST
+ 4;
65
66
/** Base URL a service published on `port` of localhost is reachable at. */
67
inline
std::string
default_url
(
int
port) {
return
"http://localhost:"
+ std::to_string(port); }
68
69
/** Default LqnsOptions::remote_url. */
70
inline
std::string
lqns_default_url
() {
return
default_url
(
LQNS_PORT
); }
71
72
namespace
detail {
73
constexpr
int
PROBE_TIMEOUT_MS = 2000;
74
75
inline
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. */
82
inline
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
*/
97
inline
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
*/
123
inline
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"
,
133
LEGACY_PORT
,
lqns_default_url
().c_str(),
LQNS_PORT
,
CONTAINER_PORT
,
134
LEGACY_PORT
);
135
return
legacy;
136
}
137
return
url;
138
}
139
140
}
// namespace ports
141
}
// namespace line
142
143
#endif
// LINE_IO_LINE_PORTS_H
http.h
Minimal HTTP/1.1 client, enough to talk to a line-*-rest service.
line::http::get
Response get(const std::string &url, int timeoutMillis)
GET a URL.
Definition
http.h:348
line::ports
Definition
line_ports.h:36
line::ports::LEGACY_PORT
constexpr int LEGACY_PORT
Host port every service used before the allocation; a verified fallback only.
Definition
line_ports.h:45
line::ports::SAGE_PORT
constexpr int SAGE_PORT
imperialqore/line-sage-rest.
Definition
line_ports.h:64
line::ports::BLOCK_FIRST
constexpr int BLOCK_FIRST
First host port of the reserved block.
Definition
line_ports.h:39
line::ports::BLOCK_LAST
constexpr int BLOCK_LAST
Last host port of the reserved block.
Definition
line_ports.h:41
line::ports::is_healthy
bool is_healthy(const std::string &base)
True when <base>/api/v1/health answers 2xx.
Definition
line_ports.h:82
line::ports::resolve_lqns_url
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
line::ports::JMT_PORT
constexpr int JMT_PORT
imperialqore/jmt-rest.
Definition
line_ports.h:58
line::ports::is_lqns_service
bool is_lqns_service(const std::string &base)
True when /api/v1/info lists a tool named lqns.
Definition
line_ports.h:97
line::ports::allocate
int allocate(int offset)
Host port for a given offset within the LINE block.
Definition
line_ports.h:48
line::ports::LQNS_PORT
constexpr int LQNS_PORT
imperialqore/line-lqns-rest (lqns, lqsim and qnsolver share the image).
Definition
line_ports.h:60
line::ports::CONTAINER_PORT
constexpr int CONTAINER_PORT
Port every service image listens on inside its container.
Definition
line_ports.h:43
line::ports::LDES_PORT
constexpr int LDES_PORT
imperialqore/ldes-rest.
Definition
line_ports.h:62
line::ports::lqns_default_url
std::string lqns_default_url()
Default LqnsOptions::remote_url.
Definition
line_ports.h:70
line::ports::default_url
std::string default_url(int port)
Base URL a service published on port of localhost is reachable at.
Definition
line_ports.h:67
line
Conservation laws of a layered queueing network, enumerated from its structure.
Definition
aoi_dist2ph.h:51
line::http::Response
An HTTP response, with the body already de-chunked.
Definition
http.h:60
line::http::Response::body
std::string body
Response body, decoded.
Definition
http.h:62
line::http::Response::status
int status
HTTP status code.
Definition
http.h:61
include
line
io
line_ports.h
Generated by
1.18.0