ManagementServer .java

net.minecraft.server.jsonrpc.ManagementServer (class, 158 lines): members: authenticationHandler, scheduleHeartbeat, startWithoutTls, serverChannel, stop.

net.minecraft.server.jsonrpcclass158 linesserver5.8 KB

API Surface (9)

boolean scheduleHeartbeat(final NotificationManager notificationManager, final long period)
void onConnected(final Connection connection)
void onDisconnected(final Connection connection)
void startWithoutTls(final MinecraftApi minecraftApi)
void startWithTls(final MinecraftApi minecraftApi, final SslContext sslContext)
void initChannel(final Channel channel)
void stop(final boolean closeNioEventLoopGroup)
void tick()
int getPort()

Fields & Constants (7)

LOGGER
hostAndPort
authenticationHandler
serverChannel
eventLoopGroup
heartbeat
connections

Full Source

ManagementServer.java158 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.jsonrpc;
2
3
import com.google.common.collect.Sets;
4
import com.google.common.net.HostAndPort;
5
import com.google.common.util.concurrent.ThreadFactoryBuilder;
6
import com.mojang.logging.LogUtils;
7
import io.netty.bootstrap.ServerBootstrap;
8
import io.netty.channel.Channel;
9
import io.netty.channel.ChannelException;
10
import io.netty.channel.ChannelFuture;
11
import io.netty.channel.ChannelInitializer;
12
import io.netty.channel.ChannelOption;
13
import io.netty.channel.ChannelPipeline;
14
import io.netty.channel.EventLoopGroup;
15
import io.netty.channel.MultiThreadIoEventLoopGroup;
16
import io.netty.channel.nio.NioIoHandler;
17
import io.netty.channel.socket.nio.NioServerSocketChannel;
18
import io.netty.handler.codec.http.HttpObjectAggregator;
19
import io.netty.handler.codec.http.HttpServerCodec;
20
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
21
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
22
import io.netty.handler.logging.LogLevel;
23
import io.netty.handler.logging.LoggingHandler;
24
import io.netty.handler.ssl.SslContext;
25
import java.net.InetSocketAddress;
26
import java.util.Set;
27
import java.util.concurrent.ScheduledFuture;
28
import java.util.concurrent.TimeUnit;
29
import java.util.function.Consumer;
30
import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
31
import net.minecraft.server.jsonrpc.security.AuthenticationHandler;
32
import net.minecraft.server.jsonrpc.websocket.JsonToWebSocketEncoder;
33
import net.minecraft.server.jsonrpc.websocket.WebSocketToJsonCodec;
34
import net.minecraft.server.notifications.NotificationManager;
35
import org.jspecify.annotations.Nullable;
36
import org.slf4j.Logger;
37
38
public class ManagementServer {
39
	private static final Logger LOGGER = LogUtils.getLogger();
40
	private final HostAndPort hostAndPort;
41
	private final AuthenticationHandler authenticationHandler;
42
	private @Nullable Channel serverChannel;
43
	private final EventLoopGroup eventLoopGroup;
44
	private @Nullable ScheduledFuture<?> heartbeat;
45
	private final Set<Connection> connections = Sets.newIdentityHashSet();
46
47
	public ManagementServer(final HostAndPort hostAndPort, final AuthenticationHandler authenticationHandler) {
48
		this.hostAndPort = hostAndPort;
49
		this.authenticationHandler = authenticationHandler;
50
		this.eventLoopGroup = new MultiThreadIoEventLoopGroup(
51
			0, new ThreadFactoryBuilder().setNameFormat("Management server IO #%d").setDaemon(true).build(), NioIoHandler.newFactory()
52
		);
53
	}
54
55
	public ManagementServer(final HostAndPort hostAndPort, final AuthenticationHandler authenticationHandler, final EventLoopGroup eventLoopGroup) {
56
		this.hostAndPort = hostAndPort;
57
		this.authenticationHandler = authenticationHandler;
58
		this.eventLoopGroup = eventLoopGroup;
59
	}
60
61
	public boolean scheduleHeartbeat(final NotificationManager notificationManager, final long period) {
62
		if (this.heartbeat != null && !this.heartbeat.cancel(true)) {
63
			LOGGER.warn("The existing heartbeat was not canceled and the new heartbeat of {} seconds has not been applied.", period);
64
			return false;
65
		}
66
67
		if (period > 0L) {
68
			this.heartbeat = this.eventLoopGroup.scheduleAtFixedRate(notificationManager::statusHeartbeat, period, period, TimeUnit.SECONDS);
69
		}
70
71
		return true;
72
	}
73
74
	public void onConnected(final Connection connection) {
75
		synchronized (this.connections) {
76
			this.connections.add(connection);
77
		}
78
	}
79
80
	public void onDisconnected(final Connection connection) {
81
		synchronized (this.connections) {
82
			this.connections.remove(connection);
83
		}
84
	}
85
86
	public void startWithoutTls(final MinecraftApi minecraftApi) {
87
		this.start(minecraftApi, null);
88
	}
89
90
	public void startWithTls(final MinecraftApi minecraftApi, final SslContext sslContext) {
91
		this.start(minecraftApi, sslContext);
92
	}
93
94
	private void start(final MinecraftApi minecraftApi, final @Nullable SslContext sslContext) {
95
		final JsonRpcLogger jsonrpcLogger = new JsonRpcLogger();
96
		ChannelFuture channel = new ServerBootstrap()
97
			.handler(new LoggingHandler(LogLevel.DEBUG))
98
			.channel(NioServerSocketChannel.class)
99
			.childHandler(
100
				new ChannelInitializer<Channel>() {
101
					@Override
102
					protected void initChannel(final Channel channel) {
103
						try {
104
							channel.config().setOption(ChannelOption.TCP_NODELAY, true);
105
						} catch (ChannelException var3) {
106
						}
107
108
						ChannelPipeline pipeline = channel.pipeline();
109
						if (sslContext != null) {
110
							pipeline.addLast(sslContext.newHandler(channel.alloc()));
111
						}
112
113
						pipeline.addLast(new HttpServerCodec())
114
							.addLast(new HttpObjectAggregator(65536))
115
							.addLast(ManagementServer.this.authenticationHandler)
116
							.addLast(new WebSocketServerProtocolHandler("/"))
117
							.addLast(new WebSocketFrameAggregator(65536))
118
							.addLast(new WebSocketToJsonCodec())
119
							.addLast(new JsonToWebSocketEncoder())
120
							.addLast(new Connection(channel, ManagementServer.this, minecraftApi, jsonrpcLogger));
121
					}
122
				}
123
			)
124
			.group(this.eventLoopGroup)
125
			.localAddress(this.hostAndPort.getHost(), this.hostAndPort.getPort())
126
			.bind();
127
		this.serverChannel = channel.channel();
128
		channel.syncUninterruptibly();
129
		LOGGER.info("Json-RPC Management connection listening on {}:{}", this.hostAndPort.getHost(), this.getPort());
130
	}
131
132
	public void stop(final boolean closeNioEventLoopGroup) throws InterruptedException {
133
		if (this.serverChannel != null) {
134
			this.serverChannel.close().sync();
135
			this.serverChannel = null;
136
		}
137
138
		this.connections.clear();
139
		if (closeNioEventLoopGroup) {
140
			this.eventLoopGroup.shutdownGracefully().sync();
141
		}
142
	}
143
144
	public void tick() {
145
		this.forEachConnection(Connection::tick);
146
	}
147
148
	public int getPort() {
149
		return this.serverChannel != null ? ((InetSocketAddress)this.serverChannel.localAddress()).getPort() : this.hostAndPort.getPort();
150
	}
151
152
	void forEachConnection(final Consumer<Connection> action) {
153
		synchronized (this.connections) {
154
			this.connections.forEach(action);
155
		}
156
	}
157
}
158

Decompiled Minecraft 26.3 source of ManagementServer (net/minecraft/server/jsonrpc/ManagementServer.java) with Mojang official mappings, stored verbatim on the site.