Search in sources :

Example 1 with ModularXmppClientToServerConnection

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection in project Smack by igniterealtime.

the class XmppConnectionDescriptor method buildWebsocketDescriptor.

public static XmppConnectionDescriptor<ModularXmppClientToServerConnection, ModularXmppClientToServerConnectionConfiguration, ModularXmppClientToServerConnectionConfiguration.Builder> buildWebsocketDescriptor(String nickname, Class<? extends WebSocketFactory> factoryClass) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    WebSocketFactory factory;
    try {
        Field instanceField = factoryClass.getField("INSTANCE");
        factory = (WebSocketFactory) instanceField.get(null);
    } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
        factory = factoryClass.getConstructor().newInstance();
    }
    WebSocketFactory finalFactory = factory;
    return XmppConnectionDescriptor.buildWith(ModularXmppClientToServerConnection.class, ModularXmppClientToServerConnectionConfiguration.class, ModularXmppClientToServerConnectionConfiguration.Builder.class).withNickname(nickname).applyExtraConfguration(cb -> {
        cb.removeAllModules();
        ModularXmppClientToServerConnectionModuleDescriptor webSocketModuleDescriptor = XmppWebSocketTransportModuleDescriptor.getBuilder(cb).setWebSocketFactory(finalFactory).build();
        cb.addModule(webSocketModuleDescriptor);
    }).build();
}
Also used : Arrays(java.util.Arrays) XmppWebSocketTransportModuleDescriptor(org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor) Collection(java.util.Collection) Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) List(java.util.List) Consumer(org.jivesoftware.smack.util.Consumer) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection) Modifier(java.lang.reflect.Modifier) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) ModularXmppClientToServerConnectionModuleDescriptor(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor) Method(java.lang.reflect.Method) Collections(java.util.Collections) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) WebSocketFactory(org.jivesoftware.smack.websocket.impl.WebSocketFactory) Field(java.lang.reflect.Field) ModularXmppClientToServerConnectionModuleDescriptor(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) WebSocketFactory(org.jivesoftware.smack.websocket.impl.WebSocketFactory) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection)

Example 2 with ModularXmppClientToServerConnection

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection in project Smack by igniterealtime.

the class SmackIntegrationTestXmppConnectionManagerTest method simpleXmppConnectionDescriptorTest.

@Test
public void simpleXmppConnectionDescriptorTest() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, KeyManagementException, NoSuchAlgorithmException, XmppStringprepException, InstantiationException {
    XmppConnectionDescriptor<ModularXmppClientToServerConnection, ModularXmppClientToServerConnectionConfiguration, ModularXmppClientToServerConnectionConfiguration.Builder> descriptor = XmppConnectionDescriptor.buildWith(ModularXmppClientToServerConnection.class, ModularXmppClientToServerConnectionConfiguration.class, ModularXmppClientToServerConnectionConfiguration.Builder.class).applyExtraConfguration(b -> b.removeAllModules().addModule(XmppTcpTransportModuleDescriptor.class)).build();
    Configuration sinttestConfiguration = Configuration.builder().setService("example.org").build();
    ModularXmppClientToServerConnection connection = descriptor.construct(sinttestConfiguration);
    assertEquals("example.org", connection.getXMPPServiceDomain().toString());
}
Also used : Test(org.junit.jupiter.api.Test) XmppTcpTransportModuleDescriptor(org.jivesoftware.smack.tcp.XmppTcpTransportModuleDescriptor) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) Test(org.junit.jupiter.api.Test)

Example 3 with ModularXmppClientToServerConnection

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection in project Smack by igniterealtime.

the class WebSocketConnectionTest method testWebSocketConnection.

public static void testWebSocketConnection(String jid, String password, String websocketEndpoint, String messageTo) throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException {
    ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder();
    builder.removeAllModules().setXmppAddressAndPassword(jid, password).setDebuggerFactory(ConsoleDebugger.Factory.INSTANCE);
    XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
    websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(websocketEndpoint, false);
    builder.addModule(websocketBuilder.build());
    ModularXmppClientToServerConnectionConfiguration config = builder.build();
    ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(config);
    connection.setReplyTimeout(5 * 60 * 1000);
    connection.addConnectionStateMachineListener((event, c) -> {
        Logger.getAnonymousLogger().info("Connection event: " + event);
    });
    connection.connect();
    connection.login();
    if (messageTo != null) {
        Message message = connection.getStanzaFactory().buildMessageStanza().to(messageTo).setBody("It is alive! " + XmppDateTime.formatXEP0082Date(new Date())).build();
        connection.sendStanza(message);
    }
    Thread.sleep(1000);
    connection.disconnect();
    ModularXmppClientToServerConnection.Stats connectionStats = connection.getStats();
    ServiceDiscoveryManager.Stats serviceDiscoveryManagerStats = ServiceDiscoveryManager.getInstanceFor(connection).getStats();
    // CHECKSTYLE:OFF
    System.out.println("WebSocket successfully finished, yeah!\n" + connectionStats + '\n' + serviceDiscoveryManagerStats);
// CHECKSTYLE:ON
}
Also used : XmppWebSocketTransportModuleDescriptor(org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor) Message(org.jivesoftware.smack.packet.Message) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) Date(java.util.Date) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager)

Example 4 with ModularXmppClientToServerConnection

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection in project Smack by igniterealtime.

the class XmppConnectionIntegrationTest method allToAllMessageSendTest.

@SmackIntegrationTest(connectionCount = 4)
public void allToAllMessageSendTest(List<AbstractXMPPConnection> connections) throws InterruptedException, NotAllMessagesReceivedException, ErrorsWhileSendingOrReceivingException {
    final long seed = 42;
    // 100
    final int messagesPerConnection = 3;
    // 512
    final int maxPayloadChunkSize = 16;
    // 32
    final int maxPayloadChunks = 4;
    // true
    final boolean intermixMessages = false;
    XmppConnectionStressTest.Configuration stressTestConfiguration = new XmppConnectionStressTest.Configuration(seed, messagesPerConnection, maxPayloadChunkSize, maxPayloadChunks, intermixMessages);
    XmppConnectionStressTest stressTest = new XmppConnectionStressTest(stressTestConfiguration);
    stressTest.run(connections, timeout);
    final Level connectionStatsLogLevel = Level.FINE;
    if (LOGGER.isLoggable(connectionStatsLogLevel)) {
        if (connections.get(0) instanceof ModularXmppClientToServerConnection) {
            for (XMPPConnection connection : connections) {
                ModularXmppClientToServerConnection xmppC2sConnection = (ModularXmppClientToServerConnection) connection;
                ModularXmppClientToServerConnection.Stats stats = xmppC2sConnection.getStats();
                LOGGER.log(connectionStatsLogLevel, "Connections stats for " + xmppC2sConnection + ":\n{}", stats);
            }
        }
    }
}
Also used : Level(java.util.logging.Level) XmppConnectionStressTest(org.igniterealtime.smack.XmppConnectionStressTest) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 5 with ModularXmppClientToServerConnection

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection in project Smack by igniterealtime.

the class XmppWebSocketTransportModuleTest method createWebSocketModuleConnectionInstanceTest.

@Test
public void createWebSocketModuleConnectionInstanceTest() throws URISyntaxException, XmppStringprepException {
    ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder();
    builder.removeAllModules();
    builder.addModule(XmppWebSocketTransportModuleDescriptor.class);
    builder.setXmppAddressAndPassword("user5@localhost.org", "user5");
    builder.setHost("localhost.org");
    XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
    websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(new URI("wss://localhost.org:7443/ws/"), false);
    ModularXmppClientToServerConnectionConfiguration config = builder.build();
    ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(config);
    assertNotNull(connection);
}
Also used : ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) URI(java.net.URI) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) Test(org.junit.jupiter.api.Test)

Aggregations

ModularXmppClientToServerConnection (org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection)7 ModularXmppClientToServerConnectionConfiguration (org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration)6 XmppWebSocketTransportModuleDescriptor (org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Test (org.junit.jupiter.api.Test)2 BufferedWriter (java.io.BufferedWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 URI (java.net.URI)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1