use of games.strategy.engine.message.ChannelMessenger in project triplea by triplea-game.
the class ChannelMessengerTest method testMultipleClients.
@Test
public void testMultipleClients() throws Exception {
// set up the client and server
// so that the client has 1 subscribor, and the server knows about it
final RemoteName test = new RemoteName(IChannelBase.class, "test");
final ChannelSubscribor client1Subscribor = new ChannelSubscribor();
clientChannelMessenger.registerChannelSubscriber(client1Subscribor, test);
assertHasChannel(test, unifiedMessengerHub);
assertEquals(1, clientChannelMessenger.getUnifiedMessenger().getLocalEndPointCount(test));
// add a new client
final String mac = MacFinder.getHashedMacAddress();
final ClientMessenger clientMessenger2 = new ClientMessenger("localhost", serverPort, "client2", mac);
final ChannelMessenger client2 = new ChannelMessenger(new UnifiedMessenger(clientMessenger2));
((IChannelBase) client2.getChannelBroadcastor(test)).testString("a");
assertCallCountIs(client1Subscribor, 1);
}
use of games.strategy.engine.message.ChannelMessenger in project triplea by triplea-game.
the class ChannelMessengerTest method setUp.
@BeforeEach
public void setUp() throws IOException {
serverMessenger = new ServerMessenger("Server", 0);
serverMessenger.setAcceptNewConnections(true);
serverPort = serverMessenger.getLocalNode().getSocketAddress().getPort();
final String mac = MacFinder.getHashedMacAddress();
clientMessenger = new ClientMessenger("localhost", serverPort, "client1", mac);
final UnifiedMessenger unifiedMessenger = new UnifiedMessenger(serverMessenger);
unifiedMessengerHub = unifiedMessenger.getHub();
serverChannelMessenger = new ChannelMessenger(unifiedMessenger);
clientChannelMessenger = new ChannelMessenger(new UnifiedMessenger(clientMessenger));
}
use of games.strategy.engine.message.ChannelMessenger in project triplea by triplea-game.
the class VaultTest method testLocal.
@Test
public void testLocal() throws Exception {
final IServerMessenger messenger = mock(IServerMessenger.class);
when(messenger.getLocalNode()).thenReturn(new Node("dummy", InetAddress.getLocalHost(), 0));
final UnifiedMessenger unifiedMessenger = new UnifiedMessenger(messenger);
final ChannelMessenger channelMessenger = new ChannelMessenger(unifiedMessenger);
// RemoteMessenger remoteMessenger = new RemoteMessenger(unifiedMessenger);
final Vault vault = new Vault(channelMessenger);
final byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };
final VaultID id = vault.lock(data);
vault.unlock(id);
assertArrayEquals(data, vault.get(id));
vault.release(id);
}
use of games.strategy.engine.message.ChannelMessenger in project triplea by triplea-game.
the class ClientModel method startGameInNewThread.
private void startGameInNewThread(final byte[] gameData, final Map<String, INode> players, final boolean gameRunning) {
final GameData data;
try {
// this normally takes a couple seconds, but can take
// up to 60 seconds for a freaking huge game
data = IoUtils.readFromMemory(gameData, GameDataManager::loadGame);
} catch (final IOException ex) {
ClientLogger.logQuietly("Failed to load game", ex);
return;
}
objectStreamFactory.setData(data);
final Map<String, String> playerMapping = playersToNodes.entrySet().stream().filter(e -> e.getValue().equals(messenger.getLocalNode().getName())).collect(Collectors.toMap(Map.Entry::getKey, e -> IGameLoader.CLIENT_PLAYER_TYPE));
final Set<IGamePlayer> playerSet = data.getGameLoader().createPlayers(playerMapping);
final Messengers messengers = new Messengers(messenger, remoteMessenger, channelMessenger);
game = new ClientGame(data, playerSet, players, messengers);
new Thread(() -> {
SwingUtilities.invokeLater(() -> JOptionPane.getFrameForComponent(ui).setVisible(false));
try {
// game will be null if we loose the connection
if (game != null) {
try {
data.getGameLoader().startGame(game, playerSet, false);
data.testLocksOnRead();
} catch (final Exception e) {
ClientLogger.logError("Failed to start Game", e);
game.shutDown();
messenger.shutDown();
gameLoadingWindow.doneWait();
// an ugly hack, we need a better
// way to get the main frame
GameRunner.clientLeftGame();
}
}
if (!gameRunning) {
((IServerReady) remoteMessenger.getRemote(CLIENT_READY_CHANNEL)).clientReady();
}
} finally {
gameLoadingWindow.doneWait();
}
}, "Client Game Launcher").start();
}
use of games.strategy.engine.message.ChannelMessenger in project triplea by triplea-game.
the class ClientModel method createClientMessenger.
boolean createClientMessenger(Component ui) {
gameDataOnStartup = gameSelectorModel.getGameData();
gameSelectorModel.setCanSelect(false);
ui = JOptionPane.getFrameForComponent(ui);
this.ui = ui;
// load in the saved name!
final ClientProps props = getProps(ui);
if (props == null) {
gameSelectorModel.setCanSelect(true);
cancel();
return false;
}
final String name = props.getName();
logger.log(Level.FINE, "Client playing as:" + name);
ClientSetting.PLAYER_NAME.save(name);
ClientSetting.flush();
final int port = props.getPort();
if (port >= 65536 || port <= 0) {
EventThreadJOptionPane.showMessageDialog(ui, "Invalid Port: " + port, "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
final String address = props.getHost();
try {
final String mac = MacFinder.getHashedMacAddress();
messenger = new ClientMessenger(address, port, name, mac, objectStreamFactory, new ClientLogin(this.ui));
} catch (final CouldNotLogInException e) {
EventThreadJOptionPane.showMessageDialog(ui, e.getMessage());
return false;
} catch (final Exception ioe) {
ioe.printStackTrace(System.out);
EventThreadJOptionPane.showMessageDialog(ui, "Unable to connect:" + ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
messenger.addErrorListener(this);
final UnifiedMessenger unifiedMessenger = new UnifiedMessenger(messenger);
channelMessenger = new ChannelMessenger(unifiedMessenger);
remoteMessenger = new RemoteMessenger(unifiedMessenger);
channelMessenger.registerChannelSubscriber(channelListener, IClientChannel.CHANNEL_NAME);
chatPanel = new ChatPanel(messenger, channelMessenger, remoteMessenger, ServerModel.CHAT_NAME, Chat.ChatSoundProfile.GAME_CHATROOM);
if (getIsServerHeadlessTest()) {
gameSelectorModel.setClientModelForHostBots(this);
((ChatPanel) chatPanel).getChatMessagePanel().addServerMessage("Welcome to an automated dedicated host service (a host bot). " + "\nIf anyone disconnects, the autosave will be reloaded (a save might be loaded right now). " + "\nYou can get the current save, or you can load a save (only saves that it has the map for).");
}
remoteMessenger.registerRemote(observerWaitingToJoin, ServerModel.getObserverWaitingToStartName(messenger.getLocalNode()));
// save this, it will be cleared later
gameDataOnStartup = gameSelectorModel.getGameData();
final IServerStartupRemote serverStartup = getServerStartup();
final PlayerListing players = serverStartup.getPlayerListing();
internalPlayerListingChanged(players);
if (!serverStartup.isGameStarted(messenger.getLocalNode())) {
remoteMessenger.unregisterRemote(ServerModel.getObserverWaitingToStartName(messenger.getLocalNode()));
}
gameSelectorModel.setIsHostHeadlessBot(hostIsHeadlessBot);
return true;
}
Aggregations