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.Hashtable; | |
24 | import java.util.Observable; | |
25 | ||
26 | import net.jxta.discovery.DiscoveryListener; | |
27 | import net.jxta.discovery.DiscoveryService; | |
28 | import net.jxta.peer.PeerID; | |
29 | import net.jxta.peergroup.PeerGroup; | |
30 | import net.jxta.protocol.PeerAdvertisement; | |
31 | ||
32 | import org.apache.log4j.Logger; | |
33 | ||
34 | import base.jdbs.JDBSPeer; | |
35 | import base.jdbs.network.util.JxtaUtil; | |
36 | ||
37 | /** | |
38 | * @author skunk | |
39 | * | |
40 | */ | |
41 | 0 | public class PeerDiscoveryThread extends Observable implements Runnable{ |
42 | ||
43 | public static final String THREAD_NAME = "Peer Discovery Thread"; | |
44 | public static final int DISCOVERY_INTERVAL = 30*1000; | |
45 | ||
46 | 0 | private static final transient Logger logger = Logger.getLogger(PeerDiscoveryThread.class.getName()); |
47 | ||
48 | 0 | private final DiscoveryService discoveryService; |
49 | private final PeerGroup peerGroup; | |
50 | 0 | private final DiscoveryListener discoveryListener; |
51 | 0 | private final Hashtable<PeerID, JDBSPeer> peer = new Hashtable<PeerID, JDBSPeer>(); |
52 | 0 | |
53 | 0 | public PeerDiscoveryThread(PeerGroup peerGroup){ |
54 | 0 | this.discoveryService = peerGroup.getDiscoveryService(); |
55 | 0 | this.peerGroup = peerGroup; |
56 | //Add ourself as discovery listener | |
57 | 0 | this.discoveryService.addDiscoveryListener(this.discoveryListener = new PeerDiscoveryListener(this)); |
58 | 0 | |
59 | 0 | } |
60 | 0 | |
61 | public synchronized JDBSPeer[] getPeer(){ | |
62 | 0 | return this.peer.values().toArray(new JDBSPeer[0]); |
63 | 0 | } |
64 | 0 | |
65 | 0 | public synchronized void addPeer(PeerAdvertisement peerAdvertisement, PeerGroup peerGroup){ |
66 | 0 | if(!this.peer.containsKey(peerAdvertisement.getPeerID()) && peerAdvertisement.getPeerGroupID().equals(peerGroup.getPeerGroupID())){ |
67 | 0 | this.setChanged(); |
68 | 0 | JDBSPeer jdbsPeer = new JDBSPeer(peerAdvertisement, peerGroup); |
69 | 0 | this.peer.put(peerAdvertisement.getPeerID(),jdbsPeer); |
70 | 0 | this.notifyObservers(); |
71 | } | |
72 | 0 | } |
73 | ||
74 | ||
75 | 0 | public void run() { |
76 | 0 | try { |
77 | 0 | while (true) { |
78 | 0 | |
79 | 0 | PeerAdvertisement[] peerAdvertisement = JxtaUtil.peersInLocalCache(this.discoveryService); |
80 | 0 | for(int i=0;i<peerAdvertisement.length; i++) |
81 | 0 | this.addPeer(peerAdvertisement[i],this.peerGroup); |
82 | 0 | |
83 | ||
84 | 0 | logger.info(THREAD_NAME+": Sending a Peer Discovery Message."); |
85 | 0 | //look for any peer |
86 | 0 | this.discoveryService.getRemoteAdvertisements(null, DiscoveryService.PEER, null, null, 10, this.discoveryListener); |
87 | 0 | //wait a bit before sending next discovery message |
88 | 0 | try { |
89 | 0 | Thread.sleep(DISCOVERY_INTERVAL); |
90 | 0 | } catch(Exception ex) { |
91 | 0 | logger.error(ex.getMessage()); |
92 | 0 | } |
93 | 0 | } |
94 | 0 | } |
95 | 0 | catch(Exception e) { |
96 | 0 | e.printStackTrace(); |
97 | 0 | } |
98 | ||
99 | 0 | } |
100 | 0 | |
101 | /** | |
102 | 0 | * @return Returns the peerGroup. |
103 | */ | |
104 | 0 | public PeerGroup getPeerGroup() { |
105 | 0 | return peerGroup; |
106 | } | |
107 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |