Line | Hits | Source |
---|---|---|
1 | package base.jdbs.network; | |
2 | ||
3 | import net.jxta.discovery.DiscoveryService; | |
4 | import net.jxta.exception.PeerGroupException; | |
5 | import net.jxta.peergroup.PeerGroup; | |
6 | import net.jxta.peergroup.PeerGroupFactory; | |
7 | import net.jxta.protocol.PeerAdvertisement; | |
8 | import net.jxta.rendezvous.RendezVousService; | |
9 | import net.jxta.rendezvous.RendezvousEvent; | |
10 | import net.jxta.rendezvous.RendezvousListener; | |
11 | ||
12 | import org.apache.log4j.Logger; | |
13 | 0 | import org.apache.log4j.xml.DOMConfigurator; |
14 | ||
15 | 0 | import base.jdbs.network.util.JxtaAuthenticationUtil; |
16 | import base.jdbs.network.util.JxtaGroupUtil; | |
17 | 0 | |
18 | public class NetworkManager implements RendezvousListener{ | |
19 | ||
20 | 0 | private static final transient Logger logger = Logger |
21 | .getLogger(NetworkManager.class.getName()); | |
22 | ||
23 | 0 | public static final String JDBS_GROUP_NAME = "JDBSGroup"; |
24 | 0 | public static final String JDBS_GROUP_DESCRIPTION = "Java Distributed Backup System PeerGroup"; |
25 | 0 | public static final boolean AUTO_RENDEZVOUS = true; |
26 | 0 | public static final int RENDEZVOUS_PERIOD= 180000; |
27 | 0 | public static final int RENDEZVOUS_CONNECTION_TIMEOUT= 0;//0 infinite wait |
28 | ||
29 | 0 | private volatile Object rendezVousConnectionLock = new Object(); |
30 | 0 | |
31 | 0 | private static NetworkManager instance = null; |
32 | 0 | |
33 | public static NetworkManager getInstance() { | |
34 | 0 | return instance != null ? instance : (instance = new NetworkManager()); |
35 | 0 | } |
36 | ||
37 | 0 | private PeerGroup netPeerGroup = null; |
38 | 0 | private PeerGroup jdbsPeerGroup = null; |
39 | ||
40 | 0 | private PeerAdvertisement localPeer = null; |
41 | 0 | |
42 | 0 | private DiscoveryService discoveryService = null; |
43 | 0 | |
44 | 0 | private RendezVousService rendezVousService = null; |
45 | 0 | |
46 | 0 | private PeerDiscoveryThread peerDiscoveryThread = null; |
47 | 0 | |
48 | 0 | private NetworkManager() { |
49 | 0 | initialize(); |
50 | 0 | } |
51 | 0 | |
52 | 0 | private void initialize() { |
53 | 0 | |
54 | 0 | /*File file = new File("target/client"); |
55 | 0 | if(file.exists() && file.isDirectory()) |
56 | 0 | FileUtil.deleteDirectory(file);*/ |
57 | 0 | startJxta(); |
58 | 0 | this.localPeer = netPeerGroup.getPeerAdvertisement(); |
59 | ||
60 | 0 | //Lets create the JDBSPeerGroup |
61 | 0 | logger.info("Creating "+JDBS_GROUP_NAME+"..."); |
62 | 0 | this.jdbsPeerGroup = JxtaGroupUtil.createGroupFromLocalCache(netPeerGroup, JDBS_GROUP_NAME, AUTO_RENDEZVOUS, 180000, this, true); |
63 | 0 | if(this.jdbsPeerGroup == null){ |
64 | 0 | logger.info(JDBS_GROUP_NAME+" has not been found in local cache... creating a new one..."); |
65 | 0 | this.jdbsPeerGroup = JxtaGroupUtil.createGroup(netPeerGroup, JDBS_GROUP_NAME, JDBS_GROUP_DESCRIPTION, AUTO_RENDEZVOUS, RENDEZVOUS_PERIOD, this, true); |
66 | 0 | } |
67 | 0 | else logger.info(JDBS_GROUP_NAME+" has been created from local cache..."); |
68 | 0 | logger.info(this.jdbsPeerGroup); |
69 | 0 | |
70 | 0 | if(JxtaAuthenticationUtil.authenticate(this.jdbsPeerGroup, "Marilisa", "Marilisa", true)){ |
71 | 0 | this.localPeer = this.jdbsPeerGroup.getPeerAdvertisement(); |
72 | 0 | logger.info(localPeer.getName() + " is authenticated in "+this.jdbsPeerGroup.getPeerGroupName()+"..."); |
73 | 0 | }else logger.info(localPeer.getName() + " has not been authenticated in "+this.jdbsPeerGroup.getPeerGroupName()+"..."); |
74 | 0 | |
75 | 0 | //Lets turn on our discovery service. |
76 | 0 | logger.info("Starting JDBS discovery service..."); |
77 | 0 | this.peerDiscoveryThread = new PeerDiscoveryThread(this.jdbsPeerGroup); |
78 | 0 | new Thread(this.peerDiscoveryThread,PeerDiscoveryThread.THREAD_NAME).start(); |
79 | 0 | |
80 | 0 | |
81 | 0 | } |
82 | 0 | |
83 | 0 | private void startJxta() { |
84 | 0 | try { |
85 | 0 | netPeerGroup = PeerGroupFactory.newNetPeerGroup(); |
86 | 0 | } catch (PeerGroupException e) { |
87 | 0 | // could not instantiate the group, print the stack and exit |
88 | 0 | System.out.println("fatal error : group creation failure"); |
89 | 0 | e.printStackTrace(); |
90 | 0 | System.exit(1); |
91 | 0 | } |
92 | 0 | |
93 | 0 | // Extract the discovery and rendezvous services from our peer group |
94 | 0 | this.discoveryService = netPeerGroup.getDiscoveryService(); |
95 | 0 | this.rendezVousService = netPeerGroup.getRendezVousService(); |
96 | //Adding our peer as rendez vous listener. | |
97 | 0 | this.rendezVousService.addListener(this); |
98 | 0 | |
99 | 0 | // Wait until we connect to a rendezvous peer |
100 | 0 | this.waitForRendezvousConncection(this.rendezVousService,RENDEZVOUS_CONNECTION_TIMEOUT); |
101 | 0 | } |
102 | 0 | |
103 | 0 | public static void main(String[] args) { |
104 | 0 | DOMConfigurator.configure("log4jConfiguration.xml"); |
105 | 0 | NetworkManager.getInstance(); |
106 | 0 | } |
107 | 0 | |
108 | 0 | /** |
109 | 0 | * @return Returns the localPeer. |
110 | */ | |
111 | 0 | public PeerAdvertisement getLocalPeer() { |
112 | 0 | return localPeer; |
113 | 0 | } |
114 | 0 | |
115 | 0 | /** |
116 | 0 | * @return Returns the peerDiscoveryThread. |
117 | 0 | */ |
118 | 0 | public PeerDiscoveryThread getPeerDiscoveryThread() { |
119 | 0 | return peerDiscoveryThread; |
120 | 0 | } |
121 | ||
122 | 0 | /** |
123 | * Blocks until a connection to rendezvous node occurs | |
124 | 0 | * |
125 | 0 | *@param timeout timeout in milliseconds |
126 | 0 | */ |
127 | 0 | public void waitForRendezvousConncection(RendezVousService rendezVousService, long timeout) { |
128 | 0 | if (!rendezVousService.isConnectedToRendezVous() || !rendezVousService.isRendezVous()) { |
129 | 0 | System.out.println("Waiting for Rendezvous Connection"); |
130 | 0 | try { |
131 | 0 | if (!rendezVousService.isConnectedToRendezVous()) { |
132 | 0 | synchronized(rendezVousConnectionLock) { |
133 | 0 | if(timeout == 0)rendezVousConnectionLock.wait(); |
134 | 0 | else rendezVousConnectionLock.wait(timeout); |
135 | 0 | } |
136 | 0 | } |
137 | 0 | System.out.println("Connected to Rendezvous!"); |
138 | 0 | } catch (InterruptedException e) {} |
139 | } | |
140 | 0 | } |
141 | 0 | |
142 | ||
143 | 0 | /** |
144 | 0 | * rendezvousEvent the rendezvous event |
145 | * | |
146 | 0 | *@param event rendezvousEvent |
147 | 0 | */ |
148 | 0 | public void rendezvousEvent(RendezvousEvent event) { |
149 | 0 | System.out.println(event.getType()); |
150 | 0 | if (event.getType() == RendezvousEvent.RDVCONNECT || |
151 | 0 | event.getType() == RendezvousEvent.RDVRECONNECT || |
152 | 0 | event.getType() == RendezvousEvent.BECAMERDV) { |
153 | 0 | synchronized (rendezVousConnectionLock) { |
154 | 0 | rendezVousConnectionLock.notify(); |
155 | 0 | } |
156 | 0 | } |
157 | 0 | } |
158 | 0 | |
159 | 0 | /** |
160 | 0 | * @return Returns the jdbsPeerGroup. |
161 | 0 | */ |
162 | 0 | public PeerGroup getJdbsPeerGroup() { |
163 | 0 | return jdbsPeerGroup; |
164 | 0 | } |
165 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |