Coverage details for base.jdbs.network.NetworkManager

LineHitsSource
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;
130import org.apache.log4j.xml.DOMConfigurator;
14  
150import base.jdbs.network.util.JxtaAuthenticationUtil;
16 import base.jdbs.network.util.JxtaGroupUtil;
170 
18 public class NetworkManager implements RendezvousListener{
19     
200    private static final transient Logger logger = Logger
21     .getLogger(NetworkManager.class.getName());
22     
230    public static final String JDBS_GROUP_NAME = "JDBSGroup";
240    public static final String JDBS_GROUP_DESCRIPTION = "Java Distributed Backup System PeerGroup";
250    public static final boolean AUTO_RENDEZVOUS = true;
260    public static final int RENDEZVOUS_PERIOD= 180000;
270    public static final int RENDEZVOUS_CONNECTION_TIMEOUT= 0;//0 infinite wait
28     
290    private volatile Object rendezVousConnectionLock = new Object();
300    
310    private static NetworkManager instance = null;
320    
33     public static NetworkManager getInstance() {
340        return instance != null ? instance : (instance = new NetworkManager());
350    }
36     
370    private PeerGroup netPeerGroup = null;
380    private PeerGroup jdbsPeerGroup = null;
39     
400    private PeerAdvertisement localPeer = null;
410    
420    private DiscoveryService discoveryService = null;
430    
440    private RendezVousService rendezVousService = null;
450    
460    private PeerDiscoveryThread peerDiscoveryThread = null;
470    
480    private NetworkManager() {
490        initialize();
500    }
510    
520    private void initialize() {
530        
540        /*File file = new File("target/client");
550         if(file.exists() && file.isDirectory())
560         FileUtil.deleteDirectory(file);*/
570        startJxta();
580        this.localPeer = netPeerGroup.getPeerAdvertisement();
59         
600        //Lets create the JDBSPeerGroup
610        logger.info("Creating "+JDBS_GROUP_NAME+"...");
620        this.jdbsPeerGroup = JxtaGroupUtil.createGroupFromLocalCache(netPeerGroup, JDBS_GROUP_NAME, AUTO_RENDEZVOUS, 180000, this, true);
630        if(this.jdbsPeerGroup == null){
640            logger.info(JDBS_GROUP_NAME+" has not been found in local cache... creating a new one...");
650            this.jdbsPeerGroup = JxtaGroupUtil.createGroup(netPeerGroup, JDBS_GROUP_NAME, JDBS_GROUP_DESCRIPTION, AUTO_RENDEZVOUS, RENDEZVOUS_PERIOD, this, true);
660        }
670        else logger.info(JDBS_GROUP_NAME+" has been created from local cache...");
680        logger.info(this.jdbsPeerGroup);
690        
700        if(JxtaAuthenticationUtil.authenticate(this.jdbsPeerGroup, "Marilisa", "Marilisa", true)){
710            this.localPeer = this.jdbsPeerGroup.getPeerAdvertisement();
720            logger.info(localPeer.getName() + " is authenticated in "+this.jdbsPeerGroup.getPeerGroupName()+"...");
730        }else logger.info(localPeer.getName() + " has not been authenticated in "+this.jdbsPeerGroup.getPeerGroupName()+"...");
740        
750        //Lets turn on our discovery service.
760        logger.info("Starting JDBS discovery service...");
770        this.peerDiscoveryThread = new PeerDiscoveryThread(this.jdbsPeerGroup);
780        new Thread(this.peerDiscoveryThread,PeerDiscoveryThread.THREAD_NAME).start();
790        
800        
810    }
820    
830    private void startJxta() {
840        try {
850            netPeerGroup = PeerGroupFactory.newNetPeerGroup();
860        } catch (PeerGroupException e) {
870            // could not instantiate the group, print the stack and exit
880            System.out.println("fatal error : group creation failure");
890            e.printStackTrace();
900            System.exit(1);
910        }
920        
930        // Extract the discovery and rendezvous services from our peer group
940        this.discoveryService = netPeerGroup.getDiscoveryService();
950        this.rendezVousService = netPeerGroup.getRendezVousService();
96         //Adding our peer as rendez vous listener.
970        this.rendezVousService.addListener(this);
980        
990        // Wait until we connect to a rendezvous peer
1000        this.waitForRendezvousConncection(this.rendezVousService,RENDEZVOUS_CONNECTION_TIMEOUT);
1010    }
1020    
1030    public static void main(String[] args) {
1040        DOMConfigurator.configure("log4jConfiguration.xml");
1050        NetworkManager.getInstance();
1060    }
1070    
1080    /**
1090     * @return Returns the localPeer.
110      */
1110    public PeerAdvertisement getLocalPeer() {
1120        return localPeer;
1130    }
1140    
1150    /**
1160     * @return Returns the peerDiscoveryThread.
1170     */
1180    public PeerDiscoveryThread getPeerDiscoveryThread() {
1190        return peerDiscoveryThread;
1200    }
121     
1220    /**
123      * Blocks until a connection to rendezvous node occurs
1240     *
1250     *@param timeout timeout in milliseconds
1260     */
1270    public void waitForRendezvousConncection(RendezVousService rendezVousService, long timeout) {
1280        if (!rendezVousService.isConnectedToRendezVous() || !rendezVousService.isRendezVous()) {
1290            System.out.println("Waiting for Rendezvous Connection");
1300            try {
1310                if (!rendezVousService.isConnectedToRendezVous()) {
1320                    synchronized(rendezVousConnectionLock) {
1330                        if(timeout == 0)rendezVousConnectionLock.wait();
1340                        else rendezVousConnectionLock.wait(timeout);
1350                    }
1360                }
1370                System.out.println("Connected to Rendezvous!");
1380            } catch (InterruptedException e) {}
139         }
1400    }
1410    
142     
1430    /**
1440     * rendezvousEvent the rendezvous event
145      *
1460     *@param event rendezvousEvent
1470     */
1480    public void rendezvousEvent(RendezvousEvent event) {
1490        System.out.println(event.getType());
1500        if (event.getType() == RendezvousEvent.RDVCONNECT ||
1510                event.getType() == RendezvousEvent.RDVRECONNECT ||
1520                event.getType() == RendezvousEvent.BECAMERDV) {
1530            synchronized (rendezVousConnectionLock) {
1540                rendezVousConnectionLock.notify();
1550            }
1560        }
1570    }
1580    
1590    /**
1600     * @return Returns the jdbsPeerGroup.
1610     */
1620    public PeerGroup getJdbsPeerGroup() {
1630        return jdbsPeerGroup;
1640    }
165 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.