mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Closable server
This commit is contained in:
@@ -22,6 +22,7 @@ import org.toop.framework.game.games.tictactoe.BitboardTicTacToe;
|
|||||||
import org.toop.framework.game.players.ArtificialPlayer;
|
import org.toop.framework.game.players.ArtificialPlayer;
|
||||||
import org.toop.framework.game.players.OnlinePlayer;
|
import org.toop.framework.game.players.OnlinePlayer;
|
||||||
import org.toop.framework.game.players.RandomAI;
|
import org.toop.framework.game.players.RandomAI;
|
||||||
|
import org.toop.framework.networking.server.MasterServer;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -32,7 +33,8 @@ import java.util.concurrent.TimeUnit;
|
|||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public final class Server {
|
public final class Server {
|
||||||
// TODO: Keep track of listeners. Remove them on Server connection close so reference is deleted.
|
private MasterServer masterServer;
|
||||||
|
|
||||||
private String user = "";
|
private String user = "";
|
||||||
private long clientId = -1;
|
private long clientId = -1;
|
||||||
|
|
||||||
@@ -60,10 +62,13 @@ public final class Server {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Server(String ip, String port, String user) {
|
||||||
|
this(ip, port, user, null);
|
||||||
|
}
|
||||||
|
|
||||||
// Server has to deal with ALL network related listen events. This "server" can then interact with the manager to make stuff happen.
|
// Server has to deal with ALL network related listen events. This "server" can then interact with the manager to make stuff happen.
|
||||||
// This prevents data races where events get sent to the game manager but the manager isn't ready yet.
|
// This prevents data races where events get sent to the game manager but the manager isn't ready yet.
|
||||||
public Server(String ip, String port, String user) {
|
public Server(String ip, String port, String user, MasterServer masterServer) {
|
||||||
if (ip.split("\\.").length < 4) {
|
if (ip.split("\\.").length < 4) {
|
||||||
new ErrorPopup("\"" + ip + "\" " + AppContext.getString("is-not-a-valid-ip-address"));
|
new ErrorPopup("\"" + ip + "\" " + AppContext.getString("is-not-a-valid-ip-address"));
|
||||||
return;
|
return;
|
||||||
@@ -83,6 +88,8 @@ public final class Server {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.masterServer = masterServer;
|
||||||
|
|
||||||
final int reconnectAttempts = 10;
|
final int reconnectAttempts = 10;
|
||||||
|
|
||||||
LoadingWidget loading = new LoadingWidget(
|
LoadingWidget loading = new LoadingWidget(
|
||||||
@@ -281,6 +288,10 @@ public final class Server {
|
|||||||
stopScheduler();
|
stopScheduler();
|
||||||
connectFlow.unsubscribeAll();
|
connectFlow.unsubscribeAll();
|
||||||
|
|
||||||
|
if (masterServer != null) {
|
||||||
|
masterServer.stop();
|
||||||
|
}
|
||||||
|
|
||||||
WidgetContainer.getCurrentView().transitionPrevious();
|
WidgetContainer.getCurrentView().transitionPrevious();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,8 @@ public class OnlineView extends ViewWidget {
|
|||||||
new Server(
|
new Server(
|
||||||
"127.0.0.1",
|
"127.0.0.1",
|
||||||
"6666",
|
"6666",
|
||||||
"host"
|
"host",
|
||||||
|
a
|
||||||
);
|
);
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,11 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class MasterServer {
|
public class MasterServer {
|
||||||
private final int port;
|
private final int port;
|
||||||
public final Server gs;
|
private final Server gs;
|
||||||
|
|
||||||
|
ChannelFuture future;
|
||||||
|
EventLoopGroup bossGroup;
|
||||||
|
EventLoopGroup workerGroup;
|
||||||
|
|
||||||
public MasterServer(int port, Map<String, Class<? extends TurnBasedGame>> gameTypes, Duration challengeDuration) {
|
public MasterServer(int port, Map<String, Class<? extends TurnBasedGame>> gameTypes, Duration challengeDuration) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
@@ -28,11 +32,10 @@ public class MasterServer {
|
|||||||
|
|
||||||
public void start() throws InterruptedException {
|
public void start() throws InterruptedException {
|
||||||
|
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
bossGroup = new NioEventLoopGroup(1);
|
||||||
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
|
workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||||
bootstrap.group(workerGroup);
|
bootstrap.group(workerGroup);
|
||||||
bootstrap.channel(NioServerSocketChannel.class);
|
bootstrap.channel(NioServerSocketChannel.class);
|
||||||
@@ -57,8 +60,7 @@ public class MasterServer {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
future = bootstrap.bind(port).sync();
|
||||||
System.out.println("MasterServer listening on port " + port);
|
|
||||||
|
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
} finally {
|
} finally {
|
||||||
@@ -66,4 +68,18 @@ public class MasterServer {
|
|||||||
workerGroup.shutdownGracefully();
|
workerGroup.shutdownGracefully();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
if (future == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
future.channel().close();
|
||||||
|
bossGroup.shutdownGracefully();
|
||||||
|
workerGroup.shutdownGracefully();
|
||||||
|
|
||||||
|
future = null;
|
||||||
|
bossGroup = null;
|
||||||
|
workerGroup = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user