From 217e3f7506b649830498b72b21fab28a5cfddcfe Mon Sep 17 00:00:00 2001 From: Wang Yuyang Date: Sat, 23 Oct 2021 22:55:31 +0800 Subject: [PATCH] first commit --- .gitignore | 23 + .idea/.gitignore | 8 + .idea/artifacts/Client.xml | 20 + .idea/artifacts/Server.xml | 20 + .idea/compiler.xml | 17 + .idea/encodings.xml | 7 + .idea/jarRepositories.xml | 20 + .../Maven__cn_hutool_hutool_all_5_7_13.xml | 13 + .../Maven__org_openjfx_javafx_base_11_0_2.xml | 13 + ...en__org_openjfx_javafx_base_mac_11_0_2.xml | 13 + ...en__org_openjfx_javafx_controls_11_0_2.xml | 13 + ...org_openjfx_javafx_controls_mac_11_0_2.xml | 13 + .../Maven__org_openjfx_javafx_fxml_11_0_2.xml | 13 + ...en__org_openjfx_javafx_fxml_mac_11_0_2.xml | 13 + ...en__org_openjfx_javafx_graphics_11_0_2.xml | 13 + ...org_openjfx_javafx_graphics_mac_11_0_2.xml | 13 + .idea/misc.xml | 13 + .idea/modules.xml | 8 + .idea/runConfigurations.xml | 10 + .idea/uiDesigner.xml | 124 +++++ .idea/vcs.xml | 6 + Client-META-INF/META-INF/MANIFEST.MF | 3 + P2P_Project.iml | 23 + Server-META-INF/META-INF/MANIFEST.MF | 3 + ...M_0fc64d0f-db44-4c73-ad9c-39ab067711be_1.a | 5 + ...M_a082b40f-9a6d-43d6-ae96-a2be5c27d1e8_2.a | 5 + ...M_ce3e0168-2bb4-4499-bd90-ee3d6bb59043_1.a | 5 + out/artifacts/Client/README.md | 0 pom.xml | 437 +++++++++++++++++ res/0.a | 5 + res/1.a | 5 + res/1.b | 5 + res/2.a | 5 + res/3.a | 5 + src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/com/echo/p2p_project/Util.java | 29 ++ .../echo/p2p_project/client/ClientMain.java | 451 ++++++++++++++++++ .../client/gui/ClientIndexController.java | 11 + .../client/gui/ClientIndexView.java | 34 ++ .../client/gui/HelloApplication.java | 23 + .../client/gui/HelloController.java | 14 + .../client/interfaces/P2P_FileRegistry.java | 17 + .../p2p_project/client/model/CHeartBeat.java | 61 +++ .../p2p_project/client/model/P2PFileImpl.java | 50 ++ .../echo/p2p_project/server/ServerMain.java | 108 +++++ .../server/gui/ServerIndexApplication.java | 78 +++ .../server/gui/ServerIndexController.java | 152 ++++++ .../server/interfaces/ConstructRegistry.java | 20 + .../server/interfaces/HeartBeatRegistry.java | 16 + .../interfaces/HelloRegistryFacade.java | 15 + .../server/interfaces/SyncingRegistry.java | 20 + .../server/model/ConstructImpl.java | 79 +++ .../server/model/HeartBeatImpl.java | 42 ++ .../server/model/HelloRegistryFacadeImpl.java | 39 ++ .../p2p_project/server/model/SyncingImpl.java | 53 ++ .../p2p_project/server/model/WatchDog.java | 63 +++ .../com/echo/p2p_project/u_model/Peer.java | 151 ++++++ .../echo/p2p_project/u_model/Resource.java | 57 +++ .../com/echo/p2p_project/client_index.fxml | 14 + src/main/resources/gui/client_index.fxml | 18 + src/main/resources/gui/server_index.fxml | 92 ++++ .../com/echo/p2p_project/client_index.fxml | 14 + target/classes/gui/client_index.fxml | 18 + target/classes/gui/server_index.fxml | 92 ++++ 64 files changed, 2733 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/artifacts/Client.xml create mode 100644 .idea/artifacts/Server.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries/Maven__cn_hutool_hutool_all_5_7_13.xml create mode 100644 .idea/libraries/Maven__org_openjfx_javafx_base_11_0_2.xml create mode 100644 .idea/libraries/Maven__org_openjfx_javafx_base_mac_11_0_2.xml create mode 100644 .idea/libraries/Maven__org_openjfx_javafx_controls_11_0_2.xml create mode 100644 .idea/libraries/Maven__org_openjfx_javafx_controls_mac_11_0_2.xml create mode 100644 .idea/libraries/Maven__org_openjfx_javafx_fxml_11_0_2.xml create mode 100644 .idea/libraries/Maven__org_openjfx_javafx_fxml_mac_11_0_2.xml create mode 100644 .idea/libraries/Maven__org_openjfx_javafx_graphics_11_0_2.xml create mode 100644 .idea/libraries/Maven__org_openjfx_javafx_graphics_mac_11_0_2.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 Client-META-INF/META-INF/MANIFEST.MF create mode 100644 P2P_Project.iml create mode 100644 Server-META-INF/META-INF/MANIFEST.MF create mode 100644 download/0fc64d0f-db44-4c73-ad9c-39ab067711be_FROM_0fc64d0f-db44-4c73-ad9c-39ab067711be_1.a create mode 100644 download/50dfcced-c877-458f-ae45-0a50108c168c_FROM_a082b40f-9a6d-43d6-ae96-a2be5c27d1e8_2.a create mode 100644 download/ce3e0168-2bb4-4499-bd90-ee3d6bb59043_FROM_ce3e0168-2bb4-4499-bd90-ee3d6bb59043_1.a create mode 100644 out/artifacts/Client/README.md create mode 100644 pom.xml create mode 100644 res/0.a create mode 100644 res/1.a create mode 100644 res/1.b create mode 100644 res/2.a create mode 100644 res/3.a create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/com/echo/p2p_project/Util.java create mode 100644 src/main/java/com/echo/p2p_project/client/ClientMain.java create mode 100644 src/main/java/com/echo/p2p_project/client/gui/ClientIndexController.java create mode 100644 src/main/java/com/echo/p2p_project/client/gui/ClientIndexView.java create mode 100644 src/main/java/com/echo/p2p_project/client/gui/HelloApplication.java create mode 100644 src/main/java/com/echo/p2p_project/client/gui/HelloController.java create mode 100644 src/main/java/com/echo/p2p_project/client/interfaces/P2P_FileRegistry.java create mode 100644 src/main/java/com/echo/p2p_project/client/model/CHeartBeat.java create mode 100644 src/main/java/com/echo/p2p_project/client/model/P2PFileImpl.java create mode 100644 src/main/java/com/echo/p2p_project/server/ServerMain.java create mode 100644 src/main/java/com/echo/p2p_project/server/gui/ServerIndexApplication.java create mode 100644 src/main/java/com/echo/p2p_project/server/gui/ServerIndexController.java create mode 100644 src/main/java/com/echo/p2p_project/server/interfaces/ConstructRegistry.java create mode 100644 src/main/java/com/echo/p2p_project/server/interfaces/HeartBeatRegistry.java create mode 100644 src/main/java/com/echo/p2p_project/server/interfaces/HelloRegistryFacade.java create mode 100644 src/main/java/com/echo/p2p_project/server/interfaces/SyncingRegistry.java create mode 100644 src/main/java/com/echo/p2p_project/server/model/ConstructImpl.java create mode 100644 src/main/java/com/echo/p2p_project/server/model/HeartBeatImpl.java create mode 100644 src/main/java/com/echo/p2p_project/server/model/HelloRegistryFacadeImpl.java create mode 100644 src/main/java/com/echo/p2p_project/server/model/SyncingImpl.java create mode 100644 src/main/java/com/echo/p2p_project/server/model/WatchDog.java create mode 100644 src/main/java/com/echo/p2p_project/u_model/Peer.java create mode 100644 src/main/java/com/echo/p2p_project/u_model/Resource.java create mode 100644 src/main/resources/com/echo/p2p_project/client_index.fxml create mode 100644 src/main/resources/gui/client_index.fxml create mode 100644 src/main/resources/gui/server_index.fxml create mode 100644 target/classes/com/echo/p2p_project/client_index.fxml create mode 100644 target/classes/gui/client_index.fxml create mode 100644 target/classes/gui/server_index.fxml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a1c2a23 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/artifacts/Client.xml b/.idea/artifacts/Client.xml new file mode 100644 index 0000000..3d24285 --- /dev/null +++ b/.idea/artifacts/Client.xml @@ -0,0 +1,20 @@ + + + $PROJECT_DIR$/out/artifacts/Client + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/artifacts/Server.xml b/.idea/artifacts/Server.xml new file mode 100644 index 0000000..8faab12 --- /dev/null +++ b/.idea/artifacts/Server.xml @@ -0,0 +1,20 @@ + + + $PROJECT_DIR$/out/artifacts/Server + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..a73844c --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..5a2f139 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__cn_hutool_hutool_all_5_7_13.xml b/.idea/libraries/Maven__cn_hutool_hutool_all_5_7_13.xml new file mode 100644 index 0000000..78f8e2c --- /dev/null +++ b/.idea/libraries/Maven__cn_hutool_hutool_all_5_7_13.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_base_11_0_2.xml b/.idea/libraries/Maven__org_openjfx_javafx_base_11_0_2.xml new file mode 100644 index 0000000..0e43ea8 --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_base_11_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_base_mac_11_0_2.xml b/.idea/libraries/Maven__org_openjfx_javafx_base_mac_11_0_2.xml new file mode 100644 index 0000000..b6df1b7 --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_base_mac_11_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_controls_11_0_2.xml b/.idea/libraries/Maven__org_openjfx_javafx_controls_11_0_2.xml new file mode 100644 index 0000000..253d3f9 --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_controls_11_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_controls_mac_11_0_2.xml b/.idea/libraries/Maven__org_openjfx_javafx_controls_mac_11_0_2.xml new file mode 100644 index 0000000..4cd5728 --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_controls_mac_11_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_fxml_11_0_2.xml b/.idea/libraries/Maven__org_openjfx_javafx_fxml_11_0_2.xml new file mode 100644 index 0000000..c4c2db0 --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_fxml_11_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_fxml_mac_11_0_2.xml b/.idea/libraries/Maven__org_openjfx_javafx_fxml_mac_11_0_2.xml new file mode 100644 index 0000000..cdf0b8e --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_fxml_mac_11_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_graphics_11_0_2.xml b/.idea/libraries/Maven__org_openjfx_javafx_graphics_11_0_2.xml new file mode 100644 index 0000000..6cfa8eb --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_graphics_11_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_openjfx_javafx_graphics_mac_11_0_2.xml b/.idea/libraries/Maven__org_openjfx_javafx_graphics_mac_11_0_2.xml new file mode 100644 index 0000000..199ac82 --- /dev/null +++ b/.idea/libraries/Maven__org_openjfx_javafx_graphics_mac_11_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d5066ab --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ee5c4ee --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Client-META-INF/META-INF/MANIFEST.MF b/Client-META-INF/META-INF/MANIFEST.MF new file mode 100644 index 0000000..df3d1c0 --- /dev/null +++ b/Client-META-INF/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.echo.p2p_project.client.ClientMain + diff --git a/P2P_Project.iml b/P2P_Project.iml new file mode 100644 index 0000000..6be892e --- /dev/null +++ b/P2P_Project.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Server-META-INF/META-INF/MANIFEST.MF b/Server-META-INF/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ebf936 --- /dev/null +++ b/Server-META-INF/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.echo.p2p_project.server.ServerMain + 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 new file mode 100644 index 0000000..c89a354 --- /dev/null +++ b/download/0fc64d0f-db44-4c73-ad9c-39ab067711be_FROM_0fc64d0f-db44-4c73-ad9c-39ab067711be_1.a @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..c48b607 --- /dev/null +++ b/download/50dfcced-c877-458f-ae45-0a50108c168c_FROM_a082b40f-9a6d-43d6-ae96-a2be5c27d1e8_2.a @@ -0,0 +1,5 @@ +2222222222222222 +================ +This File is 2.a +================ + 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 new file mode 100644 index 0000000..c89a354 --- /dev/null +++ b/download/ce3e0168-2bb4-4499-bd90-ee3d6bb59043_FROM_ce3e0168-2bb4-4499-bd90-ee3d6bb59043_1.a @@ -0,0 +1,5 @@ +hahaha +================ +This File is 1.a +================ + diff --git a/out/artifacts/Client/README.md b/out/artifacts/Client/README.md new file mode 100644 index 0000000..e69de29 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5b37424 --- /dev/null +++ b/pom.xml @@ -0,0 +1,437 @@ + + + + 4.0.0 + + com.COMP3013J + COMP3013J + 1.0-SNAPSHOT + + COMP3013J + + http://www.example.com + + + UTF-8 + 1.8 + 1.8 + + + + + + + + + + + + + + + + org.openjfx + javafx-controls + 11.0.2 + + + org.openjfx + javafx-fxml + 11.0.2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + cn.hutool + hutool-all + 5.7.13 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + + maven-resources-plugin + 3.0.2 + + + + maven-compiler-plugin + 3.8.0 + + + + maven-surefire-plugin + 2.22.1 + + + + + maven-install-plugin + 2.5.2 + + + + maven-deploy-plugin + 2.8.2 + + + + + maven-site-plugin + 3.7.1 + + + + maven-project-info-reports-plugin + 3.0.0 + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + com.util.old.Main + + + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 8 + 8 + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + jar-with-dependencies + + + + + com.util.old.Main + + + + + + + make-assembly + + package + + single + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.2 + + + package + + shade + + + false + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + .settings/** + *.classpath + *.project + *.txt + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.3 + + + + + + com/**/* + + + + + + + pre-test + + prepare-agent + + + + post-test + test + + report + + + + + + + + diff --git a/res/0.a b/res/0.a new file mode 100644 index 0000000..941e56d --- /dev/null +++ b/res/0.a @@ -0,0 +1,5 @@ +0000000000000000 +================ +This File is 0.a +================ + diff --git a/res/1.a b/res/1.a new file mode 100644 index 0000000..c89a354 --- /dev/null +++ b/res/1.a @@ -0,0 +1,5 @@ +hahaha +================ +This File is 1.a +================ + diff --git a/res/1.b b/res/1.b new file mode 100644 index 0000000..ba64e71 --- /dev/null +++ b/res/1.b @@ -0,0 +1,5 @@ +3333333333333333 +================ +This File is 1.b +================ + diff --git a/res/2.a b/res/2.a new file mode 100644 index 0000000..c48b607 --- /dev/null +++ b/res/2.a @@ -0,0 +1,5 @@ +2222222222222222 +================ +This File is 2.a +================ + diff --git a/res/3.a b/res/3.a new file mode 100644 index 0000000..191f465 --- /dev/null +++ b/res/3.a @@ -0,0 +1,5 @@ +3333333333333333 +================ +This File is 3.a +================ + diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ebf936 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.echo.p2p_project.server.ServerMain + diff --git a/src/main/java/com/echo/p2p_project/Util.java b/src/main/java/com/echo/p2p_project/Util.java new file mode 100644 index 0000000..11de37c --- /dev/null +++ b/src/main/java/com/echo/p2p_project/Util.java @@ -0,0 +1,29 @@ +package com.echo.p2p_project; + +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-17:39 + * @Project: P2P_Project + * @Package: com.echo.p2p_project + * @Description: + **/ +public class Util { + public static final int RMI_PORT = 1099; + + public static String getIP() { + try { + InetAddress ip4 = Inet4Address.getLocalHost(); + return ip4.getHostAddress().toString(); + } catch (UnknownHostException e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/src/main/java/com/echo/p2p_project/client/ClientMain.java b/src/main/java/com/echo/p2p_project/client/ClientMain.java new file mode 100644 index 0000000..47e3d30 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/ClientMain.java @@ -0,0 +1,451 @@ +package com.echo.p2p_project.client; + + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.net.NetUtil; +import com.echo.p2p_project.Util; +import com.echo.p2p_project.client.interfaces.P2P_FileRegistry; +import com.echo.p2p_project.client.model.CHeartBeat; +import com.echo.p2p_project.client.model.P2PFileImpl; +import com.echo.p2p_project.server.interfaces.ConstructRegistry; +import com.echo.p2p_project.server.interfaces.HeartBeatRegistry; +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.Resource; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.rmi.ConnectException; +import java.rmi.NotBoundException; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.util.*; + +/** + * @Author: WangYuyang + * @Date: 2021/10/19-15:22 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.client + * @Description: + **/ +public class ClientMain { + private static final String MainServerIP = "127.0.0.1"; + public static Peer peer; + public static HashMap DHRT = new LinkedHashMap(); + public static Integer retry_times = 0; + private static String name = "Peer"; + private static String IP = Util.getIP(); + private static Integer Port = 35000; + private static Integer timeoutMillis = 2000; + private static Registry registry; + private static Registry file_service; + private static ConstructRegistry constructRegistry; + private static HeartBeatRegistry heartBeatRegistry; + private static SyncingRegistry syncingRegistry; + private static Boolean hasStarted = false; + private static Scanner sc; + private static Thread service; + private static Integer download_retry_count = 0; + + + public static void main(String[] args) throws ConnectException { + service = new Thread(new Runnable() { + @Override + public void run() { + recover(); + } + }); + service.start(); + try { + service.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + sc = new Scanner(System.in); + System.out.println(""); + System.out.print(">>> "); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + switch (line) { + case "\n": + System.out.print(">>> "); + break; + case "i": + System.out.println("GUID: " + peer.getGUID()); + System.out.println("P2P_Port: " + peer.getP2P_port()); + System.out.println("Possessing: " + peer.getPossessing()); + System.out.println("Possessing Size: " + peer.getPossessing().size()); + System.out.println("DHRT: " + DHRT); + break; + case "r": + System.out.print("Register Resource Name: "); + String res_name = sc.nextLine(); + reg_file(res_name); + break; + case "l": + System.out.print("Look up Filename: "); + String file_name = sc.nextLine(); + HashMap res = look_up_file(file_name); + System.out.println(res); + break; + case "d": + System.out.print("Download Filename: "); + String file_name_to_download = sc.nextLine(); + download(file_name_to_download); + + + } + System.out.print(">>> "); + } + } + + private static void download(String file_name_to_download) { + HashMap resources = look_up_file(file_name_to_download); + download(resources); + download_retry_count = 0; + } + + private static void download(String file_name_to_download, Integer retry_count) { + if (download_retry_count <= 5) { + download_retry_count+=1; + System.out.println("Retrying " + download_retry_count); + HashMap resources = look_up_file(file_name_to_download); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + download(resources); + }else{ + System.out.println("Give up download."); + return; + } + } + + + public static void init_peer() { + Boolean center_status = false; + Boolean file_status = false; + hasStarted = false; + try { + registry = LocateRegistry.getRegistry(MainServerIP, Util.RMI_PORT); + center_status = lookup_registry() && register_peer() ? true : false; + } catch (RemoteException e) { +// e.printStackTrace(); + } + if (peer == null) { + System.out.println("Server Not Running."); + exit(); + } + + CHeartBeat.StartHeart(heartBeatRegistry, peer.getGUID()); + + //Init file services + try { + file_service = LocateRegistry.createRegistry(Port); + P2P_FileRegistry p2PFileRegistry = new P2PFileImpl(); + file_service.rebind("p2PFileRegistry", p2PFileRegistry); + System.out.println("========== Client-Side RMI Started ! =========="); + System.out.println("Port: " + Port); + System.out.println("UnicastServer: " + file_service.toString()); + System.out.println("Registered: " + Arrays.toString(file_service.list())); + System.out.println("==============================================="); + //init local file dirs + 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/"); + file_status = true; + } catch (RemoteException e) { +// e.printStackTrace(); + } + if (file_status && center_status) + hasStarted = true; + + } + + private static Boolean register_peer() { + System.out.println("+++++++ Register & Construct Peer +++++++"); + Port = NetUtil.getUsableLocalPort(35000, 45000); + try { + peer = constructRegistry.ConstructPeer(name, IP, Port); + System.out.println("+++++++ Peer Construct Success ! +++++++"); + System.out.println("Peer GUID: " + peer.getGUID()); + System.out.println("Peer: " + peer); + System.out.println("+++++++ Register Finished +++++++"); + return true; + } catch (RemoteException e) { + hasStarted = false; + System.out.println("register_peer RemoteException"); + e.printStackTrace(); + return false; + } + } + + private static Boolean lookup_registry() { + try { + HelloRegistryFacade hello = (HelloRegistryFacade) registry.lookup("HelloRegistry"); + String response = hello.helloWorld(name); + System.out.println("=======> " + response + " <======="); + if (response == null) { + System.out.println("server failed"); + return false; + } + constructRegistry = (ConstructRegistry) registry.lookup("constructRegistry"); + heartBeatRegistry = (HeartBeatRegistry) registry.lookup("heatBeatRegistry"); + syncingRegistry = (SyncingRegistry) registry.lookup("syncingRegistry"); + return true; + + } catch (NotBoundException | RemoteException e) { + System.out.println("lookup_registry RemoteException"); +// e.printStackTrace(); + return false; + } + + } + + private static Boolean reg_file(String name) { + File file = new File("res/" + name); + if (!file.exists()) { + System.out.println("File not exists !"); + System.out.println("This File is not exist in your file system !"); + return false; + } else { + System.out.println("File ok."); + System.out.println("File length: " + file.length()); + } + try { + Resource resource = constructRegistry.ConstructResource(peer.getGUID(), name); + ClientMain.DHRT.put(resource.getGUID(), resource); + DHRT.put(resource.getGUID(), resource); + System.out.println("File Register Success!"); + System.out.println("Resource GUID: " + resource.getGUID()); + sync_peer(); + } catch (RemoteException e) { + e.printStackTrace(); + System.out.println("File Register Error !"); + return false; + } + return true; + } + + private static void sync_peer() { + try { + peer = syncingRegistry.syncPeer(peer.getGUID()); + } catch (RemoteException e) { + e.printStackTrace(); + System.out.println("Peer Sync Failed !"); + } + System.out.println("Peer Sync Finished !"); + } + + private static HashMap look_up_file(String file_name) { + HashMap result = new LinkedHashMap<>(); + System.out.println("Looking for: " + file_name); + for (Resource r : DHRT.values()) { + if (r.getName().equals(file_name)) { + System.out.println("FOUND in local DHRT."); + result.put(r.getGUID(), r); + new Thread(new Runnable() { + @Override + public void run() { + try { + DHRT = syncingRegistry.syncUHRT(); + System.out.println(Thread.currentThread() + "sync UHRT Finished."); + } catch (RemoteException e) { + e.printStackTrace(); + } + } + }).start(); + } + } + if (result.size() == 0) + System.out.println("Not found in local DHRT."); + else + return result; + try { + DHRT = syncingRegistry.syncUHRT(); + 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); + } + } + if (result.size() == 0) + System.out.println("Requested Resource NOT FOUND !"); + return result; + } + + private static void download(HashMap resources) { + if (resources.size() <= 0) { + System.out.println("Resource can not be download !"); + return; + } + if (resources.size() > 1) { + System.out.println("There are more than 1 resource with identical name:"); + System.out.println("File List: "); + for (Resource r : resources.values()) { + System.out.println(r); + } + System.out.println("Please specify the GUID of the resource: "); + UUID GUID = null; + 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); + } + } + + private 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()); + file = p2PFileRegistry.download(resource.getGUID()); + } catch (RemoteException e) { + System.out.println("RemoteException"); + try { + DHRT = syncingRegistry.syncUHRT(); + download(resource.getName(), download_retry_count); + return; + } catch (RemoteException ex) { + ex.printStackTrace(); + } +// e.printStackTrace(); + } catch (NotBoundException e) { + e.printStackTrace(); + } + if (file != null) { + + System.out.println("File downloaded !"); + System.out.println("File length : " + file.length()); + + 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; + } + + } +// +// private static void P2P_download(Peer p, Resource resource, Integer retry_count) { +// retry_count += 1; +// Integer port = p.getP2P_port(); +// String IP = p.getIP(); +// Registry p2pRegistry = null; +// File file = null; +// try { +// p2pRegistry = LocateRegistry.getRegistry(IP, port); +// P2P_FileRegistry p2PFileRegistry = (P2P_FileRegistry) p2pRegistry.lookup("p2PFileRegistry"); +// file = p2PFileRegistry.download(resource.getGUID()); +// } catch (RemoteException e) { +// System.out.println("Retry Failed !"); +// try { +// DHRT = syncingRegistry.syncUHRT(); +// System.out.println("Retrying " + retry_count); +// if (retry_count <= 5) { +// try { +// Thread.sleep(1000); +// } catch (InterruptedException ex) { +// ex.printStackTrace(); +// } +// P2P_download(p, resource, retry_count); +// } +// return; +// } catch (RemoteException ex) { +// ex.printStackTrace(); +// } +//// e.printStackTrace(); +// } catch (NotBoundException e) { +// e.printStackTrace(); +// } +// if (file != null) { +// System.out.println("File downloaded !"); +// System.out.println("File length : " + file.length()); +// +// 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 exit() { + System.exit(0); + } + + public static void recover() { + init_peer(); + } + + public static void recover(int retry_count) { + retry_count += 1; + System.out.println("Try to recover......." + retry_count); + if (retry_count > 10) { + System.out.println(); + System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++"); + System.out.println("================== CLIENT ===================="); + System.out.println("================== SHUTDOWN ===================="); + System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++"); + System.out.println(); + exit(); + } + service.interrupt(); + service = new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + recover(); + } + }); + service.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 new file mode 100644 index 0000000..4c871c1 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/gui/ClientIndexController.java @@ -0,0 +1,11 @@ +package com.echo.p2p_project.client.gui; + +/** + * @Author: WangYuyang + * @Date: 2021/10/23-17:11 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.client.gui + * @Description: + **/ +public class ClientIndexController { +} diff --git a/src/main/java/com/echo/p2p_project/client/gui/ClientIndexView.java b/src/main/java/com/echo/p2p_project/client/gui/ClientIndexView.java new file mode 100644 index 0000000..b2e27f0 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/gui/ClientIndexView.java @@ -0,0 +1,34 @@ +package com.echo.p2p_project.client.gui; + +import cn.hutool.Hutool; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.resource.Resource; +import cn.hutool.core.io.resource.ResourceUtil; +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.stage.Stage; + +import java.io.IOException; + +/** + * @Author: WangYuyang + * @Date: 2021/10/23-17:11 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.client.gui + * @Description: + **/ +public class ClientIndexView extends Application { + @Override + public void start(Stage stage) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtil.getResource("gui/client_index.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 320, 240); + stage.setTitle("Client!"); + stage.setScene(scene); + stage.show(); + } + + public static void main(String[] args) { + launch(); + } +} diff --git a/src/main/java/com/echo/p2p_project/client/gui/HelloApplication.java b/src/main/java/com/echo/p2p_project/client/gui/HelloApplication.java new file mode 100644 index 0000000..494d9d3 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/gui/HelloApplication.java @@ -0,0 +1,23 @@ +package com.echo.p2p_project.client.gui; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.stage.Stage; + +import java.io.IOException; + +public class HelloApplication extends Application { + @Override + public void start(Stage stage) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 320, 240); + stage.setTitle("Hello!"); + stage.setScene(scene); + stage.show(); + } + + public static void main(String[] args) { + launch(); + } +} \ No newline at end of file diff --git a/src/main/java/com/echo/p2p_project/client/gui/HelloController.java b/src/main/java/com/echo/p2p_project/client/gui/HelloController.java new file mode 100644 index 0000000..e4428a7 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/gui/HelloController.java @@ -0,0 +1,14 @@ +package com.echo.p2p_project.client.gui; + +import javafx.fxml.FXML; +import javafx.scene.control.Label; + +public class HelloController { + @FXML + private Label welcomeText; + + @FXML + protected void onHelloButtonClick() { + welcomeText.setText("Welcome to JavaFX Application!"); + } +} \ No newline at end of file diff --git a/src/main/java/com/echo/p2p_project/client/interfaces/P2P_FileRegistry.java b/src/main/java/com/echo/p2p_project/client/interfaces/P2P_FileRegistry.java new file mode 100644 index 0000000..3022ea3 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/interfaces/P2P_FileRegistry.java @@ -0,0 +1,17 @@ +package com.echo.p2p_project.client.interfaces; + +import java.io.File; +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-21:07 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.client.interfaces + * @Description: + **/ +public interface P2P_FileRegistry extends Remote { + File download(UUID resID) throws RemoteException; +} 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 new file mode 100644 index 0000000..4b27b26 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/model/CHeartBeat.java @@ -0,0 +1,61 @@ +package com.echo.p2p_project.client.model; + +import com.echo.p2p_project.client.ClientMain; +import com.echo.p2p_project.server.interfaces.HeartBeatRegistry; + +import java.rmi.RemoteException; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-18:13 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.client.model + * @Description: + **/ +public class CHeartBeat { + public static Thread heart; + private static Boolean running = true; + + + public static void StartHeart(HeartBeatRegistry heartBeatRegistry, UUID GUID) { + running = true; + heart = new Thread(new Runnable() { + @Override + public void run() { + System.out.println("Start Heart Beat"); + while (running) { + try { + Boolean status = heartBeatRegistry.heartBeat(GUID); +// System.out.println(status); + ClientMain.retry_times = 0; + } catch (RemoteException e) { + running = false; + System.out.println("Main Server DOWN !"); + ClientMain.retry_times += 1; + ClientMain.recover(ClientMain.retry_times); +// ClientMain.exit(); +// System.out.println("Trying to recover..."); +// downCount+=1; +// System.out.println("DOWN COUNT: " + downCount); +// if(downCount > 5) { +// System.out.println("DOWN COUNT > 5: Exiting.."); +// +// } + + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + }); + heart.start(); + } + + public static void endHeart() { + running = false; + } +} diff --git a/src/main/java/com/echo/p2p_project/client/model/P2PFileImpl.java b/src/main/java/com/echo/p2p_project/client/model/P2PFileImpl.java new file mode 100644 index 0000000..33ac074 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/client/model/P2PFileImpl.java @@ -0,0 +1,50 @@ +package com.echo.p2p_project.client.model; + +import com.echo.p2p_project.client.ClientMain; +import com.echo.p2p_project.client.interfaces.P2P_FileRegistry; +import com.echo.p2p_project.u_model.Resource; + +import java.io.File; +import java.rmi.RemoteException; +import java.rmi.server.RMISocketFactory; +import java.rmi.server.UnicastRemoteObject; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-21:08 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.client.model + * @Description: + **/ +public class P2PFileImpl extends UnicastRemoteObject implements P2P_FileRegistry { + /** + * Creates and exports a new UnicastRemoteObject object using an + * anonymous port. + * + *

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 P2PFileImpl() throws RemoteException { + super(); + } + + + @Override + public File download(UUID resID) throws RemoteException{ + Resource resource = ClientMain.DHRT.get(resID); + if(resource == null){ + System.out.println("Resource Not in local DHRT."); + return null; + } + File file = new File("res/" + resource.getName()); + if (file==null) { + System.out.println("Resource Not in File System."); + return null; + } + return file; + } +} diff --git a/src/main/java/com/echo/p2p_project/server/ServerMain.java b/src/main/java/com/echo/p2p_project/server/ServerMain.java new file mode 100644 index 0000000..9c022b5 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/ServerMain.java @@ -0,0 +1,108 @@ +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.HeartBeatRegistry; +import com.echo.p2p_project.server.interfaces.HelloRegistryFacade; +import com.echo.p2p_project.server.interfaces.SyncingRegistry; +import com.echo.p2p_project.server.model.*; +import com.echo.p2p_project.u_model.Peer; +import com.echo.p2p_project.u_model.Resource; +import com.sun.javafx.collections.ObservableMapWrapper; +import javafx.collections.ObservableMap; + +import java.io.PrintStream; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.util.*; + +/** + * @Author: WangYuyang + * @Date: 2021/10/19-15:22 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server + * @Description: + **/ +public class ServerMain{ + public static ObservableMapWrapper UHPT = new ObservableMapWrapper<>(new LinkedHashMap<>()); + public static ObservableMapWrapper UHRT = new ObservableMapWrapper<>(new LinkedHashMap<>()); + private static Registry registry; + private static Boolean HasStarted = false; + private static Thread service; + + public static void main(String[] args) { + service = new Thread(new Runnable() { + @Override + public void run() { + init(); + } + }); + service.start(); + Scanner sc = new Scanner(System.in); + System.out.println(""); + System.out.print(">>> "); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + switch (line) { + case "\n": + System.out.print(">>> "); + break; + case "i": + System.out.println("UHPT: " + UHPT); + System.out.println("UHPT Size: " + UHPT.size()); + System.out.println("UHRT: " + UHRT); + System.out.println("UHRT Size: " + UHRT.size()); + System.out.println("registry: " + registry); + break; + } + System.out.print(">>> "); + } + + } + + public static void init() { + try { + // Start Registry, Port: 1099 + registry = LocateRegistry.createRegistry(Util.RMI_PORT); + reg_services(); + + + System.out.println("======= RMI Start Up! ============"); + System.out.println(registry.toString()); + System.out.println("Registered: " + Arrays.toString(registry.list())); + for (String s : registry.list()) { + System.out.println("Registered: " + s); + } + System.out.println("============ WatchDog ============"); + if (!HasStarted) + WatchDog.startWatchDog(); + System.out.println("======= Start Up Finished ========"); + HasStarted = true; + + } catch (RemoteException e) { + e.printStackTrace(); + } + } + + private static void reg_services() { + try { + HelloRegistryFacade hello = new HelloRegistryFacadeImpl(); + ConstructRegistry constructRegistry = new ConstructImpl(); + HeartBeatRegistry heartBeatRegistry = new HeartBeatImpl(); + SyncingRegistry syncingRegistry = new SyncingImpl(); + + registry.rebind("HelloRegistry", hello); + registry.rebind("constructRegistry", constructRegistry); + registry.rebind("heatBeatRegistry", heartBeatRegistry); + registry.rebind("syncingRegistry", syncingRegistry); + + } catch (RemoteException e) { + e.printStackTrace(); + } + } + + public static void exit(){ + service.interrupt(); + } +} diff --git a/src/main/java/com/echo/p2p_project/server/gui/ServerIndexApplication.java b/src/main/java/com/echo/p2p_project/server/gui/ServerIndexApplication.java new file mode 100644 index 0000000..a222d3c --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/gui/ServerIndexApplication.java @@ -0,0 +1,78 @@ +package com.echo.p2p_project.server.gui; + +import cn.hutool.core.io.resource.ResourceUtil; +import com.echo.p2p_project.server.ServerMain; +import javafx.application.Application; +import javafx.collections.MapChangeListener; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.control.TableView; +import javafx.scene.control.TextArea; +import javafx.scene.control.TreeView; +import javafx.stage.Stage; + +import java.io.IOException; + +/** + * @Author: WangYuyang + * @Date: 2021/10/23-17:20 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.gui + * @Description: + **/ +public class ServerIndexApplication extends Application { + private static Thread server_thread; + private static Thread log_thread; + private static Boolean is_started = false; +// public static ObservableMap O_UHPT; +// public static ObservableMap O_UHRT; + + public static void main(String[] args) { + launch(); + } + + + public static void init_server() { + if (is_started == false) { + is_started = true; + server_thread = new Thread(new Runnable() { + @Override + public void run() { + ServerMain.main(new String[]{}); + } + }); + server_thread.start(); + } else { + System.out.println("CAN NOT START"); + } + } + + public static void stopServer() { + if (is_started) { + ServerMain.exit(); + server_thread.interrupt(); + is_started = false; + System.exit(0); + } + } + + public static void start_log(TextArea logField, TableView uhpt_table, TableView uhrt_table, TreeView uhrt_tree) { +// log_thread = new Thread(new Runnable() { +// @Override +// public void run() { +// +// } +// }); +// log_thread.start(); + + } + + @Override + public void start(Stage stage) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtil.getResource("gui/server_index.fxml")); + Scene scene = new Scene(fxmlLoader.load()); + stage.setTitle("Server"); + stage.setScene(scene); + stage.show(); + } +} 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 new file mode 100644 index 0000000..3919a81 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/gui/ServerIndexController.java @@ -0,0 +1,152 @@ +package com.echo.p2p_project.server.gui; + +import com.echo.p2p_project.server.ServerMain; +import com.echo.p2p_project.u_model.Peer; +import com.echo.p2p_project.u_model.Resource; +import com.sun.javafx.collections.ObservableListWrapper; +import javafx.application.Platform; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.value.ObservableValue; +import javafx.collections.MapChangeListener; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.scene.control.*; +import javafx.util.Callback; + +import java.util.ArrayList; + +/** + * @Author: WangYuyang + * @Date: 2021/10/23-17:21 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.gui + * @Description: + **/ +public class ServerIndexController { + public Button StartServerButton; + public TextArea LogField; + public Button StopServerButton; + public TableView uhpt_table; + public TableView uhrt_table; + public TreeView uhrt_tree; + public TableColumn UHPT_GUID; + public TableColumn UHPT_IP; + public TableColumn UHPT_PORT; + public TableColumn UHRT_GUID; + public TableColumn UHRT_NAME; + + + public void initialize() { + UHPT_GUID.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getGUID().toString()); + } + }); + UHPT_IP.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getIP().toString()); + } + }); + UHPT_PORT.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getP2P_port().toString()); + } + }); + UHRT_GUID.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getGUID().toString()); + } + }); + UHRT_NAME.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getName().toString()); + } + }); + + ServerMain.UHPT.addListener(new MapChangeListener() { + @Override + public void onChanged(Change change) { + Platform.runLater(new Runnable() { + @Override + public void run() { + + + //update log + LogField.appendText(change.toString() + "\n\n"); + + //update table + ObservableList peers = new ObservableListWrapper(new ArrayList<>(ServerMain.UHPT.values())); + uhpt_table.setItems(peers); + + + //update tree + TreeItem rootItem = new TreeItem("Server"); + rootItem.setExpanded(true); + for (Peer p : peers) { + TreeItem item = new TreeItem(p.getGUID().toString()); + for (Resource r : p.possessing.values()) { + item.setExpanded(true); + TreeItem filename = new TreeItem(r.getName().toString()); + item.getChildren().add(filename); + } + rootItem.getChildren().add(item); + + } + uhrt_tree.setRoot(rootItem); + } + }); + + + } + }); + ServerMain.UHRT.addListener(new MapChangeListener() { + @Override + public void onChanged(Change change) { + Platform.runLater(new Runnable() { + @Override + public void run() { + //update log + LogField.appendText(change.toString() + "\n\n"); + + //update table + ObservableList resources = new ObservableListWrapper(new ArrayList<>(ServerMain.UHRT.values())); + uhrt_table.setItems(resources); + + //update tree + ObservableList peers = new ObservableListWrapper(new ArrayList<>(ServerMain.UHPT.values())); + TreeItem rootItem = new TreeItem("Server"); + rootItem.setExpanded(true); + for (Peer p : peers) { + TreeItem item = new TreeItem(p.getGUID().toString()); + for (Resource r : p.possessing.values()) { + item.setExpanded(true); + TreeItem filename = new TreeItem(r.getName().toString()); + item.getChildren().add(filename); + } + rootItem.getChildren().add(item); + + } + uhrt_tree.setRoot(rootItem); + + } + }); + } + }); + + + } + + public void StartServerButtonPressed(ActionEvent actionEvent) { + ServerIndexApplication.init_server(); + + } + + public void StopServerButtonPressed(ActionEvent actionEvent) { + ServerIndexApplication.stopServer(); + } +} 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 new file mode 100644 index 0000000..18524c6 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/interfaces/ConstructRegistry.java @@ -0,0 +1,20 @@ +package com.echo.p2p_project.server.interfaces; + +import com.echo.p2p_project.u_model.Peer; +import com.echo.p2p_project.u_model.Resource; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-17:09 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.interfaces + * @Description: + **/ +public interface ConstructRegistry extends Remote { + Peer ConstructPeer(String name, String IP, Integer P2P_port) throws RemoteException; + Resource ConstructResource(UUID PeerGUID, String name) throws RemoteException; +} diff --git a/src/main/java/com/echo/p2p_project/server/interfaces/HeartBeatRegistry.java b/src/main/java/com/echo/p2p_project/server/interfaces/HeartBeatRegistry.java new file mode 100644 index 0000000..27381b9 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/interfaces/HeartBeatRegistry.java @@ -0,0 +1,16 @@ +package com.echo.p2p_project.server.interfaces; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-17:48 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.interfaces + * @Description: + **/ +public interface HeartBeatRegistry extends Remote { + Boolean heartBeat(UUID GUID) throws RemoteException; +} diff --git a/src/main/java/com/echo/p2p_project/server/interfaces/HelloRegistryFacade.java b/src/main/java/com/echo/p2p_project/server/interfaces/HelloRegistryFacade.java new file mode 100644 index 0000000..c6e151b --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/interfaces/HelloRegistryFacade.java @@ -0,0 +1,15 @@ +package com.echo.p2p_project.server.interfaces; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-16:37 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.interfaces + * @Description: + **/ +public interface HelloRegistryFacade extends Remote { + String helloWorld(String name) throws RemoteException; +} diff --git a/src/main/java/com/echo/p2p_project/server/interfaces/SyncingRegistry.java b/src/main/java/com/echo/p2p_project/server/interfaces/SyncingRegistry.java new file mode 100644 index 0000000..9f1ddf7 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/interfaces/SyncingRegistry.java @@ -0,0 +1,20 @@ +package com.echo.p2p_project.server.interfaces; + +import com.echo.p2p_project.u_model.Peer; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.util.HashMap; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-22:45 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.interfaces + * @Description: + **/ +public interface SyncingRegistry extends Remote { + Peer syncPeer(UUID GUID) throws RemoteException; + HashMap syncUHRT() 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 new file mode 100644 index 0000000..7f43693 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/model/ConstructImpl.java @@ -0,0 +1,79 @@ +package com.echo.p2p_project.server.model; + +import com.echo.p2p_project.server.ServerMain; +import com.echo.p2p_project.server.interfaces.ConstructRegistry; +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.HashMap; +import java.util.Random; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-17:10 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.model + * @Description: + **/ +public class ConstructImpl extends UnicastRemoteObject implements ConstructRegistry { + /** + * Creates and exports a new UnicastRemoteObject object using an + * anonymous port. + * + *

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 ConstructImpl() throws RemoteException { + super(); + } + + @Override + public Peer ConstructPeer(String name, String IP_Address, Integer P2P_port) throws RemoteException { + UUID GUID = UUID.randomUUID(); + if(ServerMain.UHPT.containsKey(GUID)) { + System.out.println("DUP GUID!!!!!!!"); + return null; + } + String peerName = name; + String IP = IP_Address; + Integer port = P2P_port; + Random random = new Random(); + Integer routingMetric = random.nextInt(100); + Peer peer = new Peer(GUID, peerName, IP, port, routingMetric); + ServerMain.UHPT.put(peer.getGUID(), peer); + System.out.println("Peer Reg: " + peer); + return peer; + } + + @Override + public Resource ConstructResource(UUID PeerGUID, String name) 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); + 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; + } +} diff --git a/src/main/java/com/echo/p2p_project/server/model/HeartBeatImpl.java b/src/main/java/com/echo/p2p_project/server/model/HeartBeatImpl.java new file mode 100644 index 0000000..64b658b --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/model/HeartBeatImpl.java @@ -0,0 +1,42 @@ +package com.echo.p2p_project.server.model; + +import com.echo.p2p_project.server.ServerMain; +import com.echo.p2p_project.server.interfaces.HeartBeatRegistry; +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.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-17:49 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.model + * @Description: + **/ +public class HeartBeatImpl extends UnicastRemoteObject implements HeartBeatRegistry { + /** + * Creates and exports a new UnicastRemoteObject object using an + * anonymous port. + * + *

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 HeartBeatImpl() throws RemoteException { + super(); + } + + @Override + public Boolean heartBeat(UUID GUID) throws RemoteException { + Peer peer = ServerMain.UHPT.get(GUID); + if(peer == null) + return false; + peer.setMissedHartBeat(0); + return true; + } +} diff --git a/src/main/java/com/echo/p2p_project/server/model/HelloRegistryFacadeImpl.java b/src/main/java/com/echo/p2p_project/server/model/HelloRegistryFacadeImpl.java new file mode 100644 index 0000000..2f7cb92 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/model/HelloRegistryFacadeImpl.java @@ -0,0 +1,39 @@ +package com.echo.p2p_project.server.model; + +import com.echo.p2p_project.server.interfaces.HelloRegistryFacade; + +import java.rmi.RemoteException; +import java.rmi.server.RMISocketFactory; +import java.rmi.server.UnicastRemoteObject; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-16:38 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.model + * @Description: + **/ +public class HelloRegistryFacadeImpl extends UnicastRemoteObject implements HelloRegistryFacade { + + + /** + * Creates and exports a new UnicastRemoteObject object using an + * anonymous port. + * + *

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 HelloRegistryFacadeImpl() throws RemoteException { + super(); + System.out.println("HelloRegistryFacadeImpl"); + } + + @Override + public String helloWorld(String name) { + return "[Registry] Hi, " + name; + } + +} \ No newline at end of file diff --git a/src/main/java/com/echo/p2p_project/server/model/SyncingImpl.java b/src/main/java/com/echo/p2p_project/server/model/SyncingImpl.java new file mode 100644 index 0000000..195ebae --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/model/SyncingImpl.java @@ -0,0 +1,53 @@ +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. + * + *

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; + } +} 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 new file mode 100644 index 0000000..cd29766 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/server/model/WatchDog.java @@ -0,0 +1,63 @@ +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; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-17:52 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.server.model + * @Description: + **/ +public class WatchDog { + private static Thread watch_thread; + private static Boolean running = true; + + public static void startWatchDog() { + watch_thread = new Thread(new Runnable() { + @Override + public void run() { + while (running) { + for (int i = 0; i < ServerMain.UHPT.size(); i++) { + UUID uuid = (UUID) ServerMain.UHPT.keySet().toArray()[i]; + Peer p = ServerMain.UHPT.get(uuid); +// System.out.println(p); + if (p == null) { + System.out.println("REMOVED(peer is null): " + uuid); + ServerMain.UHPT.remove(uuid); + continue; + } + p.setMissedHartBeat(p.getMissedHartBeat() + 1); + if (p.getMissedHartBeat() > 5) { + System.out.println(); + System.out.println("==================================== REMOVE ====================================="); + System.out.println("PEER (peer MissedHartBeat > 5): " + uuid); + for (UUID guid : ServerMain.UHPT.get(uuid).possessing.keySet()) { + System.out.println("Resources: " + guid); + ServerMain.UHRT.remove(guid); + } + ServerMain.UHPT.remove(uuid); + System.out.println("================================================================================="); + } + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + System.out.println("WatchDog Interrupted"); + } + } + } + }); + watch_thread.start(); + System.out.println("WatchDog Started !"); + } + + public static void stopWatchDog() { + running = false; +// watch_thread.interrupt(); + } +} diff --git a/src/main/java/com/echo/p2p_project/u_model/Peer.java b/src/main/java/com/echo/p2p_project/u_model/Peer.java new file mode 100644 index 0000000..4836408 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/u_model/Peer.java @@ -0,0 +1,151 @@ +package com.echo.p2p_project.u_model; + +import cn.hutool.Hutool; +import com.sun.istack.internal.NotNull; + +import java.io.Serializable; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/19-15:23 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.u_model + * @Description: + **/ +public class Peer implements Serializable,Comparable { + private UUID GUID; + private String name; + private String IP; + private Integer P2P_port; + private Integer routingMetric; + private Integer MissedHartBeat = 0; + //Only use this possessing in DHRT + public HashMap possessing = new LinkedHashMap(); + + public Peer(UUID GUID, String name, String IP, Integer p2P_port, Integer routingMetric) { + this.GUID = GUID; + this.name = name; + this.IP = IP; + P2P_port = p2P_port; + this.routingMetric = routingMetric; + } + + public UUID getGUID() { + return GUID; + } + + public void setGUID(UUID GUID) { + this.GUID = GUID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIP() { + return IP; + } + + public void setIP(String IP) { + this.IP = IP; + } + + public Integer getP2P_port() { + return P2P_port; + } + + public void setP2P_port(Integer p2P_port) { + P2P_port = p2P_port; + } + + public Integer getRoutingMetric() { + return routingMetric; + } + + public void setRoutingMetric(Integer routingMetric) { + this.routingMetric = routingMetric; + } + + public HashMap getPossessing() { + return possessing; + } + + public void setPossessing(HashMap possessing) { + this.possessing = possessing; + } + + public Integer getMissedHartBeat() { + return MissedHartBeat; + } + + public void setMissedHartBeat(Integer missedHartBeat) { + MissedHartBeat = missedHartBeat; + } + + @Override + public String toString() { + return "Peer{" + + "GUID=" + GUID + + ", name='" + name + '\'' + + ", IP='" + IP + '\'' + + ", P2P_port=" + P2P_port + + ", routingMetric=" + routingMetric + + ", MissedHartBeat=" + MissedHartBeat + + ", possessing=" + possessing.keySet() + + '}'; + } + + /** + * Compares this object with the specified object for order. Returns a + * negative integer, zero, or a positive integer as this object is less + * than, equal to, or greater than the specified object. + * + *

The implementor must ensure sgn(x.compareTo(y)) == + * -sgn(y.compareTo(x)) for all x and y. (This + * implies that x.compareTo(y) must throw an exception iff + * y.compareTo(x) throws an exception.) + * + *

The implementor must also ensure that the relation is transitive: + * (x.compareTo(y)>0 && y.compareTo(z)>0) implies + * x.compareTo(z)>0. + * + *

Finally, the implementor must ensure that x.compareTo(y)==0 + * implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for + * all z. + * + *

It is strongly recommended, but not strictly required that + * (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any + * class that implements the Comparable interface and violates + * this condition should clearly indicate this fact. The recommended + * language is "Note: this class has a natural ordering that is + * inconsistent with equals." + * + *

In the foregoing description, the notation + * sgn(expression) designates the mathematical + * signum function, which is defined to return one of -1, + * 0, or 1 according to whether the value of + * expression is negative, zero or positive. + * + * @param o the object to be compared. + * @return a negative integer, zero, or a positive integer as this object + * is less than, equal to, or greater than the specified object. + * @throws NullPointerException if the specified object is null + * @throws ClassCastException if the specified object's type prevents it + * from being compared to this object. + */ + @Override + public int compareTo(@NotNull Object o) { + Peer p = (Peer) o; + return p.routingMetric - p.routingMetric; + } +} 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 new file mode 100644 index 0000000..9fbb097 --- /dev/null +++ b/src/main/java/com/echo/p2p_project/u_model/Resource.java @@ -0,0 +1,57 @@ +package com.echo.p2p_project.u_model; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.UUID; + +/** + * @Author: WangYuyang + * @Date: 2021/10/20-16:57 + * @Project: P2P_Project + * @Package: com.echo.p2p_project.u_model + * @Description: + **/ +public class Resource implements Serializable { + private UUID GUID; + private String name; + public HashMap possessedBy = new LinkedHashMap<>(); + + public Resource(UUID GUID, String name) { + this.GUID = GUID; + this.name = name; + } + + public UUID getGUID() { + return GUID; + } + + public void setGUID(UUID GUID) { + this.GUID = GUID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public HashMap getPossessedBy() { + return possessedBy; + } + + public void setPossessedBy(HashMap possessedBy) { + this.possessedBy = possessedBy; + } + + @Override + public String toString() { + return "Resource{" + + "GUID=" + GUID + + ", name='" + name + '\'' + + ", possessedBy=" + possessedBy.keySet() + + '}'; + } +} diff --git a/src/main/resources/com/echo/p2p_project/client_index.fxml b/src/main/resources/com/echo/p2p_project/client_index.fxml new file mode 100644 index 0000000..1f156ba --- /dev/null +++ b/src/main/resources/com/echo/p2p_project/client_index.fxml @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/src/main/resources/gui/client_index.fxml b/src/main/resources/gui/client_index.fxml new file mode 100644 index 0000000..9defe98 --- /dev/null +++ b/src/main/resources/gui/client_index.fxml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + +