public static void main(String args[]) { if (args.length == 0 || !args[0].startsWith("rmi:")) { System.err.println("Usage: java FibonacciClient rmi://host.domain:port/fibonacci number"); return; } try { Object o = Naming.lookup(args[0]); Fibonacci calculator = (Fibonacci) o; for (int i = 1; i < args.length; i++) { try { BigInteger index = new BigInteger(args[i]); BigInteger f = calculator.getFibonacci(index); System.out.println("The " + args[i] + "th Fibonacci number is " + f); } catch (NumberFormatException e) { System.err.println(args[i] + "is not an integer."); } } } catch (MalformedURLException ex) { System.err.println(args[0] + " is not a valid RMI URL"); } catch (RemoteException ex) { System.err.println("Remote object threw exception " + ex); } catch (NotBoundException ex) { System.err.println("Could not find the requested remote object on the server"); } }
public static void main(String[] args) { String host = args[0]; int port = Integer.parseInt(args[1]); long id = Long.parseLong(args[2]); String character = args[3]; long actorId = Long.parseLong(args[4]); // Install an RMISecurityManager, if there is not a // SecurityManager already installed if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } String name = "rmi://" + host + ":" + port + "/MovieDatabase"; try { MovieDatabase db = (MovieDatabase) Naming.lookup(name); db.noteCharacter(id, character, actorId); Movie movie = db.getMovie(id); out.println(movie.getTitle()); for (Map.Entry entry : movie.getCharacters().entrySet()) { out.println(" " + entry.getKey() + "\t" + entry.getValue()); } } catch (RemoteException | NotBoundException | MalformedURLException ex) { ex.printStackTrace(System.err); } }
/** * Create a MonitoredHostProvider instance using the given HostIdentifier. * * @param hostId the host identifier for this MonitoredHost * @throws MonitorException Thrown on any error encountered while communicating with the remote * host. */ public MonitoredHostProvider(HostIdentifier hostId) throws MonitorException { this.hostId = hostId; this.listeners = new ArrayList(); this.interval = DEFAULT_POLLING_INTERVAL; this.activeVms = new HashSet(); String rmiName; String sn = serverName; String path = hostId.getPath(); if ((path != null) && (path.length() > 0)) { sn = path; } if (hostId.getPort() != -1) { rmiName = "rmi://" + hostId.getHost() + ":" + hostId.getPort() + sn; } else { rmiName = "rmi://" + hostId.getHost() + sn; } try { remoteHost = (RemoteHost) Naming.lookup(rmiName); } catch (RemoteException e) { /* * rmi registry not available * * Access control exceptions, where the rmi server refuses a * connection based on policy file configuration, come through * here on the client side. Unfortunately, the RemoteException * doesn't contain enough information to determine the true cause * of the exception. So, we have to output a rather generic message. */ String message = "RMI Registry not available at " + hostId.getHost(); if (hostId.getPort() == -1) { message = message + ":" + java.rmi.registry.Registry.REGISTRY_PORT; } else { message = message + ":" + hostId.getPort(); } if (e.getMessage() != null) { throw new MonitorException(message + "\n" + e.getMessage(), e); } else { throw new MonitorException(message, e); } } catch (NotBoundException e) { // no server with given name String message = e.getMessage(); if (message == null) message = rmiName; throw new MonitorException("RMI Server " + message + " not available", e); } catch (MalformedURLException e) { // this is a programming problem e.printStackTrace(); throw new IllegalArgumentException("Malformed URL: " + rmiName); } this.vmManager = new RemoteVmManager(remoteHost); this.timer = new Timer(true); }
/** * Costruttore di classe. Inizializza la griglia e si collega al server. * * @throws MalformedURLException * @throws RemoteException * @throws NotBoundException */ public TicTacToeClient() throws MalformedURLException, RemoteException, NotBoundException { idGameCreated = ""; idGameJoined = ""; grid = new int[Game.GRID_DIMENSION][Game.GRID_DIMENSION]; for (int i = 0; i < Game.GRID_DIMENSION; i++) for (int j = 0; j < Game.GRID_DIMENSION; j++) grid[i][j] = Game.FREE_CELL; wrs = new LinkedList<Watcher>(); server = (TicTacToeServer) Naming.lookup("rmi://" + IP_SERVER + "/tictactoe"); }
/** * Instantiates an event handler for forwarding events from a specific object. * * @param receiverAddress The address of the RMI service to be called when the notification is * forwarded. * @param rmiConnectorServer The Rmi connector Server */ public RmiNotificationForwarder( RmiConnectorAddress receiverAddress, RmiConnectorServer rmiConnectorServer) throws RemoteException, IllegalAccessException { this.receiverAddress = receiverAddress; this.rmiConnectorServer = rmiConnectorServer; // --------------------------- // build the RMI url // --------------------------- String receiverName = new String( "rmi://" + receiverAddress.getHost() + ":" + receiverAddress.getPort() + "/" + receiverAddress.getName()); if (logger.finestOn()) { logger.finest("Constructor", "looking for " + receiverName); } // --------------------------- // We are going to try to get a reference on the receiver // --------------------------- try { this.rmiNotificationReceiver = (RmiNotificationReceiver) Naming.lookup(receiverName); connected = true; if (this.rmiNotificationReceiver == null) { if (logger.finestOn()) { logger.finest("handleEvent", "receiver is null"); } throw new RemoteException( "Can't contact RMI Notification receive server at " + receiverName); } } catch (Exception x) { throw new RemoteException("Can't contact RMI Notification receive server at " + receiverName); } }
public static void main(String[] args) { String host = args[0]; int port = Integer.parseInt(args[1]); if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } String name = "//" + host + ":" + port + "/EquationSolver"; double[][] A = { {4.0, 3.0, 1.0}, {2.0, -6.0, 4.0}, {7.0, 5.0, 3.0} }; double[] b = {17.0, 8.0, 32.0}; try { EquationSolver solver = (EquationSolver) Naming.lookup(name); double[] x = solver.solve(A, b); StringBuffer sb = new StringBuffer(); for (int i = 0; i < x.length; i++) { sb.append(x[i]); sb.append(' '); } System.out.println(sb); } catch (RemoteException ex) { ex.printStackTrace(System.err); } catch (NotBoundException ex) { ex.printStackTrace(System.err); } catch (MalformedURLException ex) { ex.printStackTrace(System.err); } }
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); } }