hash checking

master
王宇洋 3 years ago
parent d035dfbb97
commit 32f87ce7e0

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="fastRequestCollection">
<option name="detail">
<CollectionDetail>
<option name="childList">
<list>
<CollectionDetail>
<option name="groupId" value="1" />
<option name="id" value="1" />
<option name="name" value="Default Group" />
<option name="type" value="1" />
</CollectionDetail>
</list>
</option>
<option name="groupId" value="-1" />
<option name="id" value="0" />
<option name="name" value="Root" />
<option name="type" value="1" />
</CollectionDetail>
</option>
</component>
</project>

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

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

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

@ -1,10 +1,12 @@
package com.echo.p2p_project;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.io.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @Author: WangYuyang
@ -25,5 +27,32 @@ public class Util {
return null;
}
}
public static String createSha1(File file){
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
InputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int n = 0;
byte[] buffer = new byte[8192];
while (n != -1) {
try {
n = fis.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (n > 0) {
digest.update(buffer, 0, n);
}
}
return new HexBinaryAdapter().marshal(digest.digest());
}
}

@ -110,7 +110,7 @@ public class ClientMain {
private static void download(String file_name_to_download, Integer retry_count) {
if (download_retry_count <= 5) {
download_retry_count+=1;
download_retry_count += 1;
System.out.println("Retrying " + download_retry_count);
HashMap resources = look_up_file(file_name_to_download);
try {
@ -119,7 +119,7 @@ public class ClientMain {
e.printStackTrace();
}
download(resources);
}else{
} else {
System.out.println("Give up download.");
return;
}
@ -209,6 +209,8 @@ public class ClientMain {
private static Boolean reg_file(String name) {
File file = new File("res/" + name);
String hash = Util.createSha1(file);
System.out.println(hash);
if (!file.exists()) {
System.out.println("File not exists !");
System.out.println("This File is not exist in your file system !");
@ -218,7 +220,7 @@ public class ClientMain {
System.out.println("File length: " + file.length());
}
try {
Resource resource = constructRegistry.ConstructResource(peer.getGUID(), name);
Resource resource = constructRegistry.ConstructResource(peer.getGUID(), name, hash);
ClientMain.DHRT.put(resource.getGUID(), resource);
DHRT.put(resource.getGUID(), resource);
System.out.println("File Register Success!");
@ -295,13 +297,53 @@ public class ClientMain {
for (Resource r : resources.values()) {
System.out.println(r);
}
System.out.println("Please specify the GUID of the resource: ");
System.out.println("Checking file hashing...");
Boolean hash_all_same = true;
String file_hash = ((Resource) resources.values().toArray()[0]).getHash();
for (Resource r : resources.values()) {
if (!r.getHash().equals(file_hash)) {
hash_all_same = false;
break;
}
}
UUID GUID = null;
try {
GUID = UUID.fromString(sc.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("UUID Error");
return;
if (hash_all_same) {
System.out.println("Hash is the same, looking for best node...");
ArrayList<Peer> processed_peers = new ArrayList<>();
for (Resource r : resources.values()) {
for (Peer p : r.possessedBy.values()) {
processed_peers.add(p);
}
}
processed_peers.sort(new Comparator<Peer>() {
@Override
public int compare(Peer o1, Peer o2) {
return o1.getRoutingMetric() - o2.getRoutingMetric();
}
});
for (Peer p : processed_peers) {
System.out.println(p.getGUID().toString()+" <> " + p.getRoutingMetric());
}
for (Resource r : processed_peers.get(0).possessing.values()) {
System.out.println("File hash: " + file_hash);
System.out.println("Res hash : " + r.getHash());
if (r.getHash().equals(file_hash)) {
System.out.println("Best resource GUID: " + r.getGUID());
GUID = r.getGUID();
break;
}
}
} else {
System.out.println("The hash of these files is different, you need to specify the GUID");
System.out.println("Please specify the GUID of the resource: ");
try {
GUID = UUID.fromString(sc.nextLine());
} catch (IllegalArgumentException e) {
System.out.println("UUID Error");
return;
}
}
if (GUID == null) {
System.out.println("UUID Error");
@ -330,7 +372,7 @@ public class ClientMain {
File file = null;
try {
p2pRegistry = LocateRegistry.getRegistry(IP, port);
System.out.println("Try to connect: "+ p.getGUID());
System.out.println("Try to connect: " + p.getGUID());
P2P_FileRegistry p2PFileRegistry = (P2P_FileRegistry) p2pRegistry.lookup("p2PFileRegistry");
System.out.println("Connected.");
System.out.println("Downloading FROM: " + p.getGUID());

@ -10,6 +10,8 @@ import javafx.beans.value.ObservableValue;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.util.Callback;
@ -34,6 +36,9 @@ public class ServerIndexController {
public TableColumn<Peer, String> UHPT_PORT;
public TableColumn<Resource, String> UHRT_GUID;
public TableColumn<Resource, String> UHRT_NAME;
public BarChart bar_chart;
public TableColumn<Resource, String> UHRT_HASH;
XYChart.Series bar_data = new XYChart.Series();
public void initialize() {
@ -67,6 +72,14 @@ public class ServerIndexController {
return new SimpleStringProperty(param.getValue().getName().toString());
}
});
UHRT_HASH.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Resource, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Resource, String> param) {
return new SimpleStringProperty(param.getValue().getHash().toString());
}
});
bar_chart.getData().add(bar_data);
ServerMain.UHPT.addListener(new MapChangeListener() {
@Override
@ -83,7 +96,6 @@ public class ServerIndexController {
ObservableList<Peer> peers = new ObservableListWrapper<Peer>(new ArrayList<>(ServerMain.UHPT.values()));
uhpt_table.setItems(peers);
//update tree
TreeItem<String> rootItem = new TreeItem<String>("Server");
rootItem.setExpanded(true);
@ -98,6 +110,11 @@ public class ServerIndexController {
}
uhrt_tree.setRoot(rootItem);
//
setBarChartData();
}
});
@ -133,6 +150,10 @@ public class ServerIndexController {
}
uhrt_tree.setRoot(rootItem);
//
setBarChartData();
}
});
}
@ -141,6 +162,24 @@ public class ServerIndexController {
}
private void setBarChartData() {
for (int i = 0; i < ServerMain.UHPT.values().size(); i++) {
Peer p = (Peer) ServerMain.UHPT.values().toArray()[i];
Boolean contains = false;
for (Object d : bar_data.getData()) {
XYChart.Data data = (XYChart.Data) d;
System.out.println(data.getXValue().toString());
if (data.getXValue().toString().equals(p.getGUID().toString().substring(0, 4))) {
data.setYValue(p.possessing.size());
contains = true;
}
}
if (!contains)
bar_data.getData().add(new XYChart.Data<>(p.getGUID().toString().substring(0, 4), p.possessing.size()));
}
System.out.println(bar_chart.getData());
}
public void StartServerButtonPressed(ActionEvent actionEvent) {
ServerIndexApplication.init_server();

@ -16,5 +16,5 @@ import java.util.UUID;
**/
public interface ConstructRegistry extends Remote {
Peer ConstructPeer(String name, String IP, Integer P2P_port) throws RemoteException;
Resource ConstructResource(UUID PeerGUID, String name) throws RemoteException;
Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException;
}

@ -53,7 +53,7 @@ public class ConstructImpl extends UnicastRemoteObject implements ConstructRegis
}
@Override
public Resource ConstructResource(UUID PeerGUID, String name) throws RemoteException {
public Resource ConstructResource(UUID PeerGUID, String name, String hash) throws RemoteException {
Peer peer = ServerMain.UHPT.get(PeerGUID);
//If peer not registered in center
@ -69,7 +69,7 @@ public class ConstructImpl extends UnicastRemoteObject implements ConstructRegis
}
String ResName = name;
Resource res = new Resource(GUID, ResName);
Resource res = new Resource(GUID, ResName, hash);
res.possessedBy.put(peer.getGUID(), peer);
ServerMain.UHRT.put(res.getGUID(), res);
ServerMain.UHPT.get(peer.getGUID()).possessing.put(res.getGUID(), res);

@ -1,5 +1,7 @@
package com.echo.p2p_project.u_model;
import cn.hutool.core.lang.hash.Hash128;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -16,12 +18,19 @@ public class Resource implements Serializable {
private UUID GUID;
private String name;
public HashMap<UUID, Peer> possessedBy = new LinkedHashMap<>();
public String hash;
public Resource(UUID GUID, String name) {
this.GUID = GUID;
this.name = name;
}
public Resource(UUID GUID, String name, String hash) {
this.GUID = GUID;
this.name = name;
this.hash = hash;
}
public UUID getGUID() {
return GUID;
}
@ -46,12 +55,21 @@ public class Resource implements Serializable {
this.possessedBy = possessedBy;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
@Override
public String toString() {
return "Resource{" +
"GUID=" + GUID +
", name='" + name + '\'' +
", possessedBy=" + possessedBy.keySet() +
", hash='" + hash + '\'' +
'}';
}
}

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
@ -12,7 +13,7 @@
<children>
<VBox>
<children>
<HBox alignment="CENTER">
<HBox alignment="CENTER" VBox.vgrow="ALWAYS">
<children>
<VBox alignment="CENTER" spacing="16.0">
<children>
@ -30,7 +31,7 @@
<VBox alignment="CENTER" spacing="8.0">
<children>
<Label text="Log" />
<TextArea fx:id="LogField" maxHeight="150.0" prefHeight="150.0" prefWidth="676.0" wrapText="true">
<TextArea fx:id="LogField" maxHeight="150.0" maxWidth="1.7976931348623157E308" prefHeight="150.0" prefWidth="676.0" wrapText="true">
<font>
<Font name="Andale Mono" size="9.0" />
</font></TextArea>
@ -39,14 +40,30 @@
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
<VBox alignment="CENTER" spacing="8.0">
<children>
<Label text="Registed" />
<BarChart fx:id="bar_chart" prefHeight="190.0" prefWidth="500.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</BarChart>
</children>
<padding>
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
</children>
</HBox>
<HBox alignment="CENTER">
<HBox alignment="CENTER" VBox.vgrow="ALWAYS">
<children>
<VBox alignment="CENTER">
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Label text="UHPT" />
<TableView fx:id="uhpt_table">
<TableView fx:id="uhpt_table" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="UHPT_GUID" prefWidth="114.0" text="GUID" />
<TableColumn fx:id="UHPT_IP" prefWidth="115.0" text="IP" />
@ -58,13 +75,14 @@
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
<VBox alignment="CENTER">
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Label text="UHRT" />
<TableView fx:id="uhrt_table">
<TableView fx:id="uhrt_table" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308">
<columns>
<TableColumn fx:id="UHRT_GUID" prefWidth="128.0" text="GUID" />
<TableColumn fx:id="UHRT_NAME" prefWidth="115.0" text="NAME" />
<TableColumn fx:id="UHRT_NAME" prefWidth="120.0" text="NAME" />
<TableColumn fx:id="UHRT_HASH" prefWidth="120.0" text="HASH" />
</columns>
</TableView>
</children>
@ -72,10 +90,10 @@
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
<VBox alignment="CENTER">
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Label text="UHRT Tree" />
<TreeView fx:id="uhrt_tree" />
<TreeView fx:id="uhrt_tree" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" />
</children>
<padding>
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
@ -12,7 +13,7 @@
<children>
<VBox>
<children>
<HBox alignment="CENTER">
<HBox alignment="CENTER" VBox.vgrow="ALWAYS">
<children>
<VBox alignment="CENTER" spacing="16.0">
<children>
@ -30,7 +31,7 @@
<VBox alignment="CENTER" spacing="8.0">
<children>
<Label text="Log" />
<TextArea fx:id="LogField" maxHeight="150.0" prefHeight="150.0" prefWidth="676.0" wrapText="true">
<TextArea fx:id="LogField" maxHeight="150.0" maxWidth="1.7976931348623157E308" prefHeight="150.0" prefWidth="676.0" wrapText="true">
<font>
<Font name="Andale Mono" size="9.0" />
</font></TextArea>
@ -39,14 +40,30 @@
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
<VBox alignment="CENTER" spacing="8.0">
<children>
<Label text="Registed" />
<BarChart fx:id="bar_chart" prefHeight="190.0" prefWidth="500.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</BarChart>
</children>
<padding>
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
</children>
</HBox>
<HBox alignment="CENTER">
<HBox alignment="CENTER" VBox.vgrow="ALWAYS">
<children>
<VBox alignment="CENTER">
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Label text="UHPT" />
<TableView fx:id="uhpt_table">
<TableView fx:id="uhpt_table" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="UHPT_GUID" prefWidth="114.0" text="GUID" />
<TableColumn fx:id="UHPT_IP" prefWidth="115.0" text="IP" />
@ -58,13 +75,14 @@
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
<VBox alignment="CENTER">
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Label text="UHRT" />
<TableView fx:id="uhrt_table">
<TableView fx:id="uhrt_table" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308">
<columns>
<TableColumn fx:id="UHRT_GUID" prefWidth="128.0" text="GUID" />
<TableColumn fx:id="UHRT_NAME" prefWidth="115.0" text="NAME" />
<TableColumn fx:id="UHRT_NAME" prefWidth="120.0" text="NAME" />
<TableColumn fx:id="UHRT_HASH" prefWidth="120.0" text="HASH" />
</columns>
</TableView>
</children>
@ -72,10 +90,10 @@
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
<VBox alignment="CENTER">
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Label text="UHRT Tree" />
<TreeView fx:id="uhrt_tree" />
<TreeView fx:id="uhrt_tree" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" />
</children>
<padding>
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />

Loading…
Cancel
Save