Coverage details for base.jdbs.JDBSPeer

LineHitsSource
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;
22  
23 import net.jxta.endpoint.Message;
24 import net.jxta.endpoint.StringMessageElement;
25 import net.jxta.peergroup.PeerGroup;
26 import net.jxta.pipe.PipeMsgEvent;
27 import net.jxta.protocol.PeerAdvertisement;
28 import net.jxta.protocol.PipeAdvertisement;
29 import net.jxta.util.JxtaBiDiPipe;
30  
31 import org.apache.log4j.Logger;
320 
33 import base.jdbs.network.JDBSPipeMsgListener;
340import base.jdbs.network.NetworkManager;
35 import base.jdbs.network.util.PipeUtil;
36 import base.util.FileUtil;
370 
38 /**
390 * @author skunk
400 *
410 */
420public class JDBSPeer{
430    
440    private static final transient Logger logger = Logger.getLogger(JDBSPeer.class.getName());
450    
460    public static final String PIPE_ADVERTISEMENT_FILE_PATH = "pipe.adv";
47     public static final String STATUS_MESSAGE = "STATUS-MESSAGE";
480    
49     private final PeerAdvertisement peerAdvertisement;
500    private final PeerGroup peerGroup;
510    
52     private PipeAdvertisement pipeAdvertisement;
530    private JxtaBiDiPipe pipe;
540    
550    public JDBSPeer(PeerAdvertisement peerAdvertisement, PeerGroup peerGroup){
560        logger.info("JDBSPeer instantiated (pn:"+peerAdvertisement.getName()+" , pg:"+peerGroup.getPeerGroupName()+")");
570        this.peerAdvertisement = peerAdvertisement;
580        this.peerGroup = peerGroup;
590        initialize();
600    }
610    
620    protected void initialize(){
630        
640        try {
650            logger.info("Reading the pipe.adv file.");
66             /*FileInputStream is = new FileInputStream(PIPE_ADVERTISEMENT_FILE_PATH);
67             this.pipeAdvertisement = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
68             is.close();
69             */
700            System.out.println("creating the BiDi pipe");
710            this.pipe = new JxtaBiDiPipe();
720            this.pipe.setReliable(true);
730            this.pipeAdvertisement = PipeUtil.createAdv(peerGroup, "JDBSPeer Pipe", "JxtaUnicast");
740            //we don't need to wait for a rendezvous connection here because JDBS starts only after a rendezvous connection has been estabilished.
750            logger.info("Attempting to establish a connection");
760            this.pipe.connect(this.peerGroup,null,this.pipeAdvertisement,180000,
770                    // register as a message listener
780                    new JDBSPipeMsgListener(this));
790            
800            for(int i=0;i<100;i++)
810                sendStatusMessage();
820            
830        } catch (Exception e) {
840            logger.fatal(e.getMessage());
850            e.printStackTrace();
860            System.exit(-1);
870        }
880    }
890    
900     public void sendStatusMessage() {
910         logger.info(this.getPeerName()+" sending "+STATUS_MESSAGE+".");
920            try {
930        
940                Message msg = new Message();
950                String data = "This is my status #";
960                msg.addMessageElement(STATUS_MESSAGE,new StringMessageElement(STATUS_MESSAGE,data,null));
970                logger.info("Sending :"+data);
980                this.pipe.sendMessage(msg);
990            } catch (Exception ie) {
1000                ie.printStackTrace();
1010            }
1020        }
103  
104     
1050    
1060    
107     
1080    /**
1090     * @return The local peer's location.
1100     */
111     public String getLocation(){
1120        String result = this.peerAdvertisement.getPeerID().equals(NetworkManager.getInstance().getLocalPeer().getPeerID()) ? ConfigurationManager.getInstance().getUserLocation() : "Unknown";
1130        return result;
1140    }
115     
116     public String getPeerId(){
1170        return this.peerAdvertisement.getPeerID().toString();
1180    }
119     
120     /**
1210     * @return The local peer's name as defined in the JXTA configuration.
1220     */
1230    public String getPeerName(){
1240        return peerAdvertisement.getName();
125     }
1260    
127     /**
1280     * @return The difference between the declared available space and the real Repository occupied space in Mega Bytes.
1290     */
130     public long getAvailableSpace(){
1310        long result = this.peerAdvertisement.getPeerID().equals(NetworkManager.getInstance().getLocalPeer().getPeerID()) ? ConfigurationManager.getInstance().getRepository().getDeclaredAvailableSpace() - FileUtil.fileSizeInMB(ConfigurationManager.getInstance().getRepository().getLocation()) : 0;
1320        return result;
133     }
1340    
1350    /**
1360     * @return Returns the ping between this local peer and the other executing JDBS.
1370     */
1380    public int getPing(){
1390        int result = this.peerAdvertisement.getPeerID().equals(NetworkManager.getInstance().getLocalPeer().getPeerID()) ? 0 : 0;
1400        return result;
1410    }
1420    
1430    /**
144      * @return Returns the peerAdvertisement.
1450     */
1460    public PeerAdvertisement getPeerAdvertisement() {
1470        return peerAdvertisement;
1480    }
149     
150     /**
1510     * @return Returns the peerGroup.
1520     */
1530    public PeerGroup getPeerGroup() {
1540        return peerGroup;
1550    }
156     
157     public void pipeMsgEvent(PipeMsgEvent arg0) {
158         // TODO Auto-generated method stub
159         
1600    }
1610}

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.