Search in sources :

Example 1 with RemoteMessenger

use of games.strategy.engine.message.RemoteMessenger in project triplea by triplea-game.

the class RemoteMessengerTest method testRemoteCall2.

@Test
public void testRemoteCall2() throws Exception {
    final RemoteName test = new RemoteName(ITestRemote.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 RemoteMessenger serverRemoteMessenger = new RemoteMessenger(new UnifiedMessenger(server));
        final TestRemote testRemote = new TestRemote();
        serverRemoteMessenger.registerRemote(testRemote, test);
        final RemoteMessenger clientRemoteMessenger = new RemoteMessenger(new UnifiedMessenger(client));
        // call it on the client
        // should be no need to wait since the constructor should not
        // reutrn until the initial state of the messenger is good
        final int incrementedValue = ((ITestRemote) clientRemoteMessenger.getRemote(test)).increment(1);
        assertEquals(2, incrementedValue);
        assertEquals(testRemote.getLastSenderNode(), client.getLocalNode());
    } finally {
        shutdownServerAndClient(server, client);
    }
}
Also used : RemoteName(games.strategy.engine.message.RemoteName) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) ServerMessenger(games.strategy.net.ServerMessenger) IServerMessenger(games.strategy.net.IServerMessenger) ClientMessenger(games.strategy.net.ClientMessenger) Test(org.junit.jupiter.api.Test)

Example 2 with RemoteMessenger

use of games.strategy.engine.message.RemoteMessenger in project triplea by triplea-game.

the class RemoteMessengerTest method testShutDownClient.

@Test
public void testShutDownClient() throws Exception {
    // when the client shutdown, remotes created
    // on the client should not be visible on server
    final RemoteName test = new RemoteName(ITestRemote.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 clientRemoteMessenger = new RemoteMessenger(new UnifiedMessenger(client));
        clientRemoteMessenger.registerRemote(new TestRemote(), test);
        serverUnifiedMessenger.getHub().waitForNodesToImplement(test.getName());
        assertTrue(serverUnifiedMessenger.getHub().hasImplementors(test.getName()));
        client.shutDown();
        Interruptibles.sleep(200);
        assertTrue(!serverUnifiedMessenger.getHub().hasImplementors(test.getName()));
    } finally {
        shutdownServerAndClient(server, client);
    }
}
Also used : RemoteName(games.strategy.engine.message.RemoteName) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) ServerMessenger(games.strategy.net.ServerMessenger) IServerMessenger(games.strategy.net.IServerMessenger) ClientMessenger(games.strategy.net.ClientMessenger) Test(org.junit.jupiter.api.Test)

Example 3 with RemoteMessenger

use of games.strategy.engine.message.RemoteMessenger in project triplea by triplea-game.

the class RemoteMessengerTest method testRemoteCall.

@Test
public void testRemoteCall() throws Exception {
    final RemoteName test = new RemoteName(ITestRemote.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);
        unifiedMessengerHub = serverUnifiedMessenger.getHub();
        final RemoteMessenger serverRemoteMessenger = new RemoteMessenger(serverUnifiedMessenger);
        final RemoteMessenger clientRemoteMessenger = new RemoteMessenger(new UnifiedMessenger(client));
        // register it on the server
        final TestRemote testRemote = new TestRemote();
        serverRemoteMessenger.registerRemote(testRemote, test);
        // since the registration must go over a socket
        // and through a couple threads, wait for the
        // client to get it
        int waitCount = 0;
        while (!unifiedMessengerHub.hasImplementors(test.getName()) && waitCount < 20) {
            waitCount++;
            Interruptibles.sleep(50);
        }
        // call it on the client
        final int incrementedValue = ((ITestRemote) clientRemoteMessenger.getRemote(test)).increment(1);
        assertEquals(2, incrementedValue);
        assertEquals(testRemote.getLastSenderNode(), client.getLocalNode());
    } finally {
        shutdownServerAndClient(server, client);
    }
}
Also used : RemoteName(games.strategy.engine.message.RemoteName) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) ServerMessenger(games.strategy.net.ServerMessenger) IServerMessenger(games.strategy.net.IServerMessenger) ClientMessenger(games.strategy.net.ClientMessenger) Test(org.junit.jupiter.api.Test)

Example 4 with RemoteMessenger

use of games.strategy.engine.message.RemoteMessenger in project triplea by triplea-game.

the class RemoteMessengerTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    // simple set up for non networked testing
    final List<IConnectionChangeListener> connectionListeners = new CopyOnWriteArrayList<>();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) {
            connectionListeners.add(invocation.getArgument(0));
            return null;
        }
    }).when(serverMessenger).addConnectionChangeListener(any());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) {
            for (final IConnectionChangeListener listener : connectionListeners) {
                listener.connectionRemoved(invocation.getArgument(0));
            }
            return null;
        }
    }).when(serverMessenger).removeConnection(any());
    final Node dummyNode = new Node("dummy", InetAddress.getLocalHost(), 0);
    when(serverMessenger.getLocalNode()).thenReturn(dummyNode);
    when(serverMessenger.getServerNode()).thenReturn(dummyNode);
    when(serverMessenger.isServer()).thenReturn(true);
    remoteMessenger = new RemoteMessenger(new UnifiedMessenger(serverMessenger));
}
Also used : RemoteMessenger(games.strategy.engine.message.RemoteMessenger) IConnectionChangeListener(games.strategy.net.IConnectionChangeListener) InvocationOnMock(org.mockito.invocation.InvocationOnMock) INode(games.strategy.net.INode) Node(games.strategy.net.Node) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with RemoteMessenger

use of games.strategy.engine.message.RemoteMessenger 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)

Aggregations

RemoteMessenger (games.strategy.engine.message.RemoteMessenger)10 ClientMessenger (games.strategy.net.ClientMessenger)8 IServerMessenger (games.strategy.net.IServerMessenger)7 ServerMessenger (games.strategy.net.ServerMessenger)6 IRemoteMessenger (games.strategy.engine.message.IRemoteMessenger)5 RemoteName (games.strategy.engine.message.RemoteName)5 UnifiedMessenger (games.strategy.engine.message.unifiedmessenger.UnifiedMessenger)5 ChannelMessenger (games.strategy.engine.message.ChannelMessenger)4 IChannelMessenger (games.strategy.engine.message.IChannelMessenger)4 Test (org.junit.jupiter.api.Test)4 ChatPanel (games.strategy.engine.chat.ChatPanel)3 IChatPanel (games.strategy.engine.chat.IChatPanel)3 INode (games.strategy.net.INode)3 IOException (java.io.IOException)3 ClientLogger (games.strategy.debug.ClientLogger)2 TRIPLEA_PORT (games.strategy.engine.framework.CliProperties.TRIPLEA_PORT)2 GameRunner (games.strategy.engine.framework.GameRunner)2 PlayerListing (games.strategy.engine.framework.message.PlayerListing)2 ClientLogin (games.strategy.engine.framework.startup.login.ClientLogin)2 CouldNotLogInException (games.strategy.net.CouldNotLogInException)2