FileDocCategorySizeDatePackage
RMIClientPeer.javaAPI DocExample4126Wed May 08 22:35:54 BST 2002None

RMIClientPeer

public class RMIClientPeer extends Object

Fields Summary
static Enumeration
peers
static net.jxta.peergroup.PeerGroup
netpg
static int
timeout
static int
count
Constructors Summary
Methods Summary
private static voidcallPeers()

        while (peers.hasMoreElements()) {
            try {
                Object o = peers.nextElement();
                ModuleSpecAdvertisement msa = (ModuleSpecAdvertisement) o;
                StructuredTextDocument doc = (StructuredTextDocument)
                    msa.getParam();
                if (doc == null) {
                    // No params
                    System.out.println("RMIService adv. has no params; ignoring");
                    continue;
                }
                Enumeration elements = doc.getChildren();
                String stub = null;
                while (elements.hasMoreElements()) {
                    TextElement te = (TextElement) elements.nextElement();
                    String elementName = te.getName();
                    if (elementName.equals("Stub")) {
                        stub = te.getTextValue();
                        break;
                    }
                }
                if (stub == null) {
                    System.out.println("Didn't find a stub parameter; ignoring");
                    continue;
                }
                byte[] enc = stub.getBytes();
                ByteArrayInputStream bais =
                    new ByteArrayInputStream(URLBase64.decode(enc,
                                                              0, enc.length));
                ObjectInputStream ois = new ObjectInputStream(bais);
                RMIService rs = (RMIService) ois.readObject();
                System.out.println("Remote service says " + rs.sayHello());
            } catch (Exception e) {
                System.out.println("Couldn't talk to peer -- " + e);
                e.printStackTrace();
            }
        }
    
private static booleandiscoverRMIPeers()

        DiscoveryService disco = netpg.getDiscoveryService();
        disco.getRemoteAdvertisements(null,
                        DiscoveryService.ADV,
                        "Name", "JXTASPEC:RMIService:HelloService", 5, null);
        try {
            Thread.sleep(timeout);
        } catch (InterruptedException ie) {}

        System.out.println("Looking for RMI Service peers...");
        while (count-- > 0) {
            try {
                peers = disco.getLocalAdvertisements(DiscoveryService.ADV,
                        "Name", "JXTASPEC:RMIService:HelloService");
                if (peers != null && peers.hasMoreElements())
                    break;
                disco.getRemoteAdvertisements(null,
                               DiscoveryService.ADV,
                               "Name", "JXTASPEC:RMIService:HelloService", 5,
				null);
                try {
                    Thread.sleep(timeout);
                } catch (InterruptedException ie) {}
            } catch (Exception e) {
                // Try again
            }
        }

        if (peers == null || !peers.hasMoreElements())
            return false;
        return true;
    
public static voidmain(java.lang.String[] args)


           
        try {
            netpg = PeerGroupFactory.newNetPeerGroup();
        } catch (PeerGroupException pge) {
            // Couldn't initialize; can't continue
            System.out.println("Fatal error : creating the NetPeerGroup");
            System.exit(-1);
        }
        if (!discoverRMIPeers()) {
            System.out.println("Can't find RMI peers");
            System.exit(-1);
        }
        callPeers();
        System.exit(0);