diff --git a/.idea/artifacts/ClientGUI_jar.xml b/.idea/artifacts/ClientGUI_jar.xml new file mode 100644 index 0000000..a3cf680 --- /dev/null +++ b/.idea/artifacts/ClientGUI_jar.xml @@ -0,0 +1,20 @@ + + + $PROJECT_DIR$/out/artifacts/ClientGUI_jar + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/artifacts/Client.xml b/.idea/artifacts/Client_jar.xml similarity index 92% rename from .idea/artifacts/Client.xml rename to .idea/artifacts/Client_jar.xml index 3d24285..3bb5934 100644 --- a/.idea/artifacts/Client.xml +++ b/.idea/artifacts/Client_jar.xml @@ -1,20 +1,20 @@ - - $PROJECT_DIR$/out/artifacts/Client + + $PROJECT_DIR$/out/artifacts/Client_jar - - - - + + + + \ No newline at end of file diff --git a/.idea/artifacts/ServerGUI_jar.xml b/.idea/artifacts/ServerGUI_jar.xml new file mode 100644 index 0000000..5acaa07 --- /dev/null +++ b/.idea/artifacts/ServerGUI_jar.xml @@ -0,0 +1,20 @@ + + + $PROJECT_DIR$/out/artifacts/ServerGUI_jar + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Client_GUI-META-INF/META-INF/MANIFEST.MF b/Client_GUI-META-INF/META-INF/MANIFEST.MF new file mode 100644 index 0000000..ee4ea22 --- /dev/null +++ b/Client_GUI-META-INF/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.echo.p2p_project.client.gui.ClientIndexView + diff --git a/Server_GUI-META-INF/META-INF/MANIFEST.MF b/Server_GUI-META-INF/META-INF/MANIFEST.MF new file mode 100644 index 0000000..9acecf6 --- /dev/null +++ b/Server_GUI-META-INF/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.echo.p2p_project.server.gui.ServerIndexApplication + diff --git a/download/28c30b5a-b6fa-45c8-b8d2-cf4e33326f7e_FROM_03b8a253-9978-4d53-a1c3-ae7a223945ea_1.a b/download/28c30b5a-b6fa-45c8-b8d2-cf4e33326f7e_FROM_03b8a253-9978-4d53-a1c3-ae7a223945ea_1.a deleted file mode 100644 index c89a354..0000000 --- a/download/28c30b5a-b6fa-45c8-b8d2-cf4e33326f7e_FROM_03b8a253-9978-4d53-a1c3-ae7a223945ea_1.a +++ /dev/null @@ -1,5 +0,0 @@ -hahaha -================ -This File is 1.a -================ - diff --git a/download/4d796993-7e79-45f6-8371-10b00a2965a8_FROM_4d796993-7e79-45f6-8371-10b00a2965a8_2.a b/download/4d796993-7e79-45f6-8371-10b00a2965a8_FROM_4d796993-7e79-45f6-8371-10b00a2965a8_2.a deleted file mode 100644 index c48b607..0000000 --- a/download/4d796993-7e79-45f6-8371-10b00a2965a8_FROM_4d796993-7e79-45f6-8371-10b00a2965a8_2.a +++ /dev/null @@ -1,5 +0,0 @@ -2222222222222222 -================ -This File is 2.a -================ - diff --git a/download/5c068312-74fb-48e9-a88e-e15646070176_FROM_2182e82c-a54c-4b2d-8e33-021806c33dea_123.db b/download/5c068312-74fb-48e9-a88e-e15646070176_FROM_2182e82c-a54c-4b2d-8e33-021806c33dea_123.db new file mode 100644 index 0000000..1ade662 Binary files /dev/null and b/download/5c068312-74fb-48e9-a88e-e15646070176_FROM_2182e82c-a54c-4b2d-8e33-021806c33dea_123.db differ diff --git a/download/9f4035f5-9f1d-405a-9072-a46f4577f33a_FROM_d6f06e40-2bf6-4306-bb6d-f9a7947567ec_1.a b/download/9f4035f5-9f1d-405a-9072-a46f4577f33a_FROM_d6f06e40-2bf6-4306-bb6d-f9a7947567ec_1.a deleted file mode 100644 index c89a354..0000000 --- a/download/9f4035f5-9f1d-405a-9072-a46f4577f33a_FROM_d6f06e40-2bf6-4306-bb6d-f9a7947567ec_1.a +++ /dev/null @@ -1,5 +0,0 @@ -hahaha -================ -This File is 1.a -================ - diff --git a/download/bbf7e47e-0c7e-4627-ab31-3b3da6fbc9d6_FROM_3f60e4d3-0010-499f-9d3c-e914ba7db0ec_123.db b/download/bbf7e47e-0c7e-4627-ab31-3b3da6fbc9d6_FROM_3f60e4d3-0010-499f-9d3c-e914ba7db0ec_123.db new file mode 100644 index 0000000..1ade662 Binary files /dev/null and b/download/bbf7e47e-0c7e-4627-ab31-3b3da6fbc9d6_FROM_3f60e4d3-0010-499f-9d3c-e914ba7db0ec_123.db differ diff --git a/download/c8a8222c-d78d-4463-8905-bc6e4d927abf_FROM_faea0c7a-ce41-4a9c-bc89-bc4861d6570c_0.a b/download/c8a8222c-d78d-4463-8905-bc6e4d927abf_FROM_faea0c7a-ce41-4a9c-bc89-bc4861d6570c_0.a deleted file mode 100644 index 941e56d..0000000 --- a/download/c8a8222c-d78d-4463-8905-bc6e4d927abf_FROM_faea0c7a-ce41-4a9c-bc89-bc4861d6570c_0.a +++ /dev/null @@ -1,5 +0,0 @@ -0000000000000000 -================ -This File is 0.a -================ - diff --git a/download/e2b5fda8-c3e8-4b7d-9b92-ae5aa5a7ae7b_FROM_6e437793-c40e-40a4-8c75-617de5ebf164_2.a b/download/e2b5fda8-c3e8-4b7d-9b92-ae5aa5a7ae7b_FROM_6e437793-c40e-40a4-8c75-617de5ebf164_2.a deleted file mode 100644 index c48b607..0000000 --- a/download/e2b5fda8-c3e8-4b7d-9b92-ae5aa5a7ae7b_FROM_6e437793-c40e-40a4-8c75-617de5ebf164_2.a +++ /dev/null @@ -1,5 +0,0 @@ -2222222222222222 -================ -This File is 2.a -================ - diff --git a/out/artifacts/Client/README.md b/out/artifacts/Client/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF index 5ebf936..ee4ea22 100644 --- a/src/main/java/META-INF/MANIFEST.MF +++ b/src/main/java/META-INF/MANIFEST.MF @@ -1,3 +1,3 @@ Manifest-Version: 1.0 -Main-Class: com.echo.p2p_project.server.ServerMain +Main-Class: com.echo.p2p_project.client.gui.ClientIndexView 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 dc08f67..37e5c29 100644 --- a/src/main/java/com/echo/p2p_project/client/ClientMain.java +++ b/src/main/java/com/echo/p2p_project/client/ClientMain.java @@ -16,8 +16,6 @@ import com.echo.p2p_project.u_model.Resource; import com.sun.javafx.collections.ObservableMapWrapper; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.rmi.ConnectException; import java.rmi.NotBoundException; import java.rmi.RemoteException; @@ -246,7 +244,7 @@ public class ClientMain { System.out.println("Peer Sync Finished !"); } - private static HashMap look_up_file(String file_name) { + public static HashMap look_up_file(String file_name) { HashMap result = new LinkedHashMap<>(); System.out.println("Looking for: " + file_name); for (Resource r : DHRT.values()) { @@ -326,7 +324,7 @@ public class ClientMain { } }); for (Peer p : processed_peers) { - System.out.println(p.getGUID().toString()+" <> " + p.getRoutingMetric()); + System.out.println(p.getGUID().toString() + " <> " + p.getRoutingMetric()); } for (Resource r : processed_peers.get(0).possessing.values()) { @@ -368,7 +366,8 @@ public class ClientMain { P2P_download(p, resource); } } - public static void sync_DHRT(){ + + public static void sync_DHRT() { HashMap hashMap = null; try { System.out.println("Start syncUHRT"); @@ -381,50 +380,143 @@ public class ClientMain { } public static void P2P_download(Peer p, Resource resource) { - Integer port = p.getP2P_port(); - String IP = p.getIP(); - Registry p2pRegistry = null; - File file = null; - try { - p2pRegistry = LocateRegistry.getRegistry(IP, port); - 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()); - byte[] file_byte = p2PFileRegistry.download(resource.getGUID()); - System.out.println("File byte[] length: " + file_byte.length); - file = FileUtil.writeBytes(file_byte, new File("res/" + resource.getName())); + new Thread(new Runnable() { + @Override + public void run() { + long start_time = System.currentTimeMillis(); + Integer port = p.getP2P_port(); + String IP = p.getIP(); + Registry p2pRegistry = null; + File file = null; + try { + p2pRegistry = LocateRegistry.getRegistry(IP, port); + 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()); + byte[] file_byte = p2PFileRegistry.download(resource.getGUID()); + System.out.println("File byte[] length: " + file_byte.length); + long end_time = System.currentTimeMillis(); + long start_writing_time = System.currentTimeMillis(); + file = FileUtil.writeBytes(file_byte, new File("download/" + peer.getGUID() + "_FROM_" + p.getGUID() + "_" + resource.getName())); + long end_writing_time = System.currentTimeMillis(); + System.out.println("File downloaded !"); + System.out.println("File length : " + file.length()); + + float file_size_B = file.length(); + float file_size_KB = file.length() / 1024; + float file_size_MB = file_size_KB / 1024; + if (file_size_KB <= 1) { + System.out.println("################# Download Finished #################"); + System.out.println("Network COST: " + (end_time - start_time) + " ms."); + System.out.println("Disk COST: " + (end_writing_time - start_writing_time) + " ms."); + System.out.println("DATA Size: " + file_size_B + " Byte."); + System.out.println("Average Speed: " + file_size_B / ((end_time - start_time) / 1000) + " Byte/s."); + System.out.println("#####################################################"); + } else { + if (file_size_MB <= 1) { + System.out.println("################# Download Finished #################"); + System.out.println("COST: " + (end_time - start_time) + " ms."); + System.out.println("Disk COST: " + (end_writing_time - start_writing_time) + " ms."); + System.out.println("DATA Size: " + file_size_KB + " KB."); + System.out.println("Average Speed: " + file_size_KB / ((end_time - start_time) / 1000) + " KB/s."); + System.out.println("#####################################################"); + } else { + System.out.println("################# Download Finished #################"); + System.out.println("COST: " + (end_time - start_time) / 1000 + " s."); + System.out.println("Disk COST: " + (end_writing_time - start_writing_time) + " ms."); + System.out.println("DATA Size: " + file_size_MB + " MB."); + System.out.println("Average Speed: " + file_size_MB / ((end_time - start_time) / 1000) + " MB/s."); + System.out.println("#####################################################"); + } + } - } catch (RemoteException e) { - System.out.println("RemoteException"); - try { - HashMap hashMap = syncingRegistry.syncUHRT(); - DHRT.putAll(hashMap); - download(resource.getName(), download_retry_count); - return; - } catch (RemoteException ex) { - ex.printStackTrace(); + } catch (RemoteException e) { + System.out.println("RemoteException"); + try { + HashMap hashMap = syncingRegistry.syncUHRT(); + DHRT.putAll(hashMap); + download(resource.getName(), download_retry_count); + return; + } catch (RemoteException ex) { + ex.printStackTrace(); + } + } catch (NotBoundException e) { + e.printStackTrace(); + } } -// e.printStackTrace(); - } catch (NotBoundException e) { - e.printStackTrace(); - } - if (file != null) { - - System.out.println("File downloaded !"); - System.out.println("File length : " + file.length()); + }).start(); + } - try { - FileUtil.writeToStream(file, new FileOutputStream("download/" + peer.getGUID() + "_FROM_" + p.getGUID() + "_" + resource.getName())); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } else { - System.out.println("P2P_download File Error"); - return; - } + public static void P2P_download(Peer p, Resource resource, Runnable runnable) { + new Thread(new Runnable() { + @Override + public void run() { + long start_time = System.currentTimeMillis(); + Integer port = p.getP2P_port(); + String IP = p.getIP(); + Registry p2pRegistry = null; + File file = null; + try { + p2pRegistry = LocateRegistry.getRegistry(IP, port); + 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()); + byte[] file_byte = p2PFileRegistry.download(resource.getGUID()); + System.out.println("File byte[] length: " + file_byte.length); + long end_time = System.currentTimeMillis(); + long start_writing_time = System.currentTimeMillis(); + file = FileUtil.writeBytes(file_byte, new File("download/" + peer.getGUID() + "_FROM_" + p.getGUID() + "_" + resource.getName())); + long end_writing_time = System.currentTimeMillis(); + System.out.println("File downloaded !"); + System.out.println("File length : " + file.length()); + + float file_size_B = file.length(); + float file_size_KB = file.length() / 1024; + float file_size_MB = file_size_KB / 1024; + if (file_size_KB <= 1) { + System.out.println("################# Download Finished #################"); + System.out.println("Network COST: " + (end_time - start_time) + " ms."); + System.out.println("Disk COST: " + (end_writing_time - start_writing_time) + " ms."); + System.out.println("DATA Size: " + file_size_B + " Byte."); + System.out.println("Average Speed: " + file_size_B / ((end_time - start_time) / 1000) + " Byte/s."); + System.out.println("#####################################################"); + } else { + if (file_size_MB <= 1) { + System.out.println("################# Download Finished #################"); + System.out.println("COST: " + (end_time - start_time) + " ms."); + System.out.println("Disk COST: " + (end_writing_time - start_writing_time) + " ms."); + System.out.println("DATA Size: " + file_size_KB + " KB."); + System.out.println("Average Speed: " + file_size_KB / ((end_time - start_time) / 1000) + " KB/s."); + System.out.println("#####################################################"); + } else { + System.out.println("################# Download Finished #################"); + System.out.println("COST: " + (end_time - start_time) / 1000 + " s."); + System.out.println("Disk COST: " + (end_writing_time - start_writing_time) + " ms."); + System.out.println("DATA Size: " + file_size_MB + " MB."); + System.out.println("Average Speed: " + file_size_MB / ((end_time - start_time) / 1000) + " MB/s."); + System.out.println("#####################################################"); + } + } + } catch (RemoteException e) { + System.out.println("RemoteException"); + try { + HashMap hashMap = syncingRegistry.syncUHRT(); + DHRT.putAll(hashMap); + download(resource.getName(), download_retry_count); + return; + } catch (RemoteException ex) { + ex.printStackTrace(); + } + } catch (NotBoundException e) { + e.printStackTrace(); + } + runnable.run(); + } + }).start(); } // // private static void P2P_download(Peer p, Resource resource, Integer retry_count) { diff --git a/src/main/java/com/echo/p2p_project/client/Test.java b/src/main/java/com/echo/p2p_project/client/Test.java new file mode 100644 index 0000000..1dafd29 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/Test.java @@ -0,0 +1,31 @@ +package com.echo.p2p_project.client; + +import java.rmi.ConnectException; + +/** + * @Author: WangYuyang + * @Date: 2021/10/27-22:48 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.client + * @Description: + **/ +public class Test { + public static void main(String[] args) { + for(int i = 0; i < 5000; i++){ + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + new Thread(new Runnable() { + @Override + public void run() { + ClientMain.recover(); + } + }).start(); + + + } + + } +} diff --git a/src/main/java/com/echo/p2p_project/client/gui/ClientIndexController.java b/src/main/java/com/echo/p2p_project/client/gui/ClientIndexController.java index 48dedd3..ca96f99 100644 --- a/src/main/java/com/echo/p2p_project/client/gui/ClientIndexController.java +++ b/src/main/java/com/echo/p2p_project/client/gui/ClientIndexController.java @@ -47,6 +47,8 @@ public class ClientIndexController { public TableColumn DHRT_NAME; public TableColumn DHRT_HASH; public Button sync_button; + public TextField lookup_filename; + public Button lookup_button; public void initialize() { ObservableList local_file_list = new ObservableListWrapper<>(Collections.synchronizedList(new ArrayList<>())); @@ -68,7 +70,7 @@ public class ClientIndexController { remote_file_list.remove(change.getValueRemoved()); } } - if(remote_file_list.size() >= 1){ + if (remote_file_list.size() >= 1) { download_datalist.setValue(remote_file_list.get(0)); } } @@ -78,12 +80,19 @@ public class ClientIndexController { }); download_datalist.setItems(remote_file_list); - - File[] files = FileUtil.ls(System.getProperty("user.dir") + "/res/"); - local_file_list.clear(); - for (File f : files) { - local_file_list.add(f.getName()); + try { + File[] files = FileUtil.ls(System.getProperty("user.dir") + "/res/"); + local_file_list.clear(); + for (File f : files) { + local_file_list.add(f.getName()); + } + } catch (Exception e) { + System.out.println("Download DIR: " + System.getProperty("user.dir") + "/download/"); + System.out.println("Resource DIR: " + System.getProperty("user.dir") + "/res/"); + FileUtil.mkdir(System.getProperty("user.dir") + "/download/"); + FileUtil.mkdir(System.getProperty("user.dir") + "/res/"); } + //这里只监听文件或目录的修改事件 // 修改:res-> 0.a // 修改:res-> 0.a @@ -107,6 +116,8 @@ public class ClientIndexController { for (File f : files) { local_file_list.add(f.getName()); } + if (local_file_list.size() >= 1) + download_datalist.setValue(local_file_list.get(0)); } }); } @@ -123,6 +134,8 @@ public class ClientIndexController { for (File f : files) { local_file_list.add(f.getName()); } + if (local_file_list.size() >= 1) + download_datalist.setValue(local_file_list.get(0)); } }); @@ -140,6 +153,8 @@ public class ClientIndexController { for (File f : files) { local_file_list.add(f.getName()); } + if (local_file_list.size() >= 1) + download_datalist.setValue(local_file_list.get(0)); } }); } @@ -178,6 +193,7 @@ public class ClientIndexController { download_button.setDisable(false); reg_button.setDisable(false); sync_button.setDisable(false); + lookup_button.setDisable(false); } }); @@ -220,7 +236,12 @@ public class ClientIndexController { download_button.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { - Resource r = (Resource) download_datalist.getValue(); + Resource r = null; + try { + r = (Resource) download_datalist.getValue(); + } catch (Exception e) { + return; + } if (r != null) { System.out.println(r); ArrayList processed_peers = new ArrayList<>(); @@ -236,12 +257,33 @@ public class ClientIndexController { for (Peer p : processed_peers) { System.out.println(p.getGUID().toString() + " <> " + p.getRoutingMetric()); } - ClientMain.P2P_download(processed_peers.get(0), r); - Dialog dialog = new Dialog(); - dialog.show(); + ClientMain.P2P_download(processed_peers.get(0), r, new Runnable() { + @Override + public void run() { + Platform.runLater(new Runnable() { + @Override + public void run() { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("Information Dialog"); + alert.setHeaderText("File downloaded"); + alert.setContentText("Please check in your download folder!"); + + alert.showAndWait(); + } + }); + } + }); + } } }); + lookup_button.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + String filename = lookup_filename.getText(); + ClientMain.look_up_file(filename); + } + }); } diff --git a/src/main/java/com/echo/p2p_project/client/model/CHeartBeat.java b/src/main/java/com/echo/p2p_project/client/model/CHeartBeat.java index 4b27b26..9aa6a4f 100644 --- a/src/main/java/com/echo/p2p_project/client/model/CHeartBeat.java +++ b/src/main/java/com/echo/p2p_project/client/model/CHeartBeat.java @@ -26,7 +26,10 @@ public class CHeartBeat { System.out.println("Start Heart Beat"); while (running) { try { + long start = System.currentTimeMillis(); Boolean status = heartBeatRegistry.heartBeat(GUID); + long end = System.currentTimeMillis(); + System.out.println( this.toString() + ": " + (end - start)); // System.out.println(status); ClientMain.retry_times = 0; } catch (RemoteException e) { 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 151fe3d..73bc416 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 @@ -163,6 +163,7 @@ public class ServerIndexController { } private void setBarChartData() { + bar_data.getData().clear(); for (int i = 0; i < ServerMain.UHPT.values().size(); i++) { Peer p = (Peer) ServerMain.UHPT.values().toArray()[i]; Boolean contains = false; diff --git a/src/main/java/com/echo/p2p_project/server/model/WatchDog.java b/src/main/java/com/echo/p2p_project/server/model/WatchDog.java index cd29766..819a988 100644 --- a/src/main/java/com/echo/p2p_project/server/model/WatchDog.java +++ b/src/main/java/com/echo/p2p_project/server/model/WatchDog.java @@ -2,7 +2,6 @@ package com.echo.p2p_project.server.model; import com.echo.p2p_project.server.ServerMain; import com.echo.p2p_project.u_model.Peer; -import com.echo.p2p_project.u_model.Resource; import java.util.UUID; @@ -22,9 +21,16 @@ public class WatchDog { @Override public void run() { while (running) { + UUID uuid = null; for (int i = 0; i < ServerMain.UHPT.size(); i++) { - UUID uuid = (UUID) ServerMain.UHPT.keySet().toArray()[i]; + try { + uuid = (UUID) ServerMain.UHPT.keySet().toArray()[i]; + } catch (Exception e) { + + } Peer p = ServerMain.UHPT.get(uuid); + if(p == null) + return; // System.out.println(p); if (p == null) { System.out.println("REMOVED(peer is null): " + uuid); diff --git a/src/main/resources/gui/client_index.fxml b/src/main/resources/gui/client_index.fxml index dd9973c..499fa65 100644 --- a/src/main/resources/gui/client_index.fxml +++ b/src/main/resources/gui/client_index.fxml @@ -55,7 +55,7 @@ - + - + - + + + + + + + + + + + + + + - + - + - + + + + + + + + + + + + + +