diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..77c9d58 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file 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 new file mode 100644 index 0000000..c89a354 --- /dev/null +++ b/download/28c30b5a-b6fa-45c8-b8d2-cf4e33326f7e_FROM_03b8a253-9978-4d53-a1c3-ae7a223945ea_1.a @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..c48b607 --- /dev/null +++ b/download/4d796993-7e79-45f6-8371-10b00a2965a8_FROM_4d796993-7e79-45f6-8371-10b00a2965a8_2.a @@ -0,0 +1,5 @@ +2222222222222222 +================ +This File is 2.a +================ + 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 new file mode 100644 index 0000000..c89a354 --- /dev/null +++ b/download/9f4035f5-9f1d-405a-9072-a46f4577f33a_FROM_d6f06e40-2bf6-4306-bb6d-f9a7947567ec_1.a @@ -0,0 +1,5 @@ +hahaha +================ +This File is 1.a +================ + 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 new file mode 100644 index 0000000..941e56d --- /dev/null +++ b/download/c8a8222c-d78d-4463-8905-bc6e4d927abf_FROM_faea0c7a-ce41-4a9c-bc89-bc4861d6570c_0.a @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..c48b607 --- /dev/null +++ b/download/e2b5fda8-c3e8-4b7d-9b92-ae5aa5a7ae7b_FROM_6e437793-c40e-40a4-8c75-617de5ebf164_2.a @@ -0,0 +1,5 @@ +2222222222222222 +================ +This File is 2.a +================ + diff --git a/res/1.b b/res/1.b index ba64e71..f5d5b5a 100644 --- a/res/1.b +++ b/res/1.b @@ -1,4 +1,4 @@ -3333333333333333 +1111111111111111 ================ This File is 1.b ================ diff --git a/res/demo.png b/res/demo.png deleted file mode 100644 index c83271d..0000000 Binary files a/res/demo.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 edf8771..0d6123d 100644 --- a/src/main/java/com/echo/p2p_project/Util.java +++ b/src/main/java/com/echo/p2p_project/Util.java @@ -16,8 +16,6 @@ import java.security.NoSuchAlgorithmException; * @Description: **/ public class Util { - public static final int RMI_PORT = 1099; - public static String getIP() { try { InetAddress ip4 = Inet4Address.getLocalHost(); 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 4eec360..dc08f67 100644 --- a/src/main/java/com/echo/p2p_project/client/ClientMain.java +++ b/src/main/java/com/echo/p2p_project/client/ClientMain.java @@ -13,6 +13,7 @@ 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 com.sun.javafx.collections.ObservableMapWrapper; import java.io.File; import java.io.FileNotFoundException; @@ -32,9 +33,10 @@ import java.util.*; * @Description: **/ public class ClientMain { - private static final String MainServerIP = "127.0.0.1"; + public static String MainServerIP = "127.0.0.1"; + public static int RMI_PORT = 1099; public static Peer peer; - public static HashMap DHRT = new LinkedHashMap(); + public static ObservableMapWrapper DHRT = new ObservableMapWrapper<>(new LinkedHashMap<>()); public static Integer retry_times = 0; private static String name = "Peer"; private static String IP = Util.getIP(); @@ -102,7 +104,7 @@ public class ClientMain { } } - private static void download(String file_name_to_download) { + public static void download(String file_name_to_download) { HashMap resources = look_up_file(file_name_to_download); download(resources); download_retry_count = 0; @@ -131,7 +133,7 @@ public class ClientMain { Boolean file_status = false; hasStarted = false; try { - registry = LocateRegistry.getRegistry(MainServerIP, Util.RMI_PORT); + registry = LocateRegistry.getRegistry(MainServerIP, RMI_PORT); center_status = lookup_registry() && register_peer() ? true : false; } catch (RemoteException e) { // e.printStackTrace(); @@ -207,10 +209,8 @@ public class ClientMain { } - private static Boolean reg_file(String name) { + public 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 !"); @@ -219,6 +219,8 @@ public class ClientMain { System.out.println("File ok."); System.out.println("File length: " + file.length()); } + String hash = Util.createSha1(file); + System.out.println(hash); try { Resource resource = constructRegistry.ConstructResource(peer.getGUID(), name, hash); ClientMain.DHRT.put(resource.getGUID(), resource); @@ -255,7 +257,8 @@ public class ClientMain { @Override public void run() { try { - DHRT = syncingRegistry.syncUHRT(); + HashMap hashMap = syncingRegistry.syncUHRT(); + DHRT.putAll(hashMap); System.out.println(Thread.currentThread() + "sync UHRT Finished."); } catch (RemoteException e) { e.printStackTrace(); @@ -269,7 +272,8 @@ public class ClientMain { else return result; try { - DHRT = syncingRegistry.syncUHRT(); + HashMap hashMap = syncingRegistry.syncUHRT(); + DHRT.putAll(hashMap); System.out.println("DHRT Sync Finished !"); } catch (RemoteException e) { e.printStackTrace(); @@ -286,7 +290,7 @@ public class ClientMain { return result; } - private static void download(HashMap resources) { + public static void download(HashMap resources) { if (resources.size() <= 0) { System.out.println("Resource can not be download !"); return; @@ -364,8 +368,19 @@ public class ClientMain { P2P_download(p, resource); } } + public static void sync_DHRT(){ + HashMap hashMap = null; + try { + System.out.println("Start syncUHRT"); + hashMap = syncingRegistry.syncUHRT(); + DHRT.putAll(hashMap); + } catch (RemoteException e) { + e.printStackTrace(); + } + System.out.println("END syncUHRT"); + } - private static void P2P_download(Peer p, Resource resource) { + public static void P2P_download(Peer p, Resource resource) { Integer port = p.getP2P_port(); String IP = p.getIP(); Registry p2pRegistry = null; @@ -376,11 +391,15 @@ public class ClientMain { P2P_FileRegistry p2PFileRegistry = (P2P_FileRegistry) p2pRegistry.lookup("p2PFileRegistry"); System.out.println("Connected."); System.out.println("Downloading FROM: " + p.getGUID()); - file = p2PFileRegistry.download(resource.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())); + } catch (RemoteException e) { System.out.println("RemoteException"); try { - DHRT = syncingRegistry.syncUHRT(); + HashMap hashMap = syncingRegistry.syncUHRT(); + DHRT.putAll(hashMap); download(resource.getName(), download_retry_count); return; } catch (RemoteException ex) { 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 4c871c1..48dedd3 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 @@ -1,5 +1,31 @@ package com.echo.p2p_project.client.gui; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.watch.WatchMonitor; +import cn.hutool.core.io.watch.Watcher; +import cn.hutool.core.lang.Console; +import com.echo.p2p_project.client.ClientMain; +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.event.EventHandler; +import javafx.scene.control.*; +import javafx.util.Callback; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.WatchEvent; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.UUID; + /** * @Author: WangYuyang * @Date: 2021/10/23-17:11 @@ -8,4 +34,215 @@ package com.echo.p2p_project.client.gui; * @Description: **/ public class ClientIndexController { + public TextField server_ip; + public TextField server_port; + public Button connect_button; + public ChoiceBox download_datalist; + public Button download_button; + public ChoiceBox reg_datalist; + public Button reg_button; + public TableView DHRT_Table; + public TextArea log_field; + public TableColumn DHRT_GUID; + public TableColumn DHRT_NAME; + public TableColumn DHRT_HASH; + public Button sync_button; + + public void initialize() { + ObservableList local_file_list = new ObservableListWrapper<>(Collections.synchronizedList(new ArrayList<>())); + ObservableList remote_file_list = new ObservableListWrapper<>(Collections.synchronizedList(new ArrayList<>())); + ClientMain.DHRT.addListener(new MapChangeListener() { + @Override + public void onChanged(Change change) { + log_field.appendText(change.toString()); + ObservableList resources = new ObservableListWrapper(new ArrayList<>(ClientMain.DHRT.values())); + DHRT_Table.setItems(resources); + Platform.runLater(new Runnable() { + @Override + public void run() { + synchronized (remote_file_list) { + if (change.wasAdded()) { + remote_file_list.add(change.getValueAdded()); + } + if (change.wasRemoved()) { + remote_file_list.remove(change.getValueRemoved()); + } + } + if(remote_file_list.size() >= 1){ + download_datalist.setValue(remote_file_list.get(0)); + } + } + }); + + } + }); + 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()); + } + //这里只监听文件或目录的修改事件 + // 修改:res-> 0.a + // 修改:res-> 0.a + // 修改:res-> 0.a + // 创建:res-> 123 copy.db + // 创建:res-> ddd.db + // 删除:res-> 123 copy.db + // 删除:res-> ddd.db + // 修改:res-> .DS_Store + WatchMonitor watchMonitor = WatchMonitor.create(System.getProperty("user.dir") + "/res/", WatchMonitor.EVENTS_ALL); + watchMonitor.setWatcher(new Watcher() { + @Override + public void onCreate(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("Create:{}-> {}", currentPath, obj); + File[] files = FileUtil.ls(System.getProperty("user.dir") + "/res/"); + Platform.runLater(new Runnable() { + @Override + public void run() { + local_file_list.clear(); + for (File f : files) { + local_file_list.add(f.getName()); + } + } + }); + } + + @Override + public void onModify(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("Modify:{}-> {}", currentPath, obj); + File[] files = FileUtil.ls(System.getProperty("user.dir") + "/res/"); + Platform.runLater(new Runnable() { + @Override + public void run() { + local_file_list.clear(); + for (File f : files) { + local_file_list.add(f.getName()); + } + } + }); + + } + + @Override + public void onDelete(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("Delete:{}-> {}", currentPath, obj); + File[] files = FileUtil.ls(System.getProperty("user.dir") + "/res/"); + Platform.runLater(new Runnable() { + @Override + public void run() { + local_file_list.clear(); + for (File f : files) { + local_file_list.add(f.getName()); + } + } + }); + } + + @Override + public void onOverflow(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("Overflow:{}-> {}", currentPath, obj); + } + }); + //启动监听 + watchMonitor.start(); + reg_datalist.setItems(local_file_list); + if (local_file_list.size() >= 1) + reg_datalist.setValue(local_file_list.get(0)); + + + connect_button.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + ClientMain.MainServerIP = server_ip.getText(); + ClientMain.RMI_PORT = Integer.valueOf(server_port.getText()).intValue(); + new Thread(new Runnable() { + @Override + public void run() { + ClientMain.recover(); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + ClientMain.sync_DHRT(); + } + }).start(); + + download_button.setDisable(false); + reg_button.setDisable(false); + sync_button.setDisable(false); + } + }); + + DHRT_GUID.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getGUID().toString()); + } + }); + + DHRT_NAME.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getName().toString()); + } + }); + + DHRT_HASH.setCellValueFactory(new Callback, ObservableValue>() { + @Override + public ObservableValue call(TableColumn.CellDataFeatures param) { + return new SimpleStringProperty(param.getValue().getHash().toString()); + } + }); + + + sync_button.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + ClientMain.sync_DHRT(); + } + }); + reg_button.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + String filename = String.valueOf(reg_datalist.getValue()); + ClientMain.reg_file(filename); + } + }); + + download_button.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + Resource r = (Resource) download_datalist.getValue(); + if (r != null) { + System.out.println(r); + ArrayList processed_peers = new ArrayList<>(); + 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()); + } + ClientMain.P2P_download(processed_peers.get(0), r); + Dialog dialog = new Dialog(); + dialog.show(); + } + } + }); + + + } } 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 index b2e27f0..6dbdb5c 100644 --- a/src/main/java/com/echo/p2p_project/client/gui/ClientIndexView.java +++ b/src/main/java/com/echo/p2p_project/client/gui/ClientIndexView.java @@ -22,7 +22,7 @@ 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); + Scene scene = new Scene(fxmlLoader.load()); stage.setTitle("Client!"); stage.setScene(scene); stage.show(); 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 deleted file mode 100644 index 494d9d3..0000000 --- a/src/main/java/com/echo/p2p_project/client/gui/HelloApplication.java +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index e4428a7..0000000 --- a/src/main/java/com/echo/p2p_project/client/gui/HelloController.java +++ /dev/null @@ -1,14 +0,0 @@ -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 index 3022ea3..dd84967 100644 --- 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 @@ -13,5 +13,5 @@ import java.util.UUID; * @Description: **/ public interface P2P_FileRegistry extends Remote { - File download(UUID resID) throws RemoteException; + byte[] download(UUID resID) throws RemoteException; } 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 index 33ac074..ec22070 100644 --- a/src/main/java/com/echo/p2p_project/client/model/P2PFileImpl.java +++ b/src/main/java/com/echo/p2p_project/client/model/P2PFileImpl.java @@ -1,5 +1,6 @@ package com.echo.p2p_project.client.model; +import cn.hutool.core.io.FileUtil; import com.echo.p2p_project.client.ClientMain; import com.echo.p2p_project.client.interfaces.P2P_FileRegistry; import com.echo.p2p_project.u_model.Resource; @@ -34,17 +35,20 @@ public class P2PFileImpl extends UnicastRemoteObject implements P2P_FileRegistry @Override - public File download(UUID resID) throws RemoteException{ + public byte[] download(UUID resID) throws RemoteException{ + System.out.println("Income connection."); 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()); + byte[] file_byte = FileUtil.readBytes(file); + System.out.println("File byte[] length: " + file_byte.length); if (file==null) { System.out.println("Resource Not in File System."); return null; } - return file; + return file_byte; } } diff --git a/src/main/java/com/echo/p2p_project/server/ServerMain.java b/src/main/java/com/echo/p2p_project/server/ServerMain.java index 9c022b5..b365d34 100644 --- a/src/main/java/com/echo/p2p_project/server/ServerMain.java +++ b/src/main/java/com/echo/p2p_project/server/ServerMain.java @@ -25,6 +25,7 @@ import java.util.*; * @Description: **/ public class ServerMain{ + public static final int RMI_PORT = 1099; public static ObservableMapWrapper UHPT = new ObservableMapWrapper<>(new LinkedHashMap<>()); public static ObservableMapWrapper UHRT = new ObservableMapWrapper<>(new LinkedHashMap<>()); private static Registry registry; @@ -64,7 +65,7 @@ public class ServerMain{ public static void init() { try { // Start Registry, Port: 1099 - registry = LocateRegistry.createRegistry(Util.RMI_PORT); + registry = LocateRegistry.createRegistry(RMI_PORT); reg_services(); diff --git a/src/main/resources/com/echo/p2p_project/client_index.fxml b/src/main/resources/com/echo/p2p_project/client_index.fxml deleted file mode 100644 index 1f156ba..0000000 --- a/src/main/resources/com/echo/p2p_project/client_index.fxml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - diff --git a/src/main/resources/gui/client_index.fxml b/src/main/resources/gui/client_index.fxml index 9defe98..dd9973c 100644 --- a/src/main/resources/gui/client_index.fxml +++ b/src/main/resources/gui/client_index.fxml @@ -1,18 +1,141 @@ + + - - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +