TalkServerList.java



/** TalkServerを一覧表示するクラス */
import java.awt.*;
import java.io.*;
import java.net.*;
import java.rmi.*;
import java.util.StringTokenizer;
import java.util.Vector;
public class TalkServerList {

  /** マシンの一覧表 */
    public String[] hosts;

  /** 表示を行うための List */
    public List list;

  /** Listを配置するための Panel */
    public Panel panel;

  /** コンストラクタ */
    public TalkServerList () {
        hosts = loadHosts();
        panel = new Panel();
        list = new List( 10 );
        for( int i=0; i<hosts.length; i++ ) {
            try {
                String dirName = "rmi://" + hosts[i] + "/";
                String[] servers = Naming.list( dirName );
                for( int j=0; j<servers.length; j++ ) {
                    StringTokenizer st
                       = new StringTokenizer( servers[j], "/" );
                    String dummy = st.nextToken();
                    String host = st.nextToken();
                    String name = st.nextToken();
                    if( name.startsWith( "TalkServer", 0 ) )
                        list.add( host + "/" + name );
                }
            }
            catch( Exception e ) {
                System.out.println( hosts[i] + " refused" );
            }
            continue;
        }
        panel.add( list );
    }

  /** ホスト名を読み込むメソッド */
    public String[] loadHosts() {
        Vector v = new Vector();
        try {
            BufferedReader bf
                = new BufferedReader(
                      new FileReader( "hosts" ) );
            String line;
            while( ( line = bf.readLine() ) != null )
                v.add( line );
        }
        catch( Exception e ) {
           System.out.println( "Load Error:" + e.toString() );
           System.exit(-1);
        }
        return (String[])v.toArray( new String[v.size()] );
    }

  /** 起動時に呼び出されるメソッド */
    public static void main( String argv[] ) {

        System.setSecurityManager( new RMISecurityManager() );
        try {
            TalkServerList list = new TalkServerList();

            Frame frame = new Frame( "Server Lists" );
            frame.add( list.panel, "Center" );
            frame.pack();
            frame.show();
        }
        catch( Exception e ) {
           System.out.println( "Error:" + e.toString() );
           System.exit(-1);
        }
    }
}