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