Now it display old messages when we change recipient, messages now have a sort of format, the server is always running and the client is closed whit DISCONNECT.

This commit is contained in:
Paolo Manzi 2024-04-15 20:46:00 +02:00
parent 7a401e784b
commit d4e5ff6009
5 changed files with 68 additions and 39 deletions

View File

@ -239,9 +239,9 @@ public class Database {
List<Message> messages = new ArrayList<>();
while (resultSet.next()) {
String message = resultSet.getString(1);
Date sending_date = resultSet.getDate(2);
Time sending_time = resultSet.getTime(3);
boolean own_pub_key = resultSet.getBoolean(4);
Date sending_date = resultSet.getDate(3);
Time sending_time = resultSet.getTime(4);
boolean own_pub_key = resultSet.getBoolean(5);
messages.add(new Message(message, sending_date, sending_time, own_pub_key));
}
return messages;

View File

@ -6,12 +6,14 @@ import java.net.Socket;
public class Server {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(21324)) {
Socket socket = serverSocket.accept();
ServerThread serverThread = new ServerThread(socket);
serverThread.start();
} catch (IOException e) {
throw new RuntimeException(e);
while (true) {
try (ServerSocket serverSocket = new ServerSocket(21324)) {
Socket socket = serverSocket.accept();
ServerThread serverThread = new ServerThread(socket);
serverThread.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -7,6 +7,7 @@ import models.Rsa;
import java.io.*;
import java.math.BigInteger;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@ -124,23 +125,8 @@ public class ServerThread extends Thread {
int convId = Database.getConversationId(clientId, recipientId);
int convId2 = Database.getConversationId(recipientId, clientId);
List<Message> messaggi = getMessages(convId, true);
List<Message> messaggi2 = getMessages(convId2, false);
messaggi.addAll(messaggi2);
Comparator<Message> comparator = Comparator.comparing(Message -> Message.sending_date());
comparator = comparator.thenComparing(Comparator.comparing(Message -> Message.sending_time()));
messaggi = messaggi.stream().sorted(comparator).collect(Collectors.toList());
for (Message i : messaggi) {
try {
send("MESSAGGIO");
} catch (IOException e) {
throw new RuntimeException(e);
}
System.err.println(String.valueOf(i.own_pub_key()));
sendEncrypted(i.message());
sendEncrypted(i.sending_date().toString());
sendEncrypted(i.sending_time().toString());
}
sendMessages(convId,convId2);
sendEncrypted("(DISCONNECT per uscire, CD per cambiare destinatario)\n");
@ -154,8 +140,7 @@ public class ServerThread extends Thread {
}
if (message == null || "DISCONNECT".equals(rsa.decrypt(message))) break;
if ("CD".equals(rsa.decrypt(message))) {
else if ("CD".equals(rsa.decrypt(message))) {
try {
sendEncrypted("Inserisci l'username del nuovo destinatario: ");
dUsername = rsa.decrypt(fromClient.readLine());
@ -173,18 +158,27 @@ public class ServerThread extends Thread {
}
sendEncrypted(pk.e().toString());
sendEncrypted(pk.n().toString());
convId = Database.getConversationId(clientId, recipientId);
convId2 = Database.getConversationId(recipientId, clientId);
sendMessages(convId,convId2);
continue;
}
try {
String message2 = fromClient.readLine();
MessageForwarder.sendTo(recipientId, message, clientId);
Database.addMessage(convId, message, false);
Database.addMessage(convId, message2, true);
} catch (IOException e) {
throw new RuntimeException(e);
else{
try {
String message2 = fromClient.readLine();
//MessageForwarder.sendTo(recipientId, message, clientId);
Database.addMessage(convId, message, false);
Database.addMessage(convId, message2, true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
@ -228,4 +222,26 @@ public class ServerThread extends Thread {
send(String.valueOf(sender));
send(message);
}
private void sendMessages(int convId,int convId2){
List<Message> messaggi = new ArrayList<>();
List<Message> m1 = getMessages(convId, true);
List<Message> m2 = getMessages(convId2, false);
if (m1 != null) messaggi.addAll(m1);
if (m2 != null) messaggi.addAll(m2);
Comparator<Message> comparator = Comparator.comparing(Message -> Message.sending_date());
comparator = comparator.thenComparing(Comparator.comparing(Message -> Message.sending_time()));
messaggi = messaggi.stream().sorted(comparator).collect(Collectors.toList());
for (Message i : messaggi) {
try {
send("MESSAGGIO");
} catch (IOException e) {
throw new RuntimeException(e);
}
sendEncrypted(String.valueOf(i.own_pub_key()));
sendEncrypted(i.message());
sendEncrypted(i.sending_date().toString());
sendEncrypted(i.sending_time().toString());
}
}
}

View File

@ -16,7 +16,6 @@ public class ClientReceiveThread extends Thread {
}
public void run() {
while (true) {
try {
String s = in.readLine();
@ -25,10 +24,14 @@ public class ClientReceiveThread extends Thread {
BigInteger recipientN = new BigInteger(rsa.decrypt(in.readLine()));
ClientSendThread.setPK(recipientE, recipientN);
} else if ("MESSAGGIO".equals(s)) {
s = in.readLine();
boolean verso = Boolean.parseBoolean(rsa.decrypt(s));
if(verso) System.out.print("You: ");
else System.out.print(ClientSendThread.getDestinatario()+": ");
s = in.readLine();
System.out.println(rsa.decrypt(rsa.decrypt(s)));
s = in.readLine();
System.out.println(rsa.decrypt(s));
System.out.print(rsa.decrypt(s)+" ");
s = in.readLine();
System.out.println(rsa.decrypt(s));
} else System.out.print(rsa.decrypt(s));

View File

@ -15,6 +15,7 @@ public class ClientSendThread extends Thread {
private final BufferedReader in;
private static BigInteger recipientE, recipientN;
private static String destinatario;
public ClientSendThread(BufferedReader in, BufferedWriter out, BigInteger se, BigInteger sn, Rsa rsa) {
this.in = in;
@ -60,6 +61,7 @@ public class ClientSendThread extends Thread {
try {
System.out.print(rsa.decrypt(in.readLine()));
s = br.readLine();
destinatario = s;
send(rsa.encrypt(s, se, sn));
} catch (IOException ex) {
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
@ -81,10 +83,12 @@ public class ClientSendThread extends Thread {
s = br.readLine();
if ("DISCONNECT".equals(s)) {
send(rsa.encrypt(s, se, sn));
System.exit(104);
break;
} else if ("CD".equals(s)) {
send(rsa.encrypt(s, se, sn));
s = br.readLine();
destinatario = s;
send(rsa.encrypt(s, se, sn));
} else {
send(rsa.encrypt(s, recipientE, recipientN));
@ -121,4 +125,8 @@ public class ClientSendThread extends Thread {
recipientN = n;
}
public static String getDestinatario(){
return destinatario;
}
}