Cleaned up debug messages.

This commit is contained in:
Andrew Lalis 2021-09-11 23:29:43 +02:00
parent c6a2bb15da
commit ec3bfbbc09
7 changed files with 55 additions and 33 deletions

View File

@ -19,7 +19,6 @@ import nl.andrewl.concord_client.model.ClientModel;
import nl.andrewl.concord_core.msg.Encryption; import nl.andrewl.concord_core.msg.Encryption;
import nl.andrewl.concord_core.msg.Message; import nl.andrewl.concord_core.msg.Message;
import nl.andrewl.concord_core.msg.Serializer; import nl.andrewl.concord_core.msg.Serializer;
import nl.andrewl.concord_core.msg.types.Error;
import nl.andrewl.concord_core.msg.types.*; import nl.andrewl.concord_core.msg.types.*;
import java.io.IOException; import java.io.IOException;
@ -76,15 +75,7 @@ public class ConcordClient implements Runnable {
* messages, or if the server sends an unexpected response. * messages, or if the server sends an unexpected response.
*/ */
private ClientModel initializeConnectionToServer(String nickname, Path tokensFile) throws IOException { private ClientModel initializeConnectionToServer(String nickname, Path tokensFile) throws IOException {
try { this.establishEncryption();
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);
}
String token = this.getSessionToken(tokensFile); String token = this.getSessionToken(tokensFile);
this.serializer.writeMessage(new Identification(nickname, token), this.out); this.serializer.writeMessage(new Identification(nickname, token), this.out);
Message reply = this.serializer.readMessage(this.in); 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 { public void sendMessage(Message message) throws IOException {
this.serializer.writeMessage(message, this.out); this.serializer.writeMessage(message, this.out);
} }
@ -127,7 +136,7 @@ public class ConcordClient implements Runnable {
this.eventManager.handle(msg); this.eventManager.handle(msg);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
this.running = false; // this.running = false;
} }
} }
try { try {

View File

@ -19,7 +19,6 @@ public class ChatList extends AbstractListBox<Chat, ChatList> implements ChatHis
@Override @Override
public synchronized ChatList addItem(Chat item) { public synchronized ChatList addItem(Chat item) {
super.addItem(item); super.addItem(item);
this.setSelectedIndex(this.getItemCount() - 1);
return this; return this;
} }
@ -39,6 +38,7 @@ public class ChatList extends AbstractListBox<Chat, ChatList> implements ChatHis
public void chatAdded(Chat chat) { public void chatAdded(Chat chat) {
this.getTextGUI().getGUIThread().invokeLater(() -> { this.getTextGUI().getGUIThread().invokeLater(() -> {
this.addItem(chat); this.addItem(chat);
this.setSelectedIndex(this.getItemCount() - 1);
}); });
} }
@ -56,11 +56,10 @@ public class ChatList extends AbstractListBox<Chat, ChatList> implements ChatHis
public void chatUpdated(ChatHistory history) { public void chatUpdated(ChatHistory history) {
this.getTextGUI().getGUIThread().invokeLater(() -> { this.getTextGUI().getGUIThread().invokeLater(() -> {
this.clearItems(); this.clearItems();
System.out.println("Cleared chats");
for (var chat : history.getChats()) { for (var chat : history.getChats()) {
System.out.println("Adding chat: " + chat);
this.addItem(chat); this.addItem(chat);
} }
this.setSelectedIndex(this.getItemCount() - 1);
}); });
} }
} }

View File

@ -161,13 +161,11 @@ public class MessageUtils {
o.writeInt(items.size()); o.writeInt(items.size());
for (var i : items) { for (var i : items) {
i.write(o); i.write(o);
System.out.println("Wrote " + i);
} }
} }
public static <T extends Message> List<T> readList(Class<T> type, DataInputStream i) throws IOException { public static <T extends Message> List<T> readList(Class<T> type, DataInputStream i) throws IOException {
int size = i.readInt(); int size = i.readInt();
System.out.println("Read a size of " + size + " items of type " + type.getSimpleName());
try { try {
var constructor = type.getConstructor(); var constructor = type.getConstructor();
List<T> items = new ArrayList<>(size); List<T> items = new ArrayList<>(size);
@ -175,7 +173,6 @@ public class MessageUtils {
var item = constructor.newInstance(); var item = constructor.newInstance();
item.read(i); item.read(i);
items.add(item); items.add(item);
System.out.println("Read item " + (k+1) + " of " + size + ": " + item);
} }
return items; return items;
} catch (ReflectiveOperationException e) { } catch (ReflectiveOperationException e) {

View File

@ -61,7 +61,6 @@ public class Chat implements Message {
this.senderNickname = readString(i); this.senderNickname = readString(i);
this.timestamp = i.readLong(); this.timestamp = i.readLong();
this.message = readString(i); this.message = readString(i);
System.out.println("Read chat: " + this);
} }
@Override @Override

View File

@ -38,7 +38,6 @@ public class ChatHistoryResponse implements Message {
@Override @Override
public void read(DataInputStream i) throws IOException { public void read(DataInputStream i) throws IOException {
this.channelId = readUUID(i); this.channelId = readUUID(i);
System.out.println("Reading list of chats...");
this.messages = readList(Chat.class, i); this.messages = readList(Chat.class, i);
} }
} }

View File

@ -102,11 +102,9 @@ public class ClientThread extends Thread {
System.err.println("Could not identify the client; aborting connection."); System.err.println("Could not identify the client; aborting connection.");
this.running = false; this.running = false;
} }
while (this.running) { while (this.running) {
try { try {
var msg = this.server.getSerializer().readMessage(this.in); var msg = this.server.getSerializer().readMessage(this.in);
System.out.println("Received " + msg.getClass().getSimpleName() + " from " + this.clientNickname);
this.server.getEventManager().handle(msg, this); this.server.getEventManager().handle(msg, this);
} catch (IOException e) { } catch (IOException e) {
this.running = false; this.running = false;
@ -133,17 +131,11 @@ public class ClientThread extends Thread {
* false otherwise. * false otherwise.
*/ */
private boolean identifyClient() { private boolean identifyClient() {
int attempts = 0; if (!establishEncryption()) {
try { System.err.println("Could not establish end-to-end encryption with the client.");
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();
return false; return false;
} }
int attempts = 0;
while (attempts < 5) { while (attempts < 5) {
try { try {
var msg = this.server.getSerializer().readMessage(this.in); var msg = this.server.getSerializer().readMessage(this.in);
@ -159,6 +151,26 @@ public class ClientThread extends Thread {
return false; 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() { public UserData toData() {
return new UserData(this.clientId, this.clientNickname); return new UserData(this.clientId, this.clientNickname);
} }

View File

@ -58,6 +58,15 @@ public class ChatHistoryRequestHandler implements MessageHandler<ChatHistoryRequ
client.sendToClient(new ChatHistoryResponse(channel.getId(), chats)); client.sendToClient(new ChatHistoryResponse(channel.getId(), chats));
} }
/**
* Gets a response for a standard chat history request, using a standard set
* of parameters.
* @param channel The channel to get chat history from.
* @param count The number of messages to retrieve.
* @param from If not null, only include messages made after this timestamp.
* @param to If not null, only include messages made before this timestamp.
* @return A chat history response.
*/
private ChatHistoryResponse getResponse(Channel channel, long count, Long from, Long to) { private ChatHistoryResponse getResponse(Channel channel, long count, Long from, Long to) {
var col = channel.getMessageCollection(); var col = channel.getMessageCollection();
Cursor cursor; Cursor cursor;
@ -74,14 +83,12 @@ public class ChatHistoryRequestHandler implements MessageHandler<ChatHistoryRequ
} else { } else {
cursor = col.find(Filters.and(filters.toArray(new Filter[0])), options); cursor = col.find(Filters.and(filters.toArray(new Filter[0])), options);
} }
System.out.println("Found " + cursor.size() + " chats");
List<Chat> chats = new ArrayList<>((int) count); List<Chat> chats = new ArrayList<>((int) count);
for (Document doc : cursor) { for (Document doc : cursor) {
chats.add(this.read(doc)); chats.add(this.read(doc));
} }
System.out.println(chats); Collections.reverse(chats);
chats.sort(Comparator.comparingLong(Chat::getTimestamp));
return new ChatHistoryResponse(channel.getId(), chats); return new ChatHistoryResponse(channel.getId(), chats);
} }