Project

General

Profile

Revision 3

Update

View differences:

Peer.java
1
package peer;

2

  
3
import java.io.IOException;

4
import java.net.DatagramPacket;

5
import java.net.InetAddress;

6
import java.net.MulticastSocket;

7
import java.net.UnknownHostException;

8
import java.rmi.RemoteException;

9
import java.rmi.registry.LocateRegistry;

10
import java.rmi.registry.Registry;

11
import java.rmi.server.UnicastRemoteObject;

12

  
13
import disk.Disk;

14
import message.Message;

15
import peer.channels.Listener;

16
import peer.protocols.backup.Backup;

17
import peer.protocols.delete.Delete;

18
import peer.protocols.restore.Restore;

19

  
20
public class Peer /*extends UnicastRemoteObject*/ implements PeerInterface {

21

  
22
	//private static final long serialVersionUID = 4988282307993613946L;

23

  
24
	private int peerId;

25
	private Disk disk;

26

  
27
	private Registry rmiReg;

28

  
29
	private String pVersion;

30
	private String accessPoint;

31

  
32
	private MulticastSocket socket;

33

  
34
	private InetAddress mcAddress;

35
	private InetAddress mdbAddress;

36
	private InetAddress mdrAddress;

37
	private int mcPort;

38
	private int mdbPort;

39
	private int mdrPort;

40

  
41
	private Listener mcChannel;

42
	private Listener mdbChannel;

43
	private Listener mdrChannel;

44

  
45
	public Peer(String pVersion, int sid, String accessPoint, String mcAddress, int mcPort, String mdbAddress,

46
			int mdbPort, String mdrAddress, int mdrPort) throws RemoteException {

47
		
48
		this.peerId = sid;

49

  
50
		try {

51
			this.mcAddress = InetAddress.getByName(mcAddress);

52
			this.mdbAddress = InetAddress.getByName(mdbAddress);

53
			this.mdrAddress = InetAddress.getByName(mdrAddress);

54
		} catch (UnknownHostException e1) {

55
			e1.printStackTrace();

56
		}

57

  
58
		this.mcPort = mcPort;

59
		this.mdbPort = mdbPort;

60
		this.mdrPort = mdrPort;

61

  
62
		this.disk = new Disk("peer" + peerId);

63
		this.pVersion = pVersion;

64
		this.accessPoint = accessPoint;

65
		this.rmiReg = null;

66
		try {

67
			this.socket = new MulticastSocket();

68
		} catch (IOException e) {

69
			e.printStackTrace();

70
		}

71

  
72
		this.startRMI();

73

  
74
		try {

75
			this.startChannels(mcAddress, mcPort, mdbAddress, mdbPort, mdrAddress, mdrPort);

76
		} catch (IOException e) {

77
			e.printStackTrace();

78
		}

79

  
80
		System.out.println("Peer " + this.peerId + " running...");

81
	}

82

  
83
	public static void main(String[] args) {

84
		int id = Integer.parseInt(args[1]);

85
		int mcPort = Integer.parseInt(args[4]);

86
		int mdbPort = Integer.parseInt(args[6]);

87
		int mdrPort = Integer.parseInt(args[8]);

88
		Peer peer;

89
		try {

90
			peer = new Peer(args[0], id, args[2], args[3], mcPort, args[5], mdbPort, args[7], mdrPort);

91
		} catch (RemoteException e) {

92
			e.printStackTrace();

93
			return;

94
		}

95
		//String filePath = Disk.resourcesPath + "loremipsum.pdf";

96
		
97
	}

98

  
99
	public void startRMI() {

100
		try {

101
			try {

102
				this.rmiReg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);

103
			} catch(Exception e) {

104
				this.rmiReg = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);

105
			}

106
			PeerInterface stub = (PeerInterface) UnicastRemoteObject.exportObject(this, 0);

107
			rmiReg.rebind(this.accessPoint, stub);

108
		} catch (RemoteException e) {

109
			e.printStackTrace();

110
		}

111
	}

112

  
113
	public void startChannels(String mcA, int mcP, String mdbA, int mdbP, String mdrA, int mdrP) throws IOException {

114

  
115
		this.mcChannel = new Listener(this, mcA, mcP);

116
		this.mdbChannel = new Listener(this, mdbA, mdbP);

117
		this.mdrChannel = new Listener(this, mdrA, mdrP);

118

  
119
		Thread mcListener = new Thread(this.mcChannel);

120
		Thread mdbListener = new Thread(this.mdbChannel);

121
		Thread mdrListener = new Thread(this.mdrChannel);

122

  
123
		mcListener.start();

124
		mdbListener.start();

125
		mdrListener.start();

126
	}

127

  
128
	public void backup(String path, int replicationDeg) {

129
		Thread t = new Thread(new Backup(this, path, replicationDeg));

130
		t.start();

131
		try {

132
			t.join();

133
		} catch (InterruptedException e) {

134
			e.printStackTrace();

135
		}

136
	}

137

  
138
	public void delete(String path) {

139
		Thread t = new Thread(new Delete(this, path));

140
		t.start();

141
		try {

142
			t.join();

143
		} catch (InterruptedException e) {

144
			e.printStackTrace();

145
		}

146
	}

147

  
148
	public void restore(String path) {

149
		Thread t = new Thread(new Restore(this, path));

150
		t.start();

151
		try {

152
			t.join();

153
		} catch (InterruptedException e) {

154
			e.printStackTrace();

155
		}

156
	}

157

  
158
	public void reclaim(float space) {

159

  
160
	}

161

  
162
	public String state() {

163
		return "test";

164
	}

165

  
166
	public void sendToMc(Message m) throws IOException {

167

  
168
		byte[] buf = m.toBytes();

169

  
170
		DatagramPacket packet = new DatagramPacket(buf, buf.length, this.mcChannel.getAddress(),

171
				this.mcChannel.getPort());

172
		socket.send(packet);

173

  
174
	}

175

  
176
	public void sendToMdb(Message m) throws IOException {

177

  
178
		byte[] buf = m.toBytes();

179

  
180
		DatagramPacket packet = new DatagramPacket(buf, buf.length, this.mdbChannel.getAddress(),

181
				this.mdbChannel.getPort());

182
		socket.send(packet);

183
	}	

184

  
185
	public void sendToMdr(Message m) throws IOException {

186

  
187
		byte[] buf = m.toBytes();

188

  
189
		DatagramPacket packet = new DatagramPacket(buf, buf.length, this.mdrChannel.getAddress(),

190
				this.mdrChannel.getPort());

191
		socket.send(packet);

192

  
193
	}

194

  
195
	/**

196
	 * @return the peerId

197
	 */

198
	public int getPeerId() {

199
		return peerId;

200
	}

201

  
202
	/**

203
	 * @return the disk

204
	 */

205
	public Disk getDisk() {

206
		return disk;

207
	}

208

  
209
	/**

210
	 * @return the rmiReg

211
	 */

212
	public Registry getRmiReg() {

213
		return rmiReg;

214
	}

215

  
216
	/**

217
	 * @return the pVersion

218
	 */

219
	public String getpVersion() {

220
		return pVersion;

221
	}

222

  
223
	/**

224
	 * @return the accessPoint

225
	 */

226
	public String getAccessPoint() {

227
		return accessPoint;

228
	}

229

  
230
	/**

231
	 * @return the socket

232
	 */

233
	public MulticastSocket getSocket() {

234
		return socket;

235
	}

236

  
237
	/**

238
	 * @return the mcChannel

239
	 */

240
	public Listener getMcChannel() {

241
		return mcChannel;

242
	}

243

  
244
	/**

245
	 * @return the mdbChannel

246
	 */

247
	public Listener getMdbChannel() {

248
		return mdbChannel;

249
	}

250

  
251
	/**

252
	 * @return the mdrChannel

253
	 */

254
	public Listener getMdrChannel() {

255
		return mdrChannel;

256
	}

257

  
258
	/**

259
	 * @return the mcAddress

260
	 */

261
	public InetAddress getMcAddress() {

262
		return mcAddress;

263
	}

264

  
265
	/**

266
	 * @return the mcPort

267
	 */

268
	public int getMcPort() {

269
		return mcPort;

270
	}

271

  
272
	/**

273
	 * @return the mdbAddress

274
	 */

275
	public InetAddress getMdbAddress() {

276
		return mdbAddress;

277
	}

278

  
279
	/**

280
	 * @return the mdbPort

281
	 */

282
	public int getMdbPort() {

283
		return mdbPort;

284
	}

285

  
286
	/**

287
	 * @return the mdrAddress

288
	 */

289
	public InetAddress getMdrAddress() {

290
		return mdrAddress;

291
	}

292

  
293
	/**

294
	 * @return the mdrPort

295
	 */

296
	public int getMdrPort() {

297
		return mdrPort;

298
	}

299

  
1
package peer;
2

  
3
import java.io.IOException;
4
import java.net.DatagramPacket;
5
import java.net.InetAddress;
6
import java.net.MulticastSocket;
7
import java.net.UnknownHostException;
8
import java.rmi.RemoteException;
9
import java.rmi.registry.LocateRegistry;
10
import java.rmi.registry.Registry;
11
import java.rmi.server.UnicastRemoteObject;
12

  
13
import disk.Disk;
14
import message.Message;
15
import peer.channels.Listener;
16
import peer.protocols.backup.Backup;
17
import peer.protocols.delete.Delete;
18
import peer.protocols.restore.Restore;
19

  
20
public class Peer /*extends UnicastRemoteObject*/ implements PeerInterface {
21

  
22
	//private static final long serialVersionUID = 4988282307993613946L;
23

  
24
	private int peerId;
25
	private Disk disk;
26

  
27
	private Registry rmiReg;
28

  
29
	private String pVersion;
30
	private String accessPoint;
31

  
32
	private MulticastSocket socket;
33

  
34
	private InetAddress mcAddress;
35
	private InetAddress mdbAddress;
36
	private InetAddress mdrAddress;
37
	private int mcPort;
38
	private int mdbPort;
39
	private int mdrPort;
40

  
41
	private Listener mcChannel;
42
	private Listener mdbChannel;
43
	private Listener mdrChannel;
44

  
45
	public Peer(String pVersion, int sid, String accessPoint, String mcAddress, int mcPort, String mdbAddress,
46
			int mdbPort, String mdrAddress, int mdrPort) throws RemoteException {
47
		
48
		this.peerId = sid;
49

  
50
		try {
51
			this.mcAddress = InetAddress.getByName(mcAddress);
52
			this.mdbAddress = InetAddress.getByName(mdbAddress);
53
			this.mdrAddress = InetAddress.getByName(mdrAddress);
54
		} catch (UnknownHostException e1) {
55
			e1.printStackTrace();
56
		}
57

  
58
		this.mcPort = mcPort;
59
		this.mdbPort = mdbPort;
60
		this.mdrPort = mdrPort;
61

  
62
		this.disk = new Disk("peer" + peerId);
63
		this.pVersion = pVersion;
64
		this.accessPoint = accessPoint;
65
		this.rmiReg = null;
66
		try {
67
			this.socket = new MulticastSocket();
68
		} catch (IOException e) {
69
			e.printStackTrace();
70
		}
71

  
72
		this.startRMI();
73

  
74
		try {
75
			this.startChannels(mcAddress, mcPort, mdbAddress, mdbPort, mdrAddress, mdrPort);
76
		} catch (IOException e) {
77
			e.printStackTrace();
78
		}
79

  
80
		System.out.println("Peer " + this.peerId + " running...");
81
	}
82

  
83
	public static void main(String[] args) {
84
		int id = Integer.parseInt(args[1]);
85
		int mcPort = Integer.parseInt(args[4]);
86
		int mdbPort = Integer.parseInt(args[6]);
87
		int mdrPort = Integer.parseInt(args[8]);
88
		Peer peer;
89
		try {
90
			peer = new Peer(args[0], id, args[2], args[3], mcPort, args[5], mdbPort, args[7], mdrPort);
91
		} catch (RemoteException e) {
92
			e.printStackTrace();
93
			return;
94
		}
95
		//String filePath = Disk.resourcesPath + "loremipsum.pdf";
96
		
97
	}
98

  
99
	public void startRMI() {
100
		try {
101
			try {
102
				this.rmiReg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
103
			} catch(Exception e) {
104
				this.rmiReg = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);
105
			}
106
			PeerInterface stub = (PeerInterface) UnicastRemoteObject.exportObject(this, 0);
107
			rmiReg.rebind(this.accessPoint, stub);
108
		} catch (RemoteException e) {
109
			e.printStackTrace();
110
		}
111
	}
112

  
113
	public void startChannels(String mcA, int mcP, String mdbA, int mdbP, String mdrA, int mdrP) throws IOException {
114

  
115
		this.mcChannel = new Listener(this, mcA, mcP);
116
		this.mdbChannel = new Listener(this, mdbA, mdbP);
117
		this.mdrChannel = new Listener(this, mdrA, mdrP);
118

  
119
		Thread mcListener = new Thread(this.mcChannel);
120
		Thread mdbListener = new Thread(this.mdbChannel);
121
		Thread mdrListener = new Thread(this.mdrChannel);
122

  
123
		mcListener.start();
124
		mdbListener.start();
125
		mdrListener.start();
126
	}
