master
王宇洋 3 years ago
parent 80da37f845
commit 5fff86bb58

@ -1,5 +0,0 @@
hahaha
================
This File is 1.a
================

@ -8,9 +8,9 @@ import com.echo.p2p_project.client.interfaces.P2P_FileRegistry;
import com.echo.p2p_project.client.model.CHeartBeat; import com.echo.p2p_project.client.model.CHeartBeat;
import com.echo.p2p_project.client.model.P2PFileImpl; import com.echo.p2p_project.client.model.P2PFileImpl;
import com.echo.p2p_project.server.interfaces.ConstructRegistry; import com.echo.p2p_project.server.interfaces.ConstructRegistry;
import com.echo.p2p_project.server.interfaces.FileLookupRegistry;
import com.echo.p2p_project.server.interfaces.HeartBeatRegistry; import com.echo.p2p_project.server.interfaces.HeartBeatRegistry;
import com.echo.p2p_project.server.interfaces.HelloRegistryFacade; import com.echo.p2p_project.server.interfaces.HelloRegistryFacade;
import com.echo.p2p_project.server.interfaces.SyncingRegistry;
import com.echo.p2p_project.u_model.Peer; import com.echo.p2p_project.u_model.Peer;
import com.echo.p2p_project.u_model.Resource; import com.echo.p2p_project.u_model.Resource;
import com.sun.javafx.collections.ObservableMapWrapper; import com.sun.javafx.collections.ObservableMapWrapper;
@ -34,7 +34,7 @@ public class ClientMain {
public static String MainServerIP = "127.0.0.1"; public static String MainServerIP = "127.0.0.1";
public static int RMI_PORT = 1099; public static int RMI_PORT = 1099;
public static Peer peer; public static Peer peer;
public static ObservableMapWrapper<UUID, 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;
private static String name = "Peer"; private static String name = "Peer";
private static String IP = Util.getIP(); private static String IP = Util.getIP();
@ -44,7 +44,7 @@ public class ClientMain {
private static Registry file_service; private static Registry file_service;
private static ConstructRegistry constructRegistry; private static ConstructRegistry constructRegistry;
private static HeartBeatRegistry heartBeatRegistry; private static HeartBeatRegistry heartBeatRegistry;
private static SyncingRegistry syncingRegistry; private static FileLookupRegistry fileLookupRegistry;
private static Boolean hasStarted = false; private static Boolean hasStarted = false;
private static Scanner sc; private static Scanner sc;
private static Thread service; private static Thread service;
@ -86,13 +86,14 @@ public class ClientMain {
reg_file(res_name); reg_file(res_name);
break; break;
case "l": case "l":
System.out.print("Look up Filename: "); System.out.print("Look up File HASH: ");
String file_name = sc.nextLine(); String hash = sc.nextLine();
HashMap res = look_up_file(file_name); Resource res = look_up_file(hash);
System.out.println(res); if (res != null)
System.out.println(res);
break; break;
case "d": case "d":
System.out.print("Download Filename: "); System.out.print("Download File HASH: ");
String file_name_to_download = sc.nextLine(); String file_name_to_download = sc.nextLine();
download(file_name_to_download); download(file_name_to_download);
@ -102,17 +103,20 @@ public class ClientMain {
} }
} }
public static void download(String file_name_to_download) { public static void download(String hash) {
HashMap resources = look_up_file(file_name_to_download); Resource resources = look_up_file(hash);
download(resources); if (resources != null) {
System.out.println("Download: " + resources.getGUID());
download(resources);
}
download_retry_count = 0; download_retry_count = 0;
} }
private static void download(String file_name_to_download, Integer retry_count) { private static void download(String hash, Integer retry_count) {
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);
HashMap resources = look_up_file(file_name_to_download); Resource resources = look_up_file(hash);
try { try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -196,7 +200,7 @@ public class ClientMain {
} }
constructRegistry = (ConstructRegistry) registry.lookup("constructRegistry"); constructRegistry = (ConstructRegistry) registry.lookup("constructRegistry");
heartBeatRegistry = (HeartBeatRegistry) registry.lookup("heatBeatRegistry"); heartBeatRegistry = (HeartBeatRegistry) registry.lookup("heatBeatRegistry");
syncingRegistry = (SyncingRegistry) registry.lookup("syncingRegistry"); fileLookupRegistry = (FileLookupRegistry) registry.lookup("syncingRegistry");
return true; return true;
} catch (NotBoundException | RemoteException e) { } catch (NotBoundException | RemoteException e) {
@ -236,7 +240,7 @@ public class ClientMain {
private static void sync_peer() { private static void sync_peer() {
try { try {
peer = syncingRegistry.syncPeer(peer.getGUID()); peer = fileLookupRegistry.syncPeer(peer.getGUID());
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("Peer Sync Failed !"); System.out.println("Peer Sync Failed !");
@ -244,139 +248,50 @@ public class ClientMain {
System.out.println("Peer Sync Finished !"); System.out.println("Peer Sync Finished !");
} }
public static HashMap<UUID, Resource> look_up_file(String file_name) { public static Resource look_up_file(String hash) {
HashMap<UUID, Resource> result = new LinkedHashMap<>(); System.out.println("Looking HASH for: " + hash);
System.out.println("Looking for: " + file_name); Resource res = ClientMain.DHRT.get(hash);
for (Resource r : DHRT.values()) { if (res == null) {
if (r.getName().equals(file_name)) { System.out.println("Not found in local DHRT");
System.out.println("FOUND in local DHRT."); System.out.println("Lookup in UHRT...");
result.put(r.getGUID(), r); try {
new Thread(new Runnable() { res = fileLookupRegistry.lookupInUHRT(hash);
@Override } catch (RemoteException e) {
public void run() { e.printStackTrace();
try {
HashMap hashMap = syncingRegistry.syncUHRT();
DHRT.putAll(hashMap);
System.out.println(Thread.currentThread() + "sync UHRT Finished.");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}).start();
} }
} if (res == null) {
if (result.size() == 0) System.out.println("Not found in UHRT");
System.out.println("Not found in local DHRT."); System.out.println("Abort");
else return null;
return result;
try {
HashMap hashMap = syncingRegistry.syncUHRT();
DHRT.putAll(hashMap);
System.out.println("DHRT Sync Finished !");
} catch (RemoteException e) {
e.printStackTrace();
System.out.println("DHRT Sync Failed !");
}
for (Resource r : DHRT.values()) {
if (r.getName().equals(file_name)) {
System.out.println("FOUNT Resource in DHRT (After SYNC REMOTE UHRT)");
result.put(r.getGUID(), r);
} }
System.out.println("Found in UHRT: " + res.getGUID());
System.out.println("Update DHRT");
ClientMain.DHRT.put(res.getGUID(), res);
}
else{
System.out.println("Found in DHRT.");
} }
if (result.size() == 0) return res;
System.out.println("Requested Resource NOT FOUND !");
return result;
} }
public static void download(HashMap<UUID, Resource> resources) { public static void download(Resource resources) {
if (resources.size() <= 0) { if (resources == null || resources.possessedBy.size() == 0) {
System.out.println("Resource can not be download !"); System.out.println("Resource can not be download !");
return; return;
} }
if (resources.size() > 1) { ArrayList<Peer> processed_peers = new ArrayList<>();
System.out.println("There are more than 1 resource with identical name:"); for (Peer p : resources.possessedBy.values()) {
System.out.println("File List: "); processed_peers.add(p);
for (Resource r : resources.values()) {
System.out.println(r);
}
System.out.println("Checking file hashing...");
Boolean hash_all_same = true;
String file_hash = ((Resource) resources.values().toArray()[0]).getHash();
for (Resource r : resources.values()) {
if (!r.getHash().equals(file_hash)) {
hash_all_same = false;
break;
}
}
UUID GUID = null;
if (hash_all_same) {
System.out.println("Hash is the same, looking for best node...");
ArrayList<Peer> processed_peers = new ArrayList<>();
for (Resource r : resources.values()) {
for (Peer p : r.possessedBy.values()) {
processed_peers.add(p);
}
}
processed_peers.sort(new Comparator<Peer>() {
@Override
public int compare(Peer o1, Peer o2) {
return o1.getRoutingMetric() - o2.getRoutingMetric();
}
});
for (Peer p : processed_peers) {
System.out.println(p.getGUID().toString() + " <> " + p.getRoutingMetric());
}
for (Resource r : processed_peers.get(0).possessing.values()) {
System.out.println("File hash: " + file_hash);
System.out.println("Res hash : " + r.getHash());
if (r.getHash().equals(file_hash)) {
System.out.println("Best resource GUID: " + r.getGUID());
GUID = r.getGUID();
break;
}
}
} else {
System.out.println("The hash of these files is different, you need to specify the GUID");
System.out.println("Please specify the GUID of the resource: ");
try {
GUID = UUID.fromString(sc.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("UUID Error");
return;
}
}
if (GUID == null) {
System.out.println("UUID Error");
return;
}
Resource resource = resources.get(GUID);
if (resource == null) {
System.out.println("Input GUID not in DHRT");
return;
}
Peer p = (Peer) resource.possessedBy.values().stream().sorted().toArray()[0];
P2P_download(p, resource);
} else {
Resource resource = (Resource) resources.values().toArray()[0];
if (resource == null)
return;
Peer p = (Peer) resource.possessedBy.values().stream().sorted().toArray()[0];
P2P_download(p, resource);
} }
} processed_peers.sort(new Comparator<Peer>() {
@Override
public static void sync_DHRT() { public int compare(Peer o1, Peer o2) {
HashMap hashMap = null; return o1.getRoutingMetric() - o2.getRoutingMetric();
try { }
System.out.println("Start syncUHRT"); });
hashMap = syncingRegistry.syncUHRT(); Peer best_peer = processed_peers.get(0);
DHRT.putAll(hashMap); System.out.println("Best peer is: " + best_peer.getGUID());
} catch (RemoteException e) { P2P_download(best_peer, resources);
e.printStackTrace();
}
System.out.println("END syncUHRT");
} }
public static void P2P_download(Peer p, Resource resource) { public static void P2P_download(Peer p, Resource resource) {
@ -433,10 +348,13 @@ public class ClientMain {
} catch (RemoteException e) { } catch (RemoteException e) {
System.out.println("RemoteException"); System.out.println("RemoteException");
DHRT.clear();
try { try {
HashMap hashMap = syncingRegistry.syncUHRT(); Resource res = fileLookupRegistry.lookupInUHRT(resource.getGUID());
DHRT.putAll(hashMap); if (res != null)
download(resource.getName(), download_retry_count); DHRT.put(res.getGUID(), res);
System.out.println("Try to update DHRT.");
download(resource.getGUID(), download_retry_count);
return; return;
} catch (RemoteException ex) { } catch (RemoteException ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -503,10 +421,12 @@ public class ClientMain {
} catch (RemoteException e) { } catch (RemoteException e) {
System.out.println("RemoteException"); System.out.println("RemoteException");
DHRT.clear();
try { try {
HashMap hashMap = syncingRegistry.syncUHRT(); Resource res = fileLookupRegistry.lookupInUHRT(resource.getGUID());
DHRT.putAll(hashMap); if (res != null)
download(resource.getName(), download_retry_count); DHRT.put(res.getGUID(), res);
download(resource.getGUID(), download_retry_count);
return; return;
} catch (RemoteException ex) { } catch (RemoteException ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -532,7 +452,7 @@ public class ClientMain {
// } catch (RemoteException e) { // } catch (RemoteException e) {
// System.out.println("Retry Failed !"); // System.out.println("Retry Failed !");
// try { // try {
// DHRT = syncingRegistry.syncUHRT(); // DHRT = syncingRegistry.lookupInUHRT();
// System.out.println("Retrying " + retry_count); // System.out.println("Retrying " + retry_count);
// if (retry_count <= 5) { // if (retry_count <= 5) {
// try { // try {

@ -53,9 +53,9 @@ public class ClientIndexController {
public void initialize() { public void initialize() {
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<UUID, Resource>() { ClientMain.DHRT.addListener(new MapChangeListener<String, Resource>() {
@Override @Override
public void onChanged(Change<? extends UUID, ? extends Resource> change) { public void onChanged(Change<? extends String, ? extends Resource> change) {
log_field.appendText(change.toString()); log_field.appendText(change.toString());
ObservableList<Resource> resources = new ObservableListWrapper<Resource>(new ArrayList<>(ClientMain.DHRT.values())); ObservableList<Resource> resources = new ObservableListWrapper<Resource>(new ArrayList<>(ClientMain.DHRT.values()));
DHRT_Table.setItems(resources); DHRT_Table.setItems(resources);
@ -186,7 +186,7 @@ public class ClientIndexController {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
ClientMain.sync_DHRT(); // ClientMain.sync_DHRT();
} }
}).start(); }).start();
@ -222,7 +222,7 @@ public class ClientIndexController {
sync_button.setOnAction(new EventHandler<ActionEvent>() { sync_button.setOnAction(new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
ClientMain.sync_DHRT(); // ClientMain.sync_DHRT();
} }
}); });
reg_button.setOnAction(new EventHandler<ActionEvent>() { reg_button.setOnAction(new EventHandler<ActionEvent>() {

@ -13,5 +13,5 @@ import java.util.UUID;
* @Description: * @Description:
**/ **/
public interface P2P_FileRegistry extends Remote { public interface P2P_FileRegistry extends Remote {
byte[] download(UUID resID) throws RemoteException; byte[] download(String resID) throws RemoteException;
} }

@ -35,7 +35,7 @@ public class P2PFileImpl extends UnicastRemoteObject implements P2P_FileRegistry
@Override @Override
public byte[] download(UUID resID) throws RemoteException{ public byte[] download(String resID) throws RemoteException{
System.out.println("Income connection."); System.out.println("Income connection.");
Resource resource = ClientMain.DHRT.get(resID); Resource resource = ClientMain.DHRT.get(resID);
if(resource == null){ if(resource == null){

@ -1,17 +1,14 @@
package com.echo.p2p_project.server; package com.echo.p2p_project.server;
import com.echo.p2p_project.Util;
import com.echo.p2p_project.server.interfaces.ConstructRegistry; import com.echo.p2p_project.server.interfaces.ConstructRegistry;
import com.echo.p2p_project.server.interfaces.HeartBeatRegistry; import com.echo.p2p_project.server.interfaces.HeartBeatRegistry;
import com.echo.p2p_project.server.interfaces.HelloRegistryFacade; import com.echo.p2p_project.server.interfaces.HelloRegistryFacade;
import com.echo.p2p_project.server.interfaces.SyncingRegistry; import com.echo.p2p_project.server.interfaces.FileLookupRegistry;
import com.echo.p2p_project.server.model.*; import com.echo.p2p_project.server.model.*;
import com.echo.p2p_project.u_model.Peer; import com.echo.p2p_project.u_model.Peer;
import com.echo.p2p_project.u_model.Resource; import com.echo.p2p_project.u_model.Resource;
import com.sun.javafx.collections.ObservableMapWrapper; import com.sun.javafx.collections.ObservableMapWrapper;
import javafx.collections.ObservableMap;
import java.io.PrintStream;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry; import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
@ -27,7 +24,7 @@ import java.util.*;
public class ServerMain{ public class ServerMain{
public static final int RMI_PORT = 1099; public static final int RMI_PORT = 1099;
public static ObservableMapWrapper<UUID, Peer> UHPT = new ObservableMapWrapper<>(new LinkedHashMap<>()); public static ObservableMapWrapper<UUID, Peer> UHPT = new ObservableMapWrapper<>(new LinkedHashMap<>());
public static ObservableMapWrapper<UUID, Resource> UHRT = new ObservableMapWrapper<>(new LinkedHashMap<>()); public static ObservableMapWrapper<String, Resource> UHRT = new ObservableMapWrapper<>(new LinkedHashMap<>());
private static Registry registry; private static Registry registry;
private static Boolean HasStarted = false; private static Boolean HasStarted = false;
private static Thread service; private static Thread service;
@ -91,12 +88,12 @@ public class ServerMain{
HelloRegistryFacade hello = new HelloRegistryFacadeImpl(); HelloRegistryFacade hello = new HelloRegistryFacadeImpl();
ConstructRegistry constructRegistry = new ConstructImpl(); ConstructRegistry constructRegistry = new ConstructImpl();
HeartBeatRegistry heartBeatRegistry = new HeartBeatImpl(); HeartBeatRegistry heartBeatRegistry = new HeartBeatImpl();
SyncingRegistry syncingRegistry = new SyncingImpl(); FileLookupRegistry fileLookupRegistry = new FileLookupImpl();
registry.rebind("HelloRegistry", hello); registry.rebind("HelloRegistry", hello);
registry.rebind("constructRegistry", constructRegistry); registry.rebind("constructRegistry", constructRegistry);
registry.rebind("heatBeatRegistry", heartBeatRegistry); registry.rebind("heatBeatRegistry", heartBeatRegistry);
registry.rebind("syncingRegistry", syncingRegistry); registry.rebind("syncingRegistry", fileLookupRegistry);
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();

@ -16,6 +16,8 @@ import javafx.scene.control.*;
import javafx.util.Callback; import javafx.util.Callback;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
/** /**
* @Author: WangYuyang * @Author: WangYuyang
@ -37,7 +39,7 @@ public class ServerIndexController {
public TableColumn<Resource, String> UHRT_GUID; public TableColumn<Resource, String> UHRT_GUID;
public TableColumn<Resource, String> UHRT_NAME; public TableColumn<Resource, String> UHRT_NAME;
public BarChart bar_chart; public BarChart bar_chart;
public TableColumn<Resource, String> UHRT_HASH; public TableColumn<Resource, String> UHRT_By;
XYChart.Series bar_data = new XYChart.Series(); XYChart.Series bar_data = new XYChart.Series();
@ -72,15 +74,54 @@ public class ServerIndexController {
return new SimpleStringProperty(param.getValue().getName().toString()); return new SimpleStringProperty(param.getValue().getName().toString());
} }
}); });
UHRT_HASH.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Resource, String>, ObservableValue<String>>() { UHRT_By.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Resource, String>, ObservableValue<String>>() {
@Override @Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Resource, String> param) { public ObservableValue<String> call(TableColumn.CellDataFeatures<Resource, String> param) {
return new SimpleStringProperty(param.getValue().getHash().toString()); return new SimpleStringProperty(String.valueOf(param.getValue().possessedBy.size()));
} }
}); });
bar_chart.getData().add(bar_data); bar_chart.getData().add(bar_data);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
//update table
ObservableList<Peer> peers = new ObservableListWrapper<Peer>(new ArrayList<>(ServerMain.UHPT.values()));
uhpt_table.setItems(peers);
//update table
ObservableList<Resource> resources = new ObservableListWrapper<Resource>(new ArrayList<>(ServerMain.UHRT.values()));
uhrt_table.setItems(resources);
//update tree
TreeItem<String> rootItem = new TreeItem<String>("Server");
rootItem.setExpanded(true);
for (Peer p : peers) {
TreeItem<String> item = new TreeItem<String>(p.getGUID().toString());
for (Resource r : p.possessing.values()) {
item.setExpanded(true);
TreeItem<String> filename = new TreeItem<String>(r.getName().toString());
item.getChildren().add(filename);
}
rootItem.getChildren().add(item);
}
uhrt_tree.setRoot(rootItem);
//
setBarChartData();
}
});
}
}, 0, 1000);
ServerMain.UHPT.addListener(new MapChangeListener() { ServerMain.UHPT.addListener(new MapChangeListener() {
@Override @Override
public void onChanged(Change change) { public void onChanged(Change change) {

@ -1,6 +1,7 @@
package com.echo.p2p_project.server.interfaces; package com.echo.p2p_project.server.interfaces;
import com.echo.p2p_project.u_model.Peer; import com.echo.p2p_project.u_model.Peer;
import com.echo.p2p_project.u_model.Resource;
import java.rmi.Remote; import java.rmi.Remote;
import java.rmi.RemoteException; import java.rmi.RemoteException;
@ -14,7 +15,8 @@ import java.util.UUID;
* @Package: com.echo.p2p_project.server.interfaces * @Package: com.echo.p2p_project.server.interfaces
* @Description: * @Description:
**/ **/
public interface SyncingRegistry extends Remote { public interface FileLookupRegistry extends Remote {
Peer syncPeer(UUID GUID) throws RemoteException; Peer syncPeer(UUID GUID) throws RemoteException;
HashMap syncUHRT() throws RemoteException; // HashMap lookupInUHRT(String filename) throws RemoteException;
Resource lookupInUHRT(String hash) throws RemoteException;
} }

@ -51,7 +51,6 @@ public class ConstructImpl extends UnicastRemoteObject implements ConstructRegis
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 {
Peer peer = ServerMain.UHPT.get(PeerGUID); Peer peer = ServerMain.UHPT.get(PeerGUID);
@ -60,20 +59,47 @@ public class ConstructImpl extends UnicastRemoteObject implements ConstructRegis
if(peer == null) if(peer == null)
return null; // Res register failed return null; // Res register failed
UUID GUID = UUID.randomUUID();
//If duplicated GUID //If duplicated GUID
if(ServerMain.UHPT.containsKey(GUID)) { if(ServerMain.UHRT.containsKey(hash)) {
System.out.println("DUP GUID!!!!!!!"); // Res is in UHRT
return null; // Res register failed System.out.println("DUP GUID! Res is in UHRT");
Resource res = ServerMain.UHRT.get(hash);
res.possessedBy.put(peer.getGUID(), peer);
ServerMain.UHPT.get(peer.getGUID()).possessing.put(res.getGUID(), res);
return res;
} }
String ResName = name; String ResName = name;
Resource res = new Resource(GUID, ResName, hash); Resource res = new Resource(hash, ResName, hash);
res.possessedBy.put(peer.getGUID(), peer); res.possessedBy.put(peer.getGUID(), peer);
ServerMain.UHRT.put(res.getGUID(), res); ServerMain.UHRT.put(res.getGUID(), res);
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;
} }
// @Override
// public Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException {
// Peer peer = ServerMain.UHPT.get(PeerGUID);
//
// //If peer not registered in center
// if(peer == null)
// return null; // Res register failed
//
//
// UUID GUID = UUID.randomUUID();
// //If duplicated GUID
// if(ServerMain.UHPT.containsKey(GUID)) {
// System.out.println("DUP GUID!!!!!!!");
// return null; // Res register failed
// }
//
// String ResName = name;
// Resource res = new Resource(GUID, ResName, hash);
// res.possessedBy.put(peer.getGUID(), peer);
// ServerMain.UHRT.put(res.getGUID(), res);
// ServerMain.UHPT.get(peer.getGUID()).possessing.put(res.getGUID(), res);
// System.out.println("Resources Registered: " + res);
// return res;
// }
} }

@ -0,0 +1,126 @@
package com.echo.p2p_project.server.model;
import com.echo.p2p_project.server.ServerMain;
import com.echo.p2p_project.server.interfaces.FileLookupRegistry;
import com.echo.p2p_project.u_model.Peer;
import com.echo.p2p_project.u_model.Resource;
import java.rmi.RemoteException;
import java.rmi.server.RMISocketFactory;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;
/**
* @Author: WangYuyang
* @Date: 2021/10/20-22:45
* @Project: P2P_Project
* @Package: com.echo.p2p_project.server.model
* @Description:
**/
public class FileLookupImpl extends UnicastRemoteObject implements FileLookupRegistry {
/**
* Creates and exports a new UnicastRemoteObject object using an
* anonymous port.
*
* <p>The object is exported with a server socket
* created using the {@link RMISocketFactory} class.
*
* @throws RemoteException if failed to export object
* @since JDK1.1
*/
public FileLookupImpl() throws RemoteException {
super();
}
@Override
public Peer syncPeer(UUID GUID) throws RemoteException {
Peer peer = ServerMain.UHPT.get(GUID);
if (peer == null)
return null;
return peer;
}
@Override
public Resource lookupInUHRT(String hash) throws RemoteException {
Resource res = ServerMain.UHRT.get(hash);
if(res==null){
return null;
}
ArrayList<Peer> processed_peers = new ArrayList<>();
for (Peer p : res.possessedBy.values()) {
processed_peers.add(p);
}
processed_peers.sort(new Comparator<Peer>() {
@Override
public int compare(Peer o1, Peer o2) {
return o1.getRoutingMetric() - o2.getRoutingMetric();
}
});
Peer best_peer = processed_peers.get(0);
for (Peer peer: processed_peers) {
System.out.println(peer.getGUID() + " <> " + peer.getRoutingMetric());
}
System.out.println("BEST: " + best_peer.getGUID());
Resource tmp = new Resource(res.getGUID(), res.getName(), res.getHash());
tmp.possessedBy.put(best_peer.getGUID(), best_peer);
return tmp;
}
// @Override
// public HashMap lookupInUHRT(String filename) throws RemoteException {
// HashMap<UUID, Resource> hashMap = new LinkedHashMap();
//
// for (UUID key : ServerMain.UHRT.keySet()) {
// if (ServerMain.UHRT.get(key).getName().equals(filename)) {
// hashMap.put(key, ServerMain.UHRT.get(key));
// }
// }
//
// System.out.println("Checking file hashing...");
// Boolean hash_all_same = true;
// String file_hash = ((Resource) hashMap.values().toArray()[0]).getHash();
// for (Resource r : hashMap.values()) {
// if (!r.getHash().equals(file_hash)) {
// hash_all_same = false;
// break;
// }
// }
// UUID GUID = null;
// if (hash_all_same) {
// System.out.println("Hash is the same, looking for best node...");
// ArrayList<Peer> processed_peers = new ArrayList<>();
// for (Resource r : hashMap.values()) {
// for (Peer p : r.possessedBy.values()) {
// processed_peers.add(p);
// }
// }
// processed_peers.sort(new Comparator<Peer>() {
// @Override
// public int compare(Peer o1, Peer o2) {
// return o1.getRoutingMetric() - o2.getRoutingMetric();
// }
// });
// for (Peer p : processed_peers) {
// System.out.println(p.getGUID().toString() + " <> " + p.getRoutingMetric());
// }
//
// for (Resource r : processed_peers.get(0).possessing.values()) {
// System.out.println("File hash: " + file_hash);
// System.out.println("Res hash : " + r.getHash());
// if (r.getHash().equals(file_hash)) {
// System.out.println("Best resource GUID: " + r.getGUID());
// System.out.println("Best Peer GUID: " + processed_peers.get(0).getGUID());
// GUID = r.getGUID();
// HashMap tmp = new LinkedHashMap();
// tmp.put(GUID, ServerMain.UHRT.get(GUID));
// return tmp;
// }
// }
// }
//
// return hashMap;
// }
}

@ -1,53 +0,0 @@
package com.echo.p2p_project.server.model;
import com.echo.p2p_project.server.ServerMain;
import com.echo.p2p_project.server.interfaces.SyncingRegistry;
import com.echo.p2p_project.u_model.Peer;
import java.rmi.RemoteException;
import java.rmi.server.RMISocketFactory;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.UUID;
/**
* @Author: WangYuyang
* @Date: 2021/10/20-22:45
* @Project: P2P_Project
* @Package: com.echo.p2p_project.server.model
* @Description:
**/
public class SyncingImpl extends UnicastRemoteObject implements SyncingRegistry {
/**
* Creates and exports a new UnicastRemoteObject object using an
* anonymous port.
*
* <p>The object is exported with a server socket
* created using the {@link RMISocketFactory} class.
*
* @throws RemoteException if failed to export object
* @since JDK1.1
*/
public SyncingImpl() throws RemoteException {
super();
}
@Override
public Peer syncPeer(UUID GUID) throws RemoteException{
Peer peer = ServerMain.UHPT.get(GUID);
if (peer==null)
return null;
return peer;
}
@Override
public HashMap syncUHRT() throws RemoteException {
HashMap hashMap = new LinkedHashMap();
for (UUID key: ServerMain.UHRT.keySet()) {
hashMap.put(key, ServerMain.UHRT.get(key));
}
HashMap map = hashMap;
return map;
}
}

@ -2,7 +2,9 @@ package com.echo.p2p_project.server.model;
import com.echo.p2p_project.server.ServerMain; import com.echo.p2p_project.server.ServerMain;
import com.echo.p2p_project.u_model.Peer; import com.echo.p2p_project.u_model.Peer;
import com.echo.p2p_project.u_model.Resource;
import java.awt.geom.RectangularShape;
import java.util.UUID; import java.util.UUID;
/** /**
@ -42,9 +44,15 @@ public class WatchDog {
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);
for (UUID guid : ServerMain.UHPT.get(uuid).possessing.keySet()) { for (String guid : ServerMain.UHPT.get(uuid).possessing.keySet()) {
System.out.println("Resources: " + guid); System.out.println("Resources: " + guid);
ServerMain.UHRT.remove(guid); Resource resource = ServerMain.UHRT.get(guid);
if(resource.possessedBy.size() == 1) {
ServerMain.UHRT.remove(guid);
}
else {
resource.possessedBy.remove(uuid);
}
} }
ServerMain.UHPT.remove(uuid); ServerMain.UHPT.remove(uuid);
System.out.println("================================================================================="); System.out.println("=================================================================================");

@ -26,7 +26,7 @@ public class Peer implements Serializable,Comparable {
private Integer routingMetric; private Integer routingMetric;
private Integer MissedHartBeat = 0; private Integer MissedHartBeat = 0;
//Only use this possessing in DHRT //Only use this possessing in DHRT
public HashMap<UUID, Resource> possessing = new LinkedHashMap(); public HashMap<String, Resource> possessing = new LinkedHashMap();
public Peer(UUID GUID, String name, String IP, Integer p2P_port, Integer routingMetric) { public Peer(UUID GUID, String name, String IP, Integer p2P_port, Integer routingMetric) {
this.GUID = GUID; this.GUID = GUID;
@ -76,11 +76,11 @@ public class Peer implements Serializable,Comparable {
this.routingMetric = routingMetric; this.routingMetric = routingMetric;
} }
public HashMap<UUID, Resource> getPossessing() { public HashMap<String, Resource> getPossessing() {
return possessing; return possessing;
} }
public void setPossessing(HashMap<UUID, Resource> possessing) { public void setPossessing(HashMap<String, Resource> possessing) {
this.possessing = possessing; this.possessing = possessing;
} }

@ -15,27 +15,27 @@ import java.util.UUID;
* @Description: * @Description:
**/ **/
public class Resource implements Serializable { public class Resource implements Serializable {
private UUID GUID; private String GUID;
private String name; private String name;
public HashMap<UUID, Peer> possessedBy = new LinkedHashMap<>(); public HashMap<UUID, Peer> possessedBy = new LinkedHashMap<>();
public String hash; public String hash;
public Resource(UUID GUID, String name) { public Resource(String GUID, String name) {
this.GUID = GUID; this.GUID = GUID;
this.name = name; this.name = name;
} }
public Resource(UUID GUID, String name, String hash) { public Resource(String GUID, String name, String hash) {
this.GUID = GUID; this.GUID = GUID;
this.name = name; this.name = name;
this.hash = hash; this.hash = hash;
} }
public UUID getGUID() { public String getGUID() {
return GUID; return GUID;
} }
public void setGUID(UUID GUID) { public void setGUID(String GUID) {
this.GUID = GUID; this.GUID = GUID;
} }

@ -82,7 +82,7 @@
<columns> <columns>
<TableColumn fx:id="UHRT_GUID" prefWidth="128.0" text="GUID" /> <TableColumn fx:id="UHRT_GUID" prefWidth="128.0" text="GUID" />
<TableColumn fx:id="UHRT_NAME" prefWidth="120.0" text="NAME" /> <TableColumn fx:id="UHRT_NAME" prefWidth="120.0" text="NAME" />
<TableColumn fx:id="UHRT_HASH" prefWidth="120.0" text="HASH" /> <TableColumn visible="false" fx:id="UHRT_By" prefWidth="120.0" text="By" />
</columns> </columns>
</TableView> </TableView>
</children> </children>

@ -82,7 +82,7 @@
<columns> <columns>
<TableColumn fx:id="UHRT_GUID" prefWidth="128.0" text="GUID" /> <TableColumn fx:id="UHRT_GUID" prefWidth="128.0" text="GUID" />
<TableColumn fx:id="UHRT_NAME" prefWidth="120.0" text="NAME" /> <TableColumn fx:id="UHRT_NAME" prefWidth="120.0" text="NAME" />
<TableColumn fx:id="UHRT_HASH" prefWidth="120.0" text="HASH" /> <TableColumn visible="false" fx:id="UHRT_By" prefWidth="120.0" text="By" />
</columns> </columns>
</TableView> </TableView>
</children> </children>

Loading…
Cancel
Save