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