Search in sources :

Example 6 with ClientMessenger

use of games.strategy.net.ClientMessenger in project triplea by triplea-game.

the class RemoteMessengerTest method testMethodReturnsOnWait.

@Test
public void testMethodReturnsOnWait() throws Exception {
    // when the client shutdown, remotes created
    // on the client should not be visible on server
    final RemoteName test = new RemoteName(IFoo.class, "test");
    ServerMessenger server = null;
    ClientMessenger client = null;
    try {
        server = new ServerMessenger("server", 0);
        server.setAcceptNewConnections(true);
        final int serverPort = server.getLocalNode().getSocketAddress().getPort();
        final String mac = MacFinder.getHashedMacAddress();
        client = new ClientMessenger("localhost", serverPort, "client", mac);
        final UnifiedMessenger serverUnifiedMessenger = new UnifiedMessenger(server);
        final RemoteMessenger serverRemoteMessenger = new RemoteMessenger(serverUnifiedMessenger);
        final RemoteMessenger clientRemoteMessenger = new RemoteMessenger(new UnifiedMessenger(client));
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicBoolean started = new AtomicBoolean(false);
        final IFoo foo = new IFoo() {

            @Override
            public void foo() {
                started.set(true);
                Interruptibles.await(latch);
            }
        };
        clientRemoteMessenger.registerRemote(foo, test);
        serverUnifiedMessenger.getHub().waitForNodesToImplement(test.getName());
        assertTrue(serverUnifiedMessenger.getHub().hasImplementors(test.getName()));
        final AtomicReference<ConnectionLostException> rme = new AtomicReference<>(null);
        final Thread t = new Thread(() -> {
            try {
                final IFoo remoteFoo = (IFoo) serverRemoteMessenger.getRemote(test);
                remoteFoo.foo();
            } catch (final ConnectionLostException e) {
                rme.set(e);
            }
        });
        t.start();
        // wait for the thread to start
        while (started.get() == false) {
            Interruptibles.sleep(1);
        }
        Interruptibles.sleep(20);
        // TODO: we are getting a RemoteNotFoundException because the client is disconnecting before the invoke goes out
        // completely
        // Perhaps this situation should be changed to a ConnectionLostException or something else?
        client.shutDown();
        // when the client shutdowns, this should wake up.
        // and an error should be thrown
        // give the thread a chance to execute
        t.join(200);
        latch.countDown();
        assertNotNull(rme.get());
    } finally {
        shutdownServerAndClient(server, client);
    }
}
Also used : RemoteMessenger(games.strategy.engine.message.RemoteMessenger) ConnectionLostException(games.strategy.engine.message.ConnectionLostException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RemoteName(games.strategy.engine.message.RemoteName) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerMessenger(games.strategy.net.ServerMessenger) IServerMessenger(games.strategy.net.IServerMessenger) ClientMessenger(games.strategy.net.ClientMessenger) Test(org.junit.jupiter.api.Test)

Example 7 with ClientMessenger

use of games.strategy.net.ClientMessenger 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)

Example 8 with ClientMessenger

use of games.strategy.net.ClientMessenger in project triplea by triplea-game.

the class InGameLobbyWatcher method newInGameLobbyWatcher.

/**
 * Reads SystemProperties to see if we should connect to a lobby server
 *
 * <p>
 * After creation, those properties are cleared, since we should watch the first start game.
 * </p>
 *
 * @return null if no watcher should be created
 */
public static InGameLobbyWatcher newInGameLobbyWatcher(final IServerMessenger gameMessenger, final JComponent parent, final InGameLobbyWatcher oldWatcher) {
    final String host = System.getProperty(LOBBY_HOST);
    final String port = System.getProperty(LOBBY_PORT);
    final String hostedBy = System.getProperty(LOBBY_GAME_HOSTED_BY);
    if (host == null || port == null) {
        return null;
    }
    // clear the properties
    System.clearProperty(LOBBY_HOST);
    System.clearProperty(LOBBY_PORT);
    System.clearProperty(LOBBY_GAME_HOSTED_BY);
    // add them as temporary properties (in case we load an old savegame and need them again)
    System.setProperty(LOBBY_HOST + GameRunner.OLD_EXTENSION, host);
    System.setProperty(LOBBY_PORT + GameRunner.OLD_EXTENSION, port);
    System.setProperty(LOBBY_GAME_HOSTED_BY + GameRunner.OLD_EXTENSION, hostedBy);
    final IConnectionLogin login = challengeProperties -> {
        final Map<String, String> properties = new HashMap<>();
        properties.put(LobbyLoginValidator.ANONYMOUS_LOGIN, Boolean.TRUE.toString());
        properties.put(LobbyLoginValidator.LOBBY_VERSION, LobbyServer.LOBBY_VERSION.toString());
        properties.put(LobbyLoginValidator.LOBBY_WATCHER_LOGIN, Boolean.TRUE.toString());
        return properties;
    };
    try {
        System.out.println("host:" + host + " port:" + port);
        final String mac = MacFinder.getHashedMacAddress();
        final ClientMessenger messenger = new ClientMessenger(host, Integer.parseInt(port), getRealName(hostedBy) + "_" + LOBBY_WATCHER_NAME, mac, login);
        final UnifiedMessenger um = new UnifiedMessenger(messenger);
        final RemoteMessenger rm = new RemoteMessenger(um);
        final RemoteHostUtils rhu = new RemoteHostUtils(messenger.getServerNode(), gameMessenger);
        rm.registerRemote(rhu, RemoteHostUtils.getRemoteHostUtilsName(um.getLocalNode()));
        return new InGameLobbyWatcher(messenger, rm, gameMessenger, parent, oldWatcher);
    } catch (final Exception e) {
        ClientLogger.logQuietly("Failed to create in-game lobby watcher", e);
        return null;
    }
}
Also used : Observer(java.util.Observer) ClientMessenger(games.strategy.net.ClientMessenger) LOBBY_GAME_COMMENTS(games.strategy.engine.framework.CliProperties.LOBBY_GAME_COMMENTS) GUID(games.strategy.net.GUID) SERVER_PASSWORD(games.strategy.engine.framework.CliProperties.SERVER_PASSWORD) HashMap(java.util.HashMap) HeadlessGameServer(games.strategy.engine.framework.headlessGameServer.HeadlessGameServer) IConnectionLogin(games.strategy.net.IConnectionLogin) LobbyServer(games.strategy.engine.lobby.server.LobbyServer) MacFinder(games.strategy.net.MacFinder) IMessengerErrorListener(games.strategy.net.IMessengerErrorListener) SwingUtilities(javax.swing.SwingUtilities) Map(java.util.Map) IMessenger(games.strategy.net.IMessenger) GameDescription(games.strategy.engine.lobby.server.GameDescription) IConnectionChangeListener(games.strategy.net.IConnectionChangeListener) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) ClientContext(games.strategy.engine.ClientContext) JComponent(javax.swing.JComponent) LobbyLoginValidator(games.strategy.engine.lobby.server.login.LobbyLoginValidator) INode(games.strategy.net.INode) Frame(java.awt.Frame) ILobbyGameController(games.strategy.engine.lobby.server.ILobbyGameController) LOBBY_HOST(games.strategy.engine.framework.CliProperties.LOBBY_HOST) GameStepListener(games.strategy.engine.data.events.GameStepListener) GameStatus(games.strategy.engine.lobby.server.GameDescription.GameStatus) Instant(java.time.Instant) JOptionPane(javax.swing.JOptionPane) GameRunner(games.strategy.engine.framework.GameRunner) RemoteHostUtils(games.strategy.engine.lobby.server.RemoteHostUtils) TRIPLEA_PORT(games.strategy.engine.framework.CliProperties.TRIPLEA_PORT) UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) ClientLogger(games.strategy.debug.ClientLogger) GameSelectorModel(games.strategy.engine.framework.startup.mc.GameSelectorModel) IRemoteMessenger(games.strategy.engine.message.IRemoteMessenger) LOBBY_PORT(games.strategy.engine.framework.CliProperties.LOBBY_PORT) OpenFileUtility(games.strategy.net.OpenFileUtility) IServerMessenger(games.strategy.net.IServerMessenger) UrlConstants(games.strategy.triplea.UrlConstants) IGame(games.strategy.engine.framework.IGame) LOBBY_GAME_HOSTED_BY(games.strategy.engine.framework.CliProperties.LOBBY_GAME_HOSTED_BY) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) IRemoteMessenger(games.strategy.engine.message.IRemoteMessenger) UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) RemoteHostUtils(games.strategy.engine.lobby.server.RemoteHostUtils) HashMap(java.util.HashMap) Map(java.util.Map) IConnectionLogin(games.strategy.net.IConnectionLogin) ClientMessenger(games.strategy.net.ClientMessenger)

Example 9 with ClientMessenger

use of games.strategy.net.ClientMessenger in project triplea by triplea-game.

