master
王宇洋 3 years ago
parent 5fff86bb58
commit 0f41476384

@ -16,6 +16,7 @@ import java.security.NoSuchAlgorithmException;
* @Description: * @Description:
**/ **/
public class Util { public class Util {
//A util of getting the ip address.
public static String getIP() { public static String getIP() {
try { try {
InetAddress ip4 = Inet4Address.getLocalHost(); InetAddress ip4 = Inet4Address.getLocalHost();
@ -25,6 +26,7 @@ public class Util {
return null; return null;
} }
} }
//get hash using SHA-1
public static String createSha1(File file){ public static String createSha1(File file){
MessageDigest digest = null; MessageDigest digest = null;
try { try {

@ -31,8 +31,11 @@ import java.util.*;
* @Description: * @Description:
**/ **/
public class ClientMain { public class ClientMain {
//The default main server address
public static String MainServerIP = "127.0.0.1"; public static String MainServerIP = "127.0.0.1";
//The default main server port
public static int RMI_PORT = 1099; public static int RMI_PORT = 1099;
//Information about this peer
public static Peer peer; public static Peer peer;
public static ObservableMapWrapper<String, Resource> DHRT = new ObservableMapWrapper<>(new LinkedHashMap<>()); public static ObservableMapWrapper<String, Resource> DHRT = new ObservableMapWrapper<>(new LinkedHashMap<>());
public static Integer retry_times = 0; public static Integer retry_times = 0;
@ -52,6 +55,7 @@ public class ClientMain {
public static void main(String[] args) throws ConnectException { public static void main(String[] args) throws ConnectException {
//service thread
service = new Thread(new Runnable() { service = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -64,6 +68,8 @@ public class ClientMain {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
//Command line of the peer
sc = new Scanner(System.in); sc = new Scanner(System.in);
System.out.println(""); System.out.println("");
System.out.print(">>> "); System.out.print(">>> ");
@ -71,9 +77,11 @@ public class ClientMain {
String line = sc.nextLine(); String line = sc.nextLine();
switch (line) { switch (line) {
case "\n": case "\n":
//Promote
System.out.print(">>> "); System.out.print(">>> ");
break; break;
case "i": case "i":
//print the information about his peer
System.out.println("GUID: " + peer.getGUID()); System.out.println("GUID: " + peer.getGUID());
System.out.println("P2P_Port: " + peer.getP2P_port()); System.out.println("P2P_Port: " + peer.getP2P_port());
System.out.println("Possessing: " + peer.getPossessing()); System.out.println("Possessing: " + peer.getPossessing());
@ -104,8 +112,11 @@ public class ClientMain {
} }
public static void download(String hash) { public static void download(String hash) {
//download by hash
//first look up this res
Resource resources = look_up_file(hash); Resource resources = look_up_file(hash);
if (resources != null) { if (resources != null) {
//found
System.out.println("Download: " + resources.getGUID()); System.out.println("Download: " + resources.getGUID());
download(resources); download(resources);
} }
@ -113,6 +124,7 @@ public class ClientMain {
} }
private static void download(String hash, Integer retry_count) { private static void download(String hash, Integer retry_count) {
//retry download
if (download_retry_count <= 5) { if (download_retry_count <= 5) {
download_retry_count += 1; download_retry_count += 1;
System.out.println("Retrying " + download_retry_count); System.out.println("Retrying " + download_retry_count);
@ -135,20 +147,25 @@ public class ClientMain {
Boolean file_status = false; Boolean file_status = false;
hasStarted = false; hasStarted = false;
try { try {
//Connect to the main server
registry = LocateRegistry.getRegistry(MainServerIP, RMI_PORT); registry = LocateRegistry.getRegistry(MainServerIP, RMI_PORT);
//check if connection is successful
center_status = lookup_registry() && register_peer() ? true : false; center_status = lookup_registry() && register_peer() ? true : false;
} catch (RemoteException e) { } catch (RemoteException e) {
// e.printStackTrace(); // e.printStackTrace();
} }
if (peer == null) { if (peer == null) {
//can't init peer, then server is not start.
System.out.println("Server Not Running."); System.out.println("Server Not Running.");
exit(); exit();
} }
//start regular heart beat to make sure the peer is alive
CHeartBeat.StartHeart(heartBeatRegistry, peer.getGUID()); CHeartBeat.StartHeart(heartBeatRegistry, peer.getGUID());
//Init file services //Init file services
try { try {
//init local server
file_service = LocateRegistry.createRegistry(Port); file_service = LocateRegistry.createRegistry(Port);
P2P_FileRegistry p2PFileRegistry = new P2PFileImpl(); P2P_FileRegistry p2PFileRegistry = new P2PFileImpl();
file_service.rebind("p2PFileRegistry", p2PFileRegistry); file_service.rebind("p2PFileRegistry", p2PFileRegistry);
@ -173,8 +190,10 @@ public class ClientMain {
private static Boolean register_peer() { private static Boolean register_peer() {
System.out.println("+++++++ Register & Construct Peer +++++++"); System.out.println("+++++++ Register & Construct Peer +++++++");
//get one available port
Port = NetUtil.getUsableLocalPort(35000, 45000); Port = NetUtil.getUsableLocalPort(35000, 45000);
try { try {
//get peer information from server
peer = constructRegistry.ConstructPeer(name, IP, Port); peer = constructRegistry.ConstructPeer(name, IP, Port);
System.out.println("+++++++ Peer Construct Success ! +++++++"); System.out.println("+++++++ Peer Construct Success ! +++++++");
System.out.println("Peer GUID: " + peer.getGUID()); System.out.println("Peer GUID: " + peer.getGUID());
@ -182,6 +201,7 @@ public class ClientMain {
System.out.println("+++++++ Register Finished +++++++"); System.out.println("+++++++ Register Finished +++++++");
return true; return true;
} catch (RemoteException e) { } catch (RemoteException e) {
//server connect failed
hasStarted = false; hasStarted = false;
System.out.println("register_peer RemoteException"); System.out.println("register_peer RemoteException");
e.printStackTrace(); e.printStackTrace();
@ -191,6 +211,7 @@ public class ClientMain {
private static Boolean lookup_registry() { private static Boolean lookup_registry() {
try { try {
//check if server is responding.
HelloRegistryFacade hello = (HelloRegistryFacade) registry.lookup("HelloRegistry"); HelloRegistryFacade hello = (HelloRegistryFacade) registry.lookup("HelloRegistry");
String response = hello.helloWorld(name); String response = hello.helloWorld(name);
System.out.println("=======> " + response + " <======="); System.out.println("=======> " + response + " <=======");
@ -198,6 +219,7 @@ public class ClientMain {
System.out.println("server failed"); System.out.println("server failed");
return false; return false;
} }
// get the server functions
constructRegistry = (ConstructRegistry) registry.lookup("constructRegistry"); constructRegistry = (ConstructRegistry) registry.lookup("constructRegistry");
heartBeatRegistry = (HeartBeatRegistry) registry.lookup("heatBeatRegistry"); heartBeatRegistry = (HeartBeatRegistry) registry.lookup("heatBeatRegistry");
fileLookupRegistry = (FileLookupRegistry) registry.lookup("syncingRegistry"); fileLookupRegistry = (FileLookupRegistry) registry.lookup("syncingRegistry");
@ -212,6 +234,7 @@ public class ClientMain {
} }
public static Boolean reg_file(String name) { public static Boolean reg_file(String name) {
//register a file
File file = new File("res/" + name); File file = new File("res/" + name);
if (!file.exists()) { if (!file.exists()) {
System.out.println("File not exists !"); System.out.println("File not exists !");
@ -221,14 +244,18 @@ public class ClientMain {
System.out.println("File ok."); System.out.println("File ok.");
System.out.println("File length: " + file.length()); System.out.println("File length: " + file.length());
} }
//get file hash
String hash = Util.createSha1(file); String hash = Util.createSha1(file);
System.out.println(hash); System.out.println(hash);
try { try {
//send the information about the file to the server
Resource resource = constructRegistry.ConstructResource(peer.getGUID(), name, hash); Resource resource = constructRegistry.ConstructResource(peer.getGUID(), name, hash);
//save it to local DHRT
ClientMain.DHRT.put(resource.getGUID(), resource); ClientMain.DHRT.put(resource.getGUID(), resource);
DHRT.put(resource.getGUID(), resource); // DHRT.put(resource.getGUID(), resource);
System.out.println("File Register Success!"); System.out.println("File Register Success!");
System.out.println("Resource GUID: " + resource.getGUID()); System.out.println("Resource GUID: " + resource.getGUID());
//update possessing list
sync_peer(); sync_peer();
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
@ -239,6 +266,7 @@ public class ClientMain {
} }
private static void sync_peer() { private static void sync_peer() {
//update possessing list
try { try {
peer = fileLookupRegistry.syncPeer(peer.getGUID()); peer = fileLookupRegistry.syncPeer(peer.getGUID());
} catch (RemoteException e) { } catch (RemoteException e) {
@ -249,36 +277,48 @@ public class ClientMain {
} }
public static Resource look_up_file(String hash) { public static Resource look_up_file(String hash) {
//look up a file using hash
System.out.println("Looking HASH for: " + hash); System.out.println("Looking HASH for: " + hash);
//try to get from local DHRT
Resource res = ClientMain.DHRT.get(hash); Resource res = ClientMain.DHRT.get(hash);
if (res == null) { if (res == null) {
//not found in local
System.out.println("Not found in local DHRT"); System.out.println("Not found in local DHRT");
System.out.println("Lookup in UHRT..."); System.out.println("Lookup in UHRT...");
try { try {
//let server find this file
res = fileLookupRegistry.lookupInUHRT(hash); res = fileLookupRegistry.lookupInUHRT(hash);
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (res == null) { if (res == null) {
//not in server UHRT
System.out.println("Not found in UHRT"); System.out.println("Not found in UHRT");
System.out.println("Abort"); System.out.println("Abort");
return null; return null;
} }
//found the file in server UHRT
System.out.println("Found in UHRT: " + res.getGUID()); System.out.println("Found in UHRT: " + res.getGUID());
System.out.println("Update DHRT"); System.out.println("Update DHRT");
ClientMain.DHRT.put(res.getGUID(), res); ClientMain.DHRT.put(res.getGUID(), res);
} }
else{ else{
//found in local
System.out.println("Found in DHRT."); System.out.println("Found in DHRT.");
} }
return res; return res;
} }
public static void download(Resource resources) { public static void download(Resource resources) {
//download by resources
if (resources == null || resources.possessedBy.size() == 0) { if (resources == null || resources.possessedBy.size() == 0) {
//resource is invalid
System.out.println("Resource can not be download !"); System.out.println("Resource can not be download !");
return; return;
} }
//if processed by more than one peer(DHRT), then do sort.
//Although server has give the best peer, but since the DHRT is not always up-to-date
//so make sure is the best peer.
ArrayList<Peer> processed_peers = new ArrayList<>(); ArrayList<Peer> processed_peers = new ArrayList<>();
for (Peer p : resources.possessedBy.values()) { for (Peer p : resources.possessedBy.values()) {
processed_peers.add(p); processed_peers.add(p);
@ -291,19 +331,23 @@ public class ClientMain {
}); });
Peer best_peer = processed_peers.get(0); Peer best_peer = processed_peers.get(0);
System.out.println("Best peer is: " + best_peer.getGUID()); System.out.println("Best peer is: " + best_peer.getGUID());
//go download with the best peer
P2P_download(best_peer, resources); P2P_download(best_peer, resources);
} }
public static void P2P_download(Peer p, Resource resource) { public static void P2P_download(Peer p, Resource resource) {
//download in a new thread
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
//start download timing
long start_time = System.currentTimeMillis(); long start_time = System.currentTimeMillis();
Integer port = p.getP2P_port(); Integer port = p.getP2P_port();
String IP = p.getIP(); String IP = p.getIP();
Registry p2pRegistry = null; Registry p2pRegistry = null;
File file = null; File file = null;
try { try {
//get target peer connection
p2pRegistry = LocateRegistry.getRegistry(IP, port); p2pRegistry = LocateRegistry.getRegistry(IP, port);
System.out.println("Try to connect: " + p.getGUID()); System.out.println("Try to connect: " + p.getGUID());
P2P_FileRegistry p2PFileRegistry = (P2P_FileRegistry) p2pRegistry.lookup("p2PFileRegistry"); P2P_FileRegistry p2PFileRegistry = (P2P_FileRegistry) p2pRegistry.lookup("p2PFileRegistry");
@ -311,13 +355,16 @@ public class ClientMain {
System.out.println("Downloading FROM: " + p.getGUID()); System.out.println("Downloading FROM: " + p.getGUID());
byte[] file_byte = p2PFileRegistry.download(resource.getGUID()); byte[] file_byte = p2PFileRegistry.download(resource.getGUID());
System.out.println("File byte[] length: " + file_byte.length); System.out.println("File byte[] length: " + file_byte.length);
//end download timing
long end_time = System.currentTimeMillis(); long end_time = System.currentTimeMillis();
long start_writing_time = System.currentTimeMillis(); long start_writing_time = System.currentTimeMillis();
//write to file
file = FileUtil.writeBytes(file_byte, new File("download/" + peer.getGUID() + "_FROM_" + p.getGUID() + "_" + resource.getName())); file = FileUtil.writeBytes(file_byte, new File("download/" + peer.getGUID() + "_FROM_" + p.getGUID() + "_" + resource.getName()));
long end_writing_time = System.currentTimeMillis(); long end_writing_time = System.currentTimeMillis();
System.out.println("File downloaded !"); System.out.println("File downloaded !");
System.out.println("File length : " + file.length()); System.out.println("File length : " + file.length());
//display the result regarding the file size.
float file_size_B = file.length(); float file_size_B = file.length();
float file_size_KB = file.length() / 1024; float file_size_KB = file.length() / 1024;
float file_size_MB = file_size_KB / 1024; float file_size_MB = file_size_KB / 1024;
@ -347,9 +394,11 @@ public class ClientMain {
} }
} catch (RemoteException e) { } catch (RemoteException e) {
//download failed.
System.out.println("RemoteException"); System.out.println("RemoteException");
DHRT.clear(); DHRT.clear();
try { try {
//do re-lookup
Resource res = fileLookupRegistry.lookupInUHRT(resource.getGUID()); Resource res = fileLookupRegistry.lookupInUHRT(resource.getGUID());
if (res != null) if (res != null)
DHRT.put(res.getGUID(), res); DHRT.put(res.getGUID(), res);
@ -368,6 +417,7 @@ public class ClientMain {
public static void P2P_download(Peer p, Resource resource, Runnable runnable) { public static void P2P_download(Peer p, Resource resource, Runnable runnable) {
//same as the upper function, but with a customizable runnable after the file downloaded
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -496,6 +546,7 @@ public class ClientMain {
} }
public static void recover(int retry_count) { public static void recover(int retry_count) {
//self-recovery
retry_count += 1; retry_count += 1;
System.out.println("Try to recover......." + retry_count); System.out.println("Try to recover......." + retry_count);
if (retry_count > 10) { if (retry_count > 10) {
@ -512,6 +563,7 @@ public class ClientMain {
@Override @Override
public void run() { public void run() {
try { try {
//wait 1s then try again.
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();

@ -11,6 +11,7 @@ import java.rmi.ConnectException;
**/ **/
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
//Limit testing
for(int i = 0; i < 5000; i++){ for(int i = 0; i < 5000; i++){
try { try {
Thread.sleep(10); Thread.sleep(10);

@ -51,6 +51,7 @@ public class ClientIndexController {
public Button lookup_button; public Button lookup_button;
public void initialize() { public void initialize() {
//Init GUI Controller
ObservableList<String> local_file_list = new ObservableListWrapper<>(Collections.synchronizedList(new ArrayList<>())); ObservableList<String> local_file_list = new ObservableListWrapper<>(Collections.synchronizedList(new ArrayList<>()));
ObservableList<Resource> remote_file_list = new ObservableListWrapper<>(Collections.synchronizedList(new ArrayList<>())); ObservableList<Resource> remote_file_list = new ObservableListWrapper<>(Collections.synchronizedList(new ArrayList<>()));
ClientMain.DHRT.addListener(new MapChangeListener<String, Resource>() { ClientMain.DHRT.addListener(new MapChangeListener<String, Resource>() {

@ -21,6 +21,7 @@ import java.io.IOException;
public class ClientIndexView extends Application { public class ClientIndexView extends Application {
@Override @Override
public void start(Stage stage) throws IOException { public void start(Stage stage) throws IOException {
//GUI main view
FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtil.getResource("gui/client_index.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtil.getResource("gui/client_index.fxml"));
Scene scene = new Scene(fxmlLoader.load()); Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Client!"); stage.setTitle("Client!");

@ -13,5 +13,6 @@ import java.util.UUID;
* @Description: * @Description:
**/ **/
public interface P2P_FileRegistry extends Remote { public interface P2P_FileRegistry extends Remote {
//Proved the services of letting other peer download from this peer.
byte[] download(String resID) throws RemoteException; byte[] download(String resID) throws RemoteException;
} }

@ -30,6 +30,7 @@ public class ServerMain{
private static Thread service; private static Thread service;
public static void main(String[] args) { public static void main(String[] args) {
//start service
service = new Thread(new Runnable() { service = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -37,6 +38,7 @@ public class ServerMain{
} }
}); });
service.start(); service.start();
//start command line promote
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
System.out.println(""); System.out.println("");
System.out.print(">>> "); System.out.print(">>> ");
@ -47,6 +49,7 @@ public class ServerMain{
System.out.print(">>> "); System.out.print(">>> ");
break; break;
case "i": case "i":
//get information about the server
System.out.println("UHPT: " + UHPT); System.out.println("UHPT: " + UHPT);
System.out.println("UHPT Size: " + UHPT.size()); System.out.println("UHPT Size: " + UHPT.size());
System.out.println("UHRT: " + UHRT); System.out.println("UHRT: " + UHRT);
@ -73,6 +76,7 @@ public class ServerMain{
System.out.println("Registered: " + s); System.out.println("Registered: " + s);
} }
System.out.println("============ WatchDog ============"); System.out.println("============ WatchDog ============");
//start watch dog.
if (!HasStarted) if (!HasStarted)
WatchDog.startWatchDog(); WatchDog.startWatchDog();
System.out.println("======= Start Up Finished ========"); System.out.println("======= Start Up Finished ========");
@ -85,6 +89,7 @@ public class ServerMain{
private static void reg_services() { private static void reg_services() {
try { try {
//Register all the services.
HelloRegistryFacade hello = new HelloRegistryFacadeImpl(); HelloRegistryFacade hello = new HelloRegistryFacadeImpl();
ConstructRegistry constructRegistry = new ConstructImpl(); ConstructRegistry constructRegistry = new ConstructImpl();
HeartBeatRegistry heartBeatRegistry = new HeartBeatImpl(); HeartBeatRegistry heartBeatRegistry = new HeartBeatImpl();

@ -15,6 +15,8 @@ import java.util.UUID;
* @Description: * @Description:
**/ **/
public interface ConstructRegistry extends Remote { public interface ConstructRegistry extends Remote {
//saving the peer information and transfer back to the peer.
Peer ConstructPeer(String name, String IP, Integer P2P_port) throws RemoteException; Peer ConstructPeer(String name, String IP, Integer P2P_port) throws RemoteException;
//saving the resource information and transfer back to the peer.
Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException; Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException;
} }

@ -16,7 +16,8 @@ import java.util.UUID;
* @Description: * @Description:
**/ **/
public interface FileLookupRegistry extends Remote { public interface FileLookupRegistry extends Remote {
//update the peer's possessing list
Peer syncPeer(UUID GUID) throws RemoteException; Peer syncPeer(UUID GUID) throws RemoteException;
// HashMap lookupInUHRT(String filename) throws RemoteException; //lookup in server UHRT using the GUID(hash)
Resource lookupInUHRT(String hash) throws RemoteException; Resource lookupInUHRT(String hash) throws RemoteException;
} }

@ -12,5 +12,6 @@ import java.util.UUID;
* @Description: * @Description:
**/ **/
public interface HeartBeatRegistry extends Remote { public interface HeartBeatRegistry extends Remote {
//make sure the peer is alive
Boolean heartBeat(UUID GUID) throws RemoteException; Boolean heartBeat(UUID GUID) throws RemoteException;
} }

@ -11,5 +11,6 @@ import java.rmi.RemoteException;
* @Description: * @Description:
**/ **/
public interface HelloRegistryFacade extends Remote { public interface HelloRegistryFacade extends Remote {
//a test for checking if the server is responding
String helloWorld(String name) throws RemoteException; String helloWorld(String name) throws RemoteException;
} }

@ -36,23 +36,28 @@ public class ConstructImpl extends UnicastRemoteObject implements ConstructRegis
@Override @Override
public Peer ConstructPeer(String name, String IP_Address, Integer P2P_port) throws RemoteException { public Peer ConstructPeer(String name, String IP_Address, Integer P2P_port) throws RemoteException {
//get a random UUID
UUID GUID = UUID.randomUUID(); UUID GUID = UUID.randomUUID();
if(ServerMain.UHPT.containsKey(GUID)) { if(ServerMain.UHPT.containsKey(GUID)) {
//Really Rarely happen, Peer reg failed.
System.out.println("DUP GUID!!!!!!!"); System.out.println("DUP GUID!!!!!!!");
return null; return null;
} }
//information
String peerName = name; String peerName = name;
String IP = IP_Address; String IP = IP_Address;
Integer port = P2P_port; Integer port = P2P_port;
Random random = new Random(); Random random = new Random();
Integer routingMetric = random.nextInt(100); Integer routingMetric = random.nextInt(100);
Peer peer = new Peer(GUID, peerName, IP, port, routingMetric); Peer peer = new Peer(GUID, peerName, IP, port, routingMetric);
//put in to UHPT
ServerMain.UHPT.put(peer.getGUID(), peer); ServerMain.UHPT.put(peer.getGUID(), peer);
System.out.println("Peer Reg: " + peer); System.out.println("Peer Reg: " + peer);
return peer; return peer;
} }
@Override @Override
public Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException { public Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException {
//get Peer from UHPT to insure the integrity
Peer peer = ServerMain.UHPT.get(PeerGUID); Peer peer = ServerMain.UHPT.get(PeerGUID);
//If peer not registered in center //If peer not registered in center
@ -61,18 +66,23 @@ public class ConstructImpl extends UnicastRemoteObject implements ConstructRegis
//If duplicated GUID //If duplicated GUID
if(ServerMain.UHRT.containsKey(hash)) { if(ServerMain.UHRT.containsKey(hash)) {
// Res is in UHRT // Res is in UHRT, so DUP GUID
System.out.println("DUP GUID! Res is in UHRT"); System.out.println("DUP GUID! Res is in UHRT");
Resource res = ServerMain.UHRT.get(hash); Resource res = ServerMain.UHRT.get(hash);
//add to possessedBy
res.possessedBy.put(peer.getGUID(), peer); res.possessedBy.put(peer.getGUID(), peer);
//add to possessing
ServerMain.UHPT.get(peer.getGUID()).possessing.put(res.getGUID(), res); ServerMain.UHPT.get(peer.getGUID()).possessing.put(res.getGUID(), res);
return res; return res;
} }
String ResName = name; String ResName = name;
//not in UHRT, create a new one
Resource res = new Resource(hash, ResName, hash); Resource res = new Resource(hash, ResName, hash);
//add to possessedBy
res.possessedBy.put(peer.getGUID(), peer); res.possessedBy.put(peer.getGUID(), peer);
ServerMain.UHRT.put(res.getGUID(), res); ServerMain.UHRT.put(res.getGUID(), res);
//add to possessing
ServerMain.UHPT.get(peer.getGUID()).possessing.put(res.getGUID(), res); ServerMain.UHPT.get(peer.getGUID()).possessing.put(res.getGUID(), res);
System.out.println("Resources Registered: " + res); System.out.println("Resources Registered: " + res);
return res; return res;

@ -42,11 +42,13 @@ public class FileLookupImpl extends UnicastRemoteObject implements FileLookupReg
@Override @Override
public Resource lookupInUHRT(String hash) throws RemoteException { public Resource lookupInUHRT(String hash) throws RemoteException {
//try to find the resource.
Resource res = ServerMain.UHRT.get(hash); Resource res = ServerMain.UHRT.get(hash);
if(res==null){ if(res==null){
//not found
return null; return null;
} }
//find the best peer.
ArrayList<Peer> processed_peers = new ArrayList<>(); ArrayList<Peer> processed_peers = new ArrayList<>();
for (Peer p : res.possessedBy.values()) { for (Peer p : res.possessedBy.values()) {
processed_peers.add(p); processed_peers.add(p);
@ -62,6 +64,7 @@ public class FileLookupImpl extends UnicastRemoteObject implements FileLookupReg
System.out.println(peer.getGUID() + " <> " + peer.getRoutingMetric()); System.out.println(peer.getGUID() + " <> " + peer.getRoutingMetric());
} }
System.out.println("BEST: " + best_peer.getGUID()); System.out.println("BEST: " + best_peer.getGUID());
//Prepare the return result.
Resource tmp = new Resource(res.getGUID(), res.getName(), res.getHash()); Resource tmp = new Resource(res.getGUID(), res.getName(), res.getHash());
tmp.possessedBy.put(best_peer.getGUID(), best_peer); tmp.possessedBy.put(best_peer.getGUID(), best_peer);

@ -33,9 +33,11 @@ public class HeartBeatImpl extends UnicastRemoteObject implements HeartBeatRegis
@Override @Override
public Boolean heartBeat(UUID GUID) throws RemoteException { public Boolean heartBeat(UUID GUID) throws RemoteException {
//ge the peer
Peer peer = ServerMain.UHPT.get(GUID); Peer peer = ServerMain.UHPT.get(GUID);
if(peer == null) if(peer == null)
return false; return false;
//zero out the counter
peer.setMissedHartBeat(0); peer.setMissedHartBeat(0);
return true; return true;
} }

@ -28,6 +28,7 @@ public class HelloRegistryFacadeImpl extends UnicastRemoteObject implements Hell
*/ */
public HelloRegistryFacadeImpl() throws RemoteException { public HelloRegistryFacadeImpl() throws RemoteException {
super(); super();
//a test.
System.out.println("HelloRegistryFacadeImpl"); System.out.println("HelloRegistryFacadeImpl");
} }

@ -35,12 +35,16 @@ public class WatchDog {
return; return;
// System.out.println(p); // System.out.println(p);
if (p == null) { if (p == null) {
//Really rarely happen, remove the null peer.
System.out.println("REMOVED(peer is null): " + uuid); System.out.println("REMOVED(peer is null): " + uuid);
ServerMain.UHPT.remove(uuid); ServerMain.UHPT.remove(uuid);
continue; continue;
} }
//increment the counter
p.setMissedHartBeat(p.getMissedHartBeat() + 1); p.setMissedHartBeat(p.getMissedHartBeat() + 1);
if (p.getMissedHartBeat() > 5) { if (p.getMissedHartBeat() > 5) {
//if missed is grater than 5, then this peer is down.
//remove it.
System.out.println(); System.out.println();
System.out.println("==================================== REMOVE ====================================="); System.out.println("==================================== REMOVE =====================================");
System.out.println("PEER (peer MissedHartBeat > 5): " + uuid); System.out.println("PEER (peer MissedHartBeat > 5): " + uuid);
@ -48,9 +52,11 @@ public class WatchDog {
System.out.println("Resources: " + guid); System.out.println("Resources: " + guid);
Resource resource = ServerMain.UHRT.get(guid); Resource resource = ServerMain.UHRT.get(guid);
if(resource.possessedBy.size() == 1) { if(resource.possessedBy.size() == 1) {
//remove from UHRT
ServerMain.UHRT.remove(guid); ServerMain.UHRT.remove(guid);
} }
else { else {
//remove from possessedBy
resource.possessedBy.remove(uuid); resource.possessedBy.remove(uuid);
} }
} }
@ -59,6 +65,7 @@ public class WatchDog {
} }
} }
try { try {
//sleep and run again.
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
System.out.println("WatchDog Interrupted"); System.out.println("WatchDog Interrupted");

@ -19,6 +19,7 @@ import java.util.UUID;
* @Description: * @Description:
**/ **/
public class Peer implements Serializable,Comparable { public class Peer implements Serializable,Comparable {
//All the information about the Peer.
private UUID GUID; private UUID GUID;
private String name; private String name;
private String IP; private String IP;
@ -146,6 +147,6 @@ public class Peer implements Serializable,Comparable {
@Override @Override
public int compareTo(@NotNull Object o) { public int compareTo(@NotNull Object o) {
Peer p = (Peer) o; Peer p = (Peer) o;
return p.routingMetric - p.routingMetric; return p.routingMetric - this.routingMetric;
} }
} }

@ -15,6 +15,7 @@ import java.util.UUID;
* @Description: * @Description:
**/ **/
public class Resource implements Serializable { public class Resource implements Serializable {
//All the information about the resource.
private String GUID; private String GUID;
private String name; private String name;
public HashMap<UUID, Peer> possessedBy = new LinkedHashMap<>(); public HashMap<UUID, Peer> possessedBy = new LinkedHashMap<>();

Loading…
Cancel
Save