Commits

Andy Moncsek committed 81bd0ef

update to tyrus b12, login test

Comments (0)

Files changed (7)

JavaClient/src/main/java/org/jacp/chat/ChatClientEndpoit.java

+package org.jacp.chat;
+
+import org.jacp.chat.encoder.ClientLoginEncoder;
+import org.jacp.jee.messages.LoginMessage;
+
+import javax.websocket.ClientEndpoint;
+import javax.websocket.EncodeException;
+import javax.websocket.OnOpen;
+import javax.websocket.Session;
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 28.02.13
+ * Time: 09:49
+ * To change this template use File | Settings | File Templates.
+ */
+@ClientEndpoint(encoders ={ClientLoginEncoder.class})
+public class ChatClientEndpoit {
+    private Session session;
+    CountDownLatch messageLatch;
+
+    public ChatClientEndpoit(CountDownLatch messageLatch) {
+        this.messageLatch = messageLatch;
+    }
+    public ChatClientEndpoit() {
+
+    }
+    @OnOpen
+    public void onOpen(Session session) {
+        this.session = session;
+        System.out.println("Connected to endpoint: " + session.getBasicRemote());
+        messageLatch.countDown();
+    }
+
+    public void login() throws IOException, EncodeException {
+        System.out.println("myLogin"+session);
+        session.getBasicRemote().sendObject(new LoginMessage("anni"));
+
+    }
+
+}

JavaClient/src/main/java/org/jacp/chat/decoder/ClientLoginDecoder.java

-package org.jacp.chat.decoder;
-
-import org.apache.commons.lang.SerializationUtils;
-import org.jacp.jee.messages.LoginMessage;
-
-import javax.websocket.Decoder;
-import javax.websocket.EncodeException;
-import javax.websocket.Encoder;
-import java.nio.ByteBuffer;
-
-/**
- * Created with IntelliJ IDEA.
- * User: ady
- * Date: 28.02.13
- * Time: 09:50
- * To change this template use File | Settings | File Templates.
- */
-public class ClientLoginDecoder implements Encoder.Binary<LoginMessage> {
-    @Override
-    public ByteBuffer encode(LoginMessage loginMessage) throws EncodeException {
-        return ByteBuffer.wrap(SerializationUtils.serialize(loginMessage));
-    }
-}

JavaClient/src/main/java/org/jacp/chat/encoder/ClientLoginEncoder.java

+package org.jacp.chat.encoder;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.LoginMessage;
+
+import javax.websocket.Decoder;
+import javax.websocket.EncodeException;
+import javax.websocket.Encoder;
+import java.nio.ByteBuffer;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 28.02.13
+ * Time: 09:50
+ * To change this template use File | Settings | File Templates.
+ */
+public class ClientLoginEncoder implements Encoder.Binary<LoginMessage> {
+    @Override
+    public ByteBuffer encode(LoginMessage loginMessage) throws EncodeException {
+        return ByteBuffer.wrap(SerializationUtils.serialize(loginMessage));
+    }
+}

JavaClient/src/main/java/org/jacp/chat/endpoint/ChatClientEndpoit.java

-package org.jacp.chat.endpoint;
-
-import javax.websocket.ClientEndpoint;
-import javax.websocket.OnOpen;
-import javax.websocket.Session;
-import java.io.IOException;
-import java.util.concurrent.CountDownLatch;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/**
- * Created with IntelliJ IDEA.
- * User: ady
- * Date: 28.02.13
- * Time: 09:49
- * To change this template use File | Settings | File Templates.
- */
-@ClientEndpoint
-public class ChatClientEndpoit {
-    private Session session;
-    CountDownLatch messageLatch;
-
-    public ChatClientEndpoit(CountDownLatch messageLatch) {
-        this.messageLatch = messageLatch;
-    }
-    public ChatClientEndpoit() {
-
-    }
-    @OnOpen
-    public void onOpen(Session session) {
-        this.session = session;
-        System.out.println("Connected to endpoint: " + session.getBasicRemote());
-        messageLatch.countDown();
-    }
-
-}

JavaClient/src/test/java/test/ChatTest.java

 package test;
 
 import org.glassfish.tyrus.client.ClientManager;
-import org.jacp.chat.endpoint.ChatClientEndpoit;
+import org.jacp.chat.ChatClientEndpoit;
 import org.junit.Test;
 
 import javax.websocket.*;
  */
 public class ChatTest {
 
-    ExecutorService executorService2 = Executors.newFixedThreadPool(10)  ;
+    ExecutorService executorService2 = Executors.newFixedThreadPool(10);
 
-    @Test
+    // @Test
     public void connectToEcho() throws DeploymentException, IOException, InterruptedException {
         final CountDownLatch messageLatch = new CountDownLatch(1);
         ClientManager client = ClientManager.createClient();
-        Session session = client.connectToServer(new ChatClientEndpoit(messageLatch),ClientEndpointConfigurationBuilder.create().build(), getURI());
+        Session session = client.connectToServer(new ChatClientEndpoit(messageLatch), ClientEndpointConfigurationBuilder.create().build(), getURI());
         System.out.println(session.isOpen());
-       messageLatch.await(100, TimeUnit.SECONDS);
+        messageLatch.await(100, TimeUnit.SECONDS);
+    }
+
+    // @Test
+    public void parallelTest() throws DeploymentException, IOException, InterruptedException {
+        for (int i = 0; i < 10; i++) {
+            executorService2.submit(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        final CountDownLatch messageLatch = new CountDownLatch(1);
+                        ClientManager client = ClientManager.createClient();
+                        Session session = client.connectToServer(new ChatClientEndpoit(messageLatch), ClientEndpointConfigurationBuilder.create().build(), getURI());
+                        System.out.println(session.isOpen());
+
+                        messageLatch.await(100, TimeUnit.SECONDS);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+                    } catch (DeploymentException e) {
+                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+                    }
+                }
+            });
+        }
+        executorService2.shutdown();
+    }
+
+    @Test
+    public void loginTest() throws DeploymentException, IOException, InterruptedException, EncodeException {
+        final CountDownLatch messageLatch = new CountDownLatch(1);
+        ClientManager client = ClientManager.createClient();
+        ChatClientEndpoit endpoint = new ChatClientEndpoit(messageLatch);
+        Session session = client.connectToServer(endpoint, ClientEndpointConfigurationBuilder.create().build(), getURI());
+        System.out.println(session.isOpen());
+        messageLatch.await(100, TimeUnit.SECONDS);
+        endpoint.login();
+
+
+    }
+
+    @Test
+    public void parallelLoginTest() throws DeploymentException, IOException, InterruptedException {
+        final CountDownLatch latch = new CountDownLatch(10);
+        final ClientManager client = ClientManager.createClient();
+        for (int i = 0; i < 10; i++) {
+            Thread t = new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        System.out.println("login");
+                        final CountDownLatch messageLatch = new CountDownLatch(1);
+
+                        ChatClientEndpoit endpoint = new ChatClientEndpoit(messageLatch);
+                        Session session = client.connectToServer(endpoint, ClientEndpointConfigurationBuilder.create().build(), getURI());
+                        System.out.println(session.isOpen());
+                        messageLatch.await(100, TimeUnit.SECONDS);
+                        endpoint.login();
+                        latch.countDown();
+                        Thread.sleep(500);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+                    } catch (DeploymentException e) {
+                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+                    } catch (EncodeException e) {
+                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+                    } catch (IOException e) {
+                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+                    }
+                }
+
+            });
+            t.setDaemon(true);
+            t.start();
+        }
+        latch.await(100, TimeUnit.SECONDS);
+        // executorService2.shutdown();
     }
 
     private URI getURI() {

WebClient/src/main/java/org/jacp/beans/websocket/chat/ChatEndpoint.java

 
     @WebSocketMessage
     public void handleLoginRequest(LoginMessage lrm, Session session) {
+        logger.info("############login..."+lrm.getUserName()+"  socket: "+this);
         broadcastNewUser(lrm.getUserName(),session);
     }
 
         <jee.version>7.0-b77</jee.version>
         <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
         <junit.version>4.11</junit.version>
-        <tyrusWebsocket.version>1.0-SNAPSHOT</tyrusWebsocket.version>
+        <tyrusWebsocket.version>1.0-b12</tyrusWebsocket.version>
     </properties>
     <dependencyManagement>
         <dependencies>