Line | Hits | Source |
---|---|---|
1 | /******************************************************************************* | |
2 | * InternetCafe is a software solution that helps the management of Cybercafes | |
3 | * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005. | |
4 | * Copyright (C) 2006 Guido Angelo Ingenito | |
5 | ||
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version 2 | |
9 | * of the License, or (at your option) any later version. | |
10 | ||
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | *******************************************************************************/ | |
20 | ||
21 | package base.jdbs.network; | |
22 | ||
23 | import java.util.Enumeration; | |
24 | import java.util.Observable; | |
25 | ||
26 | import net.jxta.discovery.DiscoveryEvent; | |
27 | import net.jxta.discovery.DiscoveryListener; | |
28 | import net.jxta.discovery.DiscoveryService; | |
29 | import net.jxta.protocol.DiscoveryResponseMsg; | |
30 | import net.jxta.protocol.PeerAdvertisement; | |
31 | import net.jxta.protocol.PeerGroupAdvertisement; | |
32 | ||
33 | import org.apache.log4j.Logger; | |
34 | ||
35 | import base.jdbs.network.util.JxtaUtil; | |
36 | ||
37 | /** | |
38 | * @author skunk | |
39 | * | |
40 | */ | |
41 | 0 | public class GroupDiscoveryThread extends Observable implements Runnable, DiscoveryListener { |
42 | ||
43 | public static final String THREAD_NAME = "Group Discovery Thread"; | |
44 | 0 | public static final int DISCOVERY_INTERVAL = 30*1000; |
45 | ||
46 | 0 | private static final transient Logger logger = Logger.getLogger(GroupDiscoveryThread.class.getName()); |
47 | ||
48 | 0 | private final DiscoveryService discoveryService; |
49 | 0 | |
50 | 0 | public GroupDiscoveryThread(DiscoveryService discoveryService){ |
51 | 0 | this.discoveryService = discoveryService; |
52 | //Add ourself as discovery listener | |
53 | 0 | this.discoveryService.addDiscoveryListener(this); |
54 | 0 | } |
55 | 0 | |
56 | public void run() { | |
57 | 0 | try { |
58 | 0 | while (true) { |
59 | 0 | JxtaUtil.groupsInLocalCache(this.discoveryService); |
60 | ||
61 | 0 | logger.info(THREAD_NAME+": Sending a Group Discovery Message."); |
62 | 0 | //look for any peer group |
63 | 0 | this.discoveryService.getRemoteAdvertisements(null, DiscoveryService.GROUP, null, null, 10); |
64 | 0 | //wait a bit before sending next discovery message |
65 | 0 | try { |
66 | 0 | Thread.sleep(DISCOVERY_INTERVAL); |
67 | 0 | } catch(Exception ex) { |
68 | 0 | logger.error(ex.getMessage()); |
69 | 0 | } |
70 | 0 | } |
71 | } | |
72 | 0 | catch(Exception e) { |
73 | 0 | e.printStackTrace(); |
74 | } | |
75 | ||
76 | 0 | } |
77 | ||
78 | 0 | /* (non-Javadoc) |
79 | 0 | * @see net.jxta.discovery.DiscoveryListener#discoveryEvent(net.jxta.discovery.DiscoveryEvent) |
80 | */ | |
81 | 0 | public void discoveryEvent(DiscoveryEvent discoveryEvent) { |
82 | 0 | DiscoveryResponseMsg discoveryResponseMsg = discoveryEvent.getResponse(); |
83 | 0 | String name = "unknown"; |
84 | 0 | //Get the responding peer's advertisement |
85 | 0 | PeerAdvertisement peerAdvertisement = discoveryResponseMsg.getPeerAdvertisement(); |
86 | //some peers may not respond with their peerAdvertisement | |
87 | 0 | if (peerAdvertisement != null) { |
88 | 0 | name = peerAdvertisement.getName(); |
89 | } | |
90 | 0 | |
91 | 0 | logger.info(THREAD_NAME+": Got a Discovery Response [" + discoveryResponseMsg.getResponseCount()+ " elements] from peer : " + name); |
92 | 0 | |
93 | 0 | // printout each discovered peer |
94 | 0 | PeerGroupAdvertisement peerGroupArvertisement = null; |
95 | 0 | Enumeration enumeration = discoveryResponseMsg.getAdvertisements(); |
96 | 0 | if (enumeration != null ) { |
97 | 0 | while (enumeration.hasMoreElements()) { |
98 | 0 | peerGroupArvertisement = (PeerGroupAdvertisement) enumeration.nextElement(); |
99 | 0 | logger.info(" Group NAME: " + peerGroupArvertisement.getName() +" ID: "+peerGroupArvertisement.getID().toString() ); |
100 | //NetworkManager.getInstance().addDiscoveredPeer(peerArvertisement); | |
101 | 0 | } |
102 | } | |
103 | 0 | } |
104 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |