Loading src/Main.java +54 −30 Original line number Diff line number Diff line Loading @@ -21,9 +21,11 @@ public class Main { // Set up output stream to send data to the server OutputStream outStream = clientSocket.getOutputStream(); PrintWriter writer = new PrintWriter(outStream); // Write the argument to the output stream writer.println(argument); writer.flush(); // Close the streams writer.close(); outStream.close(); Loading @@ -32,38 +34,50 @@ public class Main { /** * Method to run the server side of the program */ public static void runServer(ServerSocket serverSocket, String filename) throws IOException { public static void runServer(ServerSocket serverSocket, String cmd, String moleculePath, String dbName) throws IOException { // Load the database MoleculeDatabase.load(filename); // Accept incoming client connections Socket clientSocket = serverSocket.accept(); InputStream inStream = clientSocket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); String[] arguments = reader.readLine().split(" "); String command = arguments[0]; // Continue processing commands until "quit" command is received while (!command.equals("quit")) { String filePath = arguments[1]; MoleculeDatabase moleculeDb = new MoleculeDatabase(); moleculeDb.load(dbName); // Continue processing commands until "--quit" command is received while (!cmd.equals("--quit")) { // Perform actions based on the received command if (command.equals("--addMolecule")) { if (moleculePath.equals("")) { System.out.println("moleculePath is empty. moleculePath cannot be empty"); } else if (cmd.equals("--addMolecule")) { System.out.println("received: add " + moleculePath); // Add molecule logic here } else if (command.equals("--findMolecule")) { } else if (cmd.equals("--findMolecules")) { System.out.println("received: find " + moleculePath); // Find molecule logic here } else { System.out.println("unrecognized command: " + cmd); } // Accept the next client connection clientSocket = serverSocket.accept(); inStream = clientSocket.getInputStream(); reader = new BufferedReader(new InputStreamReader(inStream)); arguments = reader.readLine().split(" "); command = arguments[0]; } // Close the streams and save the database before exiting // Accept incoming client connections Socket clientSocket = serverSocket.accept(); InputStream inStream = clientSocket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); String[] args = reader.readLine().split(" "); // close the streams reader.close(); inStream.close(); clientSocket.close(); MoleculeDatabase.save(filename); // parse argument from client cmd = args[0]; moleculePath = ""; if (args.length > 1) { moleculePath = args[1]; } } // save the database before exiting moleculeDb.save(dbName); System.out.println("Goodbye"); } Loading @@ -71,24 +85,34 @@ public class Main { * Main method to start the program */ public static void main(String[] args) throws IOException { System.out.println("\r"); final int ARG_COUNT = args.length; // Get the port number from the command line arguments final int PORT_NUMBER = Integer.parseInt(args[0]); // get other command line arguments String cmd = args[1]; String moleculePath = ""; if (ARG_COUNT > 2) { moleculePath = args[2]; } // Set the default filename for the database String filename = "molecule.db"; String dbName = "molecule.db"; // Check if an alternative filename is provided as a command line argument if (args.length > 1) { filename = args[1]; if (ARG_COUNT > 3) { dbName = args[3]; } try (Socket clientSocket = new Socket("localhost", PORT_NUMBER)) { // If a client connection is successful, run the client side of the program runClient(clientSocket, filename); runClient(clientSocket, cmd + " " + moleculePath); } catch (ConnectException e) { // If a client connection fails, run the server side of the program ServerSocket serverSocket = new ServerSocket(PORT_NUMBER); serverSocket.setSoTimeout(60 * 1000); runServer(serverSocket, filename); runServer(serverSocket, cmd, moleculePath, dbName); serverSocket.close(); } } Loading Loading
src/Main.java +54 −30 Original line number Diff line number Diff line Loading @@ -21,9 +21,11 @@ public class Main { // Set up output stream to send data to the server OutputStream outStream = clientSocket.getOutputStream(); PrintWriter writer = new PrintWriter(outStream); // Write the argument to the output stream writer.println(argument); writer.flush(); // Close the streams writer.close(); outStream.close(); Loading @@ -32,38 +34,50 @@ public class Main { /** * Method to run the server side of the program */ public static void runServer(ServerSocket serverSocket, String filename) throws IOException { public static void runServer(ServerSocket serverSocket, String cmd, String moleculePath, String dbName) throws IOException { // Load the database MoleculeDatabase.load(filename); // Accept incoming client connections Socket clientSocket = serverSocket.accept(); InputStream inStream = clientSocket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); String[] arguments = reader.readLine().split(" "); String command = arguments[0]; // Continue processing commands until "quit" command is received while (!command.equals("quit")) { String filePath = arguments[1]; MoleculeDatabase moleculeDb = new MoleculeDatabase(); moleculeDb.load(dbName); // Continue processing commands until "--quit" command is received while (!cmd.equals("--quit")) { // Perform actions based on the received command if (command.equals("--addMolecule")) { if (moleculePath.equals("")) { System.out.println("moleculePath is empty. moleculePath cannot be empty"); } else if (cmd.equals("--addMolecule")) { System.out.println("received: add " + moleculePath); // Add molecule logic here } else if (command.equals("--findMolecule")) { } else if (cmd.equals("--findMolecules")) { System.out.println("received: find " + moleculePath); // Find molecule logic here } else { System.out.println("unrecognized command: " + cmd); } // Accept the next client connection clientSocket = serverSocket.accept(); inStream = clientSocket.getInputStream(); reader = new BufferedReader(new InputStreamReader(inStream)); arguments = reader.readLine().split(" "); command = arguments[0]; } // Close the streams and save the database before exiting // Accept incoming client connections Socket clientSocket = serverSocket.accept(); InputStream inStream = clientSocket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); String[] args = reader.readLine().split(" "); // close the streams reader.close(); inStream.close(); clientSocket.close(); MoleculeDatabase.save(filename); // parse argument from client cmd = args[0]; moleculePath = ""; if (args.length > 1) { moleculePath = args[1]; } } // save the database before exiting moleculeDb.save(dbName); System.out.println("Goodbye"); } Loading @@ -71,24 +85,34 @@ public class Main { * Main method to start the program */ public static void main(String[] args) throws IOException { System.out.println("\r"); final int ARG_COUNT = args.length; // Get the port number from the command line arguments final int PORT_NUMBER = Integer.parseInt(args[0]); // get other command line arguments String cmd = args[1]; String moleculePath = ""; if (ARG_COUNT > 2) { moleculePath = args[2]; } // Set the default filename for the database String filename = "molecule.db"; String dbName = "molecule.db"; // Check if an alternative filename is provided as a command line argument if (args.length > 1) { filename = args[1]; if (ARG_COUNT > 3) { dbName = args[3]; } try (Socket clientSocket = new Socket("localhost", PORT_NUMBER)) { // If a client connection is successful, run the client side of the program runClient(clientSocket, filename); runClient(clientSocket, cmd + " " + moleculePath); } catch (ConnectException e) { // If a client connection fails, run the server side of the program ServerSocket serverSocket = new ServerSocket(PORT_NUMBER); serverSocket.setSoTimeout(60 * 1000); runServer(serverSocket, filename); runServer(serverSocket, cmd, moleculePath, dbName); serverSocket.close(); } } Loading