Server Daemon is getting somewhere
This commit is contained in:
@@ -22,7 +22,9 @@ public class ClientConnect extends JFrame{
|
||||
goButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
logger.info("Triggered a Connection request."
|
||||
logger.info("Triggered a Connection request.");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
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.net.ServerSocket;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ServerDaemon implements Runnable{
|
||||
private static final Logger logger = LoggerFactory.getLogger(ServerDaemon.class);
|
||||
@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)){
|
||||
boolean success = initRootPath(path);
|
||||
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());
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
Path fullPath = path.resolve(lockFile);
|
||||
|
||||
// Create the file
|
||||
File file = fullPath.toFile();
|
||||
|
||||
try {
|
||||
// Create the parent directories if they don't exist
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
// Create the file
|
||||
if (file.createNewFile()) {
|
||||
System.out.println("File created: " + file.getName());
|
||||
|
||||
// Write a random byte to the file
|
||||
try (FileOutputStream fos = new FileOutputStream(file)) {
|
||||
Random random = new Random();
|
||||
byte randomByte = (byte) random.nextInt(256); // Generate random byte
|
||||
fos.write(randomByte);
|
||||
logger.info("Random byte written to file.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("File already exists.");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("An error occurred: " + e.getMessage());
|
||||
}
|
||||
// create patient dir
|
||||
String patientEhrs = "patients/";
|
||||
Path patientPath = path.resolve(patientEhrs);
|
||||
File dir = patientPath.toFile();
|
||||
dir.mkdir();
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isRootPathInited(Path path) {
|
||||
// Create a File object from the given path and append "lock.lock"
|
||||
File lockFile = path.resolve("lock.lock").toFile();
|
||||
|
||||
// Check if the file exists and is a file (not a directory)
|
||||
return lockFile.exists() && lockFile.isFile();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user