scp-app

git clone git://git.codymlewis.com/scp-app.git
Log | Files | Refs | LICENSE

commit b0f73c9c8c146d6ed4239596f3070c79a0231e22
parent 5a9154c196ee11a4d5cfc4cf793fa84309fd0e74
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Sat, 18 Aug 2018 19:02:48 +1000

Started division SCP messages into their own class

Diffstat:
MChatClient.java | 38++++++++++++++++++++++++++++++--------
MChatServer.java | 34++++++++++++++++++++++------------
ASCP.java | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ASCPException.java | 15+++++++++++++++
4 files changed, 159 insertions(+), 20 deletions(-)

diff --git a/ChatClient.java b/ChatClient.java @@ -1,5 +1,4 @@ import java.util.Scanner; -import java.time.Instant; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.InputStreamReader; @@ -14,6 +13,7 @@ import java.io.IOException; * @since 2018-08-10 */ public class ChatClient { + private SCP scp = new SCP(); private Socket socket; private PrintWriter out; private BufferedReader in; @@ -64,12 +64,35 @@ public class ChatClient { * @param username client specified username * @return true on packet send */ - private boolean scpConnect(String hostName, int port, String username) { - String connectionString = String.format( - "SCP CONNECT\nSERVERADDRESS %s\nSERVERPORT %d\nREQUESTCREATED %d\nUSERNAME \"%s\"\nSCP END", - hostName, port, Instant.now().getEpochSecond(), username - ); + private boolean scpConnect(String hostName, int port, String username) throws IOException { + String connectionString = scp.connect(username, hostName, port); out.println(connectionString); + if(scpDecide(username)) { + scpAknowledge(username); + return true; + } + return false; + } + private boolean scpDecide(String username) throws IOException { + String inLine; + boolean accept = false; + while((inLine = in.readLine()).compareTo("SCP END") != 0) { + if(!accept) { + if(inLine.indexOf("SCP ACCEPT") > -1) { + accept = true; + } else { + throw new IOException(); + } + } + if(inLine.indexOf("USERNAME") > -1) { + if(inLine.indexOf(username) == -1) { + return false; + } + } + } return true; } -}- \ No newline at end of file + private void scpAknowledge(String username) { + out.println(scp.acknowledge(username, "127.0.0.1", 3400)); + } +} diff --git a/ChatServer.java b/ChatServer.java @@ -16,6 +16,7 @@ import java.net.UnknownHostException; * @since 2018-08-10 */ public class ChatServer { + private SCP scp = new SCP(); private ServerSocket serverSocket; private Socket cliSocket; private PrintWriter out; @@ -46,7 +47,7 @@ public class ChatServer { } } /** - * Host a client connectio + * Host a client connection * @param welcomeMessage the welcome message sent to the client */ private void hostConnection(String welcomeMessage) throws IOException { @@ -63,8 +64,10 @@ public class ChatServer { continue; } scpAccept(username); - out.println(welcomeMessage); - System.out.println(String.format("User %s has connected to SCP", username)); + if(acknowledged(username)) { + out.println(welcomeMessage); + System.out.println(String.format("User %s has connected to SCP", username)); + } disconnect = true; } } @@ -131,20 +134,27 @@ public class ChatServer { * @param timeDiff Difference in time of client request to server processing it */ private void reject(int timeDiff) { - out.println(String.format( - "SCP REJECT\nTIMEDIFFERENTIAL %d\nREMOTEADDRESS %s\nSCP END", - timeDiff, cliSocket.getLocalAddress().getHostAddress() - ) - ); + out.println(scp.reject(timeDiff, cliSocket.getLocalAddress().getHostAddress())); } /** * Send a SCP connect message to the client * @param username user specified name */ private void scpAccept(String username) { - out.println(String.format( - "SCP ACCEPT\nUSERNAME %s\nCLIENTADDRESS %s\nCLIENTPORT %d\nSCP END" - ) - ); + out.println(scp.accept(username, "127.0.0.1", 3400)); + } + private boolean acknowledged(String username) throws IOException { + String inLine; + boolean firstLine = true; + while((inLine = in.readLine()).compareTo("SCP END") != 0) { + if(firstLine) { + if(inLine.indexOf("SCP ACKNOWLEDGE") > -1) { + firstLine = false; + } else { + return false; + } + } + } + return true; } } diff --git a/SCP.java b/SCP.java @@ -0,0 +1,91 @@ +import java.time.Instant; +/** + * SCP.java - Seng3400A1 + * SCP parsing/packet creating library + * + * @author Cody Lewis + * @since 2018-08-10 + */ +public class SCP { + // Packet generating methods + /** + * Generate the scp connect packet + * @param username username of the sender + * @param address the server address + * @param port port number of the server + * @return A SCP Connect packet + */ + public String connect(String username, String address, int port) { + return String.format( + "SCP CONNECT\nSERVERADDRESS %s\nSERVERPORT %d\nREQUESTCREATED %d\nUSERNAME \"%s\"\nSCP END", + address, port, Instant.now().getEpochSecond(), username + ); + } + /** + * Acknowledge a disconnect + * @return A SCP Acknowledge packet for disconnection + */ + public String acknowledge() { + return "SCP ACKNOWLEDGE\nSCP END"; + } + /** + * Acknowledge a connect + * @param username username of the sender + * @param address the server address + * @param port the server port + * @return an acknowledge packet + */ + public String acknowledge(String username, String address, int port) { + return String.format( + "SCP ACKNOWLEDGE\nUSERNAME \"%s\"\nSERVERADDRESS %s\nSERVERPORT %d\nSCP END", + username, address, port + ); + } + /** + * Accept a connection + * @param username the username of the sender + * @param address address of the client + * @param port port of the client + * @return an accept packet + */ + public String accept(String username, String address, int port) { + return String.format( + "SCP ACCEPT\nUSERNAME %s\nCLIENTADDRESS %s\nCLIENTPORT %d\nSCP END", + username, address, port + ); + } + /** + * Send a message + * @param address the address of the recipient + * @param port the port of the recipient + * @param contents the contents of the message + * @return a message packet + */ + public String message(String address, int port, String contents) { + return String.format( + "SCP CHAT\nREMOTEADDRESS %s\nREMOTEPORT %d\nMESSAGECONTENT\n\n%s\nSCP END", + address, port, contents + ); + } + /** + * Reject a connection + * @param timeDiff the difference in connection times + * @param address address of the recipient + * @return a reject packet + */ + public String reject(int timeDiff, String address) { + return String.format( + "SCP REJECT\nTIMEDIFFERENTIAL %d\nREMOTEADDRESS %s\nSCP END", + timeDiff, address + ); + } + /** + * Disconnect + * @return A disconnect packet + */ + public String disconnect() { + return "SCP DISCONNECT\nSCP END"; + } + // packet parsing + +}+ \ No newline at end of file diff --git a/SCPException.java b/SCPException.java @@ -0,0 +1,14 @@ +/** + * SCPException.java - Seng3400A1 + * Exception for SCP related errors + * + * @author Cody Lewis + * @since 2018-08-10 + */ +public class SCPException extends Exception { + static final long serialVersionUID = 1L; + public SCPException() {} + public SCPException(String message) { + super(message); + } +}+ \ No newline at end of file