Added messages sending after registering
This commit is contained in:
parent
5f6c3de854
commit
9265a90300
287
.gitignore
vendored
287
.gitignore
vendored
@ -1,143 +1,144 @@
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/intellij,java
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=intellij,java
|
||||
|
||||
### Intellij ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### Intellij Patch ###
|
||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
||||
|
||||
# *.iml
|
||||
# modules.xml
|
||||
# .idea/misc.xml
|
||||
# *.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
# https://plugins.jetbrains.com/plugin/7973-sonarlint
|
||||
.idea/**/sonarlint/
|
||||
|
||||
# SonarQube Plugin
|
||||
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
|
||||
.idea/**/sonarIssues.xml
|
||||
|
||||
# Markdown Navigator plugin
|
||||
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
|
||||
.idea/**/markdown-navigator.xml
|
||||
.idea/**/markdown-navigator-enh.xml
|
||||
.idea/**/markdown-navigator/
|
||||
|
||||
# Cache file creation bug
|
||||
# See https://youtrack.jetbrains.com/issue/JBR-2257
|
||||
.idea/$CACHE_FILE$
|
||||
|
||||
# CodeStream plugin
|
||||
# https://plugins.jetbrains.com/plugin/12206-codestream
|
||||
.idea/codestream.xml
|
||||
|
||||
# Azure Toolkit for IntelliJ plugin
|
||||
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
|
||||
.idea/**/azureSettings.xml
|
||||
|
||||
### Java ###
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
replay_pid*
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/intellij,java
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/intellij,java
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=intellij,java
|
||||
|
||||
### Intellij ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### Intellij Patch ###
|
||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
||||
|
||||
# *.iml
|
||||
# modules.xml
|
||||
# .idea/misc.xml
|
||||
# *.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
# https://plugins.jetbrains.com/plugin/7973-sonarlint
|
||||
.idea/**/sonarlint/
|
||||
|
||||
# SonarQube Plugin
|
||||
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
|
||||
.idea/**/sonarIssues.xml
|
||||
|
||||
# Markdown Navigator plugin
|
||||
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
|
||||
.idea/**/markdown-navigator.xml
|
||||
.idea/**/markdown-navigator-enh.xml
|
||||
.idea/**/markdown-navigator/
|
||||
|
||||
# Cache file creation bug
|
||||
# See https://youtrack.jetbrains.com/issue/JBR-2257
|
||||
.idea/$CACHE_FILE$
|
||||
|
||||
# CodeStream plugin
|
||||
# https://plugins.jetbrains.com/plugin/12206-codestream
|
||||
.idea/codestream.xml
|
||||
|
||||
# Azure Toolkit for IntelliJ plugin
|
||||
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
|
||||
.idea/**/azureSettings.xml
|
||||
|
||||
### Java ###
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
replay_pid*
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/intellij,java
|
||||
/target/
|
||||
|
@ -1,217 +1,248 @@
|
||||
package controllers;
|
||||
|
||||
import models.Account;
|
||||
import models.Conversation;
|
||||
import models.Message;
|
||||
import org.mariadb.jdbc.MariaDbBlob;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import views.Client;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Database {
|
||||
public static boolean registerAccount(String username, String password, BigInteger e, String d, BigInteger n) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return false;
|
||||
|
||||
PasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
String hashedPass = encoder.encode(password);
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
INSERT INTO accounts(username, password, e, d, n)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""")) {
|
||||
statement.setString(1, username);
|
||||
statement.setString(2, hashedPass);
|
||||
statement.setBlob(3, new MariaDbBlob(e.toString().getBytes()));
|
||||
statement.setBytes(4, d.getBytes());
|
||||
statement.setBlob(5, new MariaDbBlob(n.toString().getBytes()));
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted == 1;
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to create a new account: " + ex);
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to open a connection: " + ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Account getAccount(String username, CharSequence password) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return null;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
SELECT id, password, e, d, n
|
||||
FROM accounts
|
||||
WHERE username = ?
|
||||
""")) {
|
||||
statement.setString(1, username);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
int id = resultSet.getInt("id");
|
||||
String hash_password = resultSet.getString("password");
|
||||
BigInteger e = new BigInteger(resultSet.getString("e"));
|
||||
byte[] d = convertToByteArray(resultSet.getString("d"));
|
||||
BigInteger n = new BigInteger(resultSet.getString("n"));
|
||||
PasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
if (encoder.matches(password, hash_password))
|
||||
return new Account(id, username, e, d, n);
|
||||
}
|
||||
return null;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to logging in: " + e);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to open a connection: " + e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] convertToByteArray(String input) {
|
||||
return Client.convertToByteArray(input);
|
||||
}
|
||||
|
||||
public static int getIdFromUsername(String username) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return -1;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
SELECT id
|
||||
FROM accounts
|
||||
WHERE username = ?
|
||||
""")) {
|
||||
statement.setString(1, username);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to retrieve the id from the username: " + e);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean addMessage(int conversationId, String message) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return false;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
INSERT INTO messages(message, conversation_id, sending_date, sending_time)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""")) {
|
||||
statement.setString(1, message);
|
||||
statement.setInt(2, conversationId);
|
||||
statement.setDate(3, Date.valueOf(LocalDateTime.now().toLocalDate()));
|
||||
statement.setTime(3, Time.valueOf(LocalDateTime.now().toLocalTime()));
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted == 1;
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to add a message: " + ex);
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to open a connection: " + ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Conversation> getConversations(int sender_id) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return null;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
SELECT id, sender, recipient
|
||||
FROM conversations
|
||||
WHERE sender = ? OR recipient = ?
|
||||
""")) {
|
||||
statement.setInt(1, sender_id);
|
||||
statement.setInt(2, sender_id);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
List<Conversation> conversations = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
int id = resultSet.getInt(1);
|
||||
int sender = resultSet.getInt(2);
|
||||
int recipient = resultSet.getInt(3);
|
||||
conversations.add(new Conversation(id, sender, recipient));
|
||||
}
|
||||
return conversations;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to get conversations: " + e);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to open a connection: " + e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean addConversation(int conversation_id) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return false;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
INSERT INTO conversations(id, sender, recipient)
|
||||
VALUES (?, ?, ?)
|
||||
""")) {
|
||||
statement.setInt(1, conversation_id);
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted == 1;
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to add a conversation: " + ex);
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to open a connection: " + ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Message> getMessages(int conversation_id) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return null;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
SELECT message, sending_date, sending_time
|
||||
FROM messages
|
||||
WHERE conversation_id = ?
|
||||
""")) {
|
||||
statement.setInt(1, conversation_id);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
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);
|
||||
messages.add(new Message(message, sending_date, sending_time));
|
||||
}
|
||||
return messages;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to get messages: " + e);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to open a connection: " + e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection(
|
||||
System.getenv("db_url"),
|
||||
System.getenv("db_user"), System.getenv("db_pass")
|
||||
);
|
||||
}
|
||||
}
|
||||
package controllers;
|
||||
|
||||
import models.Account;
|
||||
import models.Conversation;
|
||||
import models.Message;
|
||||
import org.mariadb.jdbc.MariaDbBlob;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import views.Client;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Database {
|
||||
public static boolean registerAccount(String username, String password, BigInteger e, String d, BigInteger n) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return false;
|
||||
|
||||
PasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
String hashedPass = encoder.encode(password);
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
INSERT INTO accounts(username, password, e, d, n)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""")) {
|
||||
statement.setString(1, username);
|
||||
statement.setString(2, hashedPass);
|
||||
statement.setBlob(3, new MariaDbBlob(e.toString().getBytes()));
|
||||
statement.setBytes(4, d.getBytes());
|
||||
statement.setBlob(5, new MariaDbBlob(n.toString().getBytes()));
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted == 1;
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to create a new account: " + ex);
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to open a connection: " + ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Account getAccount(String username, CharSequence password) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return null;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
SELECT id, password, e, d, n
|
||||
FROM accounts
|
||||
WHERE username = ?
|
||||
""")) {
|
||||
statement.setString(1, username);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
int id = resultSet.getInt("id");
|
||||
String hash_password = resultSet.getString("password");
|
||||
BigInteger e = new BigInteger(resultSet.getString("e"));
|
||||
byte[] d = convertToByteArray(resultSet.getString("d"));
|
||||
BigInteger n = new BigInteger(resultSet.getString("n"));
|
||||
PasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
if (encoder.matches(password, hash_password))
|
||||
return new Account(id, username, e, d, n);
|
||||
}
|
||||
return null;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to logging in: " + e);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to open a connection: " + e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] convertToByteArray(String input) {
|
||||
return Client.convertToByteArray(input);
|
||||
}
|
||||
|
||||
public static int getIdFromUsername(String username) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return -1;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
SELECT id
|
||||
FROM accounts
|
||||
WHERE username = ?
|
||||
""")) {
|
||||
statement.setString(1, username);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to retrieve the id from the username: " + e);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean addMessage(int conversationId, String message) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return false;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
INSERT INTO messages(message, conversation_id, sending_date, sending_time)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""")) {
|
||||
statement.setString(1, message);
|
||||
statement.setInt(2, conversationId);
|
||||
statement.setDate(3, Date.valueOf(LocalDateTime.now().toLocalDate()));
|
||||
statement.setTime(4, Time.valueOf(LocalDateTime.now().toLocalTime()));
|
||||
|
||||
int rowsInserted = statement.executeUpdate();
|
||||
return rowsInserted == 1;
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to add a message: " + ex);
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to open a connection: " + ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Conversation> getConversations(int sender_id) {
|
||||
try (Connection connection = getConnection()) {
|
||||
if (connection == null) return null;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
SELECT id, sender, recipient
|
||||
FROM conversations
|
||||
WHERE sender = ? OR recipient = ?
|
||||
""")) {
|
||||
statement.setInt(1, sender_id);
|
||||
statement.setInt(2, sender_id);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
List<Conversation> conversations = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
int id = resultSet.getInt(1);
|
||||
int sender = resultSet.getInt(2);
|
||||
int recipient = resultSet.getInt(3);
|
||||
conversations.add(new Conversation(id, sender, recipient));
|
||||
}
|
||||
return conversations;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to get conversations: " + e);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to open a connection: " + e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
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(sender, recipient)
|
||||
VALUES (?, ?)
|
||||
""")) {
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.err.println("Error while trying to open a connection: " + ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
try (PreparedStatement statement = connection.prepareStatement("""
|
||||
SELECT message, sending_date, sending_time
|
||||
FROM messages
|
||||
WHERE conversation_id = ?
|
||||
""")) {
|
||||
statement.setInt(1, conversation_id);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
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);
|
||||
messages.add(new Message(message, sending_date, sending_time));
|
||||
}
|
||||
return messages;
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to get messages: " + e);
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error while trying to open a connection: " + e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection(
|
||||
"jdbc:mariadb://riefolo.me:3306/chat_rsa",
|
||||
"proj_sistemi", "$o8a5#diTGg8*Agk"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,22 @@
|
||||
package controllers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MessageForwarder {
|
||||
private static final Map<Integer, ServerThread> idThread;
|
||||
|
||||
static {
|
||||
idThread = new HashMap<>();
|
||||
}
|
||||
|
||||
public static void addUser(int id, ServerThread thread) {
|
||||
idThread.put(id, thread);
|
||||
}
|
||||
|
||||
public static void sendTo(int id, String message, int senderId) throws IOException {
|
||||
idThread.get(id).sendMessage(message, senderId);
|
||||
}
|
||||
}
|
||||
package controllers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MessageForwarder {
|
||||
private static final Map<Integer, ServerThread> idThread;
|
||||
|
||||
static {
|
||||
idThread = new HashMap<>();
|
||||
}
|
||||
|
||||
public static void addUser(int id, ServerThread thread) {
|
||||
idThread.put(id, thread);
|
||||
}
|
||||
|
||||
public static void sendTo(int id, String message, int senderId) throws IOException {
|
||||
if (idThread.containsKey(id))
|
||||
idThread.get(id).sendMessage(message, senderId);
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
package controllers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
package controllers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,185 +1,190 @@
|
||||
package controllers;
|
||||
|
||||
import models.Account;
|
||||
import models.Rsa;
|
||||
import views.Client;
|
||||
|
||||
import java.io.*;
|
||||
import java.math.BigInteger;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static controllers.Database.*;
|
||||
|
||||
public class ServerThread extends Thread {
|
||||
private final Socket client;
|
||||
private BufferedReader fromClient;
|
||||
private BufferedWriter toClient;
|
||||
private final Rsa rsa;
|
||||
private BigInteger clientE, clientN;
|
||||
private int clientId;
|
||||
|
||||
public ServerThread (Socket socket) {
|
||||
this.client = socket;
|
||||
rsa = new Rsa(1024);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||
toClient = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
send(rsa.getE().toString());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try {
|
||||
send(rsa.getN().toString());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
clientE = new BigInteger(fromClient.readLine());
|
||||
clientN = new BigInteger(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
String operation;
|
||||
sendEncrypted("Quale operazione vuoi effettuare? (REGISTER|LOGIN): ");
|
||||
try {
|
||||
operation = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String username, password;
|
||||
|
||||
try {
|
||||
sendEncrypted("Inserisci l'username: ");
|
||||
username = rsa.decrypt(fromClient.readLine());
|
||||
sendEncrypted("Inserisci la password: ");
|
||||
password = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
if ("LOGIN".equals(operation)) {
|
||||
if (login(username, password)) {
|
||||
Account account = getAccount(username, password);
|
||||
if (account == null) {
|
||||
sendEncrypted("FAIL");
|
||||
break;
|
||||
}
|
||||
sendEncrypted("SUCCESS");
|
||||
sendEncrypted(String.valueOf(account.n()));
|
||||
sendEncrypted(String.valueOf(account.e()));
|
||||
sendEncrypted(Arrays.toString(account.d()));
|
||||
break;
|
||||
}
|
||||
} else if ("REGISTER".equals(operation)) {
|
||||
if (register(username, password)) {
|
||||
sendEncrypted("SUCCESS");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
sendEncrypted("FAIL");
|
||||
}
|
||||
}
|
||||
|
||||
MessageForwarder.addUser(clientId, this);
|
||||
|
||||
String message,dUsername;
|
||||
|
||||
|
||||
sendEncrypted("Inserisci l'username del destinatario: ");
|
||||
try {
|
||||
dUsername = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
int recipientId = getIdFromUsername(dUsername);
|
||||
|
||||
|
||||
for (;;) {
|
||||
try {
|
||||
sendEncrypted("Inserisci messaggio: ");
|
||||
message = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if (message == null || "DISCONNECT".equals(message)) break;
|
||||
|
||||
if ("CAMBIA_DESTINATARIO".equals(message)){
|
||||
try {
|
||||
sendEncrypted("Inserisci l'username del nuovo destinatario: ");
|
||||
dUsername = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
recipientId = getIdFromUsername(dUsername);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
MessageForwarder.sendTo(recipientId, message, clientId);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sendEncrypted(String text) {
|
||||
try {
|
||||
send(rsa.encrypt(text, clientE, clientN));
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void send(String message) throws IOException {
|
||||
toClient.write(message);
|
||||
toClient.newLine();
|
||||
toClient.flush();
|
||||
}
|
||||
|
||||
private boolean login(String username, String password) {
|
||||
Account account = getAccount(username, password);
|
||||
return account != null;
|
||||
}
|
||||
|
||||
public boolean register(String username, String password) {
|
||||
byte[] clientD;
|
||||
|
||||
try {
|
||||
String line = rsa.decrypt(fromClient.readLine());
|
||||
clientD = convertToByteArray(line);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if (!registerAccount(username, password, clientE, Arrays.toString(clientD), clientN)) return false;
|
||||
|
||||
clientId = Database.getIdFromUsername(username);
|
||||
return clientId != -1;
|
||||
}
|
||||
|
||||
public static byte[] convertToByteArray(String input) {
|
||||
return Client.convertToByteArray(input);
|
||||
}
|
||||
|
||||
|
||||
public void sendMessage(String message, int sender) throws IOException {
|
||||
send("INCOMING");
|
||||
send(String.valueOf(sender));
|
||||
send(message);
|
||||
}
|
||||
}
|
||||
package controllers;
|
||||
|
||||
import models.Account;
|
||||
import models.Rsa;
|
||||
import views.Client;
|
||||
|
||||
import java.io.*;
|
||||
import java.math.BigInteger;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static controllers.Database.*;
|
||||
|
||||
public class ServerThread extends Thread {
|
||||
private final Socket client;
|
||||
private BufferedReader fromClient;
|
||||
private BufferedWriter toClient;
|
||||
private final Rsa rsa;
|
||||
private BigInteger clientE, clientN;
|
||||
private int clientId;
|
||||
|
||||
public ServerThread (Socket socket) {
|
||||
this.client = socket;
|
||||
rsa = new Rsa(1024);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||
toClient = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
send(rsa.getE().toString());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try {
|
||||
send(rsa.getN().toString());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
clientE = new BigInteger(fromClient.readLine());
|
||||
clientN = new BigInteger(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
String operation;
|
||||
sendEncrypted("Quale operazione vuoi effettuare? (REGISTER|LOGIN): ");
|
||||
try {
|
||||
operation = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String username, password;
|
||||
|
||||
try {
|
||||
sendEncrypted("Inserisci l'username: ");
|
||||
username = rsa.decrypt(fromClient.readLine());
|
||||
sendEncrypted("Inserisci la password: ");
|
||||
password = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
if ("LOGIN".equals(operation)) {
|
||||
if (login(username, password)) {
|
||||
Account account = getAccount(username, password);
|
||||
if (account == null) {
|
||||
sendEncrypted("FAIL");
|
||||
break;
|
||||
}
|
||||
sendEncrypted("SUCCESS");
|
||||
sendEncrypted(String.valueOf(account.n()));
|
||||
sendEncrypted(String.valueOf(account.e()));
|
||||
sendEncrypted(Arrays.toString(account.d()));
|
||||
break;
|
||||
}
|
||||
} else if ("REGISTER".equals(operation)) {
|
||||
if (register(username, password)) {
|
||||
sendEncrypted("SUCCESS");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
sendEncrypted("FAIL");
|
||||
}
|
||||
}
|
||||
|
||||
MessageForwarder.addUser(clientId, this);
|
||||
|
||||
String message,dUsername;
|
||||
|
||||
|
||||
sendEncrypted("Inserisci l'username del destinatario: ");
|
||||
try {
|
||||
dUsername = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
int recipientId = getIdFromUsername(dUsername);
|
||||
Database.addConversation(clientId, recipientId);
|
||||
|
||||
|
||||
for (;;) {
|
||||
try {
|
||||
sendEncrypted("Inserisci messaggio: ");
|
||||
message = fromClient.readLine();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if (message == null || "DISCONNECT".equals(message)) break;
|
||||
|
||||
if ("CAMBIA_DESTINATARIO".equals(message)){
|
||||
try {
|
||||
sendEncrypted("Inserisci l'username del nuovo destinatario: ");
|
||||
dUsername = rsa.decrypt(fromClient.readLine());
|
||||
} catch (IOException e) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sendEncrypted(String text) {
|
||||
try {
|
||||
send(rsa.encrypt(text, clientE, clientN));
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void send(String message) throws IOException {
|
||||
toClient.write(message);
|
||||
toClient.newLine();
|
||||
toClient.flush();
|
||||
}
|
||||
|
||||
private boolean login(String username, String password) {
|
||||
Account account = getAccount(username, password);
|
||||
return account != null;
|
||||
}
|
||||
|
||||
public boolean register(String username, String password) {
|
||||
byte[] clientD;
|
||||
|
||||
try {
|
||||
String line = rsa.decrypt(fromClient.readLine());
|
||||
clientD = convertToByteArray(line);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if (!registerAccount(username, password, clientE, Arrays.toString(clientD), clientN)) return false;
|
||||
|
||||
clientId = Database.getIdFromUsername(username);
|
||||
return clientId != -1;
|
||||
}
|
||||
|
||||
public static byte[] convertToByteArray(String input) {
|
||||
return Client.convertToByteArray(input);
|
||||
}
|
||||
|
||||
|
||||
public void sendMessage(String message, int sender) throws IOException {
|
||||
send("INCOMING");
|
||||
send(String.valueOf(sender));
|
||||
send(message);
|
||||
}
|
||||
}
|
||||
|
@ -1,73 +1,92 @@
|
||||
package views;
|
||||
|
||||
import models.Rsa;
|
||||
|
||||
import java.io.*;
|
||||
import java.math.BigInteger;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class ClientSendThread extends Thread{
|
||||
private final BufferedWriter out;
|
||||
private final BigInteger se,sn;
|
||||
private final Rsa rsa;
|
||||
private final BufferedReader in;
|
||||
|
||||
public ClientSendThread(BufferedReader in,BufferedWriter out,BigInteger se, BigInteger sn,Rsa rsa){
|
||||
this.in=in;
|
||||
this.out = out;
|
||||
this.se = se;
|
||||
this.sn=sn;
|
||||
this.rsa=rsa;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
try {
|
||||
String s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
if("REGISTER".equals(s)){
|
||||
sendCredentials(br);
|
||||
try {
|
||||
if (Client.register(s)) break;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
System.err.println("Errore durante la registrazione");
|
||||
}
|
||||
}
|
||||
else if("LOGIN".equals(s)){
|
||||
sendCredentials(br);
|
||||
try {
|
||||
if (Client.login(s)) break;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
System.err.println("Errore durante l'accesso");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
ClientReceiveThread crt = new ClientReceiveThread(in,rsa);
|
||||
crt.start();
|
||||
}
|
||||
|
||||
private void sendCredentials(BufferedReader br) throws IOException {
|
||||
String s;
|
||||
s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
}
|
||||
|
||||
public void send(String message) throws IOException {
|
||||
out.write(message);
|
||||
out.newLine();
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
package views;
|
||||
|
||||
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;
|
||||
private final BigInteger se,sn;
|
||||
private final Rsa rsa;
|
||||
private final BufferedReader in;
|
||||
|
||||
public ClientSendThread(BufferedReader in,BufferedWriter out,BigInteger se, BigInteger sn,Rsa rsa){
|
||||
this.in=in;
|
||||
this.out = out;
|
||||
this.se = se;
|
||||
this.sn=sn;
|
||||
this.rsa=rsa;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
try {
|
||||
String s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
if("REGISTER".equals(s)){
|
||||
sendCredentials(br);
|
||||
try {
|
||||
if (Client.register(s)) break;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
System.err.println("Errore durante la registrazione");
|
||||
}
|
||||
}
|
||||
else if("LOGIN".equals(s)){
|
||||
sendCredentials(br);
|
||||
try {
|
||||
if (Client.login(s)) break;
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
System.err.println("Errore durante l'accesso");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
String s;
|
||||
s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
s = in.readLine();
|
||||
System.out.print(rsa.decrypt(s));
|
||||
s = br.readLine();
|
||||
send(rsa.encrypt(s, se, sn));
|
||||
}
|
||||
|
||||
public void send(String message) throws IOException {
|
||||
out.write(message);
|
||||
out.newLine();
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user