Commits

Andy Moncsek  committed 5094160

JacpFX Chat client added

  • Participants
  • Parent commits 81bd0ef

Comments (0)

Files changed (20)

File Domain/src/main/java/org/jacp/jee/messages/ChatMessage.java

     private static String MESSAGE_PROPERTY="MESSAGE";
 
     public ChatMessage(String userName,String message) {
-        super(MessageType.LOGIN);
+        super(MessageType.MESSAGE);
         super.addValue(USERNAME_PROPERTY,userName);
         super.addValue(MESSAGE_PROPERTY,message);
     }

File JacpFXChatClient/src/main/java/org/jacp/chat/decoders/ChatMessageDecoder.java

+package org.jacp.chat.decoders;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.ChatMessage;
+import org.jacp.jee.messages.Message;
+import org.jacp.jee.messages.MessageType;
+
+import javax.websocket.DecodeException;
+import javax.websocket.Decoder;
+import java.nio.ByteBuffer;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 27.02.13
+ * Time: 15:32
+ * To change this template use File | Settings | File Templates.
+ */
+public class ChatMessageDecoder implements Decoder.Binary<ChatMessage> {
+    @Override
+    public ChatMessage decode(ByteBuffer byteBuffer) throws DecodeException {
+        return (ChatMessage) SerializationUtils.deserialize(byteBuffer.array());  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public boolean willDecode(ByteBuffer byteBuffer) {
+        Message message = (Message)SerializationUtils.deserialize(byteBuffer.array()) ;
+        if(message==null) return false;
+        if(message.getType().equals(MessageType.MESSAGE)) return true;
+        return false;
+    }
+}
+
+

File JacpFXChatClient/src/main/java/org/jacp/chat/decoders/ClientAddUserDecoder.java

+package org.jacp.chat.decoders;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.Message;
+import org.jacp.jee.messages.MessageType;
+import org.jacp.jee.messages.UserAddMessage;
+
+import javax.websocket.DecodeException;
+import javax.websocket.Decoder;
+import java.nio.ByteBuffer;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 05.03.13
+ * Time: 22:11
+ * To change this template use File | Settings | File Templates.
+ */
+public class ClientAddUserDecoder implements Decoder.Binary<UserAddMessage> {
+    @Override
+    public UserAddMessage decode(ByteBuffer byteBuffer) throws DecodeException {
+        return (UserAddMessage) SerializationUtils.deserialize(byteBuffer.array());  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public boolean willDecode(ByteBuffer byteBuffer) {
+        Message message = (Message) SerializationUtils.deserialize(byteBuffer.array());
+        if (message == null) return false;
+        if (message.getType().equals(MessageType.ADD_USER)) return true;
+        return false;
+    }
+}
+
+
+
+

File JacpFXChatClient/src/main/java/org/jacp/chat/dialogs/LoginDialog.java

+package org.jacp.chat.dialogs;
+
+import javafx.fxml.FXML;
+import javafx.scene.control.TextField;
+import org.jacp.api.annotations.Dialog;
+import org.jacp.api.annotations.Resource;
+import org.jacp.api.dialog.Scope;
+import org.jacp.javafx.rcp.component.AFXComponent;
+import org.jacp.javafx.rcp.components.modalDialog.JACPModalDialog;
+import org.jacp.jee.messages.LoginMessage;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: Andy moncsek
+ * Date: 06.03.13
+ * Time: 15:43
+ * To change this template use File | Settings | File Templates.
+ */
+@Dialog(id = "id100", viewLocation = "/fxml/LoginDialog.fxml", resourceBundleLocation = "bundles.languageBundle", localeID = "en_US", scope = Scope.SINGLETON)
+public class LoginDialog {
+
+    @FXML
+    private TextField login;
+    @Resource
+    private AFXComponent parent;
+
+    @FXML
+    public void login(){
+        if(login.getText()!=null && !login.getText().isEmpty()) {
+            parent.getActionListener("id003",new LoginMessage(login.getText())).performAction(null);
+            parent.getActionListener("id002",new LoginMessage(login.getText())).performAction(null);
+            JACPModalDialog.getInstance().hideModalMessage();
+        }
+
+    }
+}

File JacpFXChatClient/src/main/java/org/jacp/chat/encoders/ChatMessageEncoder.java

+package org.jacp.chat.encoders;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.ChatMessage;
+
+import javax.websocket.EncodeException;
+import javax.websocket.Encoder;
+import java.nio.ByteBuffer;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 06.03.13
+ * Time: 10:14
+ * To change this template use File | Settings | File Templates.
+ */
+public class ChatMessageEncoder implements Encoder.Binary<ChatMessage> {
+    @Override
+    public ByteBuffer encode(ChatMessage message) throws EncodeException {
+        return ByteBuffer.wrap(SerializationUtils.serialize(message));
+    }
+}

File JacpFXChatClient/src/main/java/org/jacp/chat/encoders/ClientLoginEncoder.java

+package org.jacp.chat.encoders;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.LoginMessage;
+
+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));
+    }
+}

File JacpFXChatClient/src/main/java/org/jacp/chat/entities/User.java

+package org.jacp.chat.entities;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 06.03.13
+ * Time: 11:48
+ * To change this template use File | Settings | File Templates.
+ */
+public class User {
+    private final String userName;
+    public User(final String userName){
+              this.userName = userName;
+    }
+
+    public String getUserName() {
+        return this.userName;
+    }
+}

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

 package org.jacp.chat;
 
-import org.jacp.chat.encoder.ClientLoginEncoder;
+
+
+import org.jacp.jee.messages.ChatMessage;
 import org.jacp.jee.messages.LoginMessage;
+import org.jacp.jee.messages.UserAddMessage;
 
-import javax.websocket.ClientEndpoint;
-import javax.websocket.EncodeException;
-import javax.websocket.OnOpen;
-import javax.websocket.Session;
+import javax.websocket.*;
 import java.io.IOException;
 import java.util.concurrent.CountDownLatch;
 
  * Time: 09:49
  * To change this template use File | Settings | File Templates.
  */
-@ClientEndpoint(encoders ={ClientLoginEncoder.class})
+@ClientEndpoint(encoders ={ClientLoginEncoder.class,ChatMessageEncoder.class}, decoders = {ClientAddUserDecoder.class, ChatMessageDecoder.class})
 public class ChatClientEndpoit {
     private Session session;
     CountDownLatch messageLatch;
 
     }
 
+    public void sendMessage(String message) throws IOException, EncodeException {
+        session.getBasicRemote().sendObject(new ChatMessage("anni",message));
+
+    }
+//    @OnMessage
+//    public void onRest(String test) {
+//            System.out.println("TEST:  "+test);
+//    }
+
+    @OnMessage
+    public void onUserAdd(UserAddMessage userAdd){
+        System.out.println("user add: "+userAdd.getUserName());
+    }
+
+    @OnMessage
+    public void onChatMessage(ChatMessage chatMessage){
+        System.out.println("chat message: "+chatMessage.getMessage());
+    }
+
 }

File JavaClient/src/main/java/org/jacp/chat/ChatMessageDecoder.java

+package org.jacp.chat;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.ChatMessage;
+import org.jacp.jee.messages.Message;
+import org.jacp.jee.messages.MessageType;
+
+import javax.websocket.DecodeException;
+import javax.websocket.Decoder;
+import java.nio.ByteBuffer;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 27.02.13
+ * Time: 15:32
+ * To change this template use File | Settings | File Templates.
+ */
+public class ChatMessageDecoder implements Decoder.Binary<ChatMessage> {
+    @Override
+    public ChatMessage decode(ByteBuffer byteBuffer) throws DecodeException {
+        return (ChatMessage) SerializationUtils.deserialize(byteBuffer.array());  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public boolean willDecode(ByteBuffer byteBuffer) {
+        Message message = (Message)SerializationUtils.deserialize(byteBuffer.array()) ;
+        if(message==null) return false;
+        if(message.getType().equals(MessageType.MESSAGE)) return true;
+        return false;
+    }
+}
+
+

File JavaClient/src/main/java/org/jacp/chat/ChatMessageEncoder.java

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

File JavaClient/src/main/java/org/jacp/chat/ClientAddUserDecoder.java

+package org.jacp.chat;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.Message;
+import org.jacp.jee.messages.MessageType;
+import org.jacp.jee.messages.UserAddMessage;
+
+import javax.websocket.DecodeException;
+import javax.websocket.Decoder;
+import java.nio.ByteBuffer;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 05.03.13
+ * Time: 22:11
+ * To change this template use File | Settings | File Templates.
+ */
+public class ClientAddUserDecoder implements Decoder.Binary<UserAddMessage> {
+    @Override
+    public UserAddMessage decode(ByteBuffer byteBuffer) throws DecodeException {
+        return (UserAddMessage) SerializationUtils.deserialize(byteBuffer.array());  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    @Override
+    public boolean willDecode(ByteBuffer byteBuffer) {
+        Message message = (Message) SerializationUtils.deserialize(byteBuffer.array());
+        if (message == null) return false;
+        if (message.getType().equals(MessageType.ADD_USER)) return true;
+        return false;
+    }
+}
+
+
+
+

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

+package org.jacp.chat;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.LoginMessage;
+
+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));
+    }
+}

File 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));
-    }
-}

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

                         System.out.println(session.isOpen());
                         messageLatch.await(100, TimeUnit.SECONDS);
                         endpoint.login();
+                        Thread.sleep(1000);
+                        endpoint.sendMessage("hallo world");
+                        Thread.sleep(1000);
                         latch.countDown();
                         Thread.sleep(500);
                     } catch (InterruptedException e) {

File WebClient/pom.xml

       <dependency>
           <groupId>javax</groupId>
           <artifactId>javaee-api</artifactId>
-		<version>${jee.version}</version>
+		  <version>${jee.version}</version>
 		<scope>provided</scope>
 	 </dependency>
 		<!-- Project specific dependencies -->

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

 
 import org.jacp.beans.websocket.chat.decoder.ChatMessageDecoder;
 import org.jacp.beans.websocket.chat.decoder.LoginMessageDecoder;
-import org.jacp.beans.websocket.chat.encoder.LoginMessageEncoder;
+import org.jacp.beans.websocket.chat.encoder.ChatMessageEncoder;
+import org.jacp.beans.websocket.chat.encoder.UserAddMessageEncoder;
 import org.jacp.jee.messages.ChatMessage;
 import org.jacp.jee.messages.LoginMessage;
 import org.jacp.jee.messages.UserAddMessage;
 import javax.websocket.server.DefaultServerConfiguration;
 import javax.websocket.server.WebSocketEndpoint;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.logging.Logger;
 
 /**
         LoginMessageDecoder.class
 },
         encoders = {
-                LoginMessageEncoder.class
+                UserAddMessageEncoder.class,
+                ChatMessageEncoder.class, UserAddMessageEncoder.class
         },
-        configuration=DefaultServerConfiguration.class)
+        configuration = DefaultServerConfiguration.class)
 public class ChatEndpoint {
     final static Logger logger = Logger.getLogger("application");
+
     @WebSocketOpen
     public void init(Session s) {
-        logger.info("############Someone connected..."+s.getId()+"  socket: "+this);
+        logger.info("############Someone connected..." + s.getId() + "  socket: " + this);
     }
 
 
     @WebSocketMessage
     public void handleLoginRequest(LoginMessage lrm, Session session) {
-        logger.info("############login..."+lrm.getUserName()+"  socket: "+this);
-        broadcastNewUser(lrm.getUserName(),session);
+        logger.info("############login..." + lrm.getUserName() + "  socket: " + this);
+        session.getUserProperties().put("user", new UserAddMessage(lrm.getUserName()));
+        broadcastNewUser(lrm.getUserName(), session);
     }
 
     @WebSocketMessage
     public void handleChatMessage(ChatMessage cm, Session session) {
-        broadcastMessage(cm,session);
+        broadcastMessage(cm, session);
     }
 
     private void broadcastMessage(ChatMessage cm, Session session) {
-        for(Session s: session.getOpenSessions()) {
+        for (Session s : session.getOpenSessions()) {
             try {
+                System.out.println("send message : " + cm.getMessage());
                 s.getRemote().sendObject(cm);
             } catch (IOException e) {
                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
             }
         }
     }
-    private void broadcastNewUser(String newUsername,Session session) {
-        for(Session s: session.getOpenSessions()) {
-            try {
+
+    private void broadcastNewUser(String newUsername, Session session) {
+        try {
+            for (Session s : session.getOpenSessions()) {
                 s.getRemote().sendObject(new UserAddMessage(newUsername));
-            } catch (IOException 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.
+                if (!s.getId().equals(session.getId())){
+                    System.out.println("Send: "+((UserAddMessage)s.getUserProperties().get("user")).getUserName());
+                    session.getRemote().sendObject(s.getUserProperties().get("user"));
+                }
             }
+        } catch (IOException 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.
         }
     }
 }

File WebClient/src/main/java/org/jacp/beans/websocket/chat/encoder/ChatMessageEncoder.java

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

File WebClient/src/main/java/org/jacp/beans/websocket/chat/encoder/LoginMessageEncoder.java

-package org.jacp.beans.websocket.chat.encoder;
-
-import org.jacp.jee.messages.LoginMessage;
-
-import javax.websocket.EncodeException;
-import javax.websocket.Encoder;
-import java.nio.ByteBuffer;
-
-/**
- * Created with IntelliJ IDEA.
- * User: ady
- * Date: 27.02.13
- * Time: 12:51
- * To change this template use File | Settings | File Templates.
- */
-public class LoginMessageEncoder implements Encoder.Binary<LoginMessage>{
-    @Override
-    public ByteBuffer encode(LoginMessage loginMessage) throws EncodeException {
-        return null;  //To change body of implemented methods use File | Settings | File Templates.
-    }
-}

File WebClient/src/main/java/org/jacp/beans/websocket/chat/encoder/UserAddMessageEncoder.java

+package org.jacp.beans.websocket.chat.encoder;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.jacp.jee.messages.LoginMessage;
+import org.jacp.jee.messages.UserAddMessage;
+
+import javax.websocket.EncodeException;
+import javax.websocket.Encoder;
+import java.nio.ByteBuffer;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: ady
+ * Date: 27.02.13
+ * Time: 12:51
+ * To change this template use File | Settings | File Templates.
+ */
+public class UserAddMessageEncoder implements Encoder.Binary<UserAddMessage>{
+    @Override
+    public ByteBuffer encode(UserAddMessage userAddMessage) throws EncodeException {
+        return ByteBuffer.wrap(SerializationUtils.serialize(userAddMessage));
+    }
+}
                 <module>WebClient</module>
                 <module>EAR</module>
                 <module>JavaClient</module>
+                <module>JacpFXChatClient</module>
             </modules>
         </profile>
     </profiles>
         <module>WebClient</module>
         <module>EAR</module>
         <module>JavaClient</module>
+        <module>JacpFXChatClient</module>
     </modules>
     <properties>
         <javax.persistence.version>2.0.0</javax.persistence.version>
         <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-b12</tyrusWebsocket.version>
+       <!-- <tyrusWebsocket.version>1.0-b12</tyrusWebsocket.version>-->
+        <tyrusWebsocket.version>1.0-SNAPSHOT</tyrusWebsocket.version>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <javafx.version>2.2</javafx.version>
+        <jacp.version>1.2</jacp.version>
+        <spring.version>3.1.1.RELEASE</spring.version>
     </properties>
     <dependencyManagement>
         <dependencies>
                 <version>${project.version}</version>
                 <type>jar</type>
             </dependency>
+            <dependency>
+                <groupId>${project.groupId}</groupId>
+                <artifactId>JacpFXChatClient</artifactId>
+                <version>${project.version}</version>
+                <type>jar</type>
+            </dependency>
         </dependencies>
     </dependencyManagement>
     <dependencies>