127

  
128
	public void backup(String path, int replicationDeg) {
129
		Thread t = new Thread(new Backup(this, path, replicationDeg));
130
		t.start();
131
		try {
132
			t.join();
133
		} catch (InterruptedException e) {
134
			e.printStackTrace();
135
		}
136
	}
137

  
138
	public void delete(String path) {
139
		Thread t = new Thread(new Delete(this, path));
140
		t.start();
141
		try {
142
			t.join();
143
		} catch (InterruptedException e) {
144
			e.printStackTrace();
145
		}
146
	}
147

  
148
	public void restore(String path) {
149
		Thread t = new Thread(new Restore(this, path));
150
		t.start();
151
		try {
152
			t.join();
153
		} catch (InterruptedException e) {
154
			e.printStackTrace();
155
		}
156
	}
157

  
158
	public void reclaim(float space) {
159

  
160
	}
161

  
162
	public String state() {
163
		return "test";
164
	}
165

  
166
	public void sendToMc(Message m) throws IOException {
167

  
168
		byte[] buf = m.toBytes();
169

  
170
		DatagramPacket packet = new DatagramPacket(buf, buf.length, this.mcChannel.getAddress(),
171
				this.mcChannel.getPort());
172
		socket.send(packet);
173

  
174
	}
175

  
176
	public void sendToMdb(Message m) throws IOException {
177

  
178
		byte[] buf = m.toBytes();
179

  
180
		DatagramPacket packet = new DatagramPacket(buf, buf.length, this.mdbChannel.getAddress(),
181
				this.mdbChannel.getPort());
182
		socket.send(packet);
183
	}	
184

  
185
	public void sendToMdr(Message m) throws IOException {
186

  
187
		byte[] buf = m.toBytes();
188

  
189
		DatagramPacket packet = new DatagramPacket(buf, buf.length, this.mdrChannel.getAddress(),
190
				this.mdrChannel.getPort());
191
		socket.send(packet);
192

  
193
	}
194

  
195
	/**
196
	 * @return the peerId
197
	 */
198
	public int getPeerId() {
199
		return peerId;
200
	}
201

  
202
	/**
203
	 * @return the disk
204
	 */
205
	public Disk getDisk() {
206
		return disk;
207
	}
208

  
209
	/**
210
	 * @return the rmiReg
211
	 */
212
	public Registry getRmiReg() {
213
		return rmiReg;
214
	}
215

  
216
	/**
217
	 * @return the pVersion
218
	 */
219
	public String getpVersion() {
220
		return pVersion;
221
	}
222

  
223
	/**
224
	 * @return the accessPoint
225
	 */
226
	public String getAccessPoint() {
227
		return accessPoint;
228
	}
229

  
230
	/**
231
	 * @return the socket
232
	 */
233
	public MulticastSocket getSocket() {
234
		return socket;
235
	}
236

  
237
	/**
238
	 * @return the mcChannel
239
	 */
240
	public Listener getMcChannel() {
241
		return mcChannel;
242
	}
243

  
244
	/**
245
	 * @return the mdbChannel
246
	 */
247
	public Listener getMdbChannel() {
248
		return mdbChannel;
249
	}
250

  
251
	/**
252
	 * @return the mdrChannel
253
	 */
254
	public Listener getMdrChannel() {
255
		return mdrChannel;
256
	}
257

  
258
	/**
259
	 * @return the mcAddress
260
	 */
261
	public InetAddress getMcAddress() {
262
		return mcAddress;
263
	}
264

  
265
	/**
266
	 * @return the mcPort
267
	 */
268
	public int getMcPort() {
269
		return mcPort;
270
	}
271

  
272
	/**
273
	 * @return the mdbAddress
274
	 */
275
	public InetAddress getMdbAddress() {
276
		return mdbAddress;
277
	}
278

  
279
	/**
280
	 * @return the mdbPort
281
	 */
282
	public int getMdbPort() {
283
		return mdbPort;
284
	}
285

  
286
	/**
287
	 * @return the mdrAddress
288
	 */
289
	public InetAddress getMdrAddress() {
290
		return mdrAddress;
291
	}
292

  
293
	/**
294
	 * @return the mdrPort
295
	 */
296
	public int getMdrPort() {
297
		return mdrPort;
298
	}
299

  
300 300
}

Also available in: Unified diff