Advertisement
Guest User

Untitled

a guest
Dec 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package iVotas.Model;
  2.  
  3. import RMI.RMIinterface;
  4. import RMI.src.Classes.Departamento;
  5. import sun.rmi.runtime.Log;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.net.MalformedURLException;
  10. import java.rmi.Naming;
  11. import java.rmi.NotBoundException;
  12. import java.rmi.RemoteException;
  13. import java.rmi.registry.LocateRegistry;
  14. import java.rmi.server.UnicastRemoteObject;
  15. import java.util.ArrayList;
  16.  
  17. public class iVotasBean extends UnicastRemoteObject {
  18.  
  19. private static final long serialVersionUID = 1L;
  20.  
  21. private RMIinterface serverRMI;
  22. private String username;
  23. private String password;
  24.  
  25. public iVotasBean() throws RemoteException {
  26. this.ligarRMI();
  27. }
  28.  
  29. private void ligarRMI(){
  30. boolean ver = false;
  31. while(!ver){
  32. try {
  33. // TODO: Make lookup dynamic
  34. this.serverRMI = (RMIinterface) LocateRegistry.getRegistry(6789).lookup("HelloRMI");
  35. ver = true;
  36. this.serverRMI.sayHello();
  37. } catch (RemoteException | NotBoundException e) {
  38. System.out.println("Primary is now down.");
  39. }
  40. }
  41. }
  42.  
  43. public boolean userLogin() throws RemoteException {
  44. return serverRMI.userLogin(this.username,this.password);
  45. }
  46.  
  47. public boolean logged(String user) throws RemoteException {
  48. return serverRMI.logged(user);
  49. }
  50.  
  51. public ArrayList<String> getAllUsers() throws RemoteException {
  52. return serverRMI.getAllUsers(); // are you going to throw all exceptions?
  53. }
  54.  
  55. public RMIinterface getServerRMI() {
  56. return serverRMI;
  57. }
  58.  
  59. public String getUsername() {
  60. return username;
  61. }
  62.  
  63. public void setUsername(String username) {
  64. this.username = username;
  65. }
  66.  
  67. public boolean login(String user, String pass){
  68. while(true){
  69. try{
  70. return this.serverRMI.userLogin(user, pass);
  71. }catch(RemoteException re){
  72. this.ligarRMI();
  73. }
  74. }
  75. }
  76.  
  77. public int CriaDep(String nomeDep){
  78. try {
  79. ArrayList<Departamento> departamentos;
  80. while (true) {
  81. try {
  82. departamentos = this.serverRMI.getListaDepartamentos();
  83. break;
  84. } catch (RemoteException ignored) {
  85. this.ligarRMI();
  86. }
  87. }
  88. for (Departamento auxDep : departamentos)
  89. if ((nomeDep.toUpperCase()).equals(auxDep.getNome().toUpperCase())){
  90. System.out.println("Departamento já existente!");
  91. return 0;
  92. }
  93. Departamento dep = new Departamento(nomeDep,new ArrayList<>());
  94. while (true) {
  95. try {
  96. this.serverRMI.addDepartamento(dep);
  97. return 1;
  98. } catch (RemoteException ignored) {
  99. this.ligarRMI();
  100. }
  101. }
  102. }catch (Exception e){
  103. System.out.println(e.getMessage());
  104. }
  105. return 0;
  106. }
  107.  
  108. public String getPassword() {
  109. return password;
  110. }
  111.  
  112. public void setPassword(String password) {
  113. this.password = password;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement