| 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; | |
| 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; | |
| 32 | 0 | |
| 33 | import base.jdbs.network.JDBSPipeMsgListener; | |
| 34 | 0 | import base.jdbs.network.NetworkManager; |
| 35 | import base.jdbs.network.util.PipeUtil; | |
| 36 | import base.util.FileUtil; | |
| 37 | 0 | |
| 38 | /** | |
| 39 | 0 | * @author skunk |
| 40 | 0 | * |
| 41 | 0 | */ |
| 42 | 0 | public class JDBSPeer{ |
| 43 | 0 | |
| 44 | 0 | private static final transient Logger logger = Logger.getLogger(JDBSPeer.class.getName()); |
| 45 | 0 | |
| 46 | 0 | public static final String PIPE_ADVERTISEMENT_FILE_PATH = "pipe.adv"; |
| 47 | public static final String STATUS_MESSAGE = "STATUS-MESSAGE"; | |
| 48 | 0 | |
| 49 | private final PeerAdvertisement peerAdvertisement; | |
| 50 | 0 | private final PeerGroup peerGroup; |
| 51 | 0 | |
| 52 | private PipeAdvertisement pipeAdvertisement; | |
| 53 | 0 | private JxtaBiDiPipe pipe; |
| 54 | 0 | |
| 55 | 0 | public JDBSPeer(PeerAdvertisement peerAdvertisement, PeerGroup peerGroup){ |
| 56 | 0 | logger.info("JDBSPeer instantiated (pn:"+peerAdvertisement.getName()+" , pg:"+peerGroup.getPeerGroupName()+")"); |
| 57 | 0 | this.peerAdvertisement = peerAdvertisement; |
| 58 | 0 | this.peerGroup = peerGroup; |
| 59 | 0 | initialize(); |
| 60 | 0 | } |
| 61 | 0 | |
| 62 | 0 | protected void initialize(){ |
| 63 | 0 | |
| 64 | 0 | try { |
| 65 | 0 | 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 | */ | |
| 70 | 0 | System.out.println("creating the BiDi pipe"); |
| 71 | 0 | this.pipe = new JxtaBiDiPipe(); |
| 72 | 0 | this.pipe.setReliable(true); |
| 73 | 0 | this.pipeAdvertisement = PipeUtil.createAdv(peerGroup, "JDBSPeer Pipe", "JxtaUnicast"); |
| 74 | 0 | //we don't need to wait for a rendezvous connection here because JDBS starts only after a rendezvous connection has been estabilished. |
| 75 | 0 | logger.info("Attempting to establish a connection"); |
| 76 | 0 | this.pipe.connect(this.peerGroup,null,this.pipeAdvertisement,180000, |
| 77 | 0 | // register as a message listener |
| 78 | 0 | new JDBSPipeMsgListener(this)); |
| 79 | 0 | |
| 80 | 0 | for(int i=0;i<100;i++) |
| 81 | 0 | sendStatusMessage(); |
| 82 | 0 | |
| 83 | 0 | } catch (Exception e) { |
| 84 | 0 | logger.fatal(e.getMessage()); |
| 85 | 0 | e.printStackTrace(); |
| 86 | 0 | System.exit(-1); |
| 87 | 0 | } |
| 88 | 0 | } |
| 89 | 0 | |
| 90 | 0 | public void sendStatusMessage() { |
| 91 | 0 | logger.info(this.getPeerName()+" sending "+STATUS_MESSAGE+"."); |
| 92 | 0 | try { |
| 93 | 0 | |
| 94 | 0 | Message msg = new Message(); |
| 95 | 0 | String data = "This is my status #"; |
| 96 | 0 | msg.addMessageElement(STATUS_MESSAGE,new StringMessageElement(STATUS_MESSAGE,data,null)); |
| 97 | 0 | logger.info("Sending :"+data); |
| 98 | 0 | this.pipe.sendMessage(msg); |
| 99 | 0 | } catch (Exception ie) { |
| 100 | 0 | ie.printStackTrace(); |
| 101 | 0 | } |
| 102 | 0 | } |
| 103 | ||
| 104 | ||
| 105 | 0 | |
| 106 | 0 | |
| 107 | ||
| 108 | 0 | /** |
| 109 | 0 | * @return The local peer's location. |
| 110 | 0 | */ |
| 111 | public String getLocation(){ | |
| 112 | 0 | String result = this.peerAdvertisement.getPeerID().equals(NetworkManager.getInstance().getLocalPeer().getPeerID()) ? ConfigurationManager.getInstance().getUserLocation() : "Unknown"; |
| 113 | 0 | return result; |
| 114 | 0 | } |
| 115 | ||
| 116 | public String getPeerId(){ | |
| 117 | 0 | return this.peerAdvertisement.getPeerID().toString(); |
| 118 | 0 | } |
| 119 | ||
| 120 | /** | |
| 121 | 0 | * @return The local peer's name as defined in the JXTA configuration. |
| 122 | 0 | */ |
| 123 | 0 | public String getPeerName(){ |
| 124 | 0 | return peerAdvertisement.getName(); |
| 125 | } | |
| 126 | 0 | |
| 127 | /** | |
| 128 | 0 | * @return The difference between the declared available space and the real Repository occupied space in Mega Bytes. |
| 129 | 0 | */ |
| 130 | public long getAvailableSpace(){ | |
| 131 | 0 | long result = this.peerAdvertisement.getPeerID().equals(NetworkManager.getInstance().getLocalPeer().getPeerID()) ? ConfigurationManager.getInstance().getRepository().getDeclaredAvailableSpace() - FileUtil.fileSizeInMB(ConfigurationManager.getInstance().getRepository().getLocation()) : 0; |
| 132 | 0 | return result; |
| 133 | } | |
| 134 | 0 | |
| 135 | 0 | /** |
| 136 | 0 | * @return Returns the ping between this local peer and the other executing JDBS. |
| 137 | 0 | */ |
| 138 | 0 | public int getPing(){ |
| 139 | 0 | int result = this.peerAdvertisement.getPeerID().equals(NetworkManager.getInstance().getLocalPeer().getPeerID()) ? 0 : 0; |
| 140 | 0 | return result; |
| 141 | 0 | } |
| 142 | 0 | |
| 143 | 0 | /** |
| 144 | * @return Returns the peerAdvertisement. | |
| 145 | 0 | */ |
| 146 | 0 | public PeerAdvertisement getPeerAdvertisement() { |
| 147 | 0 | return peerAdvertisement; |
| 148 | 0 | } |
| 149 | ||
| 150 | /** | |
| 151 | 0 | * @return Returns the peerGroup. |
| 152 | 0 | */ |
| 153 | 0 | public PeerGroup getPeerGroup() { |
| 154 | 0 | return peerGroup; |
| 155 | 0 | } |
| 156 | ||
| 157 | public void pipeMsgEvent(PipeMsgEvent arg0) { | |
| 158 | // TODO Auto-generated method stub | |
| 159 | ||
| 160 | 0 | } |
| 161 | 0 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |