Skip to content

Commit

Permalink
美化代码, 补充ReadMe
Browse files Browse the repository at this point in the history
  • Loading branch information
seaswalker committed Feb 17, 2017
1 parent 87f0564 commit 2ab654e
Show file tree
Hide file tree
Showing 45 changed files with 1,450 additions and 1,404 deletions.
27 changes: 0 additions & 27 deletions .classpath

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*.class
/target/
/.idea/
/MiniNetty.iml

# Mobile Tools for Java (J2ME)
.mtj.tmp/
Expand Down
23 changes: 0 additions & 23 deletions .project

This file was deleted.

4 changes: 0 additions & 4 deletions .settings/org.eclipse.core.resources.prefs

This file was deleted.

2 changes: 0 additions & 2 deletions .settings/org.eclipse.jdt.core.prefs

This file was deleted.

4 changes: 0 additions & 4 deletions .settings/org.eclipse.m2e.core.prefs

This file was deleted.

64 changes: 60 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# MiniNetty

## 实现的功能
# 实现的功能

- channelActive/channelInActive/channelRead/channelWrite事件处理

Expand All @@ -16,7 +14,65 @@

- LineBasedDecoder

## 线程模型
# 线程模型

![ThreadMode](images/thread_mode.jpg)

# 示例

## 服务器启动

以定长解码器为例:

```java
@Test
public void lengthFieldBasedDecoder() {
Server server = new Server();
server.bind(8080).setHandlers(new HandlerInitializer() {
@Override
public Handler[] init() {
return new Handler[] {new LengthFieldBasedDecoder(0, 4),
new StringDecoder(), new SimpleInBoundHandler()};
}
}).start();
}
```

## SimpleInBoundHandler

简单地打印出事件触发以及收到的消息:

```java
public class SimpleInBoundHandler extends InBoundHandlerAdapter {
@Override
public void channelActive(HandlerContext context) {
System.out.println("channel active");
}
@Override
public void channelInActive(HandlerContext context) {
System.out.println("channel inActive");
}
@Override
public void channelRead(Object message, HandlerContext context) {
System.out.println(message.toString());
}
}
```

## 客户端

数据发送代码:

```java
@Test
public void lengthFieldBasedDecoder() throws IOException, InterruptedException {
byte[] result = new byte[35];
System.arraycopy(DataUtils.int2Bytes(31), 0, result, 0, 4);
System.arraycopy("org.apache.commons.lang.builder".getBytes(), 0, result, 4, 31);
for (int i = 0; i < 6; i++) {
bos.write(result);
}
TimeUnit.SECONDS.sleep(6);
}
```

72 changes: 41 additions & 31 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>skywalker</groupId>
<artifactId>MiniNetty</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<groupId>skywalker</groupId>
<artifactId>MiniNetty</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>MiniNetty</name>
<url>http://maven.apache.org</url>
<name>MiniNetty</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.4.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit 2ab654e

Please sign in to comment.