-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeerBinder.java
More file actions
352 lines (301 loc) · 9.58 KB
/
PeerBinder.java
File metadata and controls
352 lines (301 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Class that defines the server-side thread for all the peers. It implements
* the RMI interface of a PeerServer
*
* @author palvare3
*
*/
public class PeerBinder extends UnicastRemoteObject implements Runnable,
PeerServerInterface {
private static final long serialVersionUID = 1L;
private Peer peer;
private boolean timerOn = false;
private Stack<RegistryItem> queryRegistry = new Stack<RegistryItem>();
private int previousReqType = -1;
private Logger logger;
/**
* Creates a new PeerBinder object
*
* @param id
* of the peer
* @param p
* @throws RemoteException
*/
public PeerBinder(Peer p) throws RemoteException {
super();
this.peer = p;
this.logger = peer.getLogger();
}
@Override
public byte[] obtain(String filename) throws RemoteException {
String path;
InputStream is;
if (peer.isMaster(filename)) {
path = "./files/";
} else if (peer.isDownloaded(filename)) {
path = "./downloads/";
if (peer.hasFile(filename) != Peer.FILE_VALID) {
throw new RemoteException("File not up to date");
}
} else {
throw new RemoteException("File not found");
}
try {
is = new FileInputStream(path + filename);
File file = new File(path + filename);
// Get the size of the file
long length = file.length();
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
} catch (FileNotFoundException f) {
// This is for the case in which a file has been unexpectedly erased
// from the computer, so it is unregistered in the peer
peer.removeFile(filename);
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public void run() {
// Tries to retrieve the SecurityManager. If not possible, creates a new
// one
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
// Binds the server to a port
int port = Peer.BASE_RMI_PORT + peer.getId();
LocateRegistry.createRegistry(port);
System.out.println("java RMI registry created.");
System.out.println("Port: " + port);
System.out.println("Registration complete with id #" + peer.getId()
+ ", waiting for connections...");
Naming.rebind("//localhost:" + port + "/PeerServer" + peer.getId(),
this);
} catch (RemoteException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Set<Integer> queryServer(int senderId, String filename, int ttl,
int requestingId, int requestCode) throws RemoteException {
// Attempts to reconnect the neighbor
peer.connectNeighbor(senderId);
System.out.println();
if (requestCode == Peer.SEARCH_REQUEST) {
System.out.println("New query from " + senderId + " requesting "
+ filename + " for " + requestingId + " with TTL=" + ttl);
logger.log(Level.INFO, "New query from " + senderId + " requesting "
+ filename + " for " + requestingId + " with TTL=" + ttl);
} else {
System.out.println("New query from " + senderId + " invalidating "
+ filename + " with masterId=" + requestingId
+ " with TTL=" + ttl);
}
System.out.print("NutPeers > ");
Set<Integer> sources = new HashSet<Integer>();
if (peer.hasFile(filename) == Peer.FILE_VALID) {
if (requestCode == Peer.SEARCH_REQUEST) {
sources.add(peer.getId());
peer.incrementTotalRequests(true);
logger.log(Level.INFO, "Valid request");
} else if (requestCode == Peer.INVALIDATION_REQUEST) {
// Invalidate the file
System.out.println("Invalidating file: " + filename);
peer.invalidate(filename);
}
} else if ((peer.hasFile(filename) == Peer.FILE_TTR_EXPIRED)
&& (requestCode == Peer.SEARCH_REQUEST)) {
peer.incrementTotalRequests(false);
logger.log(Level.INFO, "Invalid request, TTR expired");
System.out.println("TTR expired in " + peer.getId()
+ " , updating...");
if (peer.lazyUpdate(filename)) {
sources.add(peer.getId());
}
} else {
peer.incrementTotalRequests(false);
logger.log(Level.INFO, "Invalid request, invalid file");
}
ttl--;
boolean same = false;
previousReqType = -1;
// The inclusion of a query registry boosts the performance of a peer,
// and by extension the whole network's. It compares the last query
// against a list of the queries the peer has received in the last 15
// seconds.
// If the requesting peer and the file are repeated, the peer does not
// forward again the query to its neighbors, saving a lot of load.
for (int i = 0; i < queryRegistry.size(); i++) {
String previousFilename = queryRegistry.get(i).getFilename();
int previousReq = queryRegistry.get(i).getRequestingId();
if ((previousFilename.equals(filename))
&& (previousReq == requestingId)
&& (previousReqType == requestCode)) {
System.out.println();
System.out.println("Same!");
System.out.print("NutPeers > ");
same = true;
}
}
// Tries to start the timer for clearing the registry
if (same) {
startTimer();
}
if ((ttl != 0) && (!same)) {
previousReqType = requestCode;
// Sets a capacity limit for the registry, so that the program
// doesn't run out of memory
if (queryRegistry.size() > 1000) {
queryRegistry.clear();
}
updateQueryRegistry(filename, requestingId);
Object[] neighbors = peer.getConnectedNeighbors().keySet()
.toArray();
// Queries to neighbors: all except the sender neighbor and the
// requesting peer in case it is a neighbor
for (int i = 0; i < neighbors.length; i++) {
if ((!neighbors[i].equals(senderId))
&& (!neighbors[i].equals(requestingId))) {
try {
PeerServerInterface neighborServer = peer
.getNeighborInterface((Integer) neighbors[i]);
if (requestCode == Peer.SEARCH_REQUEST) {
Set<Integer> destinations = neighborServer
.queryServer(peer.getId(), filename, ttl,
requestingId, Peer.SEARCH_REQUEST);
// This unifies all the sources found so far in a
// unique
// Set
sources.addAll(destinations);
} else {
neighborServer.queryServer(peer.getId(), filename,
ttl, requestingId,
Peer.INVALIDATION_REQUEST);
}
} catch (Exception e) {
System.out.println();
System.out.println("The server with id="
+ (Integer) neighbors[i]
+ " is not currently available.");
System.out.print("NutPeers > ");
peer.disconnectNeighbor((Integer) neighbors[i]);
}
}
}
}
if (requestCode == Peer.SEARCH_REQUEST) {
System.out.println();
System.out.println("Returning sources to " + senderId);
System.out.print("NutPeers > ");
}
return sources;
}
/**
* Updates the query registry with a new query
*
* @param filename
* @param requestingId
*/
private synchronized void updateQueryRegistry(String filename,
int requestingId) {
RegistryItem item = new RegistryItem(filename, requestingId);
queryRegistry.push(item);
}
/**
* Starts the timer if it hasn't yet been initialized
*/
private synchronized void startTimer() {
if (!timerOn) {
Thread timer = new TimerRestarter(this);
timer.start();
timerOn = true;
}
}
@Override
public synchronized int notifyConnection(int id) throws RemoteException {
peer.connectNeighbor(id);
System.out.println();
System.out.println("Found a neighbor with id: " + id);
System.out.print("NutPeers > ");
return PeerMain.MODE;
}
/**
* Clears the registry every 15 sec
*/
public synchronized void timeOutTimer() {
System.out.println();
System.out.println("Cleaning query registry");
System.out.print("NutPeers > ");
queryRegistry.clear();
timerOn = false;
}
/*--------- start change ----------*/
@Override
public int[] checkMetadata(String result) throws RemoteException {
return peer.getMetadata(result);
}
@Override
public void receiveNeighborsForInvalidation(
Map<Integer, Boolean> connectedNeighbors) throws RemoteException {
// Compares each set of neighbors so the invalidation is only sent to
// neighbors which are not in common
Object[] thisConnectedNeighbors = peer.getConnectedNeighbors().keySet()
.toArray();
Object[] otherConnectedNeighbors = connectedNeighbors.keySet()
.toArray();
List<Integer> notCommonDisconnected = new ArrayList<Integer>();
boolean notCommon = true;
for (Object i : otherConnectedNeighbors) {
notCommon = true;
for (Object j : thisConnectedNeighbors) {
if (j.equals(i)) {
notCommon = false;
}
}
if (notCommon)
notCommonDisconnected.add((Integer) i);
}
// Given not common disconnected, invalidates
for (Integer id : notCommonDisconnected) {
peer.invalidateFilesFromID(id);
}
}
/*--------- end change ----------*/
}