Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package iVotas.Model;
- import RMI.RMIinterface;
- import RMI.src.Classes.Departamento;
- import sun.rmi.runtime.Log;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.rmi.Naming;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.server.UnicastRemoteObject;
- import java.util.ArrayList;
- public class iVotasBean extends UnicastRemoteObject {
- private static final long serialVersionUID = 1L;
- private RMIinterface serverRMI;
- private String username;
- private String password;
- public iVotasBean() throws RemoteException {
- this.ligarRMI();
- }
- private void ligarRMI(){
- boolean ver = false;
- while(!ver){
- try {
- // TODO: Make lookup dynamic
- this.serverRMI = (RMIinterface) LocateRegistry.getRegistry(6789).lookup("HelloRMI");
- ver = true;
- this.serverRMI.sayHello();
- } catch (RemoteException | NotBoundException e) {
- System.out.println("Primary is now down.");
- }
- }
- }
- public boolean userLogin() throws RemoteException {
- return serverRMI.userLogin(this.username,this.password);
- }
- public boolean logged(String user) throws RemoteException {
- return serverRMI.logged(user);
- }
- public ArrayList<String> getAllUsers() throws RemoteException {
- return serverRMI.getAllUsers(); // are you going to throw all exceptions?
- }
- public RMIinterface getServerRMI() {
- return serverRMI;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public boolean login(String user, String pass){
- while(true){
- try{
- return this.serverRMI.userLogin(user, pass);
- }catch(RemoteException re){
- this.ligarRMI();
- }
- }
- }
- public int CriaDep(String nomeDep){
- try {
- ArrayList<Departamento> departamentos;
- while (true) {
- try {
- departamentos = this.serverRMI.getListaDepartamentos();
- break;
- } catch (RemoteException ignored) {
- this.ligarRMI();
- }
- }
- for (Departamento auxDep : departamentos)
- if ((nomeDep.toUpperCase()).equals(auxDep.getNome().toUpperCase())){
- System.out.println("Departamento já existente!");
- return 0;
- }
- Departamento dep = new Departamento(nomeDep,new ArrayList<>());
- while (true) {
- try {
- this.serverRMI.addDepartamento(dep);
- return 1;
- } catch (RemoteException ignored) {
- this.ligarRMI();
- }
- }
- }catch (Exception e){
- System.out.println(e.getMessage());
- }
- return 0;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement