-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryServiceImpl.java
More file actions
35 lines (30 loc) · 1.27 KB
/
Copy pathDirectoryServiceImpl.java
File metadata and controls
35 lines (30 loc) · 1.27 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
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DirectoryServiceImpl extends UnicastRemoteObject implements DirectoryService {
// Map<filename, list<daemon_addresses>>
private final Map<String, List<String>> fileLocations;
public DirectoryServiceImpl() throws RemoteException {
fileLocations = new HashMap<>();
}
@Override
public synchronized void registerFile(String fileName, String daemonAddress, int daemonPort) throws RemoteException {
String location = daemonAddress + ":" + daemonPort;
if (!fileLocations.containsKey(fileName)) {
fileLocations.put(fileName, new ArrayList<>());
}
List<String> locations = fileLocations.get(fileName);
if (!locations.contains(location)) {
locations.add(location);
}
System.out.println("Registered " + fileName + " at " + location);
}
@Override
public synchronized List<String> getDaemonLocations(String fileName) throws RemoteException {
// Return a list according to fileName or an empty one in 404 case
return fileLocations.getOrDefault(fileName, new ArrayList<>());
}
}