Client is not increasing

Asked

Viewed 43 times

0

I’m making a distributed system with 2 clients. Being that you are asking the same values for both, and not putting the other waiting.

import java.awt.HeadlessException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;


public class Cliente {
    public static void main(String[] args) throws HeadlessException, NumberFormatException, RemoteException {

        Interface controlador = null;


// ------------ - - - - - - - - - -  -  -  -  -  -   - Inicio: Escolha de Servidor ------------ - - - - - - - - - -  -  -  -  -  -   - 

// Ao abrir o programa, mostra a tela de escolha de servidor (até então, a variável escolha é 0)
//Se 1, se conecta ao serv1. Se 2, se conecta ao serv2. Se 3, sai do programa (If's e else if's abaixo)
//Caso for digitado um numero diferente de 1, 2 ou 3, mostra o que está no ELSE
//Repara que tem um try/catch. Serve para, se o usuario digitar algo diferente de um NUMERO (Inteiro, neste caso) ele caia pro CATCH (forçando a digitar um numero)
//Fora isso tem um laço "do while" que fica rodando pra sempre, forçando o usuário a digitar 1, 2 ou 3 (3 na verdade não quebra o laço, mas como é um "exit", fecha o programa)

        int opcao = 0;

        do {

            try {

                opcao = Integer.parseInt(JOptionPane.showInputDialog(null, "  1 - Servidor 1.\n\n  2 - Servidor 2.\n\n  3 - Sair da aplicação.", "Escolha de Servidor", JOptionPane.PLAIN_MESSAGE));
                System.out.println(opcao);


                if (opcao == 1) {                   
                    controlador = (Interface) Naming.lookup("//localhost/rmi");// Rmiregistry (Servidor1)
                    JOptionPane.showMessageDialog(null, "Conectado ao servidor 1.");
                } else if(opcao == 2) {
                    controlador = (Interface) Naming.lookup("//192.168.1.131/rmi");// Rmiregistry (Servidor2)
                    JOptionPane.showMessageDialog(null, "Conectado ao servidor 2.");
                } else if(opcao == 3) {
                    System.exit(2);
                } else {
                    JOptionPane.showMessageDialog(null, "Por favor, digite apenas 1 ou 2", "Opção Inválida", JOptionPane.ERROR_MESSAGE);
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Por favor, entre apenas com números ( 1, 2 e 3).", "Opção Inválida", JOptionPane.ERROR_MESSAGE);
                //System.err.println("Problemas de localização! " + e);
                e.printStackTrace();
                //System.exit(2);
            } 

        }   while(opcao < 1 || opcao > 2);

// ------------ - - - - - - - - - -  -  -  -  -  -   - Fim da  Escolha de Servidor ------------ - - - - - - - - - -  -  -  -  -  -   - 




// ------------ - - - - - - - - - -  -  -  -  -  -   - Inicio do Cliente 1  ------------ - - - - - - - - - -  -  -  -  -  -   -



        if(controlador.clientRole() == 0){
            controlador.incrementarCliente();

            try {

                String comp = JOptionPane.showInputDialog(null, "Digite o valor do comprimento do paralelepípedo: ", "CLIENTE 1", JOptionPane.PLAIN_MESSAGE);
                controlador.setComprimento (Float.parseFloat(comp));
                JOptionPane.showMessageDialog(null, "Valor do Comprimento: "+controlador.getComprimento());

                String altura = JOptionPane.showInputDialog(null, "Digite o valor da altura do paralelepípedo: ", "CLIENTE 1", JOptionPane.PLAIN_MESSAGE);
                controlador.setAltura(Float.parseFloat(altura));
                JOptionPane.showMessageDialog(null, "Valor da altura: "+controlador.getAltura());               

                //Cliente 1 informa o valor pago por horas trabalhadas e a quantidade de Horas Trabalhadas. 

                //o servidor retornará  o valor que o cliente receberá no dia para os clientes.



                while(true) {                               
                    if(controlador.getLargura() == -9999) {
                        System.out.println("Aguardando parametros do segundo cliente.");
                        JOptionPane.showMessageDialog(null, "Aguardando parametros do segundo cliente.", "CLIENTE 1", JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        break;
                    }
                }           

                JOptionPane.showMessageDialog(null," Cliente 1 Informou o valor " +controlador.getComprimento()+ " de comprimento. O valor de altura foi  "+controlador.getAltura()+ " , o valor do volume será: "+controlador.getAltura() * controlador.getComprimento() * controlador.getLargura(), "CLIENTE 1" , JOptionPane.INFORMATION_MESSAGE);

            } catch (RemoteException re) {
                System.err.println("Problemas com a chamada remota! " + re);
                re.printStackTrace();
                System.exit(3);
            }
// ------------ - - - - - - - - - -  -  -  -  -  -   - Fim do Cliente 1 ------------ - - - - - - - - - -  -  -  -  -  -   -



// ------------ - - - - - - - - - -  -  -  -  -  -   - Inicio do Cliente 2  ------------ - - - - - - - - - -  -  -  -  -  -   -



    try {


        while(true){                        
            if (controlador.getAltura() == 0){
                System.out.println("Aguardando parametros do primeiro cliente.");
                JOptionPane.showMessageDialog(null, "Aguardando parametros do primeiro cliente.", "CLIENTE 2", JOptionPane.INFORMATION_MESSAGE);
            } else {
                break;
            }
        }

        do {
            try {

                String largura = JOptionPane.showInputDialog(null, "Digite o valor da largura do paralelepípedo: ", "CLIENTE 2", JOptionPane.PLAIN_MESSAGE);
                controlador.setLargura(Float.parseFloat(largura));
                JOptionPane.showMessageDialog(null, "Valor da largura: " +controlador.getLargura());    

                //if (+controlador.getHt() <controlador.getHn() )  {

                    //JOptionPane.showMessageDialog(null, "Número de horas trabalhadas menor que horas necessárias");
                    //break;}


                JOptionPane.showMessageDialog(null,"O Cliente 2 informou o valor " +controlador.getLargura()+ " de altura e o valor do volume do paralelepipedo será: " +controlador.getAltura() * controlador.getComprimento() * controlador.getLargura(), "CLIENTE 2" , JOptionPane.INFORMATION_MESSAGE);
                controlador.resetar();
                break;

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Digite um valor válido! ", "CLIENTE 2", JOptionPane.ERROR_MESSAGE);
            }
        } while(true);

    } catch (RemoteException re) {
        System.err.println("Problemas com a chamada remota! " + re);
        re.printStackTrace();
        System.exit(3);

//------------ - - - - - - - - - -  -  -  -  -  -   - Fim: Cliente (2) ------------ - - - - - - - - - -  -  -  -  -  -   -
    }
        }}}


// ------------ - - - - - - - - - -  -  -  -  -  -   - Fim: Cliente (2) --------
  • The question is unclear. What is supposed to happen in the program? What did you expect to happen but is not working properly?

  • So the client was supposed to type the length, but he’s asking for length for both users. And both are customers 1.

  • You are entering 2 times in the first client’s if?

  • I’m not quite sure what the architecture of your program is, but if client 1 has this if(controlador.clientRole() == 0), in the client part 2 should not have an identico if for client 2?

  • yes, but that doesn’t happen :/

  • This is the type of problem that is solved by debugging the application and checking some things, such as: the variable opcao is populated after the user supposedly chooses an option? With what value? You are entering the if expected? Are these repeat loops working as they should? Anyway, it’s much easier and more productive than contemplating the code and mentally debugging. If after all, you can’t solve it, at least you can come back here and detail more specifically where unexpected behavior happens.

  • Nothing appears on the Bugger :/

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.