From 460c09365dabf6182b7634d0342b848e0eefe4af Mon Sep 17 00:00:00 2001 From: Mars Ultor Date: Wed, 12 Mar 2025 00:33:49 -0500 Subject: [PATCH] some more logs, javadocs --- .../net/ClientServerSelect.java | 42 ++++++--- .../net/client/ClientConnect.java | 31 +++++-- .../net/server/ServerDaemon.java | 82 +++++++++++++++--- .../net/ClientServerSelect$1.class | Bin 1150 -> 1150 bytes .../net/ClientServerSelect$2.class | Bin 1009 -> 1209 bytes .../net/ClientServerSelect.class | Bin 2163 -> 2163 bytes 6 files changed, 125 insertions(+), 30 deletions(-) diff --git a/src/main/java/net/ayyalasomayajula/net/ClientServerSelect.java b/src/main/java/net/ayyalasomayajula/net/ClientServerSelect.java index 91239cc..8169239 100644 --- a/src/main/java/net/ayyalasomayajula/net/ClientServerSelect.java +++ b/src/main/java/net/ayyalasomayajula/net/ClientServerSelect.java @@ -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. + *

+ * 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 } }); } diff --git a/src/main/java/net/ayyalasomayajula/net/client/ClientConnect.java b/src/main/java/net/ayyalasomayajula/net/client/ClientConnect.java index f3d6d14..804d52c 100644 --- a/src/main/java/net/ayyalasomayajula/net/client/ClientConnect.java +++ b/src/main/java/net/ayyalasomayajula/net/client/ClientConnect.java @@ -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. + *

+ * 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) } }); } } - diff --git a/src/main/java/net/ayyalasomayajula/net/server/ServerDaemon.java b/src/main/java/net/ayyalasomayajula/net/server/ServerDaemon.java index caed4b7..f691a2c 100644 --- a/src/main/java/net/ayyalasomayajula/net/server/ServerDaemon.java +++ b/src/main/java/net/ayyalasomayajula/net/server/ServerDaemon.java @@ -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()); + + 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 + } } diff --git a/target/classes/net/ayyalasomayajula/net/ClientServerSelect$1.class b/target/classes/net/ayyalasomayajula/net/ClientServerSelect$1.class index 5995679f2ca9ad30b9123ab63cdce517cbf3d379..2c8c4e40d3c07ad21eca34eb8129160ff670b5a0 100644 GIT binary patch delta 31 ncmeyz@sDG}4rWG)$vc_Tc%>P*7~~j)7~~lw85Ab#umk}Blm-VC delta 31 ncmeyz@sDG}4rWI2$vc_Tc%>M)7-SiQ7~~iv8RRGHumk}BlfwrP diff --git a/target/classes/net/ayyalasomayajula/net/ClientServerSelect$2.class b/target/classes/net/ayyalasomayajula/net/ClientServerSelect$2.class index 4c8eef72f9bc3a91355f57c232231b6b0cac09cf..5e9e48dd2d26d339960511ea43fd262994f57b58 100644 GIT binary patch delta 453 zcmey!zLS&d)W2Q(7#J9A7;HCkMKQ{-GsrM92xKLeCF!5 zVg?olW(EcZ)lCe%Lc%*4M41?NGe}47W{{2C&LF>?LBUsN8-ude76!&`3~C#}_JeE` zV_*eoV&F<(P-Bo}P-l>3&|pwu&;&a{0b(ND391Yx45kc>43Z2y3}y`G3``6x3>ILc iEE%jA7#LU>_!u}Dtid9D3^oi53|tJ1U~e!mFaQAh3rCIs delta 268 zcmdnV`H`LL)W2Q(7#J9A7z{RYMKOwVG4L_S@G!_S@Gvs4r(_lv zYYPM8HU{a9V9P+p^D!_pFfgbva51Pd2r{T4StG