Coverage details for base.DiscoveryDemo

LineHitsSource
1 /*
2  * Copyright (c) 2001 Sun Microsystems, Inc. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution,
18  * if any, must include the following acknowledgment:
19  * "This product includes software developed by the
20  * Sun Microsystems, Inc. for Project JXTA."
21  * Alternately, this acknowledgment may appear in the software itself,
22  * if and wherever such third-party acknowledgments normally appear.
23  *
24  * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
25  * not be used to endorse or promote products derived from this
26  * software without prior written permission. For written
27  * permission, please contact Project JXTA at http://www.jxta.org.
28  *
29  * 5. Products derived from this software may not be called "JXTA",
30  * nor may "JXTA" appear in their name, without prior written
31  * permission of Sun.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of Project JXTA. For more
49  * information on Project JXTA, please see
50  * <http://www.jxta.org/>.
51  *
52  * This license is based on the BSD license adopted by the Apache Foundation.
53  *
54  */
55  
56 package base;
57  
58 import java.util.Enumeration;
59  
60 import net.jxta.discovery.DiscoveryEvent;
61 import net.jxta.discovery.DiscoveryListener;
62 import net.jxta.discovery.DiscoveryService;
63 import net.jxta.exception.PeerGroupException;
64 import net.jxta.peergroup.PeerGroup;
65 import net.jxta.peergroup.PeerGroupFactory;
66 import net.jxta.protocol.DiscoveryResponseMsg;
67 import net.jxta.protocol.PeerAdvertisement;
68  
69 import org.apache.log4j.Logger;
70 import org.apache.log4j.xml.DOMConfigurator;
71  
720public class DiscoveryDemo implements Runnable, DiscoveryListener {
73  
740    private static final transient Logger logger = Logger.getLogger(DiscoveryDemo.class.getName());
75  
760    static PeerGroup netPeerGroup = null;
77     private DiscoveryService discovery;
78  
79    //start the JXTA platform
800    private void startJxta() {
81         try {
820            netPeerGroup = PeerGroupFactory.newNetPeerGroup();
83       
840        } catch ( PeerGroupException e) {
85             // could not instantiate the group, print the stack and exit
860                logger.error("fatal error : group creation failure");
870            e.printStackTrace();
880            System.exit(1);
890        }
900 
91         // Get the discovery service from our peer group
920        discovery = netPeerGroup.getDiscoveryService();
930    }
940 
950    /**
960     * This thread loops forever discovering peers
970     * every minute, and displaying the results.
98      */
99  
1000    public void run() {
1010        try {
102             // Add ourselves as a DiscoveryListener for DiscoveryResponse events
1030            discovery.addDiscoveryListener(this);
104             while (true) {
1050                logger.info("Sending a Discovery Message");
106                 // look for any peer
1070                discovery.getRemoteAdvertisements(null, DiscoveryService.PEER,
108                                                   null, null, 10);
109                 // wait a bit before sending next discovery message
110                 try {
1110                    Thread.sleep(30 * 1000);
1120                } catch(Exception e) {}
1130 
1140            } //end while
1150        } catch(Exception e) {
1160            e.printStackTrace();
117         }
1180    }
1190 
1200    /**
121      * by implementing DiscoveryListener we must define this method
1220     * to deal to discovery responses
1230     */
1240 
125     public void discoveryEvent(DiscoveryEvent ev) {
1260 
1270        DiscoveryResponseMsg res = ev.getResponse();
1280        String name = "unknown";
129  
130         // Get the responding peer's advertisement
1310        PeerAdvertisement peerAdv = res.getPeerAdvertisement();
1320        logger.info("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+peerAdv);
133         
134         // some peers may not respond with their peerAdv
1350        if (peerAdv != null) {
1360            name = peerAdv.getName();
137         }
138  
1390        logger.info("Got a Discovery Response [" +
1400                           res.getResponseCount() + " elements] from peer: " +
141                            name);
142         //printout each discovered peer
1430        PeerAdvertisement adv = null;
1440        Enumeration en = res.getAdvertisements();
1450        if (en != null ) {
1460            while (en.hasMoreElements()) {
1470                adv = (PeerAdvertisement) en.nextElement();
1480                logger.info(" Peer name = " + adv.getName());
1490            }
150         }
1510    }
1520 
1530    static public void main(String args[]) {
1540 
1550        //This call enables the log4j logs for JDBS.
1560        DOMConfigurator.configure("log4jConfiguration.xml");
1570        DiscoveryDemo myapp = new DiscoveryDemo();
1580        myapp.startJxta();
1590        myapp.run();
1600    }
161 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.