From ec3bfbbc09aab55843e168d5e979bea905656d41 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sat, 11 Sep 2021 23:29:43 +0200 Subject: [PATCH] Cleaned up debug messages. --- .../andrewl/concord_client/ConcordClient.java | 31 +++++++++++------ .../andrewl/concord_client/gui/ChatList.java | 5 ++- .../concord_core/msg/MessageUtils.java | 3 -- .../andrewl/concord_core/msg/types/Chat.java | 1 - .../msg/types/ChatHistoryResponse.java | 1 - .../concord_server/client/ClientThread.java | 34 +++++++++++++------ .../event/ChatHistoryRequestHandler.java | 13 +++++-- 7 files changed, 55 insertions(+), 33 deletions(-) diff --git a/client/src/main/java/nl/andrewl/concord_client/ConcordClient.java b/client/src/main/java/nl/andrewl/concord_client/ConcordClient.java index 4a708b2..c4b3f2f 100644 --- a/client/src/main/java/nl/andrewl/concord_client/ConcordClient.java +++ b/client/src/main/java/nl/andrewl/concord_client/ConcordClient.java @@ -19,7 +19,6 @@ import nl.andrewl.concord_client.model.ClientModel; import nl.andrewl.concord_core.msg.Encryption; import nl.andrewl.concord_core.msg.Message; import nl.andrewl.concord_core.msg.Serializer; -import nl.andrewl.concord_core.msg.types.Error; import nl.andrewl.concord_core.msg.types.*; import java.io.IOException; @@ -76,15 +75,7 @@ public class ConcordClient implements Runnable { * messages, or if the server sends an unexpected response. */ private ClientModel initializeConnectionToServer(String nickname, Path tokensFile) throws IOException { - try { - System.out.println("Initializing end-to-end encryption with the server..."); - var streams = Encryption.upgrade(this.in, this.out, this.serializer); - this.in = streams.first(); - this.out = streams.second(); - System.out.println("Successfully established cipher streams."); - } catch (GeneralSecurityException e) { - throw new IOException(e); - } + this.establishEncryption(); String token = this.getSessionToken(tokensFile); this.serializer.writeMessage(new Identification(nickname, token), this.out); Message reply = this.serializer.readMessage(this.in); @@ -99,6 +90,24 @@ public class ConcordClient implements Runnable { } } + /** + * Establishes an encrypted connection to the server. This should be the + * first method which interacts with the server, since it sends and receives + * specific key information, and all subsequent traffic should be encrypted. + * @throws IOException If encryption could not be established. + */ + private void establishEncryption() throws IOException { + try { + System.out.println("Initializing end-to-end encryption with the server..."); + var streams = Encryption.upgrade(this.in, this.out, this.serializer); + this.in = streams.first(); + this.out = streams.second(); + System.out.println("Successfully established cipher streams."); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + } + public void sendMessage(Message message) throws IOException { this.serializer.writeMessage(message, this.out); } @@ -127,7 +136,7 @@ public class ConcordClient implements Runnable { this.eventManager.handle(msg); } catch (IOException e) { e.printStackTrace(); - this.running = false; +// this.running = false; } } try { diff --git a/client/src/main/java/nl/andrewl/concord_client/gui/ChatList.java b/client/src/main/java/nl/andrewl/concord_client/gui/ChatList.java index 3c7bdc5..8543f05 100644 --- a/client/src/main/java/nl/andrewl/concord_client/gui/ChatList.java +++ b/client/src/main/java/nl/andrewl/concord_client/gui/ChatList.java @@ -19,7 +19,6 @@ public class ChatList extends AbstractListBox implements ChatHis @Override public synchronized ChatList addItem(Chat item) { super.addItem(item); - this.setSelectedIndex(this.getItemCount() - 1); return this; } @@ -39,6 +38,7 @@ public class ChatList extends AbstractListBox implements ChatHis public void chatAdded(Chat chat) { this.getTextGUI().getGUIThread().invokeLater(() -> { this.addItem(chat); + this.setSelectedIndex(this.getItemCount() - 1); }); } @@ -56,11 +56,10 @@ public class ChatList extends AbstractListBox implements ChatHis public void chatUpdated(ChatHistory history) { this.getTextGUI().getGUIThread().invokeLater(() -> { this.clearItems(); - System.out.println("Cleared chats"); for (var chat : history.getChats()) { - System.out.println("Adding chat: " + chat); this.addItem(chat); } + this.setSelectedIndex(this.getItemCount() - 1); }); } } diff --git a/core/src/main/java/nl/andrewl/concord_core/msg/MessageUtils.java b/core/src/main/java/nl/andrewl/concord_core/msg/MessageUtils.java index 50c6d76..f86918b 100644 --- a/core/src/main/java/nl/andrewl/concord_core/msg/MessageUtils.java +++ b/core/src/main/java/nl/andrewl/concord_core/msg/MessageUtils.java @@ -161,13 +161,11 @@ public class MessageUtils { o.writeInt(items.size()); for (var i : items) { i.write(o); - System.out.println("Wrote " + i); } } public static List readList(Class type, DataInputStream i) throws IOException { int size = i.readInt(); - System.out.println("Read a size of " + size + " items of type " + type.getSimpleName()); try { var constructor = type.getConstructor(); List items = new ArrayList<>(size); @@ -175,7 +173,6 @@ public class MessageUtils { var item = constructor.newInstance(); item.read(i); items.add(item); - System.out.println("Read item " + (k+1) + " of " + size + ": " + item); } return items; } catch (ReflectiveOperationException e) { diff --git a/core/src/main/java/nl/andrewl/concord_core/msg/types/Chat.java b/core/src/main/java/nl/andrewl/concord_core/msg/types/Chat.java index 8aaa89f..2292d97 100644 --- a/core/src/main/java/nl/andrewl/concord_core/msg/types/Chat.java +++ b/core/src/main/java/nl/andrewl/concord_core/msg/types/Chat.java @@ -61,7 +61,6 @@ public class Chat implements Message { this.senderNickname = readString(i); this.timestamp = i.readLong(); this.message = readString(i); - System.out.println("Read chat: " + this); } @Override diff --git a/core/src/main/java/nl/andrewl/concord_core/msg/types/ChatHistoryResponse.java b/core/src/main/java/nl/andrewl/concord_core/msg/types/ChatHistoryResponse.java index 3de8b2f..6a93801 100644 --- a/core/src/main/java/nl/andrewl/concord_core/msg/types/ChatHistoryResponse.java +++ b/core/src/main/java/nl/andrewl/concord_core/msg/types/ChatHistoryResponse.java @@ -38,7 +38,6 @@ public class ChatHistoryResponse implements Message { @Override public void read(DataInputStream i) throws IOException { this.channelId = readUUID(i); - System.out.println("Reading list of chats..."); this.messages = readList(Chat.class, i); } } diff --git a/server/src/main/java/nl/andrewl/concord_server/client/ClientThread.java b/server/src/main/java/nl/andrewl/concord_server/client/ClientThread.java index f61603b..ca2e855 100644 --- a/server/src/main/java/nl/andrewl/concord_server/client/ClientThread.java +++ b/server/src/main/java/nl/andrewl/concord_server/client/ClientThread.java @@ -102,11 +102,9 @@ public class ClientThread extends Thread { System.err.println("Could not identify the client; aborting connection."); this.running = false; } - while (this.running) { try { var msg = this.server.getSerializer().readMessage(this.in); - System.out.println("Received " + msg.getClass().getSimpleName() + " from " + this.clientNickname); this.server.getEventManager().handle(msg, this); } catch (IOException e) { this.running = false; @@ -133,17 +131,11 @@ public class ClientThread extends Thread { * false otherwise. */ private boolean identifyClient() { - int attempts = 0; - try { - System.out.println("Initializing end-to-end encryption with the client..."); - var streams = Encryption.upgrade(this.in, this.out, server.getSerializer()); - this.in = streams.first(); - this.out = streams.second(); - System.out.println("Successfully established cipher streams."); - } catch (Exception e) { - e.printStackTrace(); + if (!establishEncryption()) { + System.err.println("Could not establish end-to-end encryption with the client."); return false; } + int attempts = 0; while (attempts < 5) { try { var msg = this.server.getSerializer().readMessage(this.in); @@ -159,6 +151,26 @@ public class ClientThread extends Thread { return false; } + /** + * Tries to establish an encrypted connection with a client. This should be + * the first thing which is called upon to interact with the client, because + * it assumes that the client is also attempting to establish a secure + * connection as soon as it opens its socket. + * @return True if an encrypted connection could be established, or false + * otherwise. + */ + private boolean establishEncryption() { + try { + var streams = Encryption.upgrade(this.in, this.out, server.getSerializer()); + this.in = streams.first(); + this.out = streams.second(); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + public UserData toData() { return new UserData(this.clientId, this.clientNickname); } diff --git a/server/src/main/java/nl/andrewl/concord_server/event/ChatHistoryRequestHandler.java b/server/src/main/java/nl/andrewl/concord_server/event/ChatHistoryRequestHandler.java index e197729..e8b0049 100644 --- a/server/src/main/java/nl/andrewl/concord_server/event/ChatHistoryRequestHandler.java +++ b/server/src/main/java/nl/andrewl/concord_server/event/ChatHistoryRequestHandler.java @@ -58,6 +58,15 @@ public class ChatHistoryRequestHandler implements MessageHandler chats = new ArrayList<>((int) count); for (Document doc : cursor) { chats.add(this.read(doc)); } - System.out.println(chats); - chats.sort(Comparator.comparingLong(Chat::getTimestamp)); + Collections.reverse(chats); return new ChatHistoryResponse(channel.getId(), chats); }