Skip to content

Commit 8f92e79

Browse files
committed
V1.0
1 parent e64b33f commit 8f92e79

25 files changed

Lines changed: 305 additions & 398 deletions

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11

2-
settings
3-
42
target

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## 局域网文件传输系统
2+
3+
### 简介
4+
5+
基于Java socket api的P2P文件传输系统,支持文件一键共享、P2P文件传输、文件共享管理、文件下载管理以及用户管理等功能。
6+
7+
### 使用方法
8+
9+
配置好局域网环境后,使用命令java -jar 运行jar包(或者启动Main类中的main方法)

settings/setting.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# application port
2+
port=11223
3+
4+
# share file root directory
5+
share_root=D:/test
6+
7+
# download file root directory
8+
download_root=D:/test/download

src/main/java/cache/Cache.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
* @date 2023/3/23
55
*/
66

7-
import common.util.FileScanner;
8-
import common.util.PropertyParser;
97
import thread.BroadcastReceiverThread;
108
import thread.Server;
11-
import thread.ServerThread;
129

13-
import java.io.IOException;
1410
import java.net.InetAddress;
1511
import java.net.NetworkInterface;
1612
import java.net.Socket;
17-
import java.net.UnknownHostException;
1813
import java.util.*;
1914
import java.util.concurrent.ConcurrentHashMap;
2015

@@ -30,9 +25,6 @@
3025
public class Cache {
3126
public static ConcurrentHashMap<String, List<String>> ipRecourceMap = new ConcurrentHashMap<>();
3227
public static HashSet<String> ipTable = new HashSet<>();
33-
34-
public static HashMap<String, Socket> socketPool = new HashMap<>();
35-
3628
public static String localHost;
3729

3830
static {
@@ -54,7 +46,6 @@ public static String getLocalHostExactAddress() {
5446
continue;
5547
}
5648
if (currentAddr.getHostAddress().contains(":")) {
57-
// IPv6 address
5849
continue;
5950
}
6051
if (currentAddr.isSiteLocalAddress()) {
@@ -69,16 +60,6 @@ public static String getLocalHostExactAddress() {
6960
return null;
7061
}
7162

72-
public static Socket getSocketByIp(String ip) throws IOException {
73-
if (socketPool.containsKey(ip)) {
74-
return socketPool.get(ip);
75-
} else {
76-
Socket socket = new Socket(ip, 11223);
77-
socketPool.put(ip, socket);
78-
return socket;
79-
}
80-
}
81-
8263
public static BroadcastReceiverThread broadcastReceiverThread;
8364

8465
public static Server serverThread;
@@ -89,23 +70,6 @@ public static boolean checkThread(String ip) {
8970
return serverThreadMap.containsKey(ip);
9071
}
9172

92-
public static void stopAllThread() {
93-
broadcastReceiverThread.stop();
94-
for(String ip : serverThreadMap.keySet()) {
95-
try {
96-
serverThreadMap.get(ip).close();
97-
} catch (Exception e) {
98-
e.printStackTrace();
99-
}
100-
101-
}
102-
103-
try {
104-
serverThread.stop();
105-
} catch (IOException e) {
106-
e.printStackTrace();
107-
}
108-
}
10973

11074
public static void add(String ip, List<String> resources) {
11175
ipTable.add(ip);
@@ -117,22 +81,4 @@ public static void clear(String ip) {
11781
ipRecourceMap.remove(ip);
11882
}
11983

120-
public static void printResources() {
121-
HashSet<String> fileSet = new HashSet<>();
122-
for (String ip : ipRecourceMap.keySet()) {
123-
fileSet.addAll(ipRecourceMap.get(ip));
124-
}
125-
for (String file : fileSet) {
126-
System.out.println(file);
127-
}
128-
}
129-
130-
public static String getAvailableIp(String file) {
131-
for (String ip : ipRecourceMap.keySet()) {
132-
if (ipRecourceMap.get(ip).contains(file)) {
133-
return ip;
134-
}
135-
}
136-
return null;
137-
}
13884
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cache;
2+
3+
import common.message.MessageType;
4+
import common.util.FileScanner;
5+
import common.util.PropertyParser;
6+
import service.BroadcastSenderService;
7+
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.ScheduledExecutorService;
10+
import java.util.concurrent.TimeUnit;
11+
12+
public class CacheUpdate implements Runnable {
13+
private final ScheduledExecutorService executor;
14+
15+
public CacheUpdate() {
16+
this.executor = Executors.newSingleThreadScheduledExecutor(runnable -> {
17+
Thread thread = new Thread(runnable);
18+
thread.setDaemon(true);
19+
return thread;
20+
});
21+
}
22+
23+
public void start() {
24+
executor.scheduleAtFixedRate(this, 0, 5, TimeUnit.SECONDS);
25+
}
26+
27+
@Override
28+
public void run() {
29+
Cache.add(Cache.localHost, FileScanner.getAllFiles(PropertyParser.getShareRoot()));
30+
try {
31+
BroadcastSenderService.sendMessage(MessageType.HEART_BEAT_REQ);
32+
} catch (Exception e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
37+
public void stop() {
38+
executor.shutdown();
39+
}
40+
41+
}

src/main/java/common/message/MessageType.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ public interface MessageType {
1818
String FILE_DOWNLOAD_REQ = "5";
1919
// 文件下载响应
2020
String FILE_DOWNLOAD_RESP = "6";
21+
// 退出登录响应
2122
String BROADCAST_LOGOUT_RESP = "7";
22-
23+
// 关闭程序请求
2324
String CLOSE_REQ = "8";
24-
25+
// 关闭程序响应
2526
String CLOSE_RESP = "9";
27+
// 心跳检测请求
28+
String HEART_BEAT_REQ = "10";
29+
// 心跳检测响应
30+
String HEART_BEAT_RESP = "11";
2631

2732
}

src/main/java/common/util/SocketPool.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public static synchronized Socket getSocket(String host, int port) throws Except
2121
for (int i = 0; i < pool.size(); i++) {
2222

2323
Socket socket = pool.get(i);
24-
System.out.println("host: " + socket.getInetAddress().getHostAddress());
25-
System.out.println("port: " + socket.getPort());
2624

2725
if (socket.getInetAddress().getHostAddress().equals(host) && socket.getPort() == port && !socket.isClosed()) {
2826
pool.remove(i);

src/main/java/main/Main.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
package main;
22

33
import cache.Cache;
4+
import cache.CacheUpdate;
45
import common.message.MessageType;
5-
import thread.BroadcastSenderThread;
6-
import thread.BroadcastReceiverThread;
7-
import thread.Server;
8-
import thread.ServerThread;
9-
import window.FileUploader;
10-
11-
import java.io.File;
6+
import service.BroadcastSenderService;
7+
import thread.*;
8+
import window.ClientWindow;
129

1310
public class Main {
1411
public static void main(String[] args) throws Exception {
1512
// 启动广播包接收线程
1613
Cache.broadcastReceiverThread = new BroadcastReceiverThread();
17-
Thread thread1 = new Thread(Cache.broadcastReceiverThread);
18-
thread1.setDaemon(true);
19-
thread1.start();
14+
Thread broadcastReceiverThread = new Thread(Cache.broadcastReceiverThread);
15+
broadcastReceiverThread.setDaemon(true);
16+
broadcastReceiverThread.start();
2017

21-
// 启动服务线程
18+
// 启动服务端线程
2219
Cache.serverThread = new Server();
23-
Thread thread = new Thread(Cache.serverThread);
24-
thread.setDaemon(true);
25-
thread.start();
20+
Thread serverThread = new Thread(Cache.serverThread);
21+
serverThread.setDaemon(true);
22+
serverThread.start();
23+
24+
// 启动客户端线程
25+
// new Thread(new ClientThread()).start();
2626

2727
// 节点上线发送广播包
28-
new Thread(new BroadcastSenderThread(MessageType.BROADCAST_LOGIN_REQ)).start();
28+
BroadcastSenderService.sendMessage(MessageType.BROADCAST_LOGIN_REQ);
2929

3030
// 启动图形界面
31-
new FileUploader();
31+
new ClientWindow();
32+
33+
// 异步缓存更新及心跳监测线程
34+
CacheUpdate cacheUpdate = new CacheUpdate();
35+
cacheUpdate.start();
3236
}
3337
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package service;/**
2+
* @author Dantence
3+
* @description:
4+
* @date 2023/3/29
5+
*/
6+
7+
import cache.Cache;
8+
import common.dto.BroadcastLoginDTO;
9+
import common.message.Message;
10+
import common.util.FileScanner;
11+
import common.util.MessageUtil;
12+
import common.util.PropertyParser;
13+
14+
import java.net.DatagramSocket;
15+
import java.net.InetAddress;
16+
17+
/**
18+
* @projectName: p2pFileSystem
19+
* @package: service
20+
* @className: BroadcastSenderService
21+
* @author: Dantence
22+
* @description: 广播服务
23+
* @date: 2023/3/29 15:15
24+
* @version: 1.0
25+
*/
26+
public class BroadcastSenderService {
27+
public static void sendMessage(String messageType) {
28+
DatagramSocket socket = null;
29+
try {
30+
socket = new DatagramSocket();
31+
socket.setBroadcast(true);
32+
Message message = new Message();
33+
BroadcastLoginDTO broadcastLoginDTO = new BroadcastLoginDTO();
34+
broadcastLoginDTO.setIp(Cache.localHost);
35+
broadcastLoginDTO.setResources(FileScanner.getAllFiles(PropertyParser.getShareRoot()));
36+
message.setBroadcastLoginDTO(broadcastLoginDTO);
37+
message.setMessageType(messageType);
38+
MessageUtil.sendBroadcastMessage(socket, message, InetAddress.getByName("255.255.255.255"), PropertyParser.getPort());
39+
} catch (Exception e) {
40+
e.printStackTrace();
41+
} finally {
42+
if(socket != null) {
43+
socket.close();
44+
}
45+
}
46+
}
47+
}

src/main/java/service/DownloadFileService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @package: service
1818
* @className: DownloadFileService
1919
* @author: Dantence
20-
* @description: TODO
20+
* @description: 文件下载服务
2121
* @date: 2023/3/26 1:01
2222
* @version: 1.0
2323
*/

0 commit comments

Comments
 (0)