Skip to content

Commit 71214a8

Browse files
committed
Some more lifecycle related to setting up and shutting down a host.
Now one can join their own hosted game as well as termining the hosting.
1 parent d2881d7 commit 71214a8

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

sim-eth-basic/src/main/java/example/ConnectState.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void execute( Button source ) {
205205

206206
private class ConnectionObserver implements ClientStateListener, ErrorListener<Client> {
207207
public void clientConnected( final Client c ) {
208-
log.trace("clientConnected(" + c + ")");
208+
log.info("clientConnected(" + c + ")");
209209
getApplication().enqueue(new Callable() {
210210
public Object call() {
211211
connected();
@@ -215,7 +215,7 @@ public Object call() {
215215
}
216216

217217
public void clientDisconnected( final Client c, final DisconnectInfo info ) {
218-
log.trace("clientDisconnected(" + c + ", " + info + ")");
218+
log.info("clientDisconnected(" + c + ", " + info + ")");
219219
getApplication().enqueue(new Callable() {
220220
public Object call() {
221221
disconnected(info);

sim-eth-basic/src/main/java/example/HostState.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@
4545
import com.jme3.network.ConnectionListener;
4646
import com.jme3.network.HostedConnection;
4747
import com.jme3.network.Server;
48+
import com.jme3.scene.Node;
4849

50+
import com.simsilica.lemur.*;
4951
import com.simsilica.lemur.core.VersionedHolder;
52+
import com.simsilica.lemur.style.ElementId;
5053
import com.simsilica.state.DebugHudState;
5154
import com.simsilica.state.DebugHudState.Location;
5255

@@ -62,21 +65,49 @@ public class HostState extends BaseAppState {
6265
static Logger log = LoggerFactory.getLogger(HostState.class);
6366

6467
private GameServer gameServer;
68+
private int port;
6569

6670
private VersionedHolder<String> hostingState;
6771
private VersionedHolder<String> connectionCount;
6872

6973
private ConnectionListener connectionListener = new ConnectionObserver();
7074

75+
private Container hostWindow;
76+
7177
public HostState( int port, String description ) {
7278
try {
79+
this.port = port;
7380
this.gameServer = new GameServer(port, description);
7481
gameServer.getServer().addConnectionListener(connectionListener);
7582
} catch( IOException e ) {
7683
throw new RuntimeException("Error creating server", e);
7784
}
7885
}
7986

87+
protected void joinGame() {
88+
log.info("joinGame()");
89+
getStateManager().attach(new ConnectState("127.0.0.1", port));
90+
setEnabled(false); // hide our window
91+
}
92+
93+
protected void stopHosting() {
94+
log.info("stopHosting()");
95+
if( gameServer.getServer().isRunning() && !gameServer.getServer().getConnections().isEmpty() ) {
96+
String msg = "Really kick all " + gameServer.getServer().getConnections().size() + " connections?";
97+
getState(OptionPanelState.class).show("Disconnect", msg,
98+
new CallMethodAction("Yes", this, "detach"),
99+
new EmptyAction("No"),
100+
new EmptyAction("Cancel"));
101+
} else {
102+
// Just detach
103+
detach();
104+
}
105+
}
106+
107+
protected void detach() {
108+
getStateManager().detach(this);
109+
}
110+
80111
@Override
81112
protected void initialize( Application app ) {
82113
// We'll manage the server itself as part of the app state
@@ -91,24 +122,40 @@ protected void initialize( Application app ) {
91122

92123
connectionCount = getState(DebugHudState.class).createDebugValue("Connections", Location.Right);
93124
resetConnectionCount();
125+
126+
hostWindow = new Container();
127+
128+
// For now just something simple
129+
hostWindow.addChild(new Label("Hosting Control", new ElementId("title")));
130+
hostWindow.addChild(new ActionButton(new CallMethodAction("Join Game", this, "joinGame")));
131+
hostWindow.addChild(new ActionButton(new CallMethodAction("Stop Hosting", this, "stopHosting")));
94132
}
95133

96134
@Override
97135
protected void cleanup( Application app ) {
98-
gameServer.close();
136+
gameServer.close("Shutting down.");
99137
hostingState.setObject("Offline");
100138

101139
// And remove the debug messages anyway
102140
getState(DebugHudState.class).removeDebugValue("Hosting");
103141
getState(DebugHudState.class).removeDebugValue("Connections");
142+
143+
// And re-enable the main menu
144+
getState(MainMenuState.class).setEnabled(true);
104145
}
105146

106147
@Override
107148
protected void onEnable() {
149+
Node gui = ((Main)getApplication()).getGuiNode();
150+
151+
int height = getApplication().getCamera().getHeight();
152+
hostWindow.setLocalTranslation(10, height - 10, 0);
153+
gui.attachChild(hostWindow);
108154
}
109155

110156
@Override
111157
protected void onDisable() {
158+
hostWindow.removeFromParent();
112159
}
113160

114161
protected void resetConnectionCount() {

sim-eth-basic/src/main/java/example/net/server/GameServer.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import org.slf4j.*;
4242

43+
import com.jme3.network.HostedConnection;
4344
import com.jme3.network.Network;
4445
import com.jme3.network.Server;
4546
import com.jme3.network.service.rmi.RmiHostedService;
@@ -111,12 +112,18 @@ public void start() {
111112
}
112113

113114
/**
114-
* Closes the network host, stops all systems, and finally terminates
115-
* them. The GameServer is not restartable at this point.
115+
* Kicks all current connection, closes the network host, stops all systems, and
116+
* finally terminates them. The GameServer is not restartable at this point.
116117
*/
117-
public void close() {
118-
log.info("Stopping game server...");
118+
public void close( String kickMessage ) {
119+
log.info("Stopping game server..." + kickMessage);
119120
loop.stop();
121+
122+
if( kickMessage != null ) {
123+
for( HostedConnection conn : server.getConnections() ) {
124+
conn.close(kickMessage);
125+
}
126+
}
120127
server.close();
121128

122129
// The GameLoop dying should have already stopped the game systems
@@ -127,6 +134,14 @@ public void close() {
127134
log.info("Game server stopped.");
128135
}
129136

137+
/**
138+
* Closes the network host, stops all systems, and finally terminates
139+
* them. The GameServer is not restartable at this point.
140+
*/
141+
public void close() {
142+
close(null);
143+
}
144+
130145
/**
131146
* Allow running a basic dedicated server from the command line using
132147
* the default port. If we want something more advanced then we should

0 commit comments

Comments
 (0)