-
Notifications
You must be signed in to change notification settings - Fork 891
/
ChannelUri.h
356 lines (305 loc) · 10.8 KB
/
ChannelUri.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright 2014-2024 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AERON_CHANNEL_URI_H
#define AERON_CHANNEL_URI_H
#include <memory>
#include <string>
#include <unordered_map>
#include "util/StringUtil.h"
namespace aeron
{
static constexpr const char SPY_QUALIFIER[] = "aeron-spy";
static constexpr const char AERON_SCHEME[] = "aeron";
static constexpr const char AERON_PREFIX[] = "aeron:";
static constexpr const char IPC_MEDIA[] = "ipc";
static constexpr const char UDP_MEDIA[] = "udp";
static constexpr const char IPC_CHANNEL[] = "aeron:ipc";
static constexpr const char SPY_PREFIX[] = "aeron-spy:";
static constexpr const char ENDPOINT_PARAM_NAME[] = "endpoint";
static constexpr const char INTERFACE_PARAM_NAME[] = "interface";
static constexpr const char INITIAL_TERM_ID_PARAM_NAME[] = "init-term-id";
static constexpr const char TERM_ID_PARAM_NAME[] = "term-id";
static constexpr const char TERM_OFFSET_PARAM_NAME[] = "term-offset";
static constexpr const char TERM_LENGTH_PARAM_NAME[] = "term-length";
static constexpr const char MTU_LENGTH_PARAM_NAME[] = "mtu";
static constexpr const char TTL_PARAM_NAME[] = "ttl";
static constexpr const char MDC_CONTROL_PARAM_NAME[] = "control";
static constexpr const char MDC_CONTROL_MODE_PARAM_NAME[] = "control-mode";
static constexpr const char MDC_CONTROL_MODE_MANUAL[] = "manual";
static constexpr const char MDC_CONTROL_MODE_DYNAMIC[] = "dynamic";
static constexpr const char CONTROL_MODE_RESPONSE[] = "response";
static constexpr const char SESSION_ID_PARAM_NAME[] = "session-id";
static constexpr const char LINGER_PARAM_NAME[] = "linger";
static constexpr const char RELIABLE_STREAM_PARAM_NAME[] = "reliable";
static constexpr const char TAGS_PARAM_NAME[] = "tags";
static constexpr const char TAG_PREFIX[] = "tag:";
static constexpr const char SPARSE_PARAM_NAME[] = "sparse";
static constexpr const char ALIAS_PARAM_NAME[] = "alias";
static constexpr const char EOS_PARAM_NAME[] = "eos";
static constexpr const char TETHER_PARAM_NAME[] = "tether";
static constexpr const char GROUP_PARAM_NAME[] = "group";
static constexpr const char REJOIN_PARAM_NAME[] = "rejoin";
static constexpr const char CONGESTION_CONTROL_PARAM_NAME[] = "cc";
static constexpr const char FLOW_CONTROL_PARAM_NAME[] = "fc";
static constexpr const char GROUP_TAG_PARAM_NAME[] = "gtag";
static constexpr const char SPIES_SIMULATE_CONNECTION_PARAM_NAME[] = "ssc";
static constexpr const char SOCKET_SNDBUF_PARAM_NAME[] = "so-sndbuf";
static constexpr const char SOCKET_RCVBUF_PARAM_NAME[] = "so-rcvbuf";
static constexpr const char RECEIVER_WINDOW_LENGTH_PARAM_NAME[] = "rcv-wnd";
static constexpr const char MEDIA_RCV_TIMESTAMP_OFFSET_PARAM_NAME[] = "media-rcv-ts-offset";
static constexpr const char CHANNEL_RCV_TIMESTAMP_OFFSET_PARAM_NAME[] = "channel-rcv-ts-offset";
static constexpr const char CHANNEL_SND_TIMESTAMP_OFFSET_PARAM_NAME[] = "channel-snd-ts-offset";
static constexpr const char RESPONSE_CORRELATION_ID_PARAM_NAME[] = "response-correlation-id";
static constexpr const char NAK_DELAY_PARAM_NAME[] = "nak-delay";
static constexpr const char UNTETHERED_WINDOW_LIMIT_TIMEOUT_PARAM_NAME[] = "untethered-window-limit-timeout";
static constexpr const char UNTETHERED_RESTING_TIMEOUT_PARAM_NAME[] = "untethered-resting-timeout";
static constexpr const char MAX_RESEND_PARAM_NAME[] = "max-resend";
using namespace aeron::util;
class ChannelUri
{
public:
using this_t = ChannelUri;
enum State : int
{
MEDIA,
PARAMS_KEY,
PARAMS_VALUE
};
ChannelUri(
const std::string &prefix,
const std::string &media,
std::unique_ptr<std::unordered_map<std::string, std::string>> params) :
m_prefix(prefix),
m_media(media),
m_params(std::move(params))
{
}
inline std::string prefix()
{
return m_prefix;
}
inline this_t &prefix(const std::string &prefix)
{
m_prefix = prefix;
return *this;
}
inline std::string media()
{
return m_media;
}
inline this_t &media(const std::string &media)
{
if (media != IPC_MEDIA && media != UDP_MEDIA)
{
throw IllegalArgumentException("unknown media: " + media, SOURCEINFO);
}
m_media = media;
return *this;
}
inline std::string scheme()
{
return AERON_SCHEME;
}
inline std::string get(const std::string &key)
{
auto it = m_params->find(key);
if (it != m_params->end())
{
return it->second;
}
return {};
}
inline std::string get(const std::string &key, const std::string &defaultValue)
{
auto it = m_params->find(key);
if (it != m_params->end())
{
return it->second;
}
return defaultValue;
}
inline void put(const std::string &key, const std::string &value)
{
(*m_params)[key] = value;
}
inline std::string remove(const std::string &key)
{
std::string result;
auto it = m_params->find(key);
if (it != m_params->end())
{
result = it->second;
m_params->erase(it);
}
return result;
}
inline bool containsKey(const std::string &key)
{
return m_params->find(key) != m_params->end();
}
inline bool hasControlModeResponse()
{
const std::string &controlMode = get(MDC_CONTROL_MODE_PARAM_NAME);
return !controlMode.empty() && CONTROL_MODE_RESPONSE == controlMode;
}
std::string toString()
{
std::string sb;
if (m_prefix.length() == 0)
{
sb.reserve((m_params->size() * 20) + 10);
}
else
{
sb.reserve((m_params->size() * 20) + 20);
sb += m_prefix;
if (':' != m_prefix.back())
{
sb += ':';
}
}
sb += AERON_PREFIX;
sb += m_media;
if (!m_params->empty())
{
sb += '?';
for (const auto &i : *m_params)
{
sb += i.first;
sb += '=';
sb += i.second;
sb += '|';
}
sb.pop_back();
}
return sb;
}
static std::shared_ptr<ChannelUri> parse(const std::string &uri)
{
std::size_t position = 0;
std::string prefix;
if (startsWith(uri, 0, SPY_PREFIX))
{
prefix = SPY_QUALIFIER;
position = sizeof(SPY_PREFIX) - 1;
}
else
{
prefix = "";
}
if (!startsWith(uri, position, AERON_PREFIX))
{
throw IllegalArgumentException("Aeron URIs must start with 'aeron:', found: " + uri, SOURCEINFO);
}
else
{
position += sizeof(AERON_PREFIX) - 1;
}
std::string builder;
std::unique_ptr<std::unordered_map<std::string, std::string>> params(
new std::unordered_map<std::string, std::string>());
std::string media;
std::string key;
State state = State::MEDIA;
for (std::size_t i = position; i < uri.length(); i++)
{
char c = uri[i];
switch (state)
{
case State::MEDIA:
switch (c)
{
case '?':
media = builder;
builder.clear();
state = State::PARAMS_KEY;
break;
case ':':
case '|':
case '=':
throw IllegalStateException(
"encountered '" + std::to_string(c) + "' within media definition at index " +
std::to_string(i) + " in " + uri, SOURCEINFO);
default:
builder += c;
}
break;
case PARAMS_KEY:
if ('=' == c)
{
if (0 == builder.length())
{
throw IllegalStateException(
"empty key not allowed at index " + std::to_string(i) + " in " + uri, SOURCEINFO);
}
key = builder;
builder.clear();
state = State::PARAMS_VALUE;
}
else
{
if (c == '|')
{
throw IllegalStateException(
"invalid end of key at index " + std::to_string(i) + " in " + uri, SOURCEINFO);
}
builder += c;
}
break;
case PARAMS_VALUE:
if ('|' == c)
{
params->emplace(key, builder);
builder.clear();
state = State::PARAMS_KEY;
}
else
{
builder += c;
}
break;
}
}
switch (state)
{
case State::MEDIA:
media = builder;
if (media != IPC_MEDIA && media != UDP_MEDIA)
{
throw IllegalArgumentException("unknown media: " + media, SOURCEINFO);
}
break;
case PARAMS_VALUE:
params->emplace(key, builder);
break;
default:
throw IllegalArgumentException("no more input found, state=" + std::to_string(state), SOURCEINFO);
}
return std::make_shared<ChannelUri>(prefix, media, std::move(params));
}
inline static std::string addSessionId(const std::string &channel, std::int32_t sessionId)
{
std::shared_ptr<ChannelUri> channelUri = ChannelUri::parse(channel);
channelUri->put(SESSION_ID_PARAM_NAME, std::to_string(sessionId));
return channelUri->toString();
}
private:
std::string m_prefix;
std::string m_media;
std::unique_ptr<std::unordered_map<std::string, std::string>> m_params;
};
}
#endif //AERON_CHANNEL_URI_H