From 6e06487042bd087ca4ea35b6126eaf4b7063c7a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E4=B8=80=E5=B9=B3?= Date: Tue, 15 Jan 2019 14:09:02 +0800 Subject: [PATCH] =?UTF-8?q?!6=20simple=20server=20demo=20Merge=20pull=20re?= =?UTF-8?q?quest=20!6=20from=20=E9=AB=98=E4=B8=80=E5=B9=B3/v1.3.23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/simple_server/pom.xml | 35 ++++++++ .../src/main/java/com/gaoyiping/demo/App.java | 15 ++++ .../java/com/gaoyiping/demo/DemoProtocol.java | 29 +++++++ .../java/com/gaoyiping/demo/DemoService.java | 86 +++++++++++++++++++ .../test/java/com/gaoyiping/demo/AppTest.java | 38 ++++++++ 5 files changed, 203 insertions(+) create mode 100644 example/simple_server/pom.xml create mode 100644 example/simple_server/src/main/java/com/gaoyiping/demo/App.java create mode 100644 example/simple_server/src/main/java/com/gaoyiping/demo/DemoProtocol.java create mode 100644 example/simple_server/src/main/java/com/gaoyiping/demo/DemoService.java create mode 100644 example/simple_server/src/test/java/com/gaoyiping/demo/AppTest.java diff --git a/example/simple_server/pom.xml b/example/simple_server/pom.xml new file mode 100644 index 00000000..14de43d3 --- /dev/null +++ b/example/simple_server/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + + com.gaoyiping + demo + 0.0.1-SNAPSHOT + jar + + demo + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + org.smartboot.socket + aio-core + 1.3.23 + + + com.alibaba + fastjson + 1.2.54 + + + diff --git a/example/simple_server/src/main/java/com/gaoyiping/demo/App.java b/example/simple_server/src/main/java/com/gaoyiping/demo/App.java new file mode 100644 index 00000000..5d602244 --- /dev/null +++ b/example/simple_server/src/main/java/com/gaoyiping/demo/App.java @@ -0,0 +1,15 @@ +package com.gaoyiping.demo; + +import java.io.IOException; + +import org.smartboot.socket.transport.AioQuickServer; + +public class App { + public static void main( String[] args ) throws IOException { + int socketPort = 10086; + AioQuickServer server = new AioQuickServer(socketPort, new DemoProtocol(), new DemoService()); + server.setBannerEnabled(false); // 关掉万恶的宣传广告 (笑~~) + //server.setThreadNum(100); + server.start(); + } +} diff --git a/example/simple_server/src/main/java/com/gaoyiping/demo/DemoProtocol.java b/example/simple_server/src/main/java/com/gaoyiping/demo/DemoProtocol.java new file mode 100644 index 00000000..e2eefd8a --- /dev/null +++ b/example/simple_server/src/main/java/com/gaoyiping/demo/DemoProtocol.java @@ -0,0 +1,29 @@ +package com.gaoyiping.demo; + +import java.nio.ByteBuffer; + +import org.smartboot.socket.Protocol; +import org.smartboot.socket.transport.AioSession; + +public class DemoProtocol implements Protocol { + + public byte[] decode(ByteBuffer readBuffer, AioSession session) { + if (readBuffer.remaining() > 0) { + byte[] data = new byte[readBuffer.remaining()]; + readBuffer.get(data); + return data; + // type 1,2,3 message, see: + // https://smartboot.gitee.io/docs/smart-socket/second/3-type-one.html + // https://smartboot.gitee.io/docs/smart-socket/second/4-type-two.html + // https://smartboot.gitee.io/docs/smart-socket/second/5-type-three.html + } + return null; + } + + public ByteBuffer encode(byte[] msg, AioSession session) { + ByteBuffer buffer = ByteBuffer.allocate(msg.length); + buffer.put(msg); + buffer.flip(); + return buffer; + } +} diff --git a/example/simple_server/src/main/java/com/gaoyiping/demo/DemoService.java b/example/simple_server/src/main/java/com/gaoyiping/demo/DemoService.java new file mode 100644 index 00000000..96c75a51 --- /dev/null +++ b/example/simple_server/src/main/java/com/gaoyiping/demo/DemoService.java @@ -0,0 +1,86 @@ +package com.gaoyiping.demo; + +import java.io.IOException; +import java.util.HashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.smartboot.socket.MessageProcessor; +import org.smartboot.socket.StateMachineEnum; +import org.smartboot.socket.transport.AioSession; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + +public class DemoService implements MessageProcessor, Runnable { + private HashMap> clients = new HashMap>(); + private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(12); + + public DemoService() { + executorService.scheduleAtFixedRate(this, 2, 2, TimeUnit.SECONDS); + } + + public void run() { + // send data every 2 second... + if (this.clients.isEmpty()) return; + for (AioSession session: this.clients.values()) { + try { + session.write("Hey! Smart-Socket it's work...".getBytes()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public void process(AioSession session, byte[] msg) { + JSONObject jsonObject = JSON.parseObject(msg, JSONObject.class); + System.out.println(jsonObject.getString("content")); + // SomeCode... + try { + // Response + session.write("{\"result\": \"OK\"}".getBytes()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) { + // when connection state changed. + switch (stateMachineEnum) { + case NEW_SESSION: + System.out.println("StateMachineEnum.NEW_SESSION"); + break; + case INPUT_SHUTDOWN: + System.out.println("StateMachineEnum.INPUT_SHUTDOWN"); + break; + case PROCESS_EXCEPTION: + System.out.println("StateMachineEnum.PROCESS_EXCEPTION"); + break; + case DECODE_EXCEPTION: + System.out.println("StateMachineEnum.DECODE_EXCEPTION"); + break; + case INPUT_EXCEPTION: + System.out.println("StateMachineEnum.INPUT_EXCEPTION"); + break; + case OUTPUT_EXCEPTION: + System.out.println("StateMachineEnum.OUTPUT_EXCEPTION"); + break; + case SESSION_CLOSING: + System.out.println("StateMachineEnum.SESSION_CLOSING"); + break; + case SESSION_CLOSED: + System.out.println("StateMachineEnum.SESSION_CLOSED"); + break; + case FLOW_LIMIT: + System.out.println("StateMachineEnum.FLOW_LIMIT"); + break; + case RELEASE_FLOW_LIMIT: + System.out.println("StateMachineEnum.RELEASE_FLOW_LIMIT"); + break; + default: + System.out.println("StateMachineEnum.default"); + } + } + +} diff --git a/example/simple_server/src/test/java/com/gaoyiping/demo/AppTest.java b/example/simple_server/src/test/java/com/gaoyiping/demo/AppTest.java new file mode 100644 index 00000000..ceb9838d --- /dev/null +++ b/example/simple_server/src/test/java/com/gaoyiping/demo/AppTest.java @@ -0,0 +1,38 @@ +package com.gaoyiping.demo; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} -- Gitee