diff --git a/src/Console.java b/src/Console.java new file mode 100644 index 0000000..b29e4b9 --- /dev/null +++ b/src/Console.java @@ -0,0 +1,28 @@ +import java.time.format.DateTimeFormatter; + +/** + * The output console + */ +class Console { + + /** + * Writes text out to the console + * @param text The text to write out + */ + static void write(String text) { + System.out.println(text); + } + + /** + * Displays a message in the console + * @param message The message to be outputted + */ + static void write(Message message) { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm dd/MM/yyyy"); + String formattedDateTime = message.getTimestamp().format(dateTimeFormatter); + + String output = String.format("%s [%s] %s", formattedDateTime, message.getName(), message.getText()); + System.out.println(output); + } + +} diff --git a/src/Main.java b/src/Main.java index 7effb7b..eef245f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,17 +1,36 @@ +import java.io.IOException; + /** * The main interface for the server */ public class Main { /** - * Gets the port to run the server on + * Runs a server on a specified port * @param args The command line arguments */ public static void main(String[] args) { if (args != null && args.length == 1) { int port = Integer.parseInt(args[0]); + + try { + Server server = new Server(port); + + while (true) { + if (server.isReceiving()) { + try { + Message message = Message.fromJsonString(server.getMessage()); + Console.write(message); + } catch (IOException e) { + Console.write("Error receiving message"); + } + } + } + } catch (IOException e) { + Console.write("Server could not be started."); + } } else { - System.out.println("Invalid arguments. Port not specified."); + Console.write("Invalid arguments. Port not specified."); } }