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