Added messages sending after registering

This commit is contained in:
mariano.riefolo 2024-04-09 09:03:17 +02:00
parent 5f6c3de854
commit 9265a90300
6 changed files with 714 additions and 657 deletions

1
.gitignore vendored
View File

@ -141,3 +141,4 @@ hs_err_pid*
replay_pid*
# End of https://www.toptal.com/developers/gitignore/api/intellij,java
/target/

View File

@ -113,7 +113,7 @@ public class Database {
statement.setString(1, message);
statement.setInt(2, conversationId);
statement.setDate(3, Date.valueOf(LocalDateTime.now().toLocalDate()));
statement.setTime(3, Time.valueOf(LocalDateTime.now().toLocalTime()));
statement.setTime(4, Time.valueOf(LocalDateTime.now().toLocalTime()));
int rowsInserted = statement.executeUpdate();
return rowsInserted == 1;
@ -157,17 +157,22 @@ public class Database {
}
}
public static boolean addConversation(int conversation_id) {
public static boolean addConversation(int sender, int receiver) {
try (Connection connection = getConnection()) {
if (connection == null) return false;
try (PreparedStatement statement = connection.prepareStatement("""
INSERT INTO conversations(id, sender, recipient)
VALUES (?, ?, ?)
INSERT INTO conversations(sender, recipient)
VALUES (?, ?)
""")) {
statement.setInt(1, conversation_id);
System.err.println("a");
statement.setInt(1, sender);
System.err.println("a");
statement.setInt(2, receiver);
System.err.println("a");
int rowsInserted = statement.executeUpdate();
System.err.println("a");
return rowsInserted == 1;
} catch (SQLException ex) {
System.err.println("Error while trying to add a conversation: " + ex);
@ -179,6 +184,32 @@ public class Database {
}
}
public static int getConversationId(int sender, int receiver) {
try (Connection connection = getConnection()) {
if (connection == null) return -1;
try (PreparedStatement statement = connection.prepareStatement("""
SELECT id
FROM conversations
WHERE sender = ? AND recipient = ?
""")) {
statement.setInt(1, sender);
statement.setInt(2, receiver);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt(1);
}
return -1;
} catch (SQLException e) {
System.err.println("Error while trying to get the conversation id: " + e);
return -1;
}
} catch (SQLException e) {
System.err.println("Error while trying to open a connection: " + e);
throw new RuntimeException(e);
}
}
public static List<Message> getMessages(int conversation_id) {
try (Connection connection = getConnection()) {
if (connection == null) return null;
@ -210,8 +241,8 @@ public class Database {
private static Connection getConnection() throws SQLException {
return DriverManager.getConnection(
System.getenv("db_url"),
System.getenv("db_user"), System.getenv("db_pass")
"jdbc:mariadb://riefolo.me:3306/chat_rsa",
"proj_sistemi", "$o8a5#diTGg8*Agk"
);
}
}

View File

@ -16,6 +16,7 @@ public class MessageForwarder {
}
public static void sendTo(int id, String message, int senderId) throws IOException {
if (idThread.containsKey(id))
idThread.get(id).sendMessage(message, senderId);
}
}

View File

@ -106,12 +106,13 @@ public class ServerThread extends Thread {
throw new RuntimeException(e);
}
int recipientId = getIdFromUsername(dUsername);
Database.addConversation(clientId, recipientId);
for (;;) {
try {
sendEncrypted("Inserisci messaggio: ");
message = rsa.decrypt(fromClient.readLine());
message = fromClient.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
@ -126,11 +127,15 @@ public class ServerThread extends Thread {
throw new RuntimeException(e);
}
recipientId = getIdFromUsername(dUsername);
Database.addConversation(clientId, recipientId);
continue;
}
int convId = Database.getConversationId(clientId, recipientId);
try {
MessageForwarder.sendTo(recipientId, message, clientId);
Database.addMessage(convId, message);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -5,6 +5,8 @@ import models.Rsa;
import java.io.*;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ClientSendThread extends Thread{
private final BufferedWriter out;
@ -51,6 +53,23 @@ public class ClientSendThread extends Thread{
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
crt.start();
String s;
try {
s = br.readLine();
send(rsa.encrypt(s, se, sn));
} catch (IOException ex) {
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
}
while (true) {
try {
s = br.readLine();
send(rsa.encrypt(s, se, sn));
} catch (IOException ex) {
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void sendCredentials(BufferedReader br) throws IOException {