TalkServerImpl.java



/** TalkServerの実装の試作品のクラス */
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
public class TalkServerImpl extends UnicastRemoteObject
                            implements TalkServer {
  /** MessageReceiverのリストを記憶するVector */
    protected Vector receivers;

  /** コンストラクタ */
    public TalkServerImpl( String name ) throws RemoteException {
        super();
        receivers = new Vector();
        try {
            Naming.rebind( name, this );
        }
        catch( RemoteException e ) {
            throw e;
        }
        catch( Exception e ) {
            System.err.println( "Error:" + e.toString() ); 
        }
    }

  /** 新しいメッセージを処理するメソッド */
    public void updateMessage( Message message )
                               throws RemoteException {
        try {
            for( int i=0; i<receivers.size(); i++ ) {
                MessageReceiver receiver
                   = (MessageReceiver)receivers.elementAt(i);
                receiver.updateMessage( message );
            }
        }
        catch( RemoteException e ) {
            throw e;
        }
    }

  /** 新しいMessageReceiverを登録するメソッド */
    public void addMessageReceiver( String name )
                             throws RemoteException {
        try {
            MessageReceiver receiver
               = (MessageReceiver)Naming.lookup( name );
            receivers.add( receiver );
        }
        catch( RemoteException e ) {
            throw e;
        }
        catch( Exception e ) {
            System.err.println( "Error:" + e.toString() ); 
        }
    }

  /** MessageReceiverの登録を削除するメソッド */
    public void removeMessageReceiver( String name )
                             throws RemoteException {
        try {
            MessageReceiver receiver
               = (MessageReceiver)Naming.lookup( name );
            receivers.remove( receiver );
        }
        catch( RemoteException e ) {
            throw e;
        }
        catch( Exception e ) {
            System.err.println( "Error:" + e.toString() ); 
        }
    }
}