some more logs, javadocs

This commit is contained in:
2025-03-12 00:33:49 -05:00
parent dfea28ad3a
commit 460c09365d
6 changed files with 125 additions and 30 deletions

View File

@@ -1,40 +1,60 @@
package net.ayyalasomayajula.net;
import net.ayyalasomayajula.net.client.ClientConnect;
import net.ayyalasomayajula.net.server.ServerDaemon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactoryFriend;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ClientServerSelect extends JFrame{
/**
* The ClientServerSelect class represents a graphical user interface (GUI) frame for selecting
* between a client or server connection. This class extends {@link JFrame} and contains two buttons:
* one to initiate the client and the other to start the server.
* The frame allows the user to choose whether they want to run the client or the server.
*/
public class ClientServerSelect extends JFrame {
private JButton serverButton;
private JButton clientButton;
private JPanel panel;
private static final Logger logger = LoggerFactory.getLogger(ClientServerSelect.class);
public ClientServerSelect(){
setContentPane(panel);
setSize(1024,1024);
setVisible(true);
/**
* Constructs a new {@code ClientServerSelect} frame, initializing the GUI components and
* adding action listeners to the client and server buttons.
* <p>
* When the client button is clicked, the {@link ClientConnect} class is instantiated, and the
* frame is disposed. When the server button is clicked, a new thread is started to run the
* {@link ServerDaemon}, and the frame is disposed.
*/
public ClientServerSelect() {
setContentPane(panel); // Set the main panel for the JFrame
setSize(1024, 1024); // Set the size of the frame
setVisible(true); // Make the frame visible
// ActionListener for the client button
clientButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
logger.info("Client button triggered.");
// Initialize the client connection
ClientConnect connector = new ClientConnect();
dispose();
dispose(); // Close the current window after client connection is initiated
}
});
// ActionListener for the server button
serverButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
logger.info("Server Button triggered");
dispose();
// Start the server daemon in a new thread
Thread offshoot = new Thread(new ServerDaemon());
offshoot.start();
dispose(); // Close the current window after the server is started
}
});
}

View File

@@ -7,24 +7,41 @@ import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ClientConnect extends JFrame{
/**
* The ClientConnect class represents a graphical user interface (GUI) frame for capturing
* client connection details such as the hostname, port number, and authentication token.
* The user can input connection details and trigger a connection request using the provided button.
* This class extends {@link JFrame}.
*/
public class ClientConnect extends JFrame {
private JPanel panel;
private JTextField hostname;
private JTextField portNumber;
private JTextField authToken;
private JButton goButton;
private static final Logger logger = LoggerFactory.getLogger(ClientConnect.class);
public ClientConnect(){
setTitle("Client Connection Details");
setContentPane(panel);
setSize(1024,1024);
setVisible(true);
/**
* Constructs a new {@code ClientConnect} frame. It initializes the GUI components and
* sets up an action listener for the connection button.
* <p>
* When the "Go" button is clicked, the connection request is triggered, and an informational
* log entry is made to record the action. The hostname, port, and auth token input fields
* capture the connection parameters.
*/
public ClientConnect() {
setTitle("Client Connection Details"); // Set the title of the frame
setContentPane(panel); // Set the main panel for the JFrame
setSize(1024, 1024); // Set the size of the frame
setVisible(true); // Make the frame visible
// ActionListener for the "Go" button
goButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
logger.info("Triggered a Connection request.");
// Connection logic would go here (e.g., retrieve values and initiate connection)
}
});
}
}

View File

@@ -3,40 +3,78 @@ package net.ayyalasomayajula.net.server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Path;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ServerDaemon implements Runnable{
/**
* The ServerDaemon class implements a server daemon that listens for incoming client connections.
* It initializes a root path for storing data, creates necessary directories and files,
* and handles incoming socket connections using a thread pool for concurrency.
*/
public class ServerDaemon implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(ServerDaemon.class);
/**
* The entry point of the server daemon that sets up the server, initializes root paths,
* and listens for client connections.
* It also spawns a thread pool to handle incoming connections asynchronously.
*/
@Override
public void run() {
System.out.println("Welcome to the Server Daemon.");
System.out.println("What is the root path for all your data?");
System.out.println("prepare for the codebase to throw a RuntimeException if incorrectly formatted or error inducing data is submitted. This is a security feature.");
Scanner sc = new Scanner(System.in);
Path path = new File(sc.nextLine().trim()).toPath();
logger.debug("Path received: {}", path.toString());
if(!isRootPathInited(path)){
if (!isRootPathInited(path)) {
boolean success = initRootPath(path);
if(!success) throw new RuntimeException("Your Path is dysfunctional");
if (!success) throw new RuntimeException("Your Path is dysfunctional");
}
try {
ServerSocket serverSocket = new ServerSocket(8475);
logger.info("Server to handle connections has been made at: {} on 0.0.0.0", serverSocket.getLocalPort());
ThreadPoolExecutor tpe = new ThreadPoolExecutor(5, 10, 9999L, TimeUnit.HOURS, new LinkedBlockingDeque<Runnable>());
while (true) {
Socket socketToPass = serverSocket.accept();
logger.info("Received a connection");
tpe.submit(new Runnable() {
@Override
public void run() {
try {
handleConnections(socketToPass);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private boolean initRootPath(Path path){
// create the lock file
/**
* Initializes the root path by creating necessary directories and files.
* Specifically, it creates a lock file and a "patients" directory.
*
* @param path The root path to initialize.
* @return True if initialization is successful, false otherwise.
*/
private boolean initRootPath(Path path) {
// Create the lock file
String lockFile = "lock.lock"; // Set your desired file name
// Combine the base path and file name
@@ -51,7 +89,7 @@ public class ServerDaemon implements Runnable{
// Create the file
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
logger.info("File created: " + file.getName());
// Write a random byte to the file
try (FileOutputStream fos = new FileOutputStream(file)) {
@@ -66,16 +104,22 @@ public class ServerDaemon implements Runnable{
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
// create patient dir
// Create patient directory
String patientEhrs = "patients/";
Path patientPath = path.resolve(patientEhrs);
File dir = patientPath.toFile();
dir.mkdir();
return true;
}
/**
* Checks whether the root path has already been initialized by checking if the lock file exists.
*
* @param path The root path to check.
* @return True if the lock file exists, false otherwise.
*/
private boolean isRootPathInited(Path path) {
// Create a File object from the given path and append "lock.lock"
File lockFile = path.resolve("lock.lock").toFile();
@@ -83,4 +127,18 @@ public class ServerDaemon implements Runnable{
// Check if the file exists and is a file (not a directory)
return lockFile.exists() && lockFile.isFile();
}
/**
* Handles incoming socket connections. Currently, it sets up ObjectOutputStream
* and ObjectInputStream for communication but does not process the data.
*
* @param socket The socket for the incoming connection.
* @throws IOException If an I/O error occurs while setting up the streams.
*/
private void handleConnections(Socket socket) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
// Further processing can be added here
}
}