diff --git a/pom.xml b/pom.xml index 05b7c75..7f0871b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.aohe sane-service - 0.0.2 + 0.2.5 21 @@ -112,37 +112,30 @@ - org.graalvm.buildtools - native-maven-plugin - ${native.maven.plugin.version} - - false - false - ${project.artifactId} - ${start-class} - - --no-fallback - -H:-CheckToolchain - - - - - true - - - - true + io.github.fvarrui + javapackager + 1.7.5 - - - compile-no-fork - package + + package + + + + org.aohe.Main + + true + true + false + linux + + true + true + true + true + + diff --git a/src/main/java/org/aohe/Main.java b/src/main/java/org/aohe/Main.java index 4640ee2..9a23a7b 100644 --- a/src/main/java/org/aohe/Main.java +++ b/src/main/java/org/aohe/Main.java @@ -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 服务已关闭 "); })); } } \ No newline at end of file diff --git a/src/main/java/org/aohe/core/swing/LogWindow.java b/src/main/java/org/aohe/core/swing/LogWindow.java new file mode 100644 index 0000000..2421bae --- /dev/null +++ b/src/main/java/org/aohe/core/swing/LogWindow.java @@ -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"); + } + } +} + diff --git a/src/main/java/org/aohe/core/web/SocketFactory.java b/src/main/java/org/aohe/core/web/SocketFactory.java new file mode 100644 index 0000000..47087e4 --- /dev/null +++ b/src/main/java/org/aohe/core/web/SocketFactory.java @@ -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); + } + } + +}