1.优化
This commit is contained in:
parent
9567c1defe
commit
7d2bd6175b
|
@ -7,6 +7,7 @@ import com.xiaoliu.handler.FileSendClientHandler;
|
||||||
import com.xiaoliu.handler.LoginResponseHandler;
|
import com.xiaoliu.handler.LoginResponseHandler;
|
||||||
import com.xiaoliu.protocol.FilePacket;
|
import com.xiaoliu.protocol.FilePacket;
|
||||||
import com.xiaoliu.protocol.request.LoginPacket;
|
import com.xiaoliu.protocol.request.LoginPacket;
|
||||||
|
import com.xiaoliu.window.MainWindow;
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.*;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
@ -73,9 +74,10 @@ public class Client {
|
||||||
log.info("连接服务器成功");
|
log.info("连接服务器成功");
|
||||||
Channel channel = future.channel();
|
Channel channel = future.channel();
|
||||||
joinCluster(channel);
|
joinCluster(channel);
|
||||||
|
MainWindow.setStatus(0);
|
||||||
} else {
|
} else {
|
||||||
log.info("连接服务器失败");
|
log.info("连接服务器失败");
|
||||||
|
MainWindow.setStatus(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.xiaoliu.handler;
|
||||||
import com.xiaoliu.codec.Codec;
|
import com.xiaoliu.codec.Codec;
|
||||||
import com.xiaoliu.protocol.FilePacket;
|
import com.xiaoliu.protocol.FilePacket;
|
||||||
import com.xiaoliu.protocol.Packet;
|
import com.xiaoliu.protocol.Packet;
|
||||||
|
import com.xiaoliu.window.MainWindow;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
@ -43,6 +44,7 @@ public class FileSendClientHandler extends ChannelInboundHandlerAdapter {
|
||||||
ctx.writeAndFlush(fileRegion).addListener(future -> {
|
ctx.writeAndFlush(fileRegion).addListener(future -> {
|
||||||
if (future.isSuccess()) {
|
if (future.isSuccess()) {
|
||||||
log.info("{} 发送完成...", file.getName());
|
log.info("{} 发送完成...", file.getName());
|
||||||
|
MainWindow.notify("文件发送完成:" + file.getName());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,9 @@ public class MainWindow extends JFrame {
|
||||||
|
|
||||||
// 初始化UI组件
|
// 初始化UI组件
|
||||||
initializeComponents();
|
initializeComponents();
|
||||||
|
|
||||||
|
//tuichu
|
||||||
|
hock();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initStatusLabel() {
|
private void initStatusLabel() {
|
||||||
|
@ -51,6 +54,21 @@ public class MainWindow extends JFrame {
|
||||||
statusLabel.setForeground(color);
|
statusLabel.setForeground(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setStatus(int status) {
|
||||||
|
if (statusLabel == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (status == 0) {
|
||||||
|
statusLabel.setText("已连接");
|
||||||
|
statusLabel.setForeground(Color.GREEN);
|
||||||
|
notify("已连接服务器 ...");
|
||||||
|
} else {
|
||||||
|
statusLabel.setText("未连接");
|
||||||
|
statusLabel.setForeground(Color.RED);
|
||||||
|
notify("连接失败 ...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void initializeComponents() {
|
private void initializeComponents() {
|
||||||
// 在这里添加你的组件初始化代码
|
// 在这里添加你的组件初始化代码
|
||||||
// 创建一个JTextArea用于显示日志
|
// 创建一个JTextArea用于显示日志
|
||||||
|
@ -83,8 +101,6 @@ public class MainWindow extends JFrame {
|
||||||
//新起一个县城去初始化netty
|
//新起一个县城去初始化netty
|
||||||
try {
|
try {
|
||||||
sendButton.setEnabled(false);
|
sendButton.setEnabled(false);
|
||||||
setLogText("连接成功...");
|
|
||||||
setStatusLabel("已连接", Color.GREEN);
|
|
||||||
stopButton.setEnabled(true);
|
stopButton.setEnabled(true);
|
||||||
Client.init(host, Integer.parseInt(port));
|
Client.init(host, Integer.parseInt(port));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
@ -110,7 +126,6 @@ public class MainWindow extends JFrame {
|
||||||
// 示例:添加一个按钮,点击时向文本区域添加日志信息
|
// 示例:添加一个按钮,点击时向文本区域添加日志信息
|
||||||
JButton startButton = new JButton("发送");
|
JButton startButton = new JButton("发送");
|
||||||
|
|
||||||
|
|
||||||
startButton.addActionListener(e -> {
|
startButton.addActionListener(e -> {
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
fileChooser.setCurrentDirectory(new java.io.File(System.getProperty("user.home") + "/Desktop"));
|
fileChooser.setCurrentDirectory(new java.io.File(System.getProperty("user.home") + "/Desktop"));
|
||||||
|
@ -139,6 +154,7 @@ public class MainWindow extends JFrame {
|
||||||
Client.shutdown();
|
Client.shutdown();
|
||||||
setStatusLabel("未连接", Color.RED);
|
setStatusLabel("未连接", Color.RED);
|
||||||
sendButton.setEnabled(true);
|
sendButton.setEnabled(true);
|
||||||
|
stopButton.setEnabled(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -165,6 +181,13 @@ public class MainWindow extends JFrame {
|
||||||
return logTextArea;
|
return logTextArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void notify(String text) {
|
||||||
|
if (logTextArea == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
logTextArea.append(text + "\n");
|
||||||
|
}
|
||||||
|
|
||||||
public void setLogText(String text) {
|
public void setLogText(String text) {
|
||||||
logTextArea.append(text + "\n");
|
logTextArea.append(text + "\n");
|
||||||
}
|
}
|
||||||
|
@ -183,4 +206,9 @@ public class MainWindow extends JFrame {
|
||||||
getContentPane().add(scrollPane, BorderLayout.CENTER);
|
getContentPane().add(scrollPane, BorderLayout.CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void hock() {
|
||||||
|
log.info("退出执行...");
|
||||||
|
//优雅退出
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(Client::shutdown));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class FilePacketServerHandler extends SimpleChannelInboundHandler<FilePac
|
||||||
loginPacket.setExec(true);
|
loginPacket.setExec(true);
|
||||||
loginPacket.setFileName(file.getName());
|
loginPacket.setFileName(file.getName());
|
||||||
loginPacket.setFileLength(packet.getFileLength());
|
loginPacket.setFileLength(packet.getFileLength());
|
||||||
loginPacket.setFileOutputStream(new FileOutputStream(new File("c:/temp/server-receive-" + file.getName())));
|
loginPacket.setFileOutputStream(new FileOutputStream(new File("./service-receive-" + file.getName())));
|
||||||
// FileReceiveServerHandler.fileLength = file.length();
|
// FileReceiveServerHandler.fileLength = file.length();
|
||||||
// FileReceiveServerHandler.outputStream = new FileOutputStream(
|
// FileReceiveServerHandler.outputStream = new FileOutputStream(
|
||||||
// new File("./server-receive-" + file.getName())
|
// new File("./server-receive-" + file.getName())
|
||||||
|
|
|
@ -43,14 +43,16 @@ public class FileReceiveServerHandler extends ChannelInboundHandlerAdapter {
|
||||||
byteBuf.release();
|
byteBuf.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendComplete(long readLength) throws IOException {
|
// private void sendComplete(long readLength) throws IOException {
|
||||||
if (readLength >= fileLength) {
|
// if (readLength >= fileLength) {
|
||||||
log.info("文件接收完成.....");
|
// log.info("文件接收完成.....");
|
||||||
outputStream.close();
|
// outputStream.close();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
private void sendComplete(LoginPacket loginPacket) throws IOException {
|
private void sendComplete(LoginPacket loginPacket) throws IOException {
|
||||||
|
log.debug("ReadLength {}", loginPacket.getReadLength());
|
||||||
|
log.debug("FileLength {}", loginPacket.getFileLength());
|
||||||
if (loginPacket.getReadLength() >= loginPacket.getFileLength()) {
|
if (loginPacket.getReadLength() >= loginPacket.getFileLength()) {
|
||||||
log.info("文件接收完成...");
|
log.info("文件接收完成...");
|
||||||
if(loginPacket.getFileOutputStream() != null){
|
if(loginPacket.getFileOutputStream() != null){
|
||||||
|
|
Loading…
Reference in New Issue