main.java

来自「rmi chat that connects two or more clien」· Java 代码 · 共 68 行

JAVA
68
字号

import java.rmi.*;
import java.rmi.server.*;
import java.lang.*;
import java.util.*;


public class Main extends UnicastRemoteObject implements ChatInterface {
    
    private ServerList serverList = new ServerList();
    
    public Main() throws RemoteException {
    }
    
    public void join(Notify n, String name) throws RemoteException {
		serverList.add(n);
		
		serverList.incCounter();
		for (Iterator i = serverList.getCollection().iterator();
				 i.hasNext();) {
			Notify client = (Notify)i.next();
			client.joinMessage(name);
		}
		serverList.decCounter();
    }
    
    public void talk(String name, String s)
            throws RemoteException {
		serverList.incCounter();
		for (Iterator i = serverList.getCollection().iterator();
				 i.hasNext();) {
			Notify client = (Notify)i.next();
			client.sendMessage(name,s);
		}
		serverList.decCounter();
    }
    
    public synchronized void leave(Notify n, String name) throws RemoteException {
    	serverList.remove(n);
    	
    	serverList.incCounter();
    	for (Iterator i = serverList.getCollection().iterator();
				 i.hasNext();) {
			Notify client = (Notify)i.next();
			client.exitMessage(name);
		}
		serverList.decCounter();
    }

    public static void main(String[] args) {
         	try {
			
			Main server = new Main();

			Naming.rebind("rmichat", server);
		
		}
		catch (java.net.MalformedURLException e) {
			System.out.println("Malformed URL for MessageServer name "
			    + e.toString());
		}
		catch (RemoteException e) {
			System.out.println("Communication error " + e.toString());
		}
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?