User Tools

Site Tools


sysrep:rmi-correction

RMI : Corrections

Quelques éléments de corrections…

Interfaces Chat et ChatBack

Chat.java
import java.rmi.Remote; 
import java.rmi.RemoteException; 
import java.util.ArrayList;
 
public interface Chat extends Remote { 
    void connect(String nick, ChatBack ref) throws RemoteException; 
    void disconnect(String nick) throws RemoteException; 
    ArrayList<String> list() throws RemoteException; 
    void send(String src, String msg) throws RemoteException; 
    void send(String src, String dst, String msg) throws RemoteException; 
}
ChatBack.java
import java.rmi.Remote; 
import java.rmi.RemoteException; 
 
public interface ChatBack extends Remote { 
    void send(String src, String msg) throws RemoteException; 
}

Coquilles Client & Serveur

ChatServer.java
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
 
class ChatImpl implements Chat {    
 
    public ChatImpl() { /* TODO */ }
 
    public void connect(String nick, ChatBack ref) throws RemoteException { /* TODO */ }
 
    public void disconnect(String nick) throws RemoteException { /* TODO */ }
 
    public ArrayList<String> list() throws RemoteException { /* TODO */ }
 
    public void send(String src, String msg) throws RemoteException { /* TODO */ }
 
    public void send(String src, String dst, String msg) throws RemoteException { /* TODO */ }
 
}
 
public class ChatServer {    
 
    public static void main(String args[]) { 
 
        try {
	    Chat chat = new ChatImpl(); 
            Chat stub = (Chat) UnicastRemoteObject.exportObject(chat, 0);
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind("chat", stub);
 
        } catch (Exception e) { 
	    System.out.println("ChatServer Exception: " + e.getMessage()); 
	    e.printStackTrace(); 
        } 
 
    } 
}
ChatClient.java
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
 
class ChatBackImpl implements ChatBack {    
 
    public void send(String src, String msg) throws RemoteException { /* TODO */ }
 
}
 
 
public class ChatClient {
 
    public static void main(String args[]) { 
 
	if (args.length != 2) {
	    System.out.println("Usage: ChatClient host nickname");
	    System.exit(1);
	}
 
	try { 
            Registry registry = LocateRegistry.getRegistry(args[0]);
            Chat chat = (Chat) registry.lookup("chat");
	    ChatBack chatback = new ChatBackImpl();
	    ChatBack stub = (ChatBack) UnicastRemoteObject.exportObject(chatback, 0);
 
	    // sample
	    chat.connect(args[1], stub);
	    ArrayList<String> users = chat.list();
	    System.out.println("users: " + users);
	    chat.send(args[1],"hello world!");
	    chat.disconnect(args[1]);
 
	} catch (Exception e) { 
	    System.out.println("ChatClient exception: " + e.getMessage()); 
	    e.printStackTrace(); 
	} 
 
    } 
}

Correction

pas encore…

sysrep/rmi-correction.txt · Last modified: 2024/03/18 15:06 by 127.0.0.1