示例#1
0
 /**
  * @throws RemoteException
  * @throws UnknownHostException
  */
 public void stabilize() throws RemoteException, UnknownHostException {
   ChordMessageInterface succ = rmiChord(successor.getIp(), successor.getPort());
   Finger x = succ.getPredecessor();
   if (ino(x.getId(), i, successor.getId())) {
     successor = x;
   }
   InetAddress ip = InetAddress.getLocalHost();
   succ = rmiChord(successor.getIp(), successor.getPort());
   succ.notify(new Finger(ip.getHostAddress(), port, i));
 }
 public RmiServer() throws RemoteException {
   try {
     address = (InetAddress.getLocalHost()).toString();
   } catch (Exception e) {
     System.out.println("can't get inet address.");
   }
   int port1 = 3232;
   System.out.println("this address=" + address + ",port=" + port1);
   try {
     registry1 = LocateRegistry.createRegistry(port1);
     registry1.rebind("rmiServer1", this);
   } catch (RemoteException e) {
     System.out.println("remote exception" + e);
   }
 }
示例#3
0
  /**
   * @param _port
   * @param id
   * @throws RemoteException
   * @throws UnknownHostException
   */
  public Chord(int _port, int id) throws RemoteException, UnknownHostException {
    finger = new Finger[(1 << M)]; // 1 << M = 2^(M)
    // TODO: set the fingers in the array to null
    i = id;
    port = _port;
    // TODO: determine the current IP of the machine

    predecessor = null;
    InetAddress ip = InetAddress.getLocalHost();
    successor = new Finger(ip.getHostAddress(), i, i);
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(
        new TimerTask() {
          @Override
          public void run() {
            try {
              stabilize();
            } catch (RemoteException | UnknownHostException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
            fixFingers();
            checkPredecessor();
          }
        },
        500,
        500);
    try {
      // create the registry and bind the name and object.
      System.out.println("Starting RMI at port=" + port);
      registry = LocateRegistry.createRegistry(port);
      registry.rebind("Chord", this);
    } catch (RemoteException e) {
      throw e;
    }
  }
示例#4
0
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == Connect) {
          if (nbr < 3) {
            if (nom.getText().length() == 0
                || pwd.getPassword().length == 0
                || jTextField1.getText().length() == 0) {
              JOptionPane.showMessageDialog(
                  null,
                  "Vous devez donner votre pseudonyme, votre mot de passe ainsi que l'adresse du serveur de chat",
                  "Informations manquantes",
                  JOptionPane.WARNING_MESSAGE);
              nbr++;
            } else {
              try {
                J = (Contrat) Naming.lookup("rmi://" + jTextField1.getText() + ":1099/JChat");
              } catch (RemoteException ex4) {
                JOptionPane.showMessageDialog(
                    null,
                    "La connexion au serveur a échoué",
                    "Serveur non démarré",
                    JOptionPane.WARNING_MESSAGE);
                System.out.println(ex4);
                nbr++;
              } catch (MalformedURLException ex4) {
                JOptionPane.showMessageDialog(
                    null, "Connexion 2", "Erreur de connexion", JOptionPane.WARNING_MESSAGE);
                nbr++;
              } catch (NotBoundException ex4) {
                JOptionPane.showMessageDialog(
                    null,
                    "Serveur non joignable",
                    "Erreur de connexion",
                    JOptionPane.WARNING_MESSAGE);
                System.out.println(ex4);
                System.exit(0);
              }
              if (J != null) {
                try { // de la méthode connect distante
                  // Récupération de l'adresse de la machine du client
                  try {
                    adr = InetAddress.getLocalHost().getHostName();
                  } catch (UnknownHostException ex3) {
                  }
                  // Si le nom et le mot de passe fournies par le client sont valides
                  // Envoi du nom et mot de passe pour la vérification de la validité de ce client
                  // ainsi que le num de port
                  // , adresse de la machine du client et nom de l'objet distant du client pour
                  // pouvoir l'invoquer ultérieurementulté
                  num_port = J.get_num_port();
                  if (J.connect(nom.getText(), new String(pwd.getPassword()), adr, num_port)) {
                    srv_Adr = jTextField1.getText();
                    srv_state = true;
                    // fermeture de la fenetre de connexion
                    fermer();
                    try {
                      java.rmi.registry.LocateRegistry.createRegistry(num_port);
                      System.out.println("Ecoute sur le port : " + num_port);
                      // Placement de l'objet distant du client sur sa machine : localhost dans le
                      // registre pour le numéro de port spécifié
                      Naming.rebind("//" + adr + ":" + num_port + "/" + nom.getText(), retourObj());
                      System.out.println(Naming.list("//" + adr + ":" + num_port)[0]);
                    } catch (MalformedURLException ex2) {
                      System.out.println(ex2);
                    } catch (RemoteException ex1) {
                      System.out.println(ex1);
                    }
                    // Constructeur graphique de la classe Client
                    C = new Client(nom.getText(), new String(pwd.getPassword()));
                    Essai essai = new Essai();
                    SplashWindowApp splash = new SplashWindowApp(C, 2000, essai);
                    if (J.getnbrcon() >= 2) {
                      System.out.println("Mise à jour de la liste du nouveau connecté");
                      // Mise à jour de la liste des destinataires du nouveau connecté
                      for (int i = 0; i < J.getnbrcon(); i++)
                        if (J.list_con()[i].compareTo(nom.getText()) != 0) C.ajout(J.list_con()[i]);
                    }
                  } else {
                    JOptionPane.showMessageDialog(
                        null, "Vérifiez votre pseudonyme ou mot de passe");
                    nbr++;
                  }

                } catch (RemoteException ex) {
                  System.out.println(ex);
                }
              }
            }
            if (nbr == 3) {
              JOptionPane.showMessageDialog(null, "Nombre maximum d'essai est atteint");
              System.exit(0);
            }
          }
        } else {
          if (e.getSource() == Annuler) System.exit(0);
        }
      }