diff --git a/Extasys for Java Examples/Extasys.Examples.TCPChatClient/src/Extasys/Examples/TCPChatClient/TCPChatClient.java b/Extasys for Java Examples/Extasys.Examples.TCPChatClient/src/Extasys/Examples/TCPChatClient/TCPChatClient.java index ccbd93f..fd90461 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPChatClient/src/Extasys/Examples/TCPChatClient/TCPChatClient.java +++ b/Extasys for Java Examples/Extasys.Examples.TCPChatClient/src/Extasys/Examples/TCPChatClient/TCPChatClient.java @@ -20,6 +20,7 @@ of this software and associated documentation files (the "Software"), to deal package Extasys.Examples.TCPChatClient; import Extasys.DataFrame; +import Extasys.Encryption.Base64Encryptor; import Extasys.Network.TCP.Client.Connectors.TCPConnector; import Extasys.Network.TCP.Client.Exceptions.*; import java.net.InetAddress; @@ -36,17 +37,18 @@ public class TCPChatClient extends Extasys.Network.TCP.Client.ExtasysTCPClient private final String fUsername; private final frmTCPChatClient fMainForm; private String fSPT = String.valueOf(((char) 2)); // Message splitter character. - private String fMCChar = String.valueOf(((char) 3)); // Message collector character. public TCPChatClient(InetAddress serverIP, int port, String username, frmTCPChatClient frmMain) { - super("TCP Chat Client", "", 4, 8); + super("TCP Chat Client", "", 1, 2); fServerIP = serverIP; fPort = port; fUsername = username; fMainForm = frmMain; - super.AddConnector("Main Connector", serverIP, port, 20480, ((char) 3)); + TCPConnector connector = super.AddConnector("Main Connector", serverIP, port, 8192, ((char) 3)); + connector.setAutoApplyMessageSplitterState(true); // Auto apply message splitter to outgoing messages + connector.setConnectionEncryptor(new Base64Encryptor()); // Base 64 Encryption } public void Connect() @@ -66,69 +68,67 @@ public void OnDataReceive(TCPConnector connector, DataFrame data) { String[] splittedMessage = new String(data.getBytes()).split(fSPT); - if (splittedMessage[0].equals("Change_Username")) + switch (splittedMessage[0]) { - /* Message: Change_Username ((char)2) - This user must change username because this one is allready in use by an other user, - */ - fMainForm.MarkAsDisconnected(); - fMainForm.DisplayMessage("Please change your username. This one is allready in use by an other user."); - } - else if (splittedMessage[0].equals("Welcome")) - { - /* Message: Welcome ((char)2) - Server welcomes you. - */ - fMainForm.MarkAsConnected(); - fMainForm.DisplayMessage("You are now connected !!!"); - SendDataToServer("Get_List" + fSPT); - } - else if (splittedMessage[0].equals("User_List")) - { - /* Message: User_List ((char)2) list... - Server sends a list with connected clients to the client. - */ - if (!splittedMessage[1].equals("")) - { - String[] connectedUsers = splittedMessage[1].split(String.valueOf(((char) 1))); - for (int i = 0; i < connectedUsers.length; i++) + case "Change_Username": + // Message: Change_Username ((char)2) + // This user must change username because this one is allready in use by an other user. + fMainForm.MarkAsDisconnected(); + fMainForm.DisplayMessage("Please change your username. This one is allready in use by an other user."); + break; + + case "Welcome": + /* Message: Welcome ((char)2) + Server welcomes you. + */ + fMainForm.MarkAsConnected(); + fMainForm.DisplayMessage("You are now connected !!!"); + SendDataToServer("Get_List" + fSPT); + break; + + case "User_List": + // Message: User_List ((char)2) list... + // Server sends a list with connected clients to the client. + if (!splittedMessage[1].equals("")) { - if (!connectedUsers[i].equals(fUsername)) + String[] connectedUsers = splittedMessage[1].split(String.valueOf(((char) 1))); + for (int i = 0; i < connectedUsers.length; i++) { - fMainForm.AddUserInList(connectedUsers[i]); + if (!connectedUsers[i].equals(fUsername)) + { + fMainForm.AddUserInList(connectedUsers[i]); + } } } - } - } - else if (splittedMessage[0].equals("New_User")) - { - /* Message: New_User ((char)2) username - A new user connected to server. - */ - fMainForm.AddUserInList(splittedMessage[1]); - fMainForm.DisplayMessage("User " + splittedMessage[1] + " connected"); - } - else if (splittedMessage[0].equals("Remove_User")) - { - /* Message: Remove_User ((char)2) username - User disconnected from server. - */ - fMainForm.RemoveUser(splittedMessage[1]); - fMainForm.DisplayMessage("User " + splittedMessage[1] + " disconnected"); - } - else if (splittedMessage[0].equals("Message")) - { - /* Message: Message ((char)2) some_text - Server sends a message - */ - fMainForm.DisplayMessage(splittedMessage[1]); - } - else if (splittedMessage[0].equals("Ping")) - { - /* Message: Ping ((char)2) - Server pings you. - */ - SendDataToServer("Pong" + fSPT); + break; + + case "New_User": + // Message: New_User ((char)2) username + // A user connected to the server. + fMainForm.AddUserInList(splittedMessage[1]); + fMainForm.DisplayMessage("User " + splittedMessage[1] + " connected"); + break; + + case "Remove_User": + // Message: Remove_User ((char)2) username + // A user has been disconnected from server. + fMainForm.RemoveUser(splittedMessage[1]); + fMainForm.DisplayMessage("User " + splittedMessage[1] + " disconnected"); + break; + + case "Message": + // Message: Message ((char)2) some_text + // Server sends a message + fMainForm.DisplayMessage(splittedMessage[1]); + break; + + case "Ping": + // Message: Ping ((char)2) + // Server pings you. + SendDataToServer("Pong" + fSPT); + break; + default: + break; } } @@ -149,7 +149,7 @@ public void SendDataToServer(String data) { try { - super.SendData(data + fMCChar); + super.SendData(data); } catch (ConnectorDisconnectedException ex) { diff --git a/Extasys for Java Examples/Extasys.Examples.TCPChatServer/src/Extasys/Examples/TCPChatServer/TCPChatServer.java b/Extasys for Java Examples/Extasys.Examples.TCPChatServer/src/Extasys/Examples/TCPChatServer/TCPChatServer.java index 6e3c4e0..17a2b09 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPChatServer/src/Extasys/Examples/TCPChatServer/TCPChatServer.java +++ b/Extasys for Java Examples/Extasys.Examples.TCPChatServer/src/Extasys/Examples/TCPChatServer/TCPChatServer.java @@ -20,9 +20,11 @@ of this software and associated documentation files (the "Software"), to deal package Extasys.Examples.TCPChatServer; import Extasys.DataFrame; -import Extasys.Network.TCP.Server.Listener.Exceptions.*; - +import Extasys.Encryption.Base64Encryptor; +import Extasys.Network.TCP.Server.Listener.Exceptions.ClientIsDisconnectedException; +import Extasys.Network.TCP.Server.Listener.Exceptions.OutgoingPacketFailedException; import Extasys.Network.TCP.Server.Listener.TCPClientConnection; +import Extasys.Network.TCP.Server.Listener.TCPListener; import java.net.InetAddress; import java.util.HashMap; @@ -34,16 +36,21 @@ public class TCPChatServer extends Extasys.Network.TCP.Server.ExtasysTCPServer { private final HashMap fConnectedClients; - private final String fSPT = String.valueOf(((char) 2)); // Message splitter character. - private final String fMCChar = String.valueOf(((char) 3)); // Message collector character. + + private final String fSPT = String.valueOf(((char) 2)); // Message splitter character. This is not the same as the message collector character + private Thread fPingThread; private boolean fServerIsActive; private final frmTCPChatServer fMainForm; public TCPChatServer(InetAddress listenerIP, int port, frmTCPChatServer frmMain) { - super("TCP Chat Server", "", 10, 100); - super.AddListener("Main Listener", listenerIP, port, 99999, 20480, 10000, 100, ((char) 3)); + super("TCP Chat Server", "This is a simple chat server", 10, 20); + + TCPListener listener = super.AddListener("Main Listener", listenerIP, port, 9999, 8192, 10000, 100, ((char) 3)); + listener.setAutoApplyMessageSplitterState(true); // Auto apply message splitter to outgoing messages + listener.setConnectionEncryptor(new Base64Encryptor()); // Base 64 Encryption + fConnectedClients = new HashMap<>(); fMainForm = frmMain; } @@ -65,7 +72,8 @@ public void run() { try { - SendToAllClients("Ping" + fSPT); + // Send "Ping" command to all Client + ReplyToAll("Ping" + fSPT); Thread.sleep(5000); } catch (InterruptedException ex) @@ -105,79 +113,78 @@ public void OnDataReceive(TCPClientConnection sender, DataFrame data) { try { - System.out.println(new String(data.getBytes())); + // This isa the client's incoming message String[] splittedMessage = new String(data.getBytes()).split(fSPT); - if (splittedMessage[0].equals("Login")) + switch (splittedMessage[0]) { - /* Message: Login ((char)2) Username - Client wants to login. - Server checks if username is unique. - If the username is taken server replys -> "Change_Username ((char)2)" - If the username is not taken then server replys -> "Welcome ((char)2)" to the new client - and sends "New_User ((char)2) NewUsername" to all other clients. - */ - String tmpUsername = splittedMessage[1]; - if (IsUsernameTaken(tmpUsername)) - { - SendToClient("Change_Username" + fSPT, sender); - } - else - { - TCPChatUser user = new TCPChatUser(tmpUsername, sender); - fConnectedClients.put(sender.getIPAddress(), user); + case "Login": + // Message: Login ((char)2) Username + // Client wants to login. + // Server checks if username is unique. + // If the username is taken server replys -> "Change_Username ((char)2)" + // If the username is not taken then server replys -> "Welcome ((char)2)" to the new client + // and sends "New_User ((char)2) NewUsername" to all other clients. + String tmpUsername = splittedMessage[1]; + if (IsUsernameTaken(tmpUsername)) + { + sender.SendData("Change_Username" + fSPT); + } + else + { + TCPChatUser user = new TCPChatUser(tmpUsername, sender); + fConnectedClients.put(sender.getIPAddress(), user); - SendToAllClients("New_User" + fSPT + tmpUsername); - SendToClient("Welcome" + fSPT, sender); - } - } - else if (splittedMessage[0].equals("Message")) - { - /* Message: Message ((char)2) some_text - Client sends a chat message to the server. - Server checks if the client is registered to the server. - If the client is registered to the server - the server sends this message to all the other clients "Message ((char)2) Sender's username : some_text" else - it disconnects the client. - */ - if (fConnectedClients.containsKey(sender.getIPAddress())) - { - SendToAllClients("Message" + fSPT + GetClientName(sender) + ":" + splittedMessage[1]); - } - else - { - sender.DisconnectMe(); - } - } - else if (splittedMessage[0].equals("Get_List")) - { - /* Message: Get_List ((char)2) - Client requets a list with other connected clients. - If the client is registered to the server the server replys the list - else it disconnects the client. - */ - if (fConnectedClients.containsKey(sender.getIPAddress())) - { - SendToClient(GetConnectedClientsList(), sender); - } - else - { - sender.DisconnectMe(); - } - } - else if (splittedMessage[0].equals("Pong")) - { - /* Message: Pong ((char)2) - Client response to Ping. - */ - System.out.println(GetClientName(sender) + " PONG!"); - } - else - { - System.out.println(sender.getIPAddress() + " sends wrong message"); + super.ReplyToAll("New_User" + fSPT + tmpUsername); + sender.SendData("Welcome" + fSPT); + } + break; + + case "Message": + // Message: Message ((char)2) some_text + // Client sends a chat message to the server. + // Server checks if the client is registered to the server. + // If the client is registered to the server + // the server sends this message to all the other clients "Message ((char)2) Sender's username : some_text" else + // it disconnects the client. + if (fConnectedClients.containsKey(sender.getIPAddress())) + { + super.ReplyToAll("Message" + fSPT + GetClientName(sender) + ":" + splittedMessage[1]); + } + else + { + sender.DisconnectMe(); + } + break; + + case "Get_List": + // Message: Get_List ((char)2) + // Client requets a list with other connected clients. + // If the client is registered to the server the server replys the list + // else it disconnects the client. + if (fConnectedClients.containsKey(sender.getIPAddress())) + { + sender.SendData(GetConnectedClientsList()); + } + else + { + sender.DisconnectMe(); + } + break; + + case "Pong": + // Message: Pong ((char)2) + // A Client responded to Ping. + // Do nothing.... + System.out.println(GetClientName(sender) + " PONG!"); + break; + + default: + System.out.println(sender.getIPAddress() + " sends wrong message"); + break; } } - catch (Exception ex) + catch (ClientIsDisconnectedException | OutgoingPacketFailedException ex) { System.err.println(ex.getMessage()); } @@ -211,44 +218,6 @@ private String GetClientName(TCPClientConnection sender) return ""; } - private void SendToAllClients(String message) - { - message = message + fMCChar; - for (TCPChatUser user : fConnectedClients.values()) - { - try - { - user.SendData(message); - } - catch (ClientIsDisconnectedException | OutgoingPacketFailedException ex) - { - System.err.println(ex.getMessage()); - } - } - - } - - private void SendToClient(String data, TCPClientConnection sender) - { - try - { - sender.SendData(data + fMCChar); - } - catch (ClientIsDisconnectedException ex) - { - // Client disconnected. - System.err.println(ex.getMessage()); - } - catch (OutgoingPacketFailedException ex) - { - // Failed to send packet. - System.err.println(ex.getMessage()); - } - catch (Exception ex) - { - } - } - private String GetConnectedClientsList() { String list = ""; @@ -272,7 +241,7 @@ public void OnClientDisconnect(TCPClientConnection client) { if (fConnectedClients.containsKey(client.getIPAddress())) { - SendToAllClients("Remove_User" + fSPT + ((TCPChatUser) fConnectedClients.get(client.getIPAddress())).getUsername()); + super.ReplyToAll("Remove_User" + fSPT + ((TCPChatUser) fConnectedClients.get(client.getIPAddress())).getUsername()); fConnectedClients.remove(client.getIPAddress()); } fMainForm.UpdateClientsCount(); diff --git a/Extasys for Java Examples/Extasys.Examples.TCPChatServer/src/Extasys/Examples/TCPChatServer/TCPChatUser.java b/Extasys for Java Examples/Extasys.Examples.TCPChatServer/src/Extasys/Examples/TCPChatServer/TCPChatUser.java index e6d91ae..c31a436 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPChatServer/src/Extasys/Examples/TCPChatServer/TCPChatUser.java +++ b/Extasys for Java Examples/Extasys.Examples.TCPChatServer/src/Extasys/Examples/TCPChatServer/TCPChatUser.java @@ -19,7 +19,6 @@ of this software and associated documentation files (the "Software"), to deal THE SOFTWARE.*/ package Extasys.Examples.TCPChatServer; -import Extasys.Network.TCP.Server.Listener.Exceptions.*; import Extasys.Network.TCP.Server.Listener.TCPClientConnection; /** @@ -29,8 +28,8 @@ of this software and associated documentation files (the "Software"), to deal public class TCPChatUser { - private String fUsername; - private TCPClientConnection fConnection; + private final String fUsername; + private final TCPClientConnection fConnection; public TCPChatUser(String username, TCPClientConnection connection) { @@ -48,8 +47,4 @@ public TCPClientConnection getConnection() return fConnection; } - public void SendData(String data) throws ClientIsDisconnectedException, OutgoingPacketFailedException - { - fConnection.SendData(data); - } } diff --git a/Extasys for Java Examples/Extasys.Examples.TCPClient/nbproject/private/private.xml b/Extasys for Java Examples/Extasys.Examples.TCPClient/nbproject/private/private.xml index 6807a2b..e21765a 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPClient/nbproject/private/private.xml +++ b/Extasys for Java Examples/Extasys.Examples.TCPClient/nbproject/private/private.xml @@ -2,6 +2,8 @@ - + + file:/C:/Development/extasys/Extasys%20for%20Java%20Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/TCPClient.java + diff --git a/Extasys for Java Examples/Extasys.Examples.TCPServer/nbproject/private/private.xml b/Extasys for Java Examples/Extasys.Examples.TCPServer/nbproject/private/private.xml index 6807a2b..21d323f 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPServer/nbproject/private/private.xml +++ b/Extasys for Java Examples/Extasys.Examples.TCPServer/nbproject/private/private.xml @@ -2,6 +2,8 @@ - + + file:/C:/Development/extasys/Extasys%20for%20Java%20Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/TCPServer.java +