Package org.cometd.client

Examples of org.cometd.client.BayeuxClient$ConnectTransportListener


    public ClientSession get() throws SDKException {
        return openSession(createSession());
    }

    private BayeuxClient createSession() throws SDKException {
        return new BayeuxClient(buildUrl(), createTransport(createHttpClient()));
    }
View Full Code Here


                exchange.addRequestHeader(HttpHeaders.AUTHORIZATION,
                        "OAuth " + accessToken);
            }
        };

        BayeuxClient client = new BayeuxClient(getEndpointUrl(), transport);
        client.setDebugEnabled(false);
        return client;
    }
View Full Code Here

        Seti seti2 = startSeti(oort2);

        new SetiService(seti1);
        new SetiService(seti2);

        BayeuxClient client1 = startClient(oort1, null);
        Assert.assertTrue(client1.waitFor(5000, BayeuxClient.State.CONNECTED));

        final CountDownLatch presenceOnLatch = new CountDownLatch(1);
        final CountDownLatch presenceOffLatch = new CountDownLatch(1);
        Seti.PresenceListener listener = new Seti.PresenceListener()
        {
            public void presenceAdded(Event event)
            {
                presenceOnLatch.countDown();
            }

            public void presenceRemoved(Event event)
            {
                presenceOffLatch.countDown();
            }
        };
        seti2.addPresenceListener(listener);

        LatchListener publishLatch = new LatchListener();
        Map<String, Object> login1 = new HashMap<>();
        String userId = "user1";
        login1.put("user", userId);
        ClientSessionChannel loginChannel1 = client1.getChannel("/service/login");
        loginChannel1.addListener(publishLatch);
        loginChannel1.publish(login1);
        Assert.assertTrue(publishLatch.await(5, TimeUnit.SECONDS));

        Assert.assertTrue(presenceOnLatch.await(5, TimeUnit.SECONDS));

        Assert.assertTrue(seti1.isAssociated(userId));
        Assert.assertTrue(seti1.isPresent(userId));
        Assert.assertTrue(seti2.isPresent(userId));

        publishLatch.reset(1);
        Map<String, Object> logout1 = new HashMap<>();
        logout1.put("user", userId);
        ClientSessionChannel logoutChannel1 = client1.getChannel("/service/logout");
        logoutChannel1.addListener(publishLatch);
        logoutChannel1.publish(logout1);
        Assert.assertTrue(publishLatch.await(5, TimeUnit.SECONDS));

        Assert.assertTrue(presenceOffLatch.await(5, TimeUnit.SECONDS));
View Full Code Here

        Server server1 = startServer(0);
        Oort oort1 = startOort(server1);
        Seti seti1 = startSeti(oort1);
        new SetiService(seti1);

        BayeuxClient client1 = startClient(oort1, null);
        Assert.assertTrue(client1.waitFor(5000, BayeuxClient.State.CONNECTED));

        Map<String, Object> login1 = new HashMap<>();
        String userId = "user1";
        login1.put("user", userId);
        ClientSessionChannel loginChannel1 = client1.getChannel("/service/login");
        final CountDownLatch publishLatch = new CountDownLatch(1);
        loginChannel1.publish(login1, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
View Full Code Here

        prepareAndStart(initParams);

        Map<String, Object> options = new HashMap<>();
        options.put("ws.maxNetworkDelay", maxNetworkDelay);
        ClientTransport webSocketTransport = newWebSocketTransport(options);
        BayeuxClient client = new BayeuxClient(cometdURL, webSocketTransport);
        client.setOption(BayeuxClient.BACKOFF_INCREMENT_OPTION, backoffIncrement);

        bayeux.getChannel(Channel.META_CONNECT).addListener(new ServerChannel.MessageListener()
        {
            private final AtomicInteger connects = new AtomicInteger();

            public boolean onMessage(ServerSession from, ServerChannel channel, ServerMessage.Mutable message)
            {
                int connects = this.connects.incrementAndGet();
                if (connects == 2)
                {
                    try
                    {
                        // We delay the second connect, so the client can expire it
                        Thread.sleep(delay);
                    }
                    catch (InterruptedException x)
                    {
                        x.printStackTrace();
                        return false;
                    }
                }
                return true;
            }
        });

        // The second connect must fail, and should never be notified on client
        final CountDownLatch connectLatch1 = new CountDownLatch(2);
        final CountDownLatch connectLatch2 = new CountDownLatch(1);
        client.getChannel(Channel.META_CONNECT).addListener(new ClientSessionChannel.MessageListener()
        {
            private final AtomicInteger connects = new AtomicInteger();
            public String failedId;

            public void onMessage(ClientSessionChannel channel, Message message)
            {
                int connects = this.connects.incrementAndGet();
                if (connects == 1 && message.isSuccessful())
                {
                    connectLatch1.countDown();
                }
                else if (connects == 2 && !message.isSuccessful())
                {
                    connectLatch1.countDown();
                    failedId = message.getId();
                }
                else if (connects > 2 && !failedId.equals(message.getId()))
                {
                    connectLatch2.countDown();
                }
            }
        });

        client.handshake();

        Assert.assertTrue(connectLatch1.await(timeout + 2 * maxNetworkDelay, TimeUnit.MILLISECONDS));
        Assert.assertTrue(connectLatch2.await(client.getBackoffIncrement() * 2, TimeUnit.MILLISECONDS));

        disconnectBayeuxClient(client);
    }
