Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- try {
- if (System.getSecurityManager() == null) {
- System.setSecurityManager(new SecurityManager());
- }
- Client client = new Client();
- //System.out.println(client.serverInterface().Login());
- System.out.println("Pinging Server...." + client.serverInterface().Ping());
- int logged_in = 0;
- int exit_flag = 0;
- do {
- //not logged in
- if (logged_in == 0) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("" +
- "1 - Register.\n" +
- "2 - Login.\n" +
- "3 - Exit.");
- try {
- switch (Integer.parseInt(scanner.nextLine())) {
- //Register
- case 1:
- int register_flag = 0;
- do {
- String username;
- String name;
- String email;
- String password;
- String re_password;
- System.out.println("###### Register Menu #######");
- System.out.println("Please enter you USERNAME ");
- username = scanner.nextLine();
- System.out.println("Please enter you NAME: ");
- name = scanner.nextLine();
- System.out.println("Please enter you email (Required for login)");
- email = scanner.nextLine();
- int password_flag = 0;
- try {
- MessageDigest hash_function = MessageDigest.getInstance("SHA-256");
- do {
- System.out.println("Please enter you password: (Required for login)");
- hash_function.update(scanner.nextLine().getBytes());
- password = new String(hash_function.digest());
- System.out.println("Please re-enter your password: ");
- hash_function.update(scanner.nextLine().getBytes());
- re_password = new String(hash_function.digest());
- if (password.equals(re_password)) {
- password_flag = 1;
- } else System.out.println("Passwords do NOT match! Try again.");
- } while (password_flag == 0);
- } catch (NoSuchAlgorithmException nsa) {
- nsa.printStackTrace();
- break;
- }
- System.out.println("Is the following information correct?");
- System.out.println("Username ----> " + username);
- System.out.println("Name ----> " + name);
- System.out.println("Email ----> " + email);
- System.out.println("Yes(Y) / No(N)");
- String inf_correct = scanner.nextLine();
- if (inf_correct.equals("Y") || inf_correct.equals("y")) {
- register_flag = 1;
- try {
- client.serverInterface().Ping();
- if (client.serverInterface().RegisterUser(username, name, email, new String(Base64.getEncoder().encode(password.getBytes())))) {
- System.out.println("User registration Successful.");
- } else
- System.out.println("User could not be registered. Please try again.");
- } catch (RemoteException re) {
- System.out.println("Server is no longer reachable. Please try again later.");
- break;
- }
- }
- } while (register_flag == 0);
- break;
- //login
- case 2:
- try {
- MessageDigest hash_function = MessageDigest.getInstance("SHA-256");
- System.out.println("###### Login Menu #######");
- System.out.println("Please enter your email:");
- String email = scanner.nextLine();
- System.out.println("Please enter your password: ");
- hash_function.update(scanner.nextLine().getBytes());
- String hashed_password = new String(hash_function.digest());
- SecureRandom random = new SecureRandom();
- int port = Math.abs(random.nextInt()%65000+1109);
- Remote remote = client.serverInterface().Login(email, Base64.getEncoder().encodeToString(hashed_password.getBytes()), port);
- client.rmiServerLoggedInInterface = (ServerLoggedInInterface) remote;
- System.out.println("EMAIL" + client.rmiServerLoggedInInterface.getEmail());
- logged_in = 1;
- } catch (RemoteException re) {
- System.out.println("Login credentials are incorrect. Please Try Again.");
- } catch (NoSuchAlgorithmException nsa) {
- System.out.println("Could not satisfy minimum security requirements. Aborting");
- System.exit(5);
- }
- break;
- //Exit
- case 3:
- exit_flag = 1;
- break;
- //unknow option
- default:
- System.out.println("Unknown option. Try Again.");
- break;
- }
- } catch (NumberFormatException nfe) {
- System.out.println("Unexpected token received. Check your answer.");
- }
- }
- //logged in
- else {
- System.out.println("#### Hello, " + client.serverLoggedInInterface().getEmail());
- System.out.println("### Main Menu ###");
- System.out.println("1 - Ver os meus exericios submetidos.");
- System.out.println("2 - Ver as minhas soluções submetidas.");
- System.out.println("3 - Submeter novo exercicio.");
- System.out.println("4 - Apagar exercicio.");
- System.out.println("5 - Propor solução.");
- System.out.println("6 - Inserir UC.");
- System.out.println("7 - Consultar UCs.");
- System.out.println("8 - Consultar exercicios em aberto por UC.");
- System.out.println("9 - Consultar exercicios encerrados por UC.");
- System.out.println("10 - Procurar exercicio.");
- System.out.println("11 - Logout.");
- try {
- Scanner scanner = new Scanner(System.in);
- switch (Integer.parseInt(scanner.nextLine())){
- //Ver os meus exercicios submetidos.
- case 1:
- System.out.println(client.serverLoggedInInterface().getExercisesSubmitedByAuthor());
- break;
- //ver as minhas soluções submetidas
- case 2:
- System.out.println(client.serverLoggedInInterface().getSolutionsSubmittedByAuthor());
- break;
- // Submeter novo exercicio.
- case 3:
- System.out.println("### New Exercise Menu ###");
- System.out.println("Introduza o nome da UC: ");
- String uc_name = scanner.nextLine();
- System.out.println("Introduza o nome do exercicio: ");
- String ex_name = scanner.nextLine();
- System.out.println("Introduza uma descricao do exercicio: (Opcional)");
- String ex_description = scanner.nextLine();
- System.out.println("Introduza o contéudo do exercicio: ");
- String exercice_contents = scanner.nextLine();
- if (client.serverLoggedInInterface().submitNewExercise(uc_name, ex_name, ex_description, exercice_contents)){
- System.out.println("Exercicio adicionado com sucesso!");
- } else System.out.println("Erro ao inserir exercicio. Tente Novemente.");
- break;
- //Apagar exercicio.
- case 4:
- break;
- //Propor solução.
- case 5:
- System.out.println("### Inserir Solucao ###");
- System.out.println("Introduza o nome da UC: ");
- String ucname = scanner.nextLine();
- System.out.println("Introduza o ID do exercicio:");
- int ex_id = Integer.parseInt(scanner.nextLine());
- System.out.println("Introduza a sua solucao: ");
- String solution = scanner.nextLine();
- if (client.serverLoggedInInterface().submitNewSolution(solution, ex_id, ucname))
- System.out.println("A solução foi correctamente inserida.");
- else System.out.println("Houve um erro ao inserir a solucao ao exercicio.");
- break;
- //Inserir UC.
- case 6:
- System.out.println("### Inserir UC ###");
- System.out.println("Introduza o nome da UC: ");
- String nome_uc = scanner.nextLine();
- System.out.println("Introduza uma descricao da cadeira: (opcional)");
- String descricao_uc = scanner.nextLine();
- if (descricao_uc == null) descricao_uc = "";
- if (client.serverLoggedInInterface().createNewUC(nome_uc, descricao_uc)){
- System.out.println("UC criada com sucesso!");
- } else System.out.println("Ocorreu um erro na criação da UC.");
- break;
- //Consultar UCS com exercicios.
- case 7:
- String uc_with_ex = client.serverLoggedInInterface().ucWithExercises();
- if (uc_with_ex.equals("")){
- System.out.println("Não existem UCs com exercicios.");
- } else System.out.println(uc_with_ex);
- break;
- //Consultar exercicios em abertos por UC.
- case 8:
- //pedir o nome da UC, devolver exercicios em aberto.
- System.out.println("### Consultar exercicios em aberto por UC");
- String uc = scanner.nextLine();
- client.serverLoggedInInterface().getOpenExercises(uc);
- break;
- //Consultar exercicios encerrados por UC.
- case 9:
- //pedir o nome da UC, devolver exercicios encerrados.
- break;
- //Procurar exercicio.
- case 10:
- break;
- case 11:
- System.out.println("Please comeback soon! Bye!");
- client.serverLoggedInInterface().logOut();
- logged_in = 0;
- exit_flag = 1;
- break;
- default:
- break;
- }
- }catch (NumberFormatException nfe){
- System.out.println("Wrong input given. Try again.");
- }
- }
- } while (exit_flag == 0);
- } catch (ConnectException cn) {
- System.out.println("Could not connect to server. Aborting!");
- cn.printStackTrace();
- System.exit(1);
- } catch (RemoteException rm) {
- rm.printStackTrace();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement