View Javadoc

1   package base.jdbs.network;
2   
3   import java.util.Enumeration;
4   
5   import net.jxta.discovery.DiscoveryEvent;
6   import net.jxta.discovery.DiscoveryListener;
7   import net.jxta.protocol.DiscoveryResponseMsg;
8   import net.jxta.protocol.PeerAdvertisement;
9   
10  import org.apache.log4j.Logger;
11  
12  public class PeerDiscoveryListener implements DiscoveryListener {
13  
14  	private static final transient Logger logger = Logger.getLogger(PeerDiscoveryListener.class.getName());
15  
16  	private final PeerDiscoveryThread peerDiscoveryThread ;
17  	
18  	public PeerDiscoveryListener(PeerDiscoveryThread peerDiscoveryThread) {
19  		this.peerDiscoveryThread = peerDiscoveryThread;
20  	}
21  
22  	public void discoveryEvent(DiscoveryEvent event) {
23  		DiscoveryResponseMsg discoveryResponseMsg = event.getResponse(); 
24  		String name = "unknown"; 
25  		//Get the responding peer's advertisement 
26  		PeerAdvertisement peerAdvertisement = discoveryResponseMsg.getPeerAdvertisement(); 
27  		//some peers may not respond with their peerAdvertisement 
28  		if (peerAdvertisement != null) { 
29  			name = peerAdvertisement.getName(); 
30  		} 
31  		
32  		logger.info(PeerDiscoveryThread.THREAD_NAME+": Got a Discovery Response [" +  discoveryResponseMsg.getResponseCount()+ " elements] from peer : " + name); 
33  		
34  //		printout each discovered peer
35  		PeerAdvertisement peerArvertisement = null;
36  		Enumeration enumeration = discoveryResponseMsg.getAdvertisements();
37  		if (enumeration != null ) {
38  			while (enumeration.hasMoreElements()) {
39  				peerArvertisement = (PeerAdvertisement) enumeration.nextElement();
40  				logger.debug(" Peer name = " + peerArvertisement.getName());
41  				this.peerDiscoveryThread.addPeer(peerArvertisement, peerDiscoveryThread.getPeerGroup());
42  			}
43  		}
44  	}
45  
46  	
47  }