scp-app

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

commit 532050bef81cb3a9d82992022ea836df5de6a9c5
parent 614bf24cf69d51b5b0df05b7a3b9da037224c2b2
Author: Cody Lewis <luxdotsugi@gmail.com>
Date:   Sat, 25 Aug 2018 19:49:26 +1000

Added enums for the error codes and improved SCP packet parsing

Diffstat:
MChat.java | 30+++++++++++++++++++++++++++++-
MChatClient.java | 14++++++++++----
MChatServer.java | 17++++++++++-------
MSCP.java | 109+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
4 files changed, 117 insertions(+), 53 deletions(-)

diff --git a/Chat.java b/Chat.java @@ -21,12 +21,36 @@ public class Chat { protected int port; protected String username; /** + * Enum of the various error codes returned by the classes + */ + protected enum errorCodes { + SCPERROR(-1), + UNKNOWNHOSTERROR(-2), + IOERROR(-3), + NULLERROR(-4); + + private final int code; // error code variable + /** + * Default constructor + */ + private errorCodes(int code) { this.code = code; } + /** + * Get the error code value + * @return the error code of this + */ + public int value() { return code; } + } + /** * Default Constructor */ public Chat() { scp = new SCP(); console = new Scanner(System.in); } + /** + * The chat message rules + * @return A String containing the rules of the chat's messaging + */ protected String rules() { return "Press enter twice to send a message,\nType DISCONNECT to end the chat"; } @@ -36,13 +60,17 @@ public class Chat { */ protected String textToMessage() { String line = console.nextLine(), message = ""; + boolean empty = true; // Make sure an empty message is not sent do { + if(line.length() != 0) { + empty = false; + } if(line.compareTo("DISCONNECT") == 0) { return "DISCONNECT"; } message += "\n" + line; line = console.nextLine(); - } while(line.length() != 0); + } while(empty || line.length() != 0); return message.substring(1); // get rid of leading \n } /** diff --git a/ChatClient.java b/ChatClient.java @@ -19,14 +19,15 @@ public class ChatClient extends Chat { */ public static void main(String args[]) { ChatClient cc = new ChatClient(); - cc.run(args); - System.exit(0); + int exitVal = cc.run(args); + System.exit(exitVal); } /** * Main flow of the program * @param args arguments sent in with the program + * @return an end status int */ - public void run(String args[]) { + public int run(String args[]) { try { address = args.length > 0 ? InetAddress.getByName(args[0]) : InetAddress.getLocalHost(); port = args.length > 1 ? Integer.parseInt(args[1]) : 3400; @@ -37,19 +38,24 @@ public class ChatClient extends Chat { connectToServer(); System.out.println("Connected to server"); System.out.print("Input a username: "); - username = console.next(); + username = console.nextLine(); scpConnect(); System.out.println("Connected to SCP"); messageLoop(); console.close(); + return 0; } catch(SCPException scpe) { System.err.println(scpe.getMessage()); + return errorCodes.SCPERROR.value(); } catch(UnknownHostException uhe) { System.err.println(uhe.getMessage()); + return errorCodes.UNKNOWNHOSTERROR.value(); } catch(IOException ioe) { System.err.println(ioe.getMessage()); + return errorCodes.IOERROR.value(); } catch(NullPointerException npe) { System.out.println("\nError: unexpected cutoff from Server, ending program"); + return errorCodes.NULLERROR.value(); } } /** diff --git a/ChatServer.java b/ChatServer.java @@ -23,14 +23,15 @@ public class ChatServer extends Chat { */ public static void main(String args[]) { ChatServer cs = new ChatServer(); - cs.run(args); - System.exit(0); + int exitVal = cs.run(args); + System.exit(exitVal); } /** * The main flow of the program * @param args The arguments sent in with the program + * @return an end status int */ - public void run(String args[]) { + public int run(String args[]) { try { address = args.length > 0 ? InetAddress.getByName(args[0]) : InetAddress.getLocalHost(); port = args.length > 1 ? Integer.parseInt(args[1]) : 3400; @@ -41,11 +42,15 @@ public class ChatServer extends Chat { System.out.println(String.format("Starting server on %s:%d", address.getHostAddress(), port)); startSocket(); System.out.println("Started server"); - hostConnection(); + while(true) { + hostConnection(); + } } catch(SCPException scpe) { System.err.println("Error: " + scpe.getMessage()); + return errorCodes.SCPERROR.value(); } catch(IOException ioe) { System.err.println("Error: " + ioe.getMessage()); + return errorCodes.IOERROR.value(); } } /** @@ -72,8 +77,6 @@ public class ChatServer extends Chat { } } catch(NullPointerException npe) { System.out.println("\nError: unexpected cut-off from client, looking for new client"); - } finally { - hostConnection(); } } /** @@ -167,7 +170,7 @@ public class ChatServer extends Chat { private boolean acknowledged() throws SCPException, IOException { String inLine, packet = ""; while((inLine = in.readLine()).compareTo("SCP END") != 0) { - packet += inLine; + packet += inLine + "\n"; } boolean result = scp.parseAcknowledge(packet, address.getHostAddress(), port, username); if(result) { diff --git a/SCP.java b/SCP.java @@ -105,6 +105,7 @@ public class SCP { BufferedReader sstream = new BufferedReader(new StringReader(packet)); boolean firstLine = true; String line; + int lineNum = 1; while((line = sstream.readLine()) != null) { if(firstLine) { if(line.indexOf("SCP CONNECT") < 0) { @@ -112,29 +113,39 @@ public class SCP { throw new SCPException("SCP CONNECT", line); } firstLine = false; - } - if(line.indexOf("SERVERADDRESS") > -1) { - if(line.indexOf(address) == -1) { - sstream.close(); - throw new SCPException("SERVERADDRESS " + address, line); - } - } else if(line.indexOf("SERVERPORT") > -1) { - if(line.indexOf(String.format("%d", port)) == -1) { - sstream.close(); - throw new SCPException("SERVERPORT " + port, line); - } - } else if(line.indexOf("REQUESTCREATED") > -1) { - int requestTime = Integer.parseInt(line.substring(line.indexOf(" ") + 1)); - int timeDiff = findTimeDiff(requestTime); - if(timeDiff > 5) { - sstream.close(); - throw new TimeDiffException(timeDiff); + } else { + if(lineNum == 2) { + if(line.indexOf("SERVERADDRESS") == -1 && line.indexOf(address) == -1) { + sstream.close(); + throw new SCPException("SERVERADDRESS " + address, line); + } + } else if(lineNum == 3) { + if(line.indexOf("SERVERPORT") == -1 && line.indexOf(String.format("%d", port)) == -1) { + sstream.close(); + throw new SCPException("SERVERPORT " + port, line); + } + } else if(lineNum == 4) { + if(line.indexOf("REQUESTCREATED") > -1) { + int requestTime = Integer.parseInt(line.substring(line.indexOf(" ") + 1)); + int timeDiff = findTimeDiff(requestTime); + if(timeDiff > 5) { + sstream.close(); + throw new TimeDiffException(timeDiff); + } + } else { + sstream.close(); + throw new SCPException("REQUESTCREATED epochtime", line); + } + } else if(line.indexOf("USERNAME") > -1 && lineNum == 5) { + username = line.substring(line.indexOf(" ") + 1); } - } else if(line.indexOf("USERNAME") > -1) { - username = line.substring(line.indexOf(" ") + 1); } + ++lineNum; } sstream.close(); + if(lineNum < 5) { + throw new SCPException("SCP connect was not the right length"); + } return username; } /** @@ -168,6 +179,7 @@ public class SCP { BufferedReader sstream = new BufferedReader(new StringReader(packet)); boolean firstLine = true; String line; + int lineNum = 1; while((line = sstream.readLine()) != null) { if(firstLine) { if(line.indexOf("SCP ACKNOWLEDGE") < 0) { @@ -176,22 +188,26 @@ public class SCP { } firstLine = false; } - if(line.indexOf("SERVERADDRESS") > -1) { - if(line.indexOf(address) == -1) { + if(lineNum == 2) { + if(line.indexOf("USERNAME") == -1 || line.indexOf(username) == -1) { sstream.close(); - throw new SCPException("SERVERADDRESS " + address, line); + throw new SCPException("USERNAME " + username, line); } - } else if(line.indexOf("SERVERPORT") > -1) { - if(line.indexOf(String.format("%d", port)) == -1) { + } else if(lineNum == 3) { + if(line.indexOf("SERVERADDRESS") == -1 || line.indexOf(address) == -1) { sstream.close(); - throw new SCPException("SERVERPORT " + port, line); + throw new SCPException("SERVERADDRESS " + address, line); } - } else if(line.indexOf("USERNAME") > -1) { - if(line.indexOf(username) == -1) { + } else if(lineNum == 4) { + if(line.indexOf("SERVERPORT") == -1 || line.indexOf(String.format("%d", port)) == -1) { sstream.close(); - throw new SCPException("USERNAME " + username, line); + throw new SCPException("SERVERPORT " + port, line); } } + ++lineNum; + } + if(lineNum < 4) { + throw new SCPException("SCP acknowledge was not the right length"); } sstream.close(); return true; @@ -206,6 +222,7 @@ public class SCP { boolean accept = false; BufferedReader sstream = new BufferedReader(new StringReader(packet)); String line; + int lineNum = 1; while((line = sstream.readLine()) != null) { if(!accept) { if(line.indexOf("SCP ACCEPT") > -1) { @@ -214,19 +231,23 @@ public class SCP { throw new SCPException("SCP ACCEPT", line); } } - if(line.indexOf("USERNAME") > -1) { - if(line.indexOf(username) == -1) { + if(lineNum == 2) { + if(line.indexOf("USERNAME") == -1 || line.indexOf(username) == -1) { throw new SCPException("USERNAME " + username, line); } - } else if(line.indexOf("CLIENTADDRESS") > -1) { - if(line.indexOf(address) == -1) { + } else if(lineNum == 3) { + if(line.indexOf("CLIENTADDRESS") == -1 || line.indexOf(address) == -1) { throw new SCPException("CLIENTADDRESS " + address, line); } - } else if(line.indexOf("CLIENTPORT") > -1) { - if(line.indexOf(String.format("%d", port)) == -1) { + } else if(lineNum == 4) { + if(line.indexOf("CLIENTPORT") == -1 || line.indexOf(String.format("%d", port)) == -1) { throw new SCPException("CLIENTPORT " + port, line); } } + ++lineNum; + } + if(lineNum < 4) { + throw new SCPException("SCP accept was not the right length"); } return true; } @@ -241,6 +262,7 @@ public class SCP { boolean firstLine = true; BufferedReader sstream = new BufferedReader(new StringReader(packet)); String line; + int lineNum = 1; String message = ""; while((line = sstream.readLine()) != null) { if(firstLine) { @@ -249,18 +271,23 @@ public class SCP { } firstLine = false; } - if(line.indexOf("REMOTEADDRESS") > -1) { - if(line.indexOf(address) == -1) { + if(lineNum == 2) { + if(line.indexOf("REMOTEADDRESS") == -1 || line.indexOf(address) == -1) { throw new SCPException("REMOTEADDRESS " + address, line); } - } else if(line.indexOf("REMOTEPORT") > -1) { - if(Integer.parseInt(line.substring(line.indexOf(" ") + 1)) != port) { + } else if(lineNum == 3) { + if(line.indexOf("REMOTEPORT") == -1 || Integer.parseInt(line.substring(line.indexOf(" ") + 1)) != port) { throw new SCPException("REMOTEPORT " + port, line); } - } else if(line.indexOf("MESSAGECONTENT") > -1) { - message = packet.substring(packet.indexOf("\n\n") + 2); - break; + } else if(lineNum == 4) { + if(line.indexOf("MESSAGECONTENT") > -1) { + message = packet.substring(packet.indexOf("\n\n") + 2); + break; + } else { + throw new SCPException("MESSAGECONTENT", line); + } } + ++lineNum; } return message; }