Search in sources :

Example 1 with ChannelMessenger

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);
}
Also used : RemoteName(games.strategy.engine.message.RemoteName) IChannelSubscribor(games.strategy.engine.message.IChannelSubscribor) ChannelMessenger(games.strategy.engine.message.ChannelMessenger) ClientMessenger(games.strategy.net.ClientMessenger) Test(org.junit.jupiter.api.Test)

Example 2 with ChannelMessenger

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));
}
Also used : ServerMessenger(games.strategy.net.ServerMessenger) IServerMessenger(games.strategy.net.IServerMessenger) ChannelMessenger(games.strategy.engine.message.ChannelMessenger) ClientMessenger(games.strategy.net.ClientMessenger) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ChannelMessenger

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);
}
Also used : UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) Node(games.strategy.net.Node) IServerMessenger(games.strategy.net.IServerMessenger) ChannelMessenger(games.strategy.engine.message.ChannelMessenger) Test(org.junit.jupiter.api.Test)

Example 4 with ChannelMessenger

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();
}
Also used : ClientGame(games.strategy.engine.framework.ClientGame) ClientMessenger(games.strategy.net.ClientMessenger) IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) TRIPLEA_STARTED(games.strategy.engine.framework.CliProperties.TRIPLEA_STARTED) TRIPLEA_HOST(games.strategy.engine.framework.CliProperties.TRIPLEA_HOST) Map(java.util.Map) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) SetMapClientAction(games.strategy.engine.framework.network.ui.SetMapClientAction) INode(games.strategy.net.INode) PlayerListing(games.strategy.engine.framework.message.PlayerListing) Collection(java.util.Collection) Set(java.util.Set) Logger(java.util.logging.Logger) GameRunner(games.strategy.engine.framework.GameRunner) Component(java.awt.Component) Collectors(java.util.stream.Collectors) TRIPLEA_PORT(games.strategy.engine.framework.CliProperties.TRIPLEA_PORT) IChatPanel(games.strategy.engine.chat.IChatPanel) UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) IChannelMessenger(games.strategy.engine.message.IChannelMessenger) CountDownLatch(java.util.concurrent.CountDownLatch) GameData(games.strategy.engine.data.GameData) List(java.util.List) ChangeToAutosaveClientAction(games.strategy.engine.framework.network.ui.ChangeToAutosaveClientAction) EventThreadJOptionPane(games.strategy.util.EventThreadJOptionPane) TRIPLEA_NAME(games.strategy.engine.framework.CliProperties.TRIPLEA_NAME) WaitWindow(games.strategy.engine.framework.ui.background.WaitWindow) SwingAction(games.strategy.ui.SwingAction) Setter(lombok.Setter) Getter(lombok.Getter) ChangeGameOptionsClientAction(games.strategy.engine.framework.network.ui.ChangeGameOptionsClientAction) Messengers(games.strategy.net.Messengers) HashMap(java.util.HashMap) Action(javax.swing.Action) MacFinder(games.strategy.net.MacFinder) RemoteName(games.strategy.engine.message.RemoteName) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) IMessengerErrorListener(games.strategy.net.IMessengerErrorListener) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) SwingUtilities(javax.swing.SwingUtilities) ClientLogin(games.strategy.engine.framework.startup.login.ClientLogin) ClientOptions(games.strategy.engine.framework.startup.ui.ClientOptions) IMessenger(games.strategy.net.IMessenger) GameDataManager(games.strategy.engine.framework.GameDataManager) IoUtils(games.strategy.io.IoUtils) ClientSetting(games.strategy.triplea.settings.ClientSetting) Nonnull(javax.annotation.Nonnull) GetGameSaveClientAction(games.strategy.engine.framework.network.ui.GetGameSaveClientAction) ChangeGameToSaveGameClientAction(games.strategy.engine.framework.network.ui.ChangeGameToSaveGameClientAction) TRIPLEA_CLIENT(games.strategy.engine.framework.CliProperties.TRIPLEA_CLIENT) Interruptibles(games.strategy.util.Interruptibles) SaveGameFileChooser(games.strategy.engine.framework.ui.SaveGameFileChooser) IOException(java.io.IOException) Chat(games.strategy.engine.chat.Chat) JOptionPane(javax.swing.JOptionPane) IServerReady(games.strategy.engine.framework.startup.launcher.IServerReady) GameObjectStreamFactory(games.strategy.engine.framework.GameObjectStreamFactory) TimeUnit(java.util.concurrent.TimeUnit) ClientLogger(games.strategy.debug.ClientLogger) IRemoteMessenger(games.strategy.engine.message.IRemoteMessenger) ChannelMessenger(games.strategy.engine.message.ChannelMessenger) IGameLoader(games.strategy.engine.framework.IGameLoader) CouldNotLogInException(games.strategy.net.CouldNotLogInException) Preconditions(com.google.common.base.Preconditions) ChatPanel(games.strategy.engine.chat.ChatPanel) IClientMessenger(games.strategy.net.IClientMessenger) IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) Messengers(games.strategy.net.Messengers) GameData(games.strategy.engine.data.GameData) IOException(java.io.IOException) ClientGame(games.strategy.engine.framework.ClientGame) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IOException(java.io.IOException) CouldNotLogInException(games.strategy.net.CouldNotLogInException)

Example 5 with ChannelMessenger

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;
}
Also used : RemoteMessenger(games.strategy.engine.message.RemoteMessenger) IRemoteMessenger(games.strategy.engine.message.IRemoteMessenger) UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) CouldNotLogInException(games.strategy.net.CouldNotLogInException) IChatPanel(games.strategy.engine.chat.IChatPanel) ChatPanel(games.strategy.engine.chat.ChatPanel) IOException(java.io.IOException) CouldNotLogInException(games.strategy.net.CouldNotLogInException) IChannelMessenger(games.strategy.engine.message.IChannelMessenger) ChannelMessenger(games.strategy.engine.message.ChannelMessenger) PlayerListing(games.strategy.engine.framework.message.PlayerListing) ClientMessenger(games.strategy.net.ClientMessenger) IClientMessenger(games.strategy.net.IClientMessenger) ClientLogin(games.strategy.engine.framework.startup.login.ClientLogin)

Aggregations

ChannelMessenger (games.strategy.engine.message.ChannelMessenger)8 UnifiedMessenger (games.strategy.engine.message.unifiedmessenger.UnifiedMessenger)6 ClientMessenger (games.strategy.net.ClientMessenger)6 IServerMessenger (games.strategy.net.IServerMessenger)5 IChannelMessenger (games.strategy.engine.message.IChannelMessenger)4 IRemoteMessenger (games.strategy.engine.message.IRemoteMessenger)4 RemoteMessenger (games.strategy.engine.message.RemoteMessenger)4 ServerMessenger (games.strategy.net.ServerMessenger)4 ChatPanel (games.strategy.engine.chat.ChatPanel)3 IChatPanel (games.strategy.engine.chat.IChatPanel)3 IOException (java.io.IOException)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 PlayerListing (games.strategy.engine.framework.message.PlayerListing)2 ClientLogin (games.strategy.engine.framework.startup.login.ClientLogin)2 RemoteName (games.strategy.engine.message.RemoteName)2 CouldNotLogInException (games.strategy.net.CouldNotLogInException)2 IClientMessenger (games.strategy.net.IClientMessenger)2 Test (org.junit.jupiter.api.Test)2 Preconditions (com.google.common.base.Preconditions)1 ClientLogger (games.strategy.debug.ClientLogger)1