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