Skip to content

Commit

Permalink
Added received message outputting to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
bilaalrashid committed Oct 27, 2019
1 parent 6b5b9e1 commit 6d2e303
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/Console.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
23 changes: 21 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}

Expand Down

0 comments on commit 6d2e303

Please sign in to comment.