diff --git a/example/simple_server/pom.xml b/example/simple_server/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..14de43d3b4056a6bb5a78bc114dcb4fe5c1bd3dd --- /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 0000000000000000000000000000000000000000..5d602244d6a65feb0266d2d3c237f2ea9bbc9193 --- /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 0000000000000000000000000000000000000000..e2eefd8a6603de159a4299af1a77d9a0f3f454ba --- /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 0000000000000000000000000000000000000000..96c75a512cd11d89763605510095eff29465e7ed --- /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 0000000000000000000000000000000000000000..ceb9838de795c815df33effe5af2ccf017f664f5 --- /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 ); + } +}