Loading src/Main.java +76 −81 Original line number Diff line number Diff line import java.io.*; import java.net.*; package edu.bu.ec504.project; public class Main { static MoleculeDatabase moleculeDb; public static void dbLoad() throws IOException, ClassNotFoundException { FileInputStream fileInStream = new FileInputStream("molecule.db"); ObjectInputStream objInStream = new ObjectInputStream(fileInStream); moleculeDb = (MoleculeDatabase) objInStream.readObject(); objInStream.close(); fileInStream.close(); } public static void dbSave() throws IOException { FileOutputStream fileOutStream = new FileOutputStream("molecule.db"); ObjectOutputStream objOutStream = new ObjectOutputStream(fileOutStream); objOutStream.writeObject(moleculeDb); objOutStream.close(); fileOutStream.close(); } public static void runClient(Socket clientSocket, String argument) throws IOException { /** * Method to run the client side of the program * @param clientSocket * @param argument * @throws IOException */ public static void runClient(Socket clientSocket, String argument) throws IOException { // 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(); } public static void runServer(ServerSocket serverSocket) throws IOException { try { dbLoad(); } catch (IOException e) { moleculeDb = new MoleculeDatabase(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } /** * Method to run the server side of the program */ public static void runServer(ServerSocket serverSocket, String filename) 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]; // Perform actions based on the received command if (command.equals("--addMolecule")) { // Add molecule logic here } else if (command.equals("--findMolecule")) { // Find molecule logic here } // 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 reader.close(); inStream.close(); clientSocket.close(); MoleculeDatabase.save(filename); System.out.println("Goodbye"); } /** * Main method to start the program */ public static void main(String[] args) throws IOException { // Get the port number from the command line arguments final int PORT_NUMBER = Integer.parseInt(args[0]); String command = ""; String filePath = ""; if (args.length == 2) { command = args[1]; } else if (args.length == 3) { command = args[1]; filePath = args[2]; } else { System.out.println("invalid number of arguments"); // Set the default filename for the database String filename = "molecule.db"; // Check if an alternative filename is provided as a command line argument if (args.length > 1) { filename = args[1]; } try (Socket clientSocket = new Socket("localhost", PORT_NUMBER)) { runClient(clientSocket, command + " " + filePath); // clientSocket.close(); } catch (ConnectException e) { // If a client connection is successful, run the client side of the program runClient(clientSocket, filename); } 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); runServer(serverSocket, filename); serverSocket.close(); dbSave(); System.out.println("goodbye"); } } } src/MoleculeDatabase.java +68 −2 Original line number Diff line number Diff line import java.io.Serializable; import java.util.ArrayList; package edu.bu.ec504.project; /** * A class represents the molecule database */ public class MoleculeDatabase implements Serializable { public static void MoleculeDatabase() { public ArrayList<MoleculeDatabase> db; // Molecule database /** * Constructs a database */ public MoleculeDatabase() { this.db = new ArrayList<>(); } /** * Add a new molecule into the database */ public void addMolecule(Molecule molecule) { return null; // for now } /** * Find isomorphic molecule from the database */ public Molecule findMolecule(Molecule molecule) { // Iterate through the array list of molecules inside the database for (Molecule dbMolecule : db) { if (areMoleculesEqual(dbMolecule, molecule)) { return dbMolecule; // Return the isomorphic molecule } } System.out.println("No isomorphic molecule found in database."); return null; // Return null if molecule not found } /** * Save database to file system */ public void save(String filename) { try { FileOutputStream fileOutStream = new FileOutputStream(filename); ObjectOutputStream objOutStream = new ObjectOutputStream(fileOutStream); objOutStream.writeObject(this.molecules); objOutStream.close(); fileOutStream.close(); System.out.println("Database saved successfully."); } catch (IOException e) { System.err.println("Error saving database: " + e.getMessage()); } } /** * Load database from file system */ public void load(String filename) { try { FileInputStream fileInStream = new FileInputStream(filename); ObjectInputStream objInStream = new ObjectInputStream(fileInStream); this.molecules = (ArrayList<Molecule>) objInStream.readObject(); objInStream.close(); fileInStream.close(); System.out.println("Database loaded successfully."); } catch (IOException | ClassNotFoundException e) { System.err.println("Error loading database: " + e.getMessage()); } } } Loading
src/Main.java +76 −81 Original line number Diff line number Diff line import java.io.*; import java.net.*; package edu.bu.ec504.project; public class Main { static MoleculeDatabase moleculeDb; public static void dbLoad() throws IOException, ClassNotFoundException { FileInputStream fileInStream = new FileInputStream("molecule.db"); ObjectInputStream objInStream = new ObjectInputStream(fileInStream); moleculeDb = (MoleculeDatabase) objInStream.readObject(); objInStream.close(); fileInStream.close(); } public static void dbSave() throws IOException { FileOutputStream fileOutStream = new FileOutputStream("molecule.db"); ObjectOutputStream objOutStream = new ObjectOutputStream(fileOutStream); objOutStream.writeObject(moleculeDb); objOutStream.close(); fileOutStream.close(); } public static void runClient(Socket clientSocket, String argument) throws IOException { /** * Method to run the client side of the program * @param clientSocket * @param argument * @throws IOException */ public static void runClient(Socket clientSocket, String argument) throws IOException { // 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(); } public static void runServer(ServerSocket serverSocket) throws IOException { try { dbLoad(); } catch (IOException e) { moleculeDb = new MoleculeDatabase(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } /** * Method to run the server side of the program */ public static void runServer(ServerSocket serverSocket, String filename) 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]; // Perform actions based on the received command if (command.equals("--addMolecule")) { // Add molecule logic here } else if (command.equals("--findMolecule")) { // Find molecule logic here } // 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 reader.close(); inStream.close(); clientSocket.close(); MoleculeDatabase.save(filename); System.out.println("Goodbye"); } /** * Main method to start the program */ public static void main(String[] args) throws IOException { // Get the port number from the command line arguments final int PORT_NUMBER = Integer.parseInt(args[0]); String command = ""; String filePath = ""; if (args.length == 2) { command = args[1]; } else if (args.length == 3) { command = args[1]; filePath = args[2]; } else { System.out.println("invalid number of arguments"); // Set the default filename for the database String filename = "molecule.db"; // Check if an alternative filename is provided as a command line argument if (args.length > 1) { filename = args[1]; } try (Socket clientSocket = new Socket("localhost", PORT_NUMBER)) { runClient(clientSocket, command + " " + filePath); // clientSocket.close(); } catch (ConnectException e) { // If a client connection is successful, run the client side of the program runClient(clientSocket, filename); } 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); runServer(serverSocket, filename); serverSocket.close(); dbSave(); System.out.println("goodbye"); } } }
src/MoleculeDatabase.java +68 −2 Original line number Diff line number Diff line import java.io.Serializable; import java.util.ArrayList; package edu.bu.ec504.project; /** * A class represents the molecule database */ public class MoleculeDatabase implements Serializable { public static void MoleculeDatabase() { public ArrayList<MoleculeDatabase> db; // Molecule database /** * Constructs a database */ public MoleculeDatabase() { this.db = new ArrayList<>(); } /** * Add a new molecule into the database */ public void addMolecule(Molecule molecule) { return null; // for now } /** * Find isomorphic molecule from the database */ public Molecule findMolecule(Molecule molecule) { // Iterate through the array list of molecules inside the database for (Molecule dbMolecule : db) { if (areMoleculesEqual(dbMolecule, molecule)) { return dbMolecule; // Return the isomorphic molecule } } System.out.println("No isomorphic molecule found in database."); return null; // Return null if molecule not found } /** * Save database to file system */ public void save(String filename) { try { FileOutputStream fileOutStream = new FileOutputStream(filename); ObjectOutputStream objOutStream = new ObjectOutputStream(fileOutStream); objOutStream.writeObject(this.molecules); objOutStream.close(); fileOutStream.close(); System.out.println("Database saved successfully."); } catch (IOException e) { System.err.println("Error saving database: " + e.getMessage()); } } /** * Load database from file system */ public void load(String filename) { try { FileInputStream fileInStream = new FileInputStream(filename); ObjectInputStream objInStream = new ObjectInputStream(fileInStream); this.molecules = (ArrayList<Molecule>) objInStream.readObject(); objInStream.close(); fileInStream.close(); System.out.println("Database loaded successfully."); } catch (IOException | ClassNotFoundException e) { System.err.println("Error loading database: " + e.getMessage()); } } }