-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNetworkConfig.java
More file actions
367 lines (309 loc) · 10.8 KB
/
NetworkConfig.java
File metadata and controls
367 lines (309 loc) · 10.8 KB
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
357
358
359
360
361
362
363
364
365
366
367
package dev.hytaleone.query.config;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import io.lettuce.core.RedisURI;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Configuration for network mode.
*
* <p>Example configuration for a game server (publish only):
* <pre>
* {
* "Network": {
* "Enabled": true,
* "ServerId": "survival-0",
* "NetworkId": "my-network",
* "Mode": "PUBLISH",
* "Store": {
* "Type": "redis",
* "Redis": {
* "Host": "redis.example.com",
* "Port": 6379
* }
* }
* }
* }
* </pre>
*
* <p>Example configuration for a lobby/hub (aggregate mode):
* <pre>
* {
* "Network": {
* "Enabled": true,
* "ServerId": "lobby-0",
* "NetworkId": "my-network",
* "Mode": "AGGREGATE",
* "Store": {
* "Type": "redis",
* "Redis": {
* "Host": "redis.example.com",
* "Port": 6379
* }
* }
* }
* }
* </pre>
*
* @see NetworkMode
*/
public class NetworkConfig {
public static final BuilderCodec<NetworkConfig> CODEC = BuilderCodec.builder(NetworkConfig.class, NetworkConfig::new)
.addField(new KeyedCodec<>("Enabled", Codec.BOOLEAN),
(o, v) -> o.enabled = v, o -> o.enabled)
.addField(new KeyedCodec<>("ServerId", Codec.STRING),
(o, v) -> o.serverId = v, o -> o.serverId)
.addField(new KeyedCodec<>("NetworkId", Codec.STRING),
(o, v) -> o.networkId = v, o -> o.networkId)
.addField(new KeyedCodec<>("Mode", Codec.STRING),
(o, v) -> o.mode = parseMode(v), o -> o.mode.name())
.addField(new KeyedCodec<>("Store", StoreConfig.CODEC),
(o, v) -> o.store = v != null ? v : new StoreConfig(),
o -> o.store)
.addField(new KeyedCodec<>("Timing", TimingConfig.CODEC),
(o, v) -> o.timing = v != null ? v : new TimingConfig(),
o -> o.timing)
.build();
private boolean enabled = false;
private String serverId = "server-1";
private String networkId = "default";
private NetworkMode mode = NetworkMode.AGGREGATE;
private StoreConfig store = new StoreConfig();
private TimingConfig timing = new TimingConfig();
private static NetworkMode parseMode(String value) {
if (value == null || value.isBlank()) {
return NetworkMode.AGGREGATE;
}
try {
return NetworkMode.valueOf(value.toUpperCase());
} catch (IllegalArgumentException e) {
return NetworkMode.AGGREGATE;
}
}
public NetworkConfig() {
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Nonnull
public String getServerId() {
return serverId != null && !serverId.isBlank() ? serverId : "server-1";
}
public void setServerId(@Nullable String serverId) {
this.serverId = serverId;
}
@Nonnull
public String getNetworkId() {
return networkId != null ? networkId : "default";
}
public void setNetworkId(@Nullable String networkId) {
this.networkId = networkId;
}
@Nonnull
public NetworkMode getMode() {
return mode;
}
public void setMode(@Nonnull NetworkMode mode) {
this.mode = mode;
}
/**
* Check if this server subscribes to network events (SYNC or AGGREGATE mode).
*/
public boolean shouldSync() {
return mode == NetworkMode.SYNC || mode == NetworkMode.AGGREGATE;
}
/**
* Check if this server serves aggregated data in responses (AGGREGATE mode).
*/
public boolean shouldAggregate() {
return mode == NetworkMode.AGGREGATE;
}
@Nonnull
public StoreConfig getStore() {
return store;
}
public void setStore(@Nonnull StoreConfig store) {
this.store = store;
}
@Nonnull
public TimingConfig getTiming() {
return timing;
}
public void setTiming(@Nonnull TimingConfig timing) {
this.timing = timing;
}
/**
* Configuration for the state store.
*/
public static class StoreConfig {
public static final BuilderCodec<StoreConfig> CODEC = BuilderCodec.builder(StoreConfig.class, StoreConfig::new)
.addField(new KeyedCodec<>("Type", Codec.STRING),
(o, v) -> o.type = v, o -> o.type)
.addField(new KeyedCodec<>("Redis", RedisConfig.CODEC),
(o, v) -> o.redis = v != null ? v : new RedisConfig(),
o -> o.redis)
.build();
private String type = "redis";
private RedisConfig redis = new RedisConfig();
public StoreConfig() {
}
@Nonnull
public String getType() {
return type != null ? type : "redis";
}
public void setType(@Nullable String type) {
this.type = type;
}
public boolean isRedis() {
return "redis".equalsIgnoreCase(type) || type == null;
}
@Nonnull
public RedisConfig getRedis() {
return redis;
}
public void setRedis(@Nonnull RedisConfig redis) {
this.redis = redis;
}
}
/**
* Redis connection configuration.
*/
public static class RedisConfig {
public static final BuilderCodec<RedisConfig> CODEC = BuilderCodec.builder(RedisConfig.class, RedisConfig::new)
.addField(new KeyedCodec<>("Host", Codec.STRING),
(o, v) -> o.host = v, o -> o.host)
.addField(new KeyedCodec<>("Port", Codec.INTEGER),
(o, v) -> o.port = v, o -> o.port)
.addField(new KeyedCodec<>("Username", Codec.STRING),
(o, v) -> o.username = v, o -> o.username)
.addField(new KeyedCodec<>("Password", Codec.STRING),
(o, v) -> o.password = v, o -> o.password)
.addField(new KeyedCodec<>("Database", Codec.INTEGER),
(o, v) -> o.database = v, o -> o.database)
.addField(new KeyedCodec<>("UseTLS", Codec.BOOLEAN),
(o, v) -> o.useTLS = v, o -> o.useTLS)
.build();
private String host = "localhost";
private int port = 6379;
private String username = null;
private String password = null;
private int database = 0;
private boolean useTLS = false;
public RedisConfig() {
}
@Nonnull
public String getHost() {
return host != null ? host : "localhost";
}
public void setHost(@Nullable String host) {
this.host = host;
}
public int getPort() {
return port > 0 ? port : 6379;
}
public void setPort(int port) {
this.port = port;
}
@Nullable
public String getUsername() {
return username;
}
public void setUsername(@Nullable String username) {
this.username = username;
}
@Nullable
public String getPassword() {
return password;
}
public void setPassword(@Nullable String password) {
this.password = password;
}
public int getDatabase() {
return database >= 0 ? database : 0;
}
public void setDatabase(int database) {
this.database = database;
}
public boolean isUseTLS() {
return useTLS;
}
public void setUseTLS(boolean useTLS) {
this.useTLS = useTLS;
}
/**
* Build a Redis URI string for Lettuce.
*/
@Deprecated
@Nonnull
public String toRedisUri() {
StringBuilder sb = new StringBuilder();
sb.append(useTLS ? "rediss://" : "redis://");
if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
sb.append(username).append(":").append(password).append("@");
} else if (password != null && !password.isEmpty()) {
sb.append(":").append(password).append("@");
}
sb.append(host).append(":").append(port);
if (database > 0) {
sb.append("/").append(database);
}
return sb.toString();
}
/**
* Build a Redis URI for Lettuce connection.
*/
@Nonnull
public RedisURI toRedisURI() {
RedisURI.Builder builder = RedisURI.builder()
.withHost(getHost())
.withPort(getPort())
.withDatabase(getDatabase());
if (getUsername() != null && !getUsername().isEmpty()) {
builder.withAuthentication(getUsername(), getPassword() != null ? getPassword().toCharArray() : new char[0]);
} else if (getPassword() != null && !getPassword().isEmpty()) {
builder.withPassword(getPassword().toCharArray());
}
if (isUseTLS()) {
builder.withSsl(true);
}
return builder.build();
}
}
/**
* Timeout configuration.
*/
public static class TimingConfig {
public static final BuilderCodec<TimingConfig> CODEC = BuilderCodec.builder(TimingConfig.class, TimingConfig::new)
.addField(new KeyedCodec<>("HeartbeatIntervalSeconds", Codec.INTEGER),
(o, v) -> o.heartbeatIntervalSeconds = v, o -> o.heartbeatIntervalSeconds)
.addField(new KeyedCodec<>("CacheRefreshSeconds", Codec.INTEGER),
(o, v) -> o.cacheRefreshSeconds = v, o -> o.cacheRefreshSeconds)
.build();
private int heartbeatIntervalSeconds = 15;
private int cacheRefreshSeconds = 60;
public TimingConfig() {
}
public int getHeartbeatIntervalSeconds() {
return Math.max(5, Math.min(heartbeatIntervalSeconds, 300));
}
public void setHeartbeatIntervalSeconds(int seconds) {
this.heartbeatIntervalSeconds = seconds;
}
public long getHeartbeatIntervalMillis() {
return getHeartbeatIntervalSeconds() * 1000L;
}
public int getCacheRefreshSeconds() {
return Math.max(10, Math.min(cacheRefreshSeconds, 300));
}
public void setCacheRefreshSeconds(int seconds) {
this.cacheRefreshSeconds = seconds;
}
public long getCacheRefreshMillis() {
return getCacheRefreshSeconds() * 1000L;
}
}
}