Added messages sending after registering
This commit is contained in:
parent
5f6c3de854
commit
9265a90300
1
.gitignore
vendored
1
.gitignore
vendored
@ -141,3 +141,4 @@ hs_err_pid*
|
|||||||
replay_pid*
|
replay_pid*
|
||||||
|
|
||||||
# End of https://www.toptal.com/developers/gitignore/api/intellij,java
|
# End of https://www.toptal.com/developers/gitignore/api/intellij,java
|
||||||
|
/target/
|
||||||
|
@ -113,7 +113,7 @@ public class Database {
|
|||||||
statement.setString(1, message);
|
statement.setString(1, message);
|
||||||
statement.setInt(2, conversationId);
|
statement.setInt(2, conversationId);
|
||||||
statement.setDate(3, Date.valueOf(LocalDateTime.now().toLocalDate()));
|
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();
|
int rowsInserted = statement.executeUpdate();
|
||||||
return rowsInserted == 1;
|
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()) {
|
try (Connection connection = getConnection()) {
|
||||||
if (connection == null) return false;
|
if (connection == null) return false;
|
||||||
|
|
||||||
try (PreparedStatement statement = connection.prepareStatement("""
|
try (PreparedStatement statement = connection.prepareStatement("""
|
||||||
INSERT INTO conversations(id, sender, recipient)
|
INSERT INTO conversations(sender, recipient)
|
||||||
VALUES (?, ?, ?)
|
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();
|
int rowsInserted = statement.executeUpdate();
|
||||||
|
System.err.println("a");
|
||||||
return rowsInserted == 1;
|
return rowsInserted == 1;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
System.err.println("Error while trying to add a conversation: " + 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) {
|
public static List<Message> getMessages(int conversation_id) {
|
||||||
try (Connection connection = getConnection()) {
|
try (Connection connection = getConnection()) {
|
||||||
if (connection == null) return null;
|
if (connection == null) return null;
|
||||||
@ -210,8 +241,8 @@ public class Database {
|
|||||||
|
|
||||||
private static Connection getConnection() throws SQLException {
|
private static Connection getConnection() throws SQLException {
|
||||||
return DriverManager.getConnection(
|
return DriverManager.getConnection(
|
||||||
System.getenv("db_url"),
|
"jdbc:mariadb://riefolo.me:3306/chat_rsa",
|
||||||
System.getenv("db_user"), System.getenv("db_pass")
|
"proj_sistemi", "$o8a5#diTGg8*Agk"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ public class MessageForwarder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void sendTo(int id, String message, int senderId) throws IOException {
|
public static void sendTo(int id, String message, int senderId) throws IOException {
|
||||||
|
if (idThread.containsKey(id))
|
||||||
idThread.get(id).sendMessage(message, senderId);
|
idThread.get(id).sendMessage(message, senderId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,12 +106,13 @@ public class ServerThread extends Thread {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
int recipientId = getIdFromUsername(dUsername);
|
int recipientId = getIdFromUsername(dUsername);
|
||||||
|
Database.addConversation(clientId, recipientId);
|
||||||
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
try {
|
try {
|
||||||
sendEncrypted("Inserisci messaggio: ");
|
sendEncrypted("Inserisci messaggio: ");
|
||||||
message = rsa.decrypt(fromClient.readLine());
|
message = fromClient.readLine();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -126,11 +127,15 @@ public class ServerThread extends Thread {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
recipientId = getIdFromUsername(dUsername);
|
recipientId = getIdFromUsername(dUsername);
|
||||||
|
Database.addConversation(clientId, recipientId);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int convId = Database.getConversationId(clientId, recipientId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
MessageForwarder.sendTo(recipientId, message, clientId);
|
MessageForwarder.sendTo(recipientId, message, clientId);
|
||||||
|
Database.addMessage(convId, message);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ import models.Rsa;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class ClientSendThread extends Thread{
|
public class ClientSendThread extends Thread{
|
||||||
private final BufferedWriter out;
|
private final BufferedWriter out;
|
||||||
@ -51,6 +53,23 @@ public class ClientSendThread extends Thread{
|
|||||||
|
|
||||||
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
|
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
|
||||||
crt.start();
|
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 {
|
private void sendCredentials(BufferedReader br) throws IOException {
|
||||||
|
Loading…
Reference in New Issue
Block a user