diff --git a/.idea/fastRequestCollection.xml b/.idea/fastRequestCollection.xml new file mode 100644 index 0000000..a433492 --- /dev/null +++ b/.idea/fastRequestCollection.xml @@ -0,0 +1,23 @@ + + + + + + \ No newline at end of file diff --git a/download/0fc64d0f-db44-4c73-ad9c-39ab067711be_FROM_0fc64d0f-db44-4c73-ad9c-39ab067711be_1.a b/download/0fc64d0f-db44-4c73-ad9c-39ab067711be_FROM_0fc64d0f-db44-4c73-ad9c-39ab067711be_1.a deleted file mode 100644 index c89a354..0000000 --- a/download/0fc64d0f-db44-4c73-ad9c-39ab067711be_FROM_0fc64d0f-db44-4c73-ad9c-39ab067711be_1.a +++ /dev/null @@ -1,5 +0,0 @@ -hahaha -================ -This File is 1.a -================ - diff --git a/download/50dfcced-c877-458f-ae45-0a50108c168c_FROM_a082b40f-9a6d-43d6-ae96-a2be5c27d1e8_2.a b/download/50dfcced-c877-458f-ae45-0a50108c168c_FROM_a082b40f-9a6d-43d6-ae96-a2be5c27d1e8_2.a deleted file mode 100644 index c48b607..0000000 --- a/download/50dfcced-c877-458f-ae45-0a50108c168c_FROM_a082b40f-9a6d-43d6-ae96-a2be5c27d1e8_2.a +++ /dev/null @@ -1,5 +0,0 @@ -2222222222222222 -================ -This File is 2.a -================ - diff --git a/download/9d4c75d4-aad6-4dc4-8232-45416137b9eb_FROM_efdbfa0f-3e0f-40e8-b244-73530e9513f3_123.db b/download/9d4c75d4-aad6-4dc4-8232-45416137b9eb_FROM_efdbfa0f-3e0f-40e8-b244-73530e9513f3_123.db deleted file mode 100644 index 1ade662..0000000 Binary files a/download/9d4c75d4-aad6-4dc4-8232-45416137b9eb_FROM_efdbfa0f-3e0f-40e8-b244-73530e9513f3_123.db and /dev/null differ diff --git a/download/ce3e0168-2bb4-4499-bd90-ee3d6bb59043_FROM_ce3e0168-2bb4-4499-bd90-ee3d6bb59043_1.a b/download/ce3e0168-2bb4-4499-bd90-ee3d6bb59043_FROM_ce3e0168-2bb4-4499-bd90-ee3d6bb59043_1.a deleted file mode 100644 index c89a354..0000000 --- a/download/ce3e0168-2bb4-4499-bd90-ee3d6bb59043_FROM_ce3e0168-2bb4-4499-bd90-ee3d6bb59043_1.a +++ /dev/null @@ -1,5 +0,0 @@ -hahaha -================ -This File is 1.a -================ - diff --git a/img.png b/img.png deleted file mode 100644 index ea84bfa..0000000 Binary files a/img.png and /dev/null differ diff --git a/src/main/java/com/echo/p2p_project/Util.java b/src/main/java/com/echo/p2p_project/Util.java index 11de37c..edf8771 100644 --- a/src/main/java/com/echo/p2p_project/Util.java +++ b/src/main/java/com/echo/p2p_project/Util.java @@ -1,10 +1,12 @@ package com.echo.p2p_project; -import java.io.OutputStream; -import java.io.PrintStream; +import javax.xml.bind.annotation.adapters.HexBinaryAdapter; +import java.io.*; import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; /** * @Author: WangYuyang @@ -25,5 +27,32 @@ public class Util { return null; } } + public static String createSha1(File file){ + MessageDigest digest = null; + try { + digest = MessageDigest.getInstance("SHA-1"); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + InputStream fis = null; + try { + fis = new FileInputStream(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + int n = 0; + byte[] buffer = new byte[8192]; + while (n != -1) { + try { + n = fis.read(buffer); + } catch (IOException e) { + e.printStackTrace(); + } + if (n > 0) { + digest.update(buffer, 0, n); + } + } + return new HexBinaryAdapter().marshal(digest.digest()); + } } diff --git a/src/main/java/com/echo/p2p_project/client/ClientMain.java b/src/main/java/com/echo/p2p_project/client/ClientMain.java index 47e3d30..4eec360 100644 --- a/src/main/java/com/echo/p2p_project/client/ClientMain.java +++ b/src/main/java/com/echo/p2p_project/client/ClientMain.java @@ -110,7 +110,7 @@ public class ClientMain { private static void download(String file_name_to_download, Integer retry_count) { if (download_retry_count <= 5) { - download_retry_count+=1; + download_retry_count += 1; System.out.println("Retrying " + download_retry_count); HashMap resources = look_up_file(file_name_to_download); try { @@ -119,7 +119,7 @@ public class ClientMain { e.printStackTrace(); } download(resources); - }else{ + } else { System.out.println("Give up download."); return; } @@ -209,6 +209,8 @@ public class ClientMain { private static Boolean reg_file(String name) { File file = new File("res/" + name); + String hash = Util.createSha1(file); + System.out.println(hash); if (!file.exists()) { System.out.println("File not exists !"); System.out.println("This File is not exist in your file system !"); @@ -218,7 +220,7 @@ public class ClientMain { System.out.println("File length: " + file.length()); } try { - Resource resource = constructRegistry.ConstructResource(peer.getGUID(), name); + Resource resource = constructRegistry.ConstructResource(peer.getGUID(), name, hash); ClientMain.DHRT.put(resource.getGUID(), resource); DHRT.put(resource.getGUID(), resource); System.out.println("File Register Success!"); @@ -295,13 +297,53 @@ public class ClientMain { for (Resource r : resources.values()) { System.out.println(r); } - System.out.println("Please specify the GUID of the resource: "); + 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; - try { - GUID = UUID.fromString(sc.nextLine()); - } catch (IllegalArgumentException e) { - System.out.println("UUID Error"); - return; + if (hash_all_same) { + System.out.println("Hash is the same, looking for best node..."); + ArrayList processed_peers = new ArrayList<>(); + for (Resource r : resources.values()) { + for (Peer p : r.possessedBy.values()) { + processed_peers.add(p); + } + } + processed_peers.sort(new Comparator() { + @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"); @@ -330,7 +372,7 @@ public class ClientMain { File file = null; try { 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"); System.out.println("Connected."); System.out.println("Downloading FROM: " + p.getGUID()); diff --git a/src/main/java/com/echo/p2p_project/server/gui/ServerIndexController.java b/src/main/java/com/echo/p2p_project/server/gui/ServerIndexController.java index 3919a81..151fe3d 100644 --- a/src/main/java/com/echo/p2p_project/server/gui/ServerIndexController.java +++ b/src/main/java/com/echo/p2p_project/server/gui/ServerIndexController.java @@ -10,6 +10,8 @@ import javafx.beans.value.ObservableValue; import javafx.collections.MapChangeListener; import javafx.collections.ObservableList; import javafx.event.ActionEvent; +import javafx.scene.chart.BarChart; +import javafx.scene.chart.XYChart; import javafx.scene.control.*; import javafx.util.Callback; @@ -34,6 +36,9 @@ public class ServerIndexController { public TableColumn UHPT_PORT; public TableColumn UHRT_GUID; public TableColumn UHRT_NAME; + public BarChart bar_chart; + public TableColumn UHRT_HASH; + XYChart.Series bar_data = new XYChart.Series(); public void initialize() { @@ -67,6 +72,14 @@ public class ServerIndexController { return new SimpleStringProperty(param.getValue().getName().toString()); } }); + UHRT_HASH.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getHash().toString()); + } + }); + + bar_chart.getData().add(bar_data); ServerMain.UHPT.addListener(new MapChangeListener() { @Override @@ -83,7 +96,6 @@ public class ServerIndexController { ObservableList peers = new ObservableListWrapper(new ArrayList<>(ServerMain.UHPT.values())); uhpt_table.setItems(peers); - //update tree TreeItem rootItem = new TreeItem("Server"); rootItem.setExpanded(true); @@ -98,6 +110,11 @@ public class ServerIndexController { } uhrt_tree.setRoot(rootItem); + + // + setBarChartData(); + + } }); @@ -133,6 +150,10 @@ public class ServerIndexController { } uhrt_tree.setRoot(rootItem); + // + setBarChartData(); + + } }); } @@ -141,6 +162,24 @@ public class ServerIndexController { } + private void setBarChartData() { + for (int i = 0; i < ServerMain.UHPT.values().size(); i++) { + Peer p = (Peer) ServerMain.UHPT.values().toArray()[i]; + Boolean contains = false; + for (Object d : bar_data.getData()) { + XYChart.Data data = (XYChart.Data) d; + System.out.println(data.getXValue().toString()); + if (data.getXValue().toString().equals(p.getGUID().toString().substring(0, 4))) { + data.setYValue(p.possessing.size()); + contains = true; + } + } + if (!contains) + bar_data.getData().add(new XYChart.Data<>(p.getGUID().toString().substring(0, 4), p.possessing.size())); + } + System.out.println(bar_chart.getData()); + } + public void StartServerButtonPressed(ActionEvent actionEvent) { ServerIndexApplication.init_server(); diff --git a/src/main/java/com/echo/p2p_project/server/interfaces/ConstructRegistry.java b/src/main/java/com/echo/p2p_project/server/interfaces/ConstructRegistry.java index 18524c6..13ebd20 100644 --- a/src/main/java/com/echo/p2p_project/server/interfaces/ConstructRegistry.java +++ b/src/main/java/com/echo/p2p_project/server/interfaces/ConstructRegistry.java @@ -16,5 +16,5 @@ import java.util.UUID; **/ public interface ConstructRegistry extends Remote { Peer ConstructPeer(String name, String IP, Integer P2P_port) throws RemoteException; - Resource ConstructResource(UUID PeerGUID, String name) throws RemoteException; + Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException; } diff --git a/src/main/java/com/echo/p2p_project/server/model/ConstructImpl.java b/src/main/java/com/echo/p2p_project/server/model/ConstructImpl.java index 7f43693..46996b8 100644 --- a/src/main/java/com/echo/p2p_project/server/model/ConstructImpl.java +++ b/src/main/java/com/echo/p2p_project/server/model/ConstructImpl.java @@ -53,7 +53,7 @@ public class ConstructImpl extends UnicastRemoteObject implements ConstructRegis } @Override - public Resource ConstructResource(UUID PeerGUID, String name) throws RemoteException { + public Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException { Peer peer = ServerMain.UHPT.get(PeerGUID); //If peer not registered in center @@ -69,7 +69,7 @@ public class ConstructImpl extends UnicastRemoteObject implements ConstructRegis } String ResName = name; - Resource res = new Resource(GUID, ResName); + 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); diff --git a/src/main/java/com/echo/p2p_project/u_model/Resource.java b/src/main/java/com/echo/p2p_project/u_model/Resource.java index 9fbb097..6855835 100644 --- a/src/main/java/com/echo/p2p_project/u_model/Resource.java +++ b/src/main/java/com/echo/p2p_project/u_model/Resource.java @@ -1,5 +1,7 @@ package com.echo.p2p_project.u_model; +import cn.hutool.core.lang.hash.Hash128; + import java.io.Serializable; import java.util.HashMap; import java.util.LinkedHashMap; @@ -16,12 +18,19 @@ public class Resource implements Serializable { private UUID GUID; private String name; public HashMap possessedBy = new LinkedHashMap<>(); + public String hash; public Resource(UUID GUID, String name) { this.GUID = GUID; this.name = name; } + public Resource(UUID GUID, String name, String hash) { + this.GUID = GUID; + this.name = name; + this.hash = hash; + } + public UUID getGUID() { return GUID; } @@ -46,12 +55,21 @@ public class Resource implements Serializable { this.possessedBy = possessedBy; } + public String getHash() { + return hash; + } + + public void setHash(String hash) { + this.hash = hash; + } + @Override public String toString() { return "Resource{" + "GUID=" + GUID + ", name='" + name + '\'' + ", possessedBy=" + possessedBy.keySet() + + ", hash='" + hash + '\'' + '}'; } } diff --git a/src/main/resources/gui/server_index.fxml b/src/main/resources/gui/server_index.fxml index 0f13976..a6a592c 100644 --- a/src/main/resources/gui/server_index.fxml +++ b/src/main/resources/gui/server_index.fxml @@ -1,5 +1,6 @@ + @@ -12,7 +13,7 @@ - + @@ -30,7 +31,7 @@ + + + + + + + - + - + - + @@ -72,10 +90,10 @@ - + diff --git a/target/classes/gui/server_index.fxml b/target/classes/gui/server_index.fxml index 0f13976..a6a592c 100644 --- a/target/classes/gui/server_index.fxml +++ b/target/classes/gui/server_index.fxml @@ -1,5 +1,6 @@ + @@ -12,7 +13,7 @@ - + @@ -30,7 +31,7 @@ + + + + + + + - + - + - + @@ -72,10 +90,10 @@ - +