View Full Code Here

        Seti seti2 = startSeti(oort2);

        new SetiService(seti1);
        new SetiService(seti2);

        BayeuxClient client1 = startClient(oort1, null);
        Assert.assertTrue(client1.waitFor(5000, BayeuxClient.State.CONNECTED));
        BayeuxClient client2 = startClient(oort2, null);
        Assert.assertTrue(client2.waitFor(5000, BayeuxClient.State.CONNECTED));

        final CountDownLatch localPresenceOnLatch = new CountDownLatch(1);
        final CountDownLatch remotePresenceOnLatch = new CountDownLatch(1);
        final CountDownLatch localPresenceOffLatch = new CountDownLatch(1);
        final CountDownLatch remotePresenceOffLatch = new CountDownLatch(1);
        Seti.PresenceListener listener = new Seti.PresenceListener()
        {
            public void presenceAdded(Event event)
            {
                if (event.isLocal())
                    localPresenceOnLatch.countDown();
                else
                    remotePresenceOnLatch.countDown();
            }

            public void presenceRemoved(Event event)
            {
                if (event.isLocal())
                    localPresenceOffLatch.countDown();
                else
                    remotePresenceOffLatch.countDown();
            }
        };
        seti2.addPresenceListener(listener);

        // Login user1
        final CountDownLatch loginLatch1 = new CountDownLatch(1);
        Map<String, Object> login1 = new HashMap<>();
        String userId1 = "user1";
        login1.put("user", userId1);
        ClientSessionChannel loginChannel1 = client1.getChannel("/service/login");
        loginChannel1.publish(login1, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                loginLatch1.countDown();
            }
        });
        Assert.assertTrue(loginLatch1.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(remotePresenceOnLatch.await(5, TimeUnit.SECONDS));

        // Login user2
        final CountDownLatch loginLatch2 = new CountDownLatch(1);
        Map<String, Object> login2 = new HashMap<>();
        String userId2 = "user2";
        login2.put("user", userId2);
        ClientSessionChannel loginChannel2 = client2.getChannel("/service/login");
        loginChannel2.publish(login2, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                loginLatch2.countDown();
            }
        });
        Assert.assertTrue(loginLatch2.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(localPresenceOnLatch.await(5, TimeUnit.SECONDS));

        // Logout user2
        final CountDownLatch logoutLatch2 = new CountDownLatch(1);
        Map<String, Object> logout2 = new HashMap<>();
        logout2.put("user", userId2);
        ClientSessionChannel logoutChannel2 = client2.getChannel("/service/logout");
        logoutChannel2.publish(logout2, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                logoutLatch2.countDown();
View Full Code Here

        Seti seti2 = startSeti(oort2);

        new SetiService(seti1);
        new SetiService(seti2);

        BayeuxClient client1 = startClient(oort1, null);
        Assert.assertTrue(client1.waitFor(5000, BayeuxClient.State.CONNECTED));
        BayeuxClient client2 = startClient(oort2, null);
        Assert.assertTrue(client2.waitFor(5000, BayeuxClient.State.CONNECTED));

        final CountDownLatch presenceAddedLatch = new CountDownLatch(4);
        seti1.addPresenceListener(new UserPresentListener(presenceAddedLatch));
        seti2.addPresenceListener(new UserPresentListener(presenceAddedLatch));

        // Login user1
        final CountDownLatch loginLatch1 = new CountDownLatch(1);
        Map<String, Object> login1 = new HashMap<>();
        String userId1 = "user1";
        login1.put("user", userId1);
        ClientSessionChannel loginChannel1 = client1.getChannel("/service/login");
        loginChannel1.publish(login1, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                loginLatch1.countDown();
            }
        });
        Assert.assertTrue(loginLatch1.await(5, TimeUnit.SECONDS));

        // Login user2
        final CountDownLatch loginLatch2 = new CountDownLatch(1);
        Map<String, Object> login2 = new HashMap<>();
        String userId2 = "user2";
        login2.put("user", userId2);
        ClientSessionChannel loginChannel2 = client2.getChannel("/service/login");
        loginChannel2.publish(login2, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                loginLatch2.countDown();
View Full Code Here

        prepareAndStart(serverOptions);

        Map<String, Object> clientOptions = new HashMap<>();
        clientOptions.put("ws.maxMessageSize", maxMessageSize);
        ClientTransport webSocketTransport = newWebSocketTransport(clientOptions);
        BayeuxClient client = new BayeuxClient(cometdURL, webSocketTransport);

        client.handshake();
        Assert.assertTrue(client.waitFor(5000, BayeuxClient.State.CONNECTED));

        ClientSessionChannel channel = client.getChannel("/test");
        final CountDownLatch latch = new CountDownLatch(1);
        channel.subscribe(new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
View Full Code Here

        Seti seti2 = startSeti(oort2);

        new SetiService(seti1);
        new SetiService(seti2);

        BayeuxClient client1 = startClient(oort1, null);
        Assert.assertTrue(client1.waitFor(5000, BayeuxClient.State.CONNECTED));
        BayeuxClient client2 = startClient(oort2, null);
        Assert.assertTrue(client2.waitFor(5000, BayeuxClient.State.CONNECTED));

        CountDownLatch presenceAddedLatch = new CountDownLatch(4);
        seti1.addPresenceListener(new UserPresentListener(presenceAddedLatch));
        seti2.addPresenceListener(new UserPresentListener(presenceAddedLatch));

        // Login user1
        final CountDownLatch loginLatch1 = new CountDownLatch(1);
        Map<String, Object> login1 = new HashMap<>();
        String userId1 = "user1";
        login1.put("user", userId1);
        ClientSessionChannel loginChannel1 = client1.getChannel("/service/login");
        loginChannel1.publish(login1, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                loginLatch1.countDown();
            }
        });
        Assert.assertTrue(loginLatch1.await(5, TimeUnit.SECONDS));

        // Login user2
        final CountDownLatch loginLatch2 = new CountDownLatch(1);
        Map<String, Object> login2 = new HashMap<>();
        String userId2 = "user2";
        login2.put("user", userId2);
        ClientSessionChannel loginChannel2 = client2.getChannel("/service/login");
        loginChannel2.publish(login2, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                loginLatch2.countDown();
View Full Code Here

        Seti seti2 = startSeti(oort2);

        new SetiService(seti1);
        new SetiService(seti2);

        BayeuxClient client1 = startClient(oort1, null);
        Assert.assertTrue(client1.waitFor(5000, BayeuxClient.State.CONNECTED));

        CountDownLatch presenceAddedLatch = new CountDownLatch(2);
        seti1.addPresenceListener(new UserPresentListener(presenceAddedLatch));
        seti2.addPresenceListener(new UserPresentListener(presenceAddedLatch));

        // Login user1
        final CountDownLatch loginLatch1 = new CountDownLatch(1);
        Map<String, Object> login1 = new HashMap<>();
        String userId1 = "user1";
        login1.put("user", userId1);
        ClientSessionChannel loginChannel1 = client1.getChannel("/service/login");
        loginChannel1.publish(login1, new ClientSessionChannel.MessageListener()
        {
            public void onMessage(ClientSessionChannel channel, Message message)
            {
                loginLatch1.countDown();
            }
        });
        Assert.assertTrue(loginLatch1.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(presenceAddedLatch.await(5, TimeUnit.SECONDS));

        int switches = 2;
        for (int i = 0; i < switches; ++i)
        {
            // Simulate network crash
            oortComet12.disconnect();
            oortComet12.waitFor(5000, BayeuxClient.State.DISCONNECTED);
            // The other OortComet is automatically disconnected
            oortComet21.waitFor(5000, BayeuxClient.State.DISCONNECTED);

            // Stop node1
            int port1 = ((NetworkConnector)server1.getConnectors()[0]).getLocalPort();
            stopSeti(seti1);
            stopOort(oort1);
            stopServer(server1);

            // Disconnect user and login it to node2
            client1.disconnect();
            Assert.assertTrue(client1.waitFor(5000, BayeuxClient.State.DISCONNECTED));
            client1 = startClient(oort2, null);
            final CountDownLatch loginLatch2 = new CountDownLatch(1);
            loginChannel1 = client1.getChannel("/service/login");
            loginChannel1.publish(login1, new ClientSessionChannel.MessageListener()
            {
                public void onMessage(ClientSessionChannel channel, Message message)
                {
                    loginLatch2.countDown();
                }
            });
            Assert.assertTrue(loginLatch2.await(5, TimeUnit.SECONDS));

            // Bring node1 back online
            server1 = startServer(port1);
            oort1 = startOort(server1);
            oortLatch = new CountDownLatch(1);
            oort2.addCometListener(new CometJoinedListener(oortLatch));
            oortComet12 = oort1.observeComet(oort2.getURL());
            Assert.assertTrue(oortComet12.waitFor(5000, BayeuxClient.State.CONNECTED));
            Assert.assertTrue(oortLatch.await(5, TimeUnit.SECONDS));
            oortComet21 = oort2.findComet(oort1.getURL());
            Assert.assertTrue(oortComet21.waitFor(5000, BayeuxClient.State.CONNECTED));
            seti1 = startSeti(oort1);
            new SetiService(seti1);
            // Wait for cloud/seti notifications to happen
            Thread.sleep(1000);

            System.err.println(seti1.dump());
            System.err.println(seti2.dump());

            Assert.assertFalse(seti1.isAssociated(userId1));
            Assert.assertTrue(seti1.isPresent(userId1));
            Assert.assertTrue(seti2.isAssociated(userId1));
            Assert.assertTrue(seti2.isPresent(userId1));

            // Simulate network crash
            oortComet12.disconnect();
            oortComet12.waitFor(5000, BayeuxClient.State.DISCONNECTED);
            // The other OortComet is automatically disconnected
            oortComet21.waitFor(5000, BayeuxClient.State.DISCONNECTED);

            // Stop node2
            int port2 = ((NetworkConnector)server2.getConnectors()[0]).getLocalPort();
            stopSeti(seti2);
            stopOort(oort2);
            stopServer(server2);

            // Disconnect user and login it to node1
            client1.disconnect();
            Assert.assertTrue(client1.waitFor(5000, BayeuxClient.State.DISCONNECTED));
            client1 = startClient(oort1, null);
            final CountDownLatch loginLatch3 = new CountDownLatch(1);
            loginChannel1 = client1.getChannel("/service/login");
            loginChannel1.publish(login1, new ClientSessionChannel.MessageListener()
            {
                public void onMessage(ClientSessionChannel channel, Message message)
                {
                    loginLatch3.countDown();
View Full Code Here

TOP

Related Classes of org.cometd.client.BayeuxClient$ConnectTransportListener

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.