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 72 public class DiscoveryDemo implements Runnable, DiscoveryListener { 73 74 private static final transient Logger logger = Logger.getLogger(DiscoveryDemo.class.getName()); 75 76 static PeerGroup netPeerGroup = null; 77 private DiscoveryService discovery; 78 79 //start the JXTA platform 80 private void startJxta() { 81 try { 82 netPeerGroup = PeerGroupFactory.newNetPeerGroup(); 83 84 } catch ( PeerGroupException e) { 85 // could not instantiate the group, print the stack and exit 86 logger.error("fatal error : group creation failure"); 87 e.printStackTrace(); 88 System.exit(1); 89 } 90 91 // Get the discovery service from our peer group 92 discovery = netPeerGroup.getDiscoveryService(); 93 } 94 95 /*** 96 * This thread loops forever discovering peers 97 * every minute, and displaying the results. 98 */ 99 100 public void run() { 101 try { 102 // Add ourselves as a DiscoveryListener for DiscoveryResponse events 103 discovery.addDiscoveryListener(this); 104 while (true) { 105 logger.info("Sending a Discovery Message"); 106 // look for any peer 107 discovery.getRemoteAdvertisements(null, DiscoveryService.PEER, 108 null, null, 10); 109 // wait a bit before sending next discovery message 110 try { 111 Thread.sleep(30 * 1000); 112 } catch(Exception e) {} 113 114 } //end while 115 } catch(Exception e) { 116 e.printStackTrace(); 117 } 118 } 119 120 /*** 121 * by implementing DiscoveryListener we must define this method 122 * to deal to discovery responses 123 */ 124 125 public void discoveryEvent(DiscoveryEvent ev) { 126 127 DiscoveryResponseMsg res = ev.getResponse(); 128 String name = "unknown"; 129 130 // Get the responding peer's advertisement 131 PeerAdvertisement peerAdv = res.getPeerAdvertisement(); 132 logger.info("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+peerAdv); 133 134 // some peers may not respond with their peerAdv 135 if (peerAdv != null) { 136 name = peerAdv.getName(); 137 } 138 139 logger.info("Got a Discovery Response [" + 140 res.getResponseCount() + " elements] from peer: " + 141 name); 142 //printout each discovered peer 143 PeerAdvertisement adv = null; 144 Enumeration en = res.getAdvertisements(); 145 if (en != null ) { 146 while (en.hasMoreElements()) { 147 adv = (PeerAdvertisement) en.nextElement(); 148 logger.info(" Peer name = " + adv.getName()); 149 } 150 } 151 } 152 153 static public void main(String args[]) { 154 155 //This call enables the log4j logs for JDBS. 156 DOMConfigurator.configure("log4jConfiguration.xml"); 157 DiscoveryDemo myapp = new DiscoveryDemo(); 158 myapp.startJxta(); 159 myapp.run(); 160 } 161 }