[CT414]: Add Assignment 1 progress
This commit is contained in:
@ -0,0 +1,61 @@
|
|||||||
|
package client;
|
||||||
|
|
||||||
|
import interfaces.ApplicationForm;
|
||||||
|
import interfaces.ApplicationHandler;
|
||||||
|
|
||||||
|
import java.rmi.registry.LocateRegistry;
|
||||||
|
import java.rmi.registry.Registry;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class ApplicationClient {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// connect to registry
|
||||||
|
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
|
||||||
|
System.out.println("Connected to RMI Registry");
|
||||||
|
|
||||||
|
// look up application handler in registry
|
||||||
|
ApplicationHandler handler = (ApplicationHandler) registry.lookup("ApplicationHandler");
|
||||||
|
System.out.println("ApplicationHandler found in registry");
|
||||||
|
|
||||||
|
// login
|
||||||
|
System.out.println("Enter your username\n> ");
|
||||||
|
String username = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter your password\n> ");
|
||||||
|
String password = scanner.nextLine();
|
||||||
|
|
||||||
|
long sessionID = handler.login(username, password);
|
||||||
|
System.out.println("Successfully logged in with session ID: " + sessionID);
|
||||||
|
|
||||||
|
// download application form
|
||||||
|
ApplicationForm form = handler.downloadApplicationForm(sessionID);
|
||||||
|
System.out.println("Successfully downloaded application form");
|
||||||
|
|
||||||
|
// print form info
|
||||||
|
System.out.println("------ FORM INFORMATION ------");
|
||||||
|
System.out.println(form.getFormInfo());
|
||||||
|
System.out.println("Number of questions to be answered: " + form.getTotalQuestions());
|
||||||
|
System.out.println("------------------------------");
|
||||||
|
|
||||||
|
// answer questiosn
|
||||||
|
for (int i = 0; i < form.getTotalQuestions(); i++) {
|
||||||
|
System.out.println("Q" + i + ": " + form.getQuestion(i));
|
||||||
|
System.out.println("A> ");
|
||||||
|
form.answerQuestion(i, scanner.nextLine());
|
||||||
|
}
|
||||||
|
System.out.println("Form filled successfully.");
|
||||||
|
|
||||||
|
// submit form
|
||||||
|
handler.submitApplicationForm(sessionID, form);
|
||||||
|
System.out.println("Successfully submitted application form.");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -43,6 +43,11 @@ public class ApplicationFormV1 implements ApplicationForm {
|
|||||||
answers[questionNumber] = answer;
|
answers[questionNumber] = answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() throws RemoteException {
|
||||||
|
return answers[0];
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
@ -0,0 +1,55 @@
|
|||||||
|
package implementations;
|
||||||
|
import exceptions.InvalidCredentialsException;
|
||||||
|
import exceptions.InvalidSessionIDException;
|
||||||
|
import interfaces.*;
|
||||||
|
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class ApplicationHandlerImpl implements ApplicationHandler {
|
||||||
|
private ArrayList<Long> sessions = new ArrayList<Long>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long login(String username, String password) throws RemoteException, InvalidCredentialsException {
|
||||||
|
// hardcoded username and password (great practice for security)
|
||||||
|
if (username.equals("admin") && password.equals("admin")) {
|
||||||
|
long sessionId = System.currentTimeMillis(); // use current time as session id
|
||||||
|
sessions.add(sessionId);
|
||||||
|
|
||||||
|
System.out.println("User " + username + " logged in with session ID: " + sessionId);
|
||||||
|
|
||||||
|
return sessionId;
|
||||||
|
} else {
|
||||||
|
throw new InvalidCredentialsException("Invalid username or password.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationForm downloadApplicationForm(long sessionID) throws RemoteException, InvalidSessionIDException {
|
||||||
|
if (sessions.contains(sessionID)) {
|
||||||
|
System.out.println("Returning application form for session ID: " + sessionID);
|
||||||
|
return new ApplicationFormV1();
|
||||||
|
} else {
|
||||||
|
throw new InvalidSessionIDException("Invalid session ID.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void submitApplicationForm(long sessionID, ApplicationForm applicationForm) throws RemoteException, InvalidSessionIDException {
|
||||||
|
if (sessions.contains(sessionID)) {
|
||||||
|
String filename = applicationForm.getName().replace("\\s", "_") + ".txt";
|
||||||
|
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
||||||
|
System.out.println("Saving application form to file: " + filename);
|
||||||
|
writer.write(applicationForm.toString());
|
||||||
|
System.out.println("Successfully saved application form to file: " + filename);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Failed to save application form to file:" + filename);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new InvalidSessionIDException("Invalid session ID.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,4 +15,7 @@ public interface ApplicationForm extends Remote {
|
|||||||
|
|
||||||
// method to provide a String answer to a question based off its questionNumber
|
// method to provide a String answer to a question based off its questionNumber
|
||||||
void answerQuestion(int questionNumber, String answer) throws RemoteException, IllegalArgumentException;
|
void answerQuestion(int questionNumber, String answer) throws RemoteException, IllegalArgumentException;
|
||||||
|
|
||||||
|
// method to return the applicant's name, for use in naming application files
|
||||||
|
String getName() throws RemoteException;
|
||||||
}
|
}
|
||||||
|
14
year4/semester2/CT414/assignments/assignment1/code/src/run.sh
Executable file
14
year4/semester2/CT414/assignments/assignment1/code/src/run.sh
Executable file
@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
src_path="/home/andrew/edu/year4/semester2/CT414/assignments/assignment1/code/src"
|
||||||
|
|
||||||
|
# compile code
|
||||||
|
javac -d . server/*.java client/*.java interfaces/*.java implementations/*.java
|
||||||
|
|
||||||
|
# start rmi registry
|
||||||
|
rmiregistry -J-Djava.rmi.server.codebase=file:$src_path &
|
||||||
|
trap "kill $! 2>/dev/null" EXIT
|
||||||
|
|
||||||
|
# start server
|
||||||
|
java -cp $src_path -Djava.rmi.server.codebase=file:$src_path/ server.ApplicationServer &
|
||||||
|
trap "kill $! 2>/dev/null" EXIT
|
@ -0,0 +1,31 @@
|
|||||||
|
package server;
|
||||||
|
|
||||||
|
import implementations.ApplicationHandlerImpl;
|
||||||
|
|
||||||
|
import java.rmi.Naming;
|
||||||
|
import java.rmi.registry.LocateRegistry;
|
||||||
|
import java.rmi.registry.Registry;
|
||||||
|
import java.rmi.server.UnicastRemoteObject;
|
||||||
|
|
||||||
|
public class ApplicationServer {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
// start RMI registry
|
||||||
|
Registry registry = LocateRegistry.getRegistry();
|
||||||
|
|
||||||
|
// create applicationhandlerstub and bind it
|
||||||
|
ApplicationHandlerImpl applicationHandler = new ApplicationHandlerImpl();
|
||||||
|
System.out.println("Instance of ApplicationHandlerImpl created");
|
||||||
|
|
||||||
|
ApplicationHandlerImpl stub = (ApplicationHandlerImpl) UnicastRemoteObject.exportObject(applicationHandler, 0);
|
||||||
|
Naming.rebind("ApplicationHandler", stub);
|
||||||
|
System.out.println("Name rebind completed");
|
||||||
|
|
||||||
|
System.out.println("Application Server up and running");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.out.println("Failed to start server");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user