the class ChatIntegrationTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    serverMessenger = new ServerMessenger("Server", 0);
    serverMessenger.setAcceptNewConnections(true);
    final int serverPort = serverMessenger.getLocalNode().getSocketAddress().getPort();
    final String mac = MacFinder.getHashedMacAddress();
    client1Messenger = new ClientMessenger("localhost", serverPort, "client1", mac);
    client2Messenger = new ClientMessenger("localhost", serverPort, "client2", mac);
    final UnifiedMessenger serverUnifiedMessenger = new UnifiedMessenger(serverMessenger);
    serverRemoteMessenger = new RemoteMessenger(serverUnifiedMessenger);
    serverChannelMessenger = new ChannelMessenger(serverUnifiedMessenger);
    final UnifiedMessenger client1UnifiedMessenger = new UnifiedMessenger(client1Messenger);
    client1RemoteMessenger = new RemoteMessenger(client1UnifiedMessenger);
    client1ChannelMessenger = new ChannelMessenger(client1UnifiedMessenger);
    final UnifiedMessenger client2UnifiedMessenger = new UnifiedMessenger(client2Messenger);
    client2RemoteMessenger = new RemoteMessenger(client2UnifiedMessenger);
    client2ChannelMessenger = new ChannelMessenger(client2UnifiedMessenger);
    serverModeratorController = new NullModeratorController(serverMessenger, null);
    serverModeratorController.register(serverRemoteMessenger);
}
Also used : IRemoteMessenger(games.strategy.engine.message.IRemoteMessenger) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) ServerMessenger(games.strategy.net.ServerMessenger) IServerMessenger(games.strategy.net.IServerMessenger) NullModeratorController(games.strategy.engine.lobby.server.NullModeratorController) IChannelMessenger(games.strategy.engine.message.IChannelMessenger) ChannelMessenger(games.strategy.engine.message.ChannelMessenger) ClientMessenger(games.strategy.net.ClientMessenger) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with ClientMessenger

use of games.strategy.net.ClientMessenger in project triplea by triplea-game.

the class VaultTest method setUp.

@BeforeEach
public void setUp() throws IOException {
    serverMessenger = new ServerMessenger("Server", 0);
    serverMessenger.setAcceptNewConnections(true);
    final int serverPort = serverMessenger.getLocalNode().getSocketAddress().getPort();
    final String mac = MacFinder.getHashedMacAddress();
    clientMessenger = new ClientMessenger("localhost", serverPort, "client1", mac);
    final UnifiedMessenger serverUnifiedMessenger = new UnifiedMessenger(serverMessenger);
    final UnifiedMessenger clientUnifiedMessenger = new UnifiedMessenger(clientMessenger);
    serverVault = new Vault(new ChannelMessenger(serverUnifiedMessenger));
    clientVault = new Vault(new ChannelMessenger(clientUnifiedMessenger));
    Thread.yield();
}
Also used : UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) 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)

Aggregations

ClientMessenger (games.strategy.net.ClientMessenger)10 IServerMessenger (games.strategy.net.IServerMessenger)8 RemoteMessenger (games.strategy.engine.message.RemoteMessenger)7 ServerMessenger (games.strategy.net.ServerMessenger)7 ChannelMessenger (games.strategy.engine.message.ChannelMessenger)5 RemoteName (games.strategy.engine.message.RemoteName)5 Test (org.junit.jupiter.api.Test)5 UnifiedMessenger (games.strategy.engine.message.unifiedmessenger.UnifiedMessenger)4 IRemoteMessenger (games.strategy.engine.message.IRemoteMessenger)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 IChannelMessenger (games.strategy.engine.message.IChannelMessenger)2 ClientLogger (games.strategy.debug.ClientLogger)1 ClientContext (games.strategy.engine.ClientContext)1 ChatPanel (games.strategy.engine.chat.ChatPanel)1 IChatPanel (games.strategy.engine.chat.IChatPanel)1 GameStepListener (games.strategy.engine.data.events.GameStepListener)1 LOBBY_GAME_COMMENTS (games.strategy.engine.framework.CliProperties.LOBBY_GAME_COMMENTS)1 LOBBY_GAME_HOSTED_BY (games.strategy.engine.framework.CliProperties.LOBBY_GAME_HOSTED_BY)1 LOBBY_HOST (games.strategy.engine.framework.CliProperties.LOBBY_HOST)1 LOBBY_PORT (games.strategy.engine.framework.CliProperties.LOBBY_PORT)1