parent
c35289b93c
commit
7653f15af1
51
pom.xml
51
pom.xml
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>org.aohe</groupId>
|
||||
<artifactId>sane-service</artifactId>
|
||||
<version>0.0.2</version>
|
||||
<version>0.2.5</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
|
@ -112,37 +112,30 @@
|
|||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.graalvm.buildtools</groupId>
|
||||
<artifactId>native-maven-plugin</artifactId>
|
||||
<version>${native.maven.plugin.version}</version>
|
||||
<configuration>
|
||||
<skip>false</skip>
|
||||
<useArgFile>false</useArgFile>
|
||||
<imageName>${project.artifactId}</imageName>
|
||||
<mainClass>${start-class}</mainClass>
|
||||
<buildArgs>
|
||||
--no-fallback
|
||||
-H:-CheckToolchain
|
||||
</buildArgs>
|
||||
<!-- GraalVM 可达性元数据支持 -->
|
||||
<!-- https://github.com/oracle/graalvm-reachability-metadata -->
|
||||
<metadataRepository>
|
||||
<enabled>true</enabled>
|
||||
</metadataRepository>
|
||||
<!--
|
||||
<agent>
|
||||
<enabled>true</enabled>
|
||||
</agent>
|
||||
-->
|
||||
</configuration>
|
||||
<extensions>true</extensions>
|
||||
<groupId>io.github.fvarrui</groupId>
|
||||
<artifactId>javapackager</artifactId>
|
||||
<version>1.7.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<!-- [WARNING] 'native:build' goal is deprecated. Use 'native:compile-no-fork' instead. -->
|
||||
<goal>compile-no-fork</goal>
|
||||
</goals>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>package</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<!-- mandatory -->
|
||||
<mainClass>org.aohe.Main</mainClass>
|
||||
<!-- optional -->
|
||||
<bundleJre>true</bundleJre>
|
||||
<generateInstaller>true</generateInstaller>
|
||||
<administratorRequired>false</administratorRequired>
|
||||
<platform>linux</platform>
|
||||
<linuxConfig>
|
||||
<generateAppImage>true</generateAppImage>
|
||||
<generateDeb>true</generateDeb>
|
||||
<generateRpm>true</generateRpm>
|
||||
<wrapJar>true</wrapJar>
|
||||
</linuxConfig>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
|
|
@ -4,73 +4,48 @@ import java.io.IOException;
|
|||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aohe.core.sane.utils.SaneSessionUtils;
|
||||
import org.aohe.core.swing.LogWindow;
|
||||
import org.aohe.core.utils.CommandUtils;
|
||||
import org.aohe.core.utils.SystemUtils;
|
||||
import org.aohe.core.web.SocketServer;
|
||||
import org.aohe.core.web.SocketFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.UnknownHostException;
|
||||
import javax.swing.*;
|
||||
|
||||
@Slf4j
|
||||
public class Main {
|
||||
|
||||
private static SocketServer socketServer;
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
log.info(" 当前系统类型: {}", SystemUtils.getOsName());
|
||||
//初始化Sane Socket 守护进程
|
||||
init();
|
||||
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
String in = sysin.readLine();
|
||||
socketServer.broadcast(in);
|
||||
if (in.equals("exit")) {
|
||||
socketServer.stop(1000);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
//设置窗口主题
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
|
||||
UnsupportedLookAndFeelException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
//初始化其他资源
|
||||
init();
|
||||
SwingUtilities.invokeLater(() -> new LogWindow().setVisible(true));
|
||||
|
||||
}
|
||||
|
||||
public static void init(){
|
||||
log.info("程序初始化开始...");
|
||||
CommandUtils.initSanedSocket();
|
||||
log.info("Saned Socket 检查完成...");
|
||||
initSocket();
|
||||
log.info("Socket 服务启动成功...");
|
||||
stopGracefully();
|
||||
log.info("Hook注册成功...");
|
||||
}
|
||||
|
||||
public static void initSocket() {
|
||||
int port = 8997; // 8997 is must port
|
||||
|
||||
try {
|
||||
socketServer = new SocketServer(port);
|
||||
} catch (UnknownHostException e) {
|
||||
log.error("socket 服务启动失败, 服务关闭", e);
|
||||
System.exit(0);
|
||||
}
|
||||
socketServer.start();
|
||||
log.info("scan-service started on port: {}", socketServer.getPort());
|
||||
}
|
||||
|
||||
public static void stopGracefully() {
|
||||
log.info("优雅退出Hook注册");
|
||||
//优雅退出,在程序停止时,销毁关键资源占用
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
try {
|
||||
log.info("开始资源释放");
|
||||
//释放sane资源
|
||||
SaneSessionUtils.resource();
|
||||
if (socketServer != null) {
|
||||
//关闭socket绑定程序
|
||||
socketServer.stop(1000);
|
||||
log.info("socket 服务已关闭 ");
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
log.info("开始资源释放");
|
||||
//释放sane资源
|
||||
SaneSessionUtils.resource();
|
||||
SocketFactory.stop();
|
||||
log.info("socket 服务已关闭 ");
|
||||
}));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
package org.aohe.core.swing;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aohe.core.web.SocketFactory;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@Slf4j
|
||||
public class LogWindow extends JFrame {
|
||||
|
||||
private static boolean serviceStatus = false;
|
||||
|
||||
private static JTextArea logTextArea;
|
||||
|
||||
public LogWindow() {
|
||||
// 设置窗口标题
|
||||
setTitle("扫描仪控制器");
|
||||
|
||||
// 设置默认的关闭操作
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
// 设置窗口大小
|
||||
setSize(400, 300);
|
||||
|
||||
// 居中显示窗口
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
// 初始化UI组件
|
||||
initializeComponents();
|
||||
}
|
||||
|
||||
private void initializeComponents() {
|
||||
// 在这里添加你的组件初始化代码
|
||||
// 创建一个JTextArea用于显示日志
|
||||
logTextArea = new JTextArea();
|
||||
logTextArea.setEditable(false); // 设置为不可编辑
|
||||
|
||||
// 将JTextArea放入JScrollPane中以支持滚动
|
||||
JScrollPane scrollPane = new JScrollPane(logTextArea);
|
||||
|
||||
// 将JScrollPane添加到窗口中
|
||||
getContentPane().add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
JLabel textField = new JLabel();
|
||||
textField.setText("服务状态:" + getServiceStatus());
|
||||
|
||||
JPanel bottomPanel = new JPanel();
|
||||
|
||||
// 示例:添加一个按钮,点击时向文本区域添加日志信息
|
||||
JButton startButton = new JButton("开始");
|
||||
JButton stopButton = new JButton("停止");
|
||||
stopButton.setEnabled(false);
|
||||
startButton.addActionListener(e -> {
|
||||
startButton.setEnabled(false);
|
||||
stopButton.setEnabled(true);
|
||||
initSocket();
|
||||
setView("服务已打开...");
|
||||
serviceStatus = true;
|
||||
textField.setText("服务状态:" + getServiceStatus());
|
||||
});
|
||||
stopButton.addActionListener(e -> {
|
||||
stopButton.setEnabled(false);
|
||||
logTextArea.append("关闭服务中...\n");
|
||||
new Thread(() -> {
|
||||
//开个新线程关闭,防止主UI线程卡死
|
||||
stopSocket();
|
||||
serviceStatus = false;
|
||||
startButton.setEnabled(true);
|
||||
textField.setText("服务状态:" + getServiceStatus());
|
||||
}).start();
|
||||
|
||||
});
|
||||
|
||||
bottomPanel.add(startButton, BorderLayout.CENTER);
|
||||
bottomPanel.add(stopButton, BorderLayout.CENTER);
|
||||
getContentPane().add(textField, BorderLayout.NORTH);
|
||||
|
||||
getContentPane().add(bottomPanel, BorderLayout.SOUTH);
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
log.info("服务启动中...");
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (UnsupportedLookAndFeelException e) {
|
||||
System.err.println("UnsupportedLookAndFeelException: " + e.getMessage());
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
||||
log.error("出错了 " , e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
SwingUtilities.invokeLater(() -> new LogWindow().setVisible(true));
|
||||
}
|
||||
|
||||
public static String getServiceStatus() {
|
||||
if (serviceStatus){
|
||||
return "已启动";
|
||||
}
|
||||
return "未启动";
|
||||
}
|
||||
|
||||
public static void initSocket() {
|
||||
SocketFactory.start();
|
||||
}
|
||||
|
||||
public void stopSocket() {
|
||||
try {
|
||||
SocketFactory.stop();
|
||||
Thread.sleep(1000);
|
||||
setView("服务关闭");
|
||||
log.info("服务关闭");
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setView(String msg){
|
||||
if(logTextArea != null){
|
||||
logTextArea.append(msg +"\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package org.aohe.core.web;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* Socket 工厂
|
||||
*/
|
||||
@Slf4j
|
||||
public class SocketFactory {
|
||||
|
||||
/**
|
||||
* -- GETTER --
|
||||
* get 方法
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Getter
|
||||
private static SocketServer socketServer;
|
||||
|
||||
private static SocketFactory instance ;
|
||||
|
||||
private SocketFactory() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Socket客户端
|
||||
* @return socketServer
|
||||
*/
|
||||
private static SocketServer getSocket(){
|
||||
try {
|
||||
log.info("Create Socket ...");
|
||||
socketServer = new SocketServer(8997);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return socketServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动socket服务
|
||||
*/
|
||||
public static void start(){
|
||||
getSocket().start();
|
||||
log.info("socket 已启动");
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止socket服务
|
||||
*/
|
||||
public static void stop(){
|
||||
try {
|
||||
SocketServer server = getSocketServer();
|
||||
if(server != null){
|
||||
server.stop();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue