scp-app

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

commit 1d1b00c359777febd848f646eb328d566bc7648d
parent 532050bef81cb3a9d82992022ea836df5de6a9c5
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Fri, 31 Aug 2018 20:17:10 +1000

Added a cover-sheet and added some refinements

Diffstat:
AAssessmentItemCoverSheet.pdf | 0
MChat.java | 13+++++--------
MChatClient.java | 32++++++++++++++++----------------
MChatServer.java | 26+++++++++++++-------------
MSCP.java | 39+++++++++++++++++----------------------
MSCPException.java | 3+--
MTimeDiffException.java | 3+--
7 files changed, 53 insertions(+), 63 deletions(-)

diff --git a/AssessmentItemCoverSheet.pdf b/AssessmentItemCoverSheet.pdf Binary files differ. diff --git a/Chat.java b/Chat.java @@ -12,7 +12,6 @@ import java.net.Socket; * @since 2018-08-19 */ public class Chat { - protected SCP scp; // Must be a class as it uses non-static variables protected Scanner console; protected PrintWriter out; protected BufferedReader in; @@ -44,7 +43,6 @@ public class Chat { * Default Constructor */ public Chat() { - scp = new SCP(); console = new Scanner(System.in); } /** @@ -81,18 +79,18 @@ public class Chat { while((line = in.readLine()).compareTo("SCP END") != 0) { packet += line + "\n"; } - if(scp.parseDisconnect(packet)) { + if(SCP.parseDisconnect(packet)) { return "DISCONNECT"; } - if(scp.parseAcknowledge(packet)) { + if(SCP.parseAcknowledge(packet)) { return "ACKNOWLEDGE"; } - return scp.parseMessage(packet, address.getHostAddress(), port); + return SCP.parseMessage(packet, address.getHostAddress(), port); } /** * Send a SCP disconnect to the other user */ protected void disconnect() { - out.println(scp.disconnect()); + out.println(SCP.disconnect()); } -}- \ No newline at end of file +} diff --git a/ChatClient.java b/ChatClient.java @@ -39,19 +39,19 @@ public class ChatClient extends Chat { System.out.println("Connected to server"); System.out.print("Input a username: "); username = console.nextLine(); - scpConnect(); + SCPConnect(); System.out.println("Connected to SCP"); messageLoop(); console.close(); return 0; - } catch(SCPException scpe) { - System.err.println(scpe.getMessage()); + } catch(SCPException SCPe) { + System.err.println("Error: " + SCPe.getMessage()); return errorCodes.SCPERROR.value(); } catch(UnknownHostException uhe) { - System.err.println(uhe.getMessage()); + System.err.println("Error: " + uhe.getMessage()); return errorCodes.UNKNOWNHOSTERROR.value(); } catch(IOException ioe) { - System.err.println(ioe.getMessage()); + System.err.println("Error: " + ioe.getMessage()); return errorCodes.IOERROR.value(); } catch(NullPointerException npe) { System.out.println("\nError: unexpected cutoff from Server, ending program"); @@ -69,7 +69,7 @@ public class ChatClient extends Chat { recievedMessage = recieveMessage(); System.out.println(); if(recievedMessage == "DISCONNECT") { - out.println(scp.acknowledge()); + out.println(SCP.acknowledge()); System.out.println("Server disconnected"); disconnect = true; break; @@ -78,7 +78,7 @@ public class ChatClient extends Chat { System.out.print("Send a message: "); message = textToMessage(); if(message.compareTo("DISCONNECT") == 0) { - out.println(scp.disconnect()); + out.println(SCP.disconnect()); if(recieveMessage().compareTo("ACKNOWLEDGE") == 0) { System.out.println("Disconnected from server"); disconnect = true; @@ -88,7 +88,7 @@ public class ChatClient extends Chat { } } System.out.println("Waiting for message to send"); - out.println(scp.message(address.getHostAddress(), port, message)); + out.println(SCP.message(address.getHostAddress(), port, message)); System.out.print("Server is typing..."); } } @@ -111,11 +111,11 @@ public class ChatClient extends Chat { * @param username client specified username * @return true on packet send */ - private boolean scpConnect() throws SCPException, IOException { - String connectionString = scp.connect(username, address.getHostAddress(), port); + private boolean SCPConnect() throws SCPException, IOException { + String connectionString = SCP.connect(username, address.getHostAddress(), port); out.println(connectionString); - if(scpDecide()) { - scpAcknowledge(); + if(SCPDecide()) { + SCPAcknowledge(); return true; } return false; @@ -124,17 +124,17 @@ public class ChatClient extends Chat { * Find out whether the server accepted or rejected by the server * @return true if accepted, false if rejected */ - private boolean scpDecide() throws SCPException, IOException { + private boolean SCPDecide() throws SCPException, IOException { String inLine, packet = ""; while((inLine = in.readLine()).compareTo("SCP END") != 0) { packet += inLine + "\n"; } - return scp.parseAccept(packet, username, address.getHostAddress(), port); + return SCP.parseAccept(packet, username, address.getHostAddress(), port); } /** * Send a acknowledge SCP packet to the server */ - private void scpAcknowledge() { - out.println(scp.acknowledge(username, address.getHostAddress(), port)); + private void SCPAcknowledge() { + out.println(SCP.acknowledge(username, address.getHostAddress(), port)); } } diff --git a/ChatServer.java b/ChatServer.java @@ -45,8 +45,8 @@ public class ChatServer extends Chat { while(true) { hostConnection(); } - } catch(SCPException scpe) { - System.err.println("Error: " + scpe.getMessage()); + } catch(SCPException SCPe) { + System.err.println("Error: " + SCPe.getMessage()); return errorCodes.SCPERROR.value(); } catch(IOException ioe) { System.err.println("Error: " + ioe.getMessage()); @@ -68,7 +68,7 @@ public class ChatServer extends Chat { cliSocket.close(); System.out.println("Rejected client for time differential greater than 5, trying again"); } else { - scpAccept(); + SCPAccept(); if(acknowledged()) { System.out.println(String.format("User %s has connected to SCP", username)); System.out.println(); @@ -87,14 +87,14 @@ public class ChatServer extends Chat { String recievedMessage; boolean disconnect = false; System.out.println(rules()); - while(!disconnect) { + while(!disconnect) { // I assume this gets sigkilled to end System.out.println("Waiting for message to send"); - out.println(scp.message(address.getHostAddress(), port, message)); + out.println(SCP.message(address.getHostAddress(), port, message)); System.out.print(String.format("%s is typing...", username)); recievedMessage = recieveMessage(); System.out.println(); if(recievedMessage == "DISCONNECT") { - out.println(scp.acknowledge()); + out.println(SCP.acknowledge()); System.out.println("Client disconnected"); disconnect = true; break; @@ -103,7 +103,7 @@ public class ChatServer extends Chat { System.out.print("Send a message: "); message = textToMessage(); if(message.compareTo("DISCONNECT") == 0) { - out.println(scp.disconnect()); + out.println(SCP.disconnect()); if(recieveMessage().compareTo("ACKNOWLEDGE") == 0) { disconnect = true; System.out.println("Successfully disconnected from Client"); @@ -135,7 +135,7 @@ public class ChatServer extends Chat { return true; } /** - * Recieve a scp connection + * Recieve a SCP connection * @return Client's username */ private String clientConnect() throws SCPException, IOException { @@ -144,7 +144,7 @@ public class ChatServer extends Chat { packet += inLine + "\n"; } try { - result = scp.parseConnect(packet, address.getHostAddress(), port); + result = SCP.parseConnect(packet, address.getHostAddress(), port); } catch(TimeDiffException tde) { reject(tde.getTimeDiff()); } @@ -155,14 +155,14 @@ public class ChatServer extends Chat { * @param timeDiff Difference in time of client request to server processing it */ private void reject(int timeDiff) { - out.println(scp.reject(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() { - out.println(scp.accept(username, address.getHostAddress(), port)); + private void SCPAccept() { + out.println(SCP.accept(username, address.getHostAddress(), port)); } /** * Check if the client has been acknowledged by the server @@ -172,7 +172,7 @@ public class ChatServer extends Chat { while((inLine = in.readLine()).compareTo("SCP END") != 0) { packet += inLine + "\n"; } - boolean result = scp.parseAcknowledge(packet, address.getHostAddress(), port, username); + boolean result = SCP.parseAcknowledge(packet, address.getHostAddress(), port, username); if(result) { return true; } else { diff --git a/SCP.java b/SCP.java @@ -12,17 +12,13 @@ import java.io.IOException; public class SCP { // Packet generating methods /** - * Default constructor - */ - public SCP() {} - /** * 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) { + public static 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 @@ -32,7 +28,7 @@ public class SCP { * Acknowledge a disconnect * @return A SCP Acknowledge packet for disconnection */ - public String acknowledge() { + public static String acknowledge() { return "SCP ACKNOWLEDGE\nSCP END"; } /** @@ -42,7 +38,7 @@ public class SCP { * @param port the server port * @return an acknowledge packet */ - public String acknowledge(String username, String address, int port) { + public static String acknowledge(String username, String address, int port) { return String.format( "SCP ACKNOWLEDGE\nUSERNAME \"%s\"\nSERVERADDRESS %s\nSERVERPORT %d\nSCP END", username, address, port @@ -55,7 +51,7 @@ public class SCP { * @param port port of the client * @return an accept packet */ - public String accept(String username, String address, int port) { + public static String accept(String username, String address, int port) { return String.format( "SCP ACCEPT\nUSERNAME %s\nCLIENTADDRESS %s\nCLIENTPORT %d\nSCP END", username, address, port @@ -68,7 +64,7 @@ public class SCP { * @param contents the contents of the message * @return a message packet */ - public String message(String address, int port, String contents) { + public static 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 @@ -80,7 +76,7 @@ public class SCP { * @param address address of the recipient * @return a reject packet */ - public String reject(int timeDiff, String address) { + public static String reject(int timeDiff, String address) { return String.format( "SCP REJECT\nTIMEDIFFERENTIAL %d\nREMOTEADDRESS %s\nSCP END", timeDiff, address @@ -90,7 +86,7 @@ public class SCP { * Disconnect * @return A disconnect packet */ - public String disconnect() { + public static String disconnect() { return "SCP DISCONNECT\nSCP END"; } // packet parsing @@ -100,13 +96,13 @@ public class SCP { * @param address the caller's ip address * @return The client's username */ - public String parseConnect(String packet, String address, int port) throws TimeDiffException, SCPException, IOException { + public static String parseConnect(String packet, String address, int port) throws TimeDiffException, SCPException, IOException { String username = ""; BufferedReader sstream = new BufferedReader(new StringReader(packet)); boolean firstLine = true; String line; int lineNum = 1; - while((line = sstream.readLine()) != null) { + while((line = sstream.readLine()) != null) { // check that the packet matchs expected input if(firstLine) { if(line.indexOf("SCP CONNECT") < 0) { sstream.close(); @@ -153,7 +149,7 @@ public class SCP { * @param otherTime specified time * @return The difference in times */ - private int findTimeDiff(int otherTime) { + private static int findTimeDiff(int otherTime) { return Math.abs((int)Instant.now().getEpochSecond() - otherTime); } /** @@ -161,8 +157,8 @@ public class SCP { * @param packet a SCP packet * @return true on successful parse else false */ - public boolean parseAcknowledge(String packet) { - if (packet.indexOf("SCP ACKNOWLEDGE") > -1) { + public static boolean parseAcknowledge(String packet) { + if (packet.indexOf("SCP ACKNOWLEDGE") > -1) { // packet is only 2 lines return true; } return false; @@ -175,7 +171,7 @@ public class SCP { * @param username the client's username * @return true on successful parse */ - public boolean parseAcknowledge(String packet, String address, int port, String username) throws SCPException, IOException { + public static boolean parseAcknowledge(String packet, String address, int port, String username) throws SCPException, IOException { BufferedReader sstream = new BufferedReader(new StringReader(packet)); boolean firstLine = true; String line; @@ -218,7 +214,7 @@ public class SCP { * @param username the Client's username * @return true if successful parse else false */ - public boolean parseAccept(String packet, String username, String address, int port) throws SCPException, IOException { + public static boolean parseAccept(String packet, String username, String address, int port) throws SCPException, IOException { boolean accept = false; BufferedReader sstream = new BufferedReader(new StringReader(packet)); String line; @@ -258,7 +254,7 @@ public class SCP { * @param port the caller's port used for the application * @return a String of the Message contents */ - public String parseMessage(String packet, String address, int port) throws SCPException, IOException { + public static String parseMessage(String packet, String address, int port) throws SCPException, IOException { boolean firstLine = true; BufferedReader sstream = new BufferedReader(new StringReader(packet)); String line; @@ -296,10 +292,10 @@ public class SCP { * @param packet a SCP packet * @return true if successful parse else false */ - public boolean parseDisconnect(String packet) { + public static boolean parseDisconnect(String packet) { if (packet.indexOf("SCP DISCONNECT") > -1) { return true; } return false; } -}- \ No newline at end of file +} diff --git a/SCPException.java b/SCPException.java @@ -24,4 +24,4 @@ public class SCPException extends Exception { public SCPException(String expected, String received) { super("Expected " + expected + " but got " + received); } -}- \ No newline at end of file +} diff --git a/TimeDiffException.java b/TimeDiffException.java @@ -22,4 +22,4 @@ public class TimeDiffException extends Exception { * @return the Difference in times */ public int getTimeDiff() { return timeDiff; } -}- \ No newline at end of file +}