Advertisement
Guest User

Untitled

a guest
Apr 9th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.31 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. try {
  4.  
  5. if (System.getSecurityManager() == null) {
  6. System.setSecurityManager(new SecurityManager());
  7. }
  8.  
  9. Client client = new Client();
  10. //System.out.println(client.serverInterface().Login());
  11. System.out.println("Pinging Server...." + client.serverInterface().Ping());
  12.  
  13. int logged_in = 0;
  14. int exit_flag = 0;
  15.  
  16. do {
  17. //not logged in
  18. if (logged_in == 0) {
  19. Scanner scanner = new Scanner(System.in);
  20. System.out.println("" +
  21. "1 - Register.\n" +
  22. "2 - Login.\n" +
  23. "3 - Exit.");
  24. try {
  25. switch (Integer.parseInt(scanner.nextLine())) {
  26. //Register
  27. case 1:
  28. int register_flag = 0;
  29. do {
  30. String username;
  31. String name;
  32. String email;
  33. String password;
  34. String re_password;
  35.  
  36. System.out.println("###### Register Menu #######");
  37. System.out.println("Please enter you USERNAME ");
  38. username = scanner.nextLine();
  39. System.out.println("Please enter you NAME: ");
  40. name = scanner.nextLine();
  41. System.out.println("Please enter you email (Required for login)");
  42. email = scanner.nextLine();
  43.  
  44. int password_flag = 0;
  45. try {
  46. MessageDigest hash_function = MessageDigest.getInstance("SHA-256");
  47.  
  48. do {
  49.  
  50. System.out.println("Please enter you password: (Required for login)");
  51. hash_function.update(scanner.nextLine().getBytes());
  52. password = new String(hash_function.digest());
  53. System.out.println("Please re-enter your password: ");
  54. hash_function.update(scanner.nextLine().getBytes());
  55. re_password = new String(hash_function.digest());
  56.  
  57. if (password.equals(re_password)) {
  58. password_flag = 1;
  59. } else System.out.println("Passwords do NOT match! Try again.");
  60.  
  61.  
  62. } while (password_flag == 0);
  63.  
  64. } catch (NoSuchAlgorithmException nsa) {
  65. nsa.printStackTrace();
  66. break;
  67. }
  68. System.out.println("Is the following information correct?");
  69. System.out.println("Username ----> " + username);
  70. System.out.println("Name ----> " + name);
  71. System.out.println("Email ----> " + email);
  72. System.out.println("Yes(Y) / No(N)");
  73. String inf_correct = scanner.nextLine();
  74. if (inf_correct.equals("Y") || inf_correct.equals("y")) {
  75. register_flag = 1;
  76. try {
  77. client.serverInterface().Ping();
  78.  
  79. if (client.serverInterface().RegisterUser(username, name, email, new String(Base64.getEncoder().encode(password.getBytes())))) {
  80. System.out.println("User registration Successful.");
  81. } else
  82. System.out.println("User could not be registered. Please try again.");
  83.  
  84. } catch (RemoteException re) {
  85. System.out.println("Server is no longer reachable. Please try again later.");
  86. break;
  87. }
  88. }
  89.  
  90. } while (register_flag == 0);
  91.  
  92. break;
  93.  
  94. //login
  95. case 2:
  96.  
  97. try {
  98. MessageDigest hash_function = MessageDigest.getInstance("SHA-256");
  99.  
  100. System.out.println("###### Login Menu #######");
  101. System.out.println("Please enter your email:");
  102. String email = scanner.nextLine();
  103. System.out.println("Please enter your password: ");
  104. hash_function.update(scanner.nextLine().getBytes());
  105. String hashed_password = new String(hash_function.digest());
  106. SecureRandom random = new SecureRandom();
  107. int port = Math.abs(random.nextInt()%65000+1109);
  108. Remote remote = client.serverInterface().Login(email, Base64.getEncoder().encodeToString(hashed_password.getBytes()), port);
  109. client.rmiServerLoggedInInterface = (ServerLoggedInInterface) remote;
  110. System.out.println("EMAIL" + client.rmiServerLoggedInInterface.getEmail());
  111.  
  112. logged_in = 1;
  113.  
  114. } catch (RemoteException re) {
  115. System.out.println("Login credentials are incorrect. Please Try Again.");
  116. } catch (NoSuchAlgorithmException nsa) {
  117. System.out.println("Could not satisfy minimum security requirements. Aborting");
  118. System.exit(5);
  119. }
  120. break;
  121.  
  122. //Exit
  123. case 3:
  124. exit_flag = 1;
  125. break;
  126. //unknow option
  127. default:
  128. System.out.println("Unknown option. Try Again.");
  129. break;
  130. }
  131. } catch (NumberFormatException nfe) {
  132. System.out.println("Unexpected token received. Check your answer.");
  133. }
  134. }
  135. //logged in
  136. else {
  137. System.out.println("#### Hello, " + client.serverLoggedInInterface().getEmail());
  138. System.out.println("### Main Menu ###");
  139. System.out.println("1 - Ver os meus exericios submetidos.");
  140. System.out.println("2 - Ver as minhas soluções submetidas.");
  141. System.out.println("3 - Submeter novo exercicio.");
  142. System.out.println("4 - Apagar exercicio.");
  143. System.out.println("5 - Propor solução.");
  144. System.out.println("6 - Inserir UC.");
  145. System.out.println("7 - Consultar UCs.");
  146. System.out.println("8 - Consultar exercicios em aberto por UC.");
  147. System.out.println("9 - Consultar exercicios encerrados por UC.");
  148. System.out.println("10 - Procurar exercicio.");
  149. System.out.println("11 - Logout.");
  150.  
  151. try {
  152. Scanner scanner = new Scanner(System.in);
  153. switch (Integer.parseInt(scanner.nextLine())){
  154.  
  155. //Ver os meus exercicios submetidos.
  156. case 1:
  157. System.out.println(client.serverLoggedInInterface().getExercisesSubmitedByAuthor());
  158. break;
  159.  
  160. //ver as minhas soluções submetidas
  161. case 2:
  162. System.out.println(client.serverLoggedInInterface().getSolutionsSubmittedByAuthor());
  163. break;
  164. // Submeter novo exercicio.
  165. case 3:
  166. System.out.println("### New Exercise Menu ###");
  167. System.out.println("Introduza o nome da UC: ");
  168. String uc_name = scanner.nextLine();
  169. System.out.println("Introduza o nome do exercicio: ");
  170. String ex_name = scanner.nextLine();
  171. System.out.println("Introduza uma descricao do exercicio: (Opcional)");
  172. String ex_description = scanner.nextLine();
  173. System.out.println("Introduza o contéudo do exercicio: ");
  174. String exercice_contents = scanner.nextLine();
  175.  
  176. if (client.serverLoggedInInterface().submitNewExercise(uc_name, ex_name, ex_description, exercice_contents)){
  177. System.out.println("Exercicio adicionado com sucesso!");
  178. } else System.out.println("Erro ao inserir exercicio. Tente Novemente.");
  179.  
  180. break;
  181.  
  182. //Apagar exercicio.
  183. case 4:
  184. break;
  185.  
  186. //Propor solução.
  187. case 5:
  188. System.out.println("### Inserir Solucao ###");
  189. System.out.println("Introduza o nome da UC: ");
  190. String ucname = scanner.nextLine();
  191. System.out.println("Introduza o ID do exercicio:");
  192. int ex_id = Integer.parseInt(scanner.nextLine());
  193. System.out.println("Introduza a sua solucao: ");
  194. String solution = scanner.nextLine();
  195.  
  196. if (client.serverLoggedInInterface().submitNewSolution(solution, ex_id, ucname))
  197. System.out.println("A solução foi correctamente inserida.");
  198. else System.out.println("Houve um erro ao inserir a solucao ao exercicio.");
  199.  
  200. break;
  201. //Inserir UC.
  202. case 6:
  203. System.out.println("### Inserir UC ###");
  204. System.out.println("Introduza o nome da UC: ");
  205. String nome_uc = scanner.nextLine();
  206. System.out.println("Introduza uma descricao da cadeira: (opcional)");
  207. String descricao_uc = scanner.nextLine();
  208.  
  209. if (descricao_uc == null) descricao_uc = "";
  210.  
  211. if (client.serverLoggedInInterface().createNewUC(nome_uc, descricao_uc)){
  212. System.out.println("UC criada com sucesso!");
  213. } else System.out.println("Ocorreu um erro na criação da UC.");
  214.  
  215. break;
  216. //Consultar UCS com exercicios.
  217. case 7:
  218. String uc_with_ex = client.serverLoggedInInterface().ucWithExercises();
  219. if (uc_with_ex.equals("")){
  220. System.out.println("Não existem UCs com exercicios.");
  221. } else System.out.println(uc_with_ex);
  222. break;
  223.  
  224. //Consultar exercicios em abertos por UC.
  225. case 8:
  226. //pedir o nome da UC, devolver exercicios em aberto.
  227. System.out.println("### Consultar exercicios em aberto por UC");
  228. String uc = scanner.nextLine();
  229. client.serverLoggedInInterface().getOpenExercises(uc);
  230. break;
  231.  
  232. //Consultar exercicios encerrados por UC.
  233. case 9:
  234. //pedir o nome da UC, devolver exercicios encerrados.
  235. break;
  236.  
  237. //Procurar exercicio.
  238. case 10:
  239. break;
  240.  
  241. case 11:
  242. System.out.println("Please comeback soon! Bye!");
  243. client.serverLoggedInInterface().logOut();
  244. logged_in = 0;
  245. exit_flag = 1;
  246. break;
  247.  
  248. default:
  249. break;
  250.  
  251. }
  252.  
  253. }catch (NumberFormatException nfe){
  254. System.out.println("Wrong input given. Try again.");
  255. }
  256.  
  257.  
  258. }
  259. } while (exit_flag == 0);
  260.  
  261. } catch (ConnectException cn) {
  262. System.out.println("Could not connect to server. Aborting!");
  263. cn.printStackTrace();
  264. System.exit(1);
  265. } catch (RemoteException rm) {
  266. rm.printStackTrace();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement