server finally works. only thing left is to write queries from the client

This commit is contained in:
2025-03-12 00:41:52 -05:00
parent 460c09365d
commit bbee3979d0
9 changed files with 56 additions and 24 deletions

View File

@@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.net.Socket;
/** /**
* The ClientConnect class represents a graphical user interface (GUI) frame for capturing * The ClientConnect class represents a graphical user interface (GUI) frame for capturing
@@ -41,7 +42,26 @@ public class ClientConnect extends JFrame {
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
logger.info("Triggered a Connection request."); logger.info("Triggered a Connection request.");
// Connection logic would go here (e.g., retrieve values and initiate connection) // Connection logic would go here (e.g., retrieve values and initiate connection)
} }
}); });
} }
private String getSanitizedText(JTextField textField) {
// Get the text from the JTextField and trim leading/trailing whitespace
String sanitizedText = textField.getText().trim();
// Remove unwanted characters (single quotes, double quotes, non-alphanumeric characters)
sanitizedText = sanitizedText.replaceAll("[^a-zA-Z0-9]", ""); // Remove all non-alphanumeric characters
// Optionally, remove single quotes and double quotes
sanitizedText = sanitizedText.replaceAll("[\'\"]", "");
// If the text is empty after sanitization, set a default value
if (sanitizedText.isEmpty()) {
sanitizedText = "default"; // or you can return an empty string, or throw an exception, depending on the case
}
return sanitizedText;
}
} }

View File

@@ -73,46 +73,58 @@ public class ServerDaemon implements Runnable {
* @param path The root path to initialize. * @param path The root path to initialize.
* @return True if initialization is successful, false otherwise. * @return True if initialization is successful, false otherwise.
*/ */
private boolean initRootPath(Path path) { private boolean initRootPath(Path path) {
// Create the lock file // Create the lock file
String lockFile = "lock.lock"; // Set your desired file name String lockFile = "lock.lock"; // Set your desired file name
Path fullPath = path.resolve(lockFile); // Combine the base path and file name
// Combine the base path and file name
Path fullPath = path.resolve(lockFile);
// Create the file
File file = fullPath.toFile(); File file = fullPath.toFile();
try { try {
// Create the parent directories if they don't exist // Create the parent directories if they don't exist
file.getParentFile().mkdirs(); if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
logger.error("Failed to create directories for lock file.");
return false;
}
// Create the file // Create the lock file if it doesn't already exist
if (!file.exists()) {
if (file.createNewFile()) { if (file.createNewFile()) {
logger.info("File created: " + file.getName()); logger.info("Lock file created: " + file.getName());
// Write a random byte to the file // Write a random byte to the file
try (FileOutputStream fos = new FileOutputStream(file)) { try (FileOutputStream fos = new FileOutputStream(file)) {
Random random = new Random(); Random random = new Random();
byte randomByte = (byte) random.nextInt(256); // Generate random byte byte randomByte = (byte) random.nextInt(256); // Generate random byte
fos.write(randomByte); fos.write(randomByte);
logger.info("Random byte written to file."); logger.info("Random byte written to lock file.");
} }
} else { } else {
System.out.println("File already exists."); logger.error("Failed to create lock file.");
return false;
} }
} catch (IOException e) { } else {
System.err.println("An error occurred: " + e.getMessage()); logger.info("Lock file already exists.");
} }
// Create patient directory // Create the patient directory
String patientEhrs = "patients/"; String patientEhrs = "patients/";
Path patientPath = path.resolve(patientEhrs); Path patientPath = path.resolve(patientEhrs);
File dir = patientPath.toFile(); File dir = patientPath.toFile();
dir.mkdir();
if (!dir.exists() && !dir.mkdirs()) {
logger.error("Failed to create patient directory.");
return false;
}
return true; return true;
} catch (IOException e) {
logger.error("An error occurred during lock file creation: {}", e.getMessage());
return false;
} }
}
/** /**
* Checks whether the root path has already been initialized by checking if the lock file exists. * Checks whether the root path has already been initialized by checking if the lock file exists.