From fa168c66177e8f2522f3a82ed6860d5ea5b2dc46 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 29 Aug 2017 20:41:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DMiaoScriptEngine?= =?UTF-8?q?=E5=9C=A8MOD=E6=9C=8D=E4=B8=8B=E6=9C=AA=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=BC=95=E6=93=8E=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/YumCore/bukkit/Log.java | 2 +- src/main/java/pw/yumc/YumCore/bukkit/P.java | 12 +- .../yumc/YumCore/engine/MiaoScriptEngine.java | 4 +- .../pw/yumc/YumCore/update/HotSwapTask.java | 8 -- .../yumc/YumCore/update/HotSwapTaskTest.java | 36 ++++++ .../pw/yumc/YumCore/update/NIOServer.java | 115 ++++++++++++++++++ src/{main => test}/resources/hotswap.js | 0 src/test/resources/nio-client.js | 31 +++++ 8 files changed, 191 insertions(+), 17 deletions(-) create mode 100644 src/test/java/pw/yumc/YumCore/update/HotSwapTaskTest.java create mode 100644 src/test/java/pw/yumc/YumCore/update/NIOServer.java rename src/{main => test}/resources/hotswap.js (100%) create mode 100644 src/test/resources/nio-client.js diff --git a/src/main/java/pw/yumc/YumCore/bukkit/Log.java b/src/main/java/pw/yumc/YumCore/bukkit/Log.java index e5cfb60..e4ec280 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/Log.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/Log.java @@ -116,7 +116,7 @@ public class Log { */ public static void d(String msg, Object... object) { if (debug) { - logger.info(String.format(msg, object)); + logger.info(String.format("[DEBUG] " + msg, object)); } } diff --git a/src/main/java/pw/yumc/YumCore/bukkit/P.java b/src/main/java/pw/yumc/YumCore/bukkit/P.java index 7ace358..15766f0 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/P.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/P.java @@ -39,13 +39,13 @@ public class P { Field field = pluginClassLoader.getClass().getDeclaredField("plugin"); field.setAccessible(true); instance = (JavaPlugin) field.get(pluginClassLoader); + try { + getInjectConfigMethod = instance.getClass().getMethod("get" + instance.getName() + "Config"); + } catch (NoSuchMethodException e) { + Log.d("配置方法 get%sConfig 未找到 将返回getConfig 调用结果!", instance.getName()); + } } catch (Exception e) { - Log.d(e); - } - try { - getInjectConfigMethod = instance.getClass().getMethod("get" + instance.getName() + "Config"); - } catch (NoSuchMethodException e) { - Log.d("配置方法 get%sConfig 未找到 将返回getConfig 调用结果!", instance.getName()); + Log.d("P 类初始化失败 %s:%s", e.getClass().getName(), e.getMessage()); } } diff --git a/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java b/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java index edea361..c8771dc 100644 --- a/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java +++ b/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java @@ -14,7 +14,7 @@ import javax.script.SimpleBindings; /** * 喵式脚本引擎 - * + * * @author 喵♂呜 * @since 2016年8月29日 上午7:51:43 */ @@ -24,7 +24,7 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable { private ScriptEngine engine; static { - manager = new ScriptEngineManager(); + manager = new ScriptEngineManager(ClassLoader.getSystemClassLoader()); } public static void setBindings(Bindings bindings) { diff --git a/src/main/java/pw/yumc/YumCore/update/HotSwapTask.java b/src/main/java/pw/yumc/YumCore/update/HotSwapTask.java index 976c59a..82cb8f8 100644 --- a/src/main/java/pw/yumc/YumCore/update/HotSwapTask.java +++ b/src/main/java/pw/yumc/YumCore/update/HotSwapTask.java @@ -1,7 +1,6 @@ package pw.yumc.YumCore.update; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.net.URL; @@ -10,8 +9,6 @@ import java.nio.file.StandardCopyOption; import javax.script.ScriptException; -import org.junit.Test; - import pw.yumc.YumCore.bungee.Log; import pw.yumc.YumCore.engine.MiaoScriptEngine; @@ -41,9 +38,4 @@ public class HotSwapTask { Log.d("热更新脚本加载失败!", e); } } - - @Test - public void test() throws FileNotFoundException, ScriptException { - engine.eval(new FileReader(new File("src/main/resources/hotswap.js"))); - } } \ No newline at end of file diff --git a/src/test/java/pw/yumc/YumCore/update/HotSwapTaskTest.java b/src/test/java/pw/yumc/YumCore/update/HotSwapTaskTest.java new file mode 100644 index 0000000..5998850 --- /dev/null +++ b/src/test/java/pw/yumc/YumCore/update/HotSwapTaskTest.java @@ -0,0 +1,36 @@ +package pw.yumc.YumCore.update; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; + +import javax.script.ScriptException; + +import org.junit.Test; + +import pw.yumc.YumCore.engine.MiaoScriptEngine; + +/** + * Created with IntelliJ IDEA + * + * @author 喵♂呜 + * Created on 2017/8/29 20:24. + */ +public class HotSwapTaskTest { + private MiaoScriptEngine engine; + + public HotSwapTaskTest() { + this.engine = new MiaoScriptEngine(); + engine.put("$", this); + } + + @Test + public void test() throws FileNotFoundException, ScriptException { + engine.eval(new FileReader(new File("src/test/resources/hotswap.js"))); + } + + @Test + public void testClient() throws FileNotFoundException, ScriptException { + engine.eval(new FileReader(new File("src/test/resources/nio-client.js"))); + } +} \ No newline at end of file diff --git a/src/test/java/pw/yumc/YumCore/update/NIOServer.java b/src/test/java/pw/yumc/YumCore/update/NIOServer.java new file mode 100644 index 0000000..d0a41c0 --- /dev/null +++ b/src/test/java/pw/yumc/YumCore/update/NIOServer.java @@ -0,0 +1,115 @@ +package pw.yumc.YumCore.update; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.util.Iterator; + +/** + * NIO服务端 + * + * @author 小路 + */ +public class NIOServer { + //通道管理器 + private Selector selector; + + /** + * 获得一个ServerSocket通道,并对该通道做一些初始化的工作 + * + * @param port + * 绑定的端口号 + * @throws IOException + */ + public void initServer(int port) throws IOException { + // 获得一个ServerSocket通道 + ServerSocketChannel serverChannel = ServerSocketChannel.open(); + // 设置通道为非阻塞 + serverChannel.configureBlocking(false); + // 将该通道对应的ServerSocket绑定到port端口 + serverChannel.socket().bind(new InetSocketAddress(port)); + // 获得一个通道管理器 + this.selector = Selector.open(); + //将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后, + //当该事件到达时,selector.select()会返回,如果该事件没到达selector.select()会一直阻塞。 + serverChannel.register(selector, SelectionKey.OP_ACCEPT); + } + + /** + * 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理 + * + * @throws IOException + */ + @SuppressWarnings("unchecked") + public void listen() throws IOException { + System.out.println("服务端启动成功!"); + // 轮询访问selector + while (!Thread.currentThread().isInterrupted()) { + //当注册的事件到达时,方法返回;否则,该方法会一直阻塞 + selector.select(); + // 获得selector中选中的项的迭代器,选中的项为注册的事件 + Iterator ite = this.selector.selectedKeys().iterator(); + while (ite.hasNext()) { + SelectionKey key = (SelectionKey) ite.next(); + // 删除已选的key,以防重复处理 + ite.remove(); + // 客户端请求连接事件 + if (key.isAcceptable()) { + ServerSocketChannel server = (ServerSocketChannel) key + .channel(); + // 获得和客户端连接的通道 + SocketChannel channel = server.accept(); + // 设置成非阻塞 + channel.configureBlocking(false); + + //在这里可以给客户端发送信息哦 + channel.write(ByteBuffer.wrap("向客户端发送了一条信息".getBytes())); + //在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。 + channel.register(this.selector, SelectionKey.OP_READ); + + // 获得了可读的事件 + } else if (key.isReadable()) { + try { + read(key); + } catch (Exception ignored) { + } + } + } + } + } + + /** + * 处理读取客户端发来的信息 的事件 + * + * @param key + * @throws IOException + */ + public void read(SelectionKey key) throws IOException { + // 服务器可读取消息:得到事件发生的Socket通道 + SocketChannel channel = (SocketChannel) key.channel(); + // 创建读取的缓冲区 + ByteBuffer buffer = ByteBuffer.allocate(10); + channel.read(buffer); + byte[] data = buffer.array(); + String msg = new String(data).trim(); + System.out.println("服务端收到信息:" + msg); + ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes()); + channel.write(outBuffer);// 将消息回送给客户端 + } + + /** + * 启动服务端测试 + * + * @throws IOException + */ + public static void main(String[] args) throws IOException { + NIOServer server = new NIOServer(); + server.initServer(8233); + server.listen(); + } + +} diff --git a/src/main/resources/hotswap.js b/src/test/resources/hotswap.js similarity index 100% rename from src/main/resources/hotswap.js rename to src/test/resources/hotswap.js diff --git a/src/test/resources/nio-client.js b/src/test/resources/nio-client.js new file mode 100644 index 0000000..3a73162 --- /dev/null +++ b/src/test/resources/nio-client.js @@ -0,0 +1,31 @@ +var SocketChannel = java.nio.channels.SocketChannel; +var Selector = java.nio.channels.Selector; +var SelectionKey = java.nio.channels.SelectionKey; +var InetSocketAddress = java.net.InetSocketAddress; +var ByteBuffer = java.nio.ByteBuffer; +var Iterator = java.util.Iterator; + +var channel = SocketChannel.open(); +channel.configureBlocking(false); +var selector = Selector.open(); +channel.connect(new InetSocketAddress('127.0.0.1', 8233)); +channel.register(selector, SelectionKey.OP_CONNECT); + +while (true) { + selector.select(); + var ite = selector.selectedKeys().iterator(); + while (ite.hasNext()) { + var key = ite.next(); + ite.remove(); + if (key.isConnectable()) { + var client = key.channel(); + if (client.isConnectionPending()) { + client.finishConnect(); + } + client.configureBlocking(false); + client.write(ByteBuffer.wrap("向服务端发送了一条信息".getBytes())); + client.register(selector, SelectionKey.OP_READ); + } else if (key.isReadable()) { + } + } +} \ No newline at end of file