Skip to content

Commit

Permalink
家里的电脑提交的请求
Browse files Browse the repository at this point in the history
  • Loading branch information
20162430413 committed Oct 22, 2019
1 parent fddcbbc commit a5ccef8
Show file tree
Hide file tree
Showing 127 changed files with 4,895 additions and 13 deletions.
1 change: 1 addition & 0 deletions EchoClient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>run-server</id>
Expand Down
4 changes: 3 additions & 1 deletion EchoServer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.jar.plugin-version>3.1.1</maven.jar.plugin-version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>run-server</id>
Expand All @@ -29,7 +31,7 @@
</execution>
</executions>
<configuration>
<mainClass>com.plort.polarlights.raincloud.EchoServer</mainClass>
<mainClass>com.plort.polarlights.raincloud.ServerStart</mainClass>
<!-- <arguments>
<argument>${echo-server.port}</argument>
</arguments> -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class EchoServer {
public class EchoServer implements Server{

private final int port;
private int port = 5555;

public EchoServer() {

}

public EchoServer(int port) {
this.port = port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
Expand All @@ -19,13 +20,28 @@ public class EchoServerHandler extends ChannelInboundHandlerAdapter{

public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
System.out.println("Server received" + in.toString(CharsetUtil.UTF_8));
System.out.println("Server received" + in.toString(CharsetUtil.UTF_8));
ctx.write(in);
}

public void channelReadComplete(ChannelHandlerContext ctx) {
ChannelFuture writeAndFlush = ctx.writeAndFlush(Unpooled.EMPTY_BUFFER); //继续写入
writeAndFlush.addListener(ChannelFutureListener.CLOSE);
Channel channel = ctx.channel(); //可以使用ctx里的channel写,同样的效果
// ChannelFuture writeAndFlush = ctx.writeAndFlush(Unpooled.EMPTY_BUFFER); //继续写入
// ChannelFuture writeAndFlush = channel.writeAndFlush(Unpooled.copiedBuffer("write end",CharsetUtil.UTF_8)); //继续写入
ChannelFuture writeAndFlush = channel.writeAndFlush(Unpooled.EMPTY_BUFFER); //继续写入
// writeAndFlush.addListener(ChannelFutureListener.CLOSE);
writeAndFlush.addListener(new ChannelFutureListener() {

public void operationComplete(ChannelFuture future) throws Exception {
if(future.isSuccess()) {
System.out.println("write successfully!");
}else {
System.out.println("write failed!!");
future.cause().printStackTrace();
}
}

});
}

public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.oio.OioServerSocketChannel;

public class NettyOioServer {
public class NettyOioServer implements Server{

int port = 5556;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.plort.polarlights.raincloud;

public interface Server {

void start() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,32 @@ public class ServerStart {

public static void main(String[] args) {
try {

// 开始EchoServer
// new EchoServer(5555).start();
new ServerStart().start(EchoServer.class);

//开始一个OioServer
new NettyOioServer(5556).start();
// new ServerStart().start(NettyOioServer.class);
}catch(Exception e) {
e.printStackTrace();
System.out.println("main exception.");
}
}

public void start(Class<? extends Server> serverClass) {
try {
serverClass.newInstance().start();
}catch(Exception e) {
e.printStackTrace();
}
}

public void start(Class<? extends Server> serverClass,int port) {

try {
serverClass.newInstance().start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@

<build>
<plugins>
<plugin>
<!-- <plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
</plugin> -->
<!-- <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
</plugin> -->
</plugins>
</build>
</project>
9 changes: 9 additions & 0 deletions 相关资料/netty-in-action-2.0-SNAPSHOT/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
target
bin
.settings
.project
.classpath
.idea
.DS_Store
*.iml
/.idea
26 changes: 26 additions & 0 deletions 相关资料/netty-in-action-2.0-SNAPSHOT/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
This Repository contains the source-code for all chapters of the book [Netty in Action](http://manning.com/maurer)
by Norman Maurer and Marvin Allen Wolfthal.

Latest version: https://github.com/normanmaurer/netty-in-action/tree/2.0-SNAPSHOT

Enjoy! Feedback and PR's welcome!


Prerequisites

JDK 1.7.0u71 or better

Maven 3.3.9 or better


If you want to build everything at once, from the top directory run

mvn install


If you want to build only single projects then from the top directory first run

mvn install -pl utils


This will make the utils jar available to all the projects.
21 changes: 21 additions & 0 deletions 相关资料/netty-in-action-2.0-SNAPSHOT/chapter1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<parent>
<groupId>nia</groupId>
<artifactId>nia-samples-parent</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>

<artifactId>chapter1</artifactId>
<version>2.0-SNAPSHOT</version>

<packaging>jar</packaging>
<name>Chapter 1. Netty—asynchronous and event-driven</name>
<description>
Build an Echo Server and Client
</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package nia.chapter1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
* Created by kerr.
*
* Listing 1.1 Blocking I/O example
*/
public class BlockingIoExample {

/**
* Listing 1.1 Blocking I/O example
* */
public void serve(int portNumber) throws IOException {
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
String request, response;
while ((request = in.readLine()) != null) {
if ("Done".equals(request)) {
break;
}
response = processRequest(request);
out.println(response);
}
}

private String processRequest(String request){
return "Processed";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package nia.chapter1;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
* Created by kerr.
*
* Listing 1.3 Asynchronous connect
*
* Listing 1.4 Callback in action
*/
public class ConnectExample {
private static final Channel CHANNEL_FROM_SOMEWHERE = new NioSocketChannel();

/**
* Listing 1.3 Asynchronous connect
*
* Listing 1.4 Callback in action
* */
public static void connect() {
Channel channel = CHANNEL_FROM_SOMEWHERE; //reference form somewhere
// Does not block
ChannelFuture future = channel.connect(
new InetSocketAddress("192.168.0.1", 25));
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
ByteBuf buffer = Unpooled.copiedBuffer(
"Hello", Charset.defaultCharset());
ChannelFuture wf = future.channel()
.writeAndFlush(buffer);
// ...
} else {
Throwable cause = future.cause();
cause.printStackTrace();
}
}
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package nia.chapter1;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
* Created by kerr.
*
* Listing 1.2 ChannelHandler triggered by a callback
*/
public class ConnectHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
System.out.println(
"Client " + ctx.channel().remoteAddress() + " connected");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* kerr.
*
* Listing 1.1 Blocking I/O example {@link nia.chapter1.BlockingIoExample#serve(int)}
*
* Listing 1.2 ChannelHandler triggered by a callback {@link nia.chapter1.ConnectHandler}
*
* Listing 1.3 Asynchronous connect {@link nia.chapter1.ConnectExample#connect()}
*
* Listing 1.4 Callback in action {@link nia.chapter1.ConnectExample#connect()}
*/
package nia.chapter1;
15 changes: 15 additions & 0 deletions 相关资料/netty-in-action-2.0-SNAPSHOT/chapter10/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="ISO-8859-15"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nia</groupId>
<artifactId>nia-samples-parent</artifactId>
<version>2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>chapter10</artifactId>
<name>Chapter 10: The codec framework</name>
<description>An overview of decoders, encoders and codecs</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nia.chapter10;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
* Listing 10.8 Class ByteToCharDecoder
*
* @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
*/
public class ByteToCharDecoder extends ByteToMessageDecoder {
@Override
public void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception {
if (in.readableBytes() >= 2) {
out.add(in.readChar());
}
}
}

Loading

0 comments on commit a5ccef8

Please sign in to comment.