Skip to content

Commit

Permalink
NettyRPC 2.2 server's interface ability show.
Browse files Browse the repository at this point in the history
NettyRPC 2.2 server's interface ability show.
  • Loading branch information
tang-jie committed May 2, 2017
1 parent 39e1351 commit 3d93f9d
Show file tree
Hide file tree
Showing 15 changed files with 323 additions and 25 deletions.
Binary file added docs/echo-api-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/echo-api-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
* @since 2017/4/7
*/
public interface AbilityDetail {
StringBuilder listAbilityDetail();
StringBuilder listAbilityDetail(boolean html);
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,73 @@
* @since 2017/4/7
*/
public class AbilityDetailProvider implements AbilityDetail {
private final static String STYLE = "<style type=\"text/css\">\n" +
"table.gridtable {\n" +
" font-family: verdana,arial,sans-serif;\n" +
" font-size:11px;\n" +
" color:#333333;\n" +
" border-width: 1px;\n" +
" border-color: #666666;\n" +
" border-collapse: collapse;\n" +
"}\n" +
"table.gridtable th {\n" +
" border-width: 1px;\n" +
" padding: 8px;\n" +
" border-style: solid;\n" +
" border-color: #666666;\n" +
" background-color: #dedede;\n" +
"}\n" +
"table.gridtable td {\n" +
" border-width: 1px;\n" +
" padding: 8px;\n" +
" border-style: solid;\n" +
" border-color: #666666;\n" +
" background-color: #ffffff;\n" +
"}\n" +
"</style>";

private final static String HEADER = "<table class=\"gridtable\">\n" +
"<tr>\n" +
" <th>NettyRPC Ability Detail</th>\n" +
"</tr>";

private final static String TAIL = "</table>";
private final static String CELL_BEGIN = "<tr><td>";
private final static String CELL_END = "</td></tr>";

@Override
public StringBuilder listAbilityDetail() {
public StringBuilder listAbilityDetail(boolean html) {
Map<String, Object> map = MessageRecvExecutor.getInstance().getHandlerMap();

ReflectionUtils utils = new ReflectionUtils();

if (html) {
utils.getProvider().append(STYLE).append(HEADER);
}

Set<String> s = (Set<String>) map.keySet();
Iterator<String> iter = s.iterator();
String key;
while (iter.hasNext()) {
key = iter.next();
try {
utils.listRpcProviderDetail(Class.forName(key));
if (html) {
utils.getProvider().append(CELL_BEGIN);
utils.listRpcProviderDetail(Class.forName(key), html);
utils.getProvider().append(CELL_END);
} else {
utils.listRpcProviderDetail(Class.forName(key), html);
}

} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

if (html) {
utils.getProvider().append(TAIL);
}

return utils.getProvider();
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/newlandframework/rpc/core/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ private void listTypes(Class<?>[] types) {
}
}

private void listField(Field f) {
provider.append(" " + modifiers(f.getModifiers()) +
private void listField(Field f, boolean html) {
provider.append((html ? "&nbsp&nbsp" : " ") + modifiers(f.getModifiers()) +
getType(f.getType()) + " " +
f.getName() + ";");
f.getName() + (html ? ";<br>" : ";\n"));
}

private void listMethod(Executable member) {
provider.append("\n " + modifiers(member.getModifiers()));
private void listMethod(Executable member, boolean html) {
provider.append(html ? "<br>&nbsp&nbsp" : "\n " + modifiers(member.getModifiers()));
if (member instanceof Method) {
provider.append(getType(((Method) member).getReturnType()) + " ");
}
Expand All @@ -164,29 +164,29 @@ private void listMethod(Executable member) {
provider.append(";");
}

public void listRpcProviderDetail(Class<?> c) {
public void listRpcProviderDetail(Class<?> c, boolean html) {
if (!c.isInterface()) {
return;
} else {
provider.append(Modifier.toString(c.getModifiers()) + " " + c.getName());
provider.append(" {\n");
provider.append(html ? " {<br>" : " {\n");

boolean hasFields = false;
Field[] fields = c.getDeclaredFields();
if (fields.length != 0) {
provider.append(" // Fields\n");
provider.append(html ? "&nbsp&nbsp//&nbspFields<br>" : " // Fields\n");
hasFields = true;
for (Field field : fields) {
listField(field);
listField(field, html);
}
}

provider.append(hasFields ? "\n // Methods" : " // Methods");
provider.append(hasFields ? (html ? "<br>&nbsp&nbsp//&nbspMethods" : "\n // Methods") : (html ? "&nbsp&nbsp//&nbspMethods" : " // Methods"));
Method[] methods = c.getDeclaredMethods();
for (Method method : methods) {
listMethod(method);
listMethod(method, html);
}
provider.append("\n}\n");
provider.append(html ? "<br>}<p>" : "\n}\n\n");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorCompletionService;
import java.util.logging.Level;

import com.newlandframework.rpc.core.RpcSystemConfig;
Expand All @@ -49,6 +52,7 @@
import com.newlandframework.rpc.serialize.RpcSerializeProtocol;
import com.newlandframework.rpc.compiler.AccessAdaptiveProvider;
import com.newlandframework.rpc.core.AbilityDetailProvider;
import com.newlandframework.rpc.netty.resolver.ApiEchoResolver;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
Expand All @@ -64,13 +68,15 @@
public class MessageRecvExecutor implements ApplicationContextAware {

private String serverAddress;
private int echoApiPort;
private RpcSerializeProtocol serializeProtocol = RpcSerializeProtocol.JDKSERIALIZE;
private static final String DELIMITER = RpcSystemConfig.DELIMITER;
private int parallel = RpcSystemConfig.PARALLEL * 2;
private static int threadNums = RpcSystemConfig.SYSTEM_PROPERTY_THREADPOOL_THREAD_NUMS;
private static int queueNums = RpcSystemConfig.SYSTEM_PROPERTY_THREADPOOL_QUEUE_NUMS;
private static volatile ListeningExecutorService threadPoolExecutor;
private Map<String, Object> handlerMap = new ConcurrentHashMap<String, Object>();
private int numberOfEchoThreadsPool = 1;

ThreadFactory threadRpcFactory = new NamedThreadFactory("NettyRPC ThreadFactory");
EventLoopGroup boss = new NioEventLoopGroup();
Expand Down Expand Up @@ -147,8 +153,24 @@ public void start() {
int port = Integer.parseInt(ipAddr[1]);
ChannelFuture future = null;
future = bootstrap.bind(host, port).sync();
System.out.printf("[author tangjie] Netty RPC Server start success!\nip:%s\nport:%d\nprotocol:%s\n\n", host, port, serializeProtocol);
future.channel().closeFuture().sync();

future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture channelFuture) throws Exception {
if (channelFuture.isSuccess()) {
ExecutorService executor = Executors.newFixedThreadPool(numberOfEchoThreadsPool);
ExecutorCompletionService<Boolean> completionService = new ExecutorCompletionService<Boolean>(executor);
completionService.submit(new ApiEchoResolver(host, echoApiPort));
System.out.printf("[author tangjie] Netty RPC Server start success!\nip:%s\nport:%d\nprotocol:%s\n\n", host, port, serializeProtocol);
channelFuture.channel().closeFuture().sync().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
executor.shutdownNow();
}
});
}
}
});
} else {
System.out.printf("[author tangjie] Netty RPC Server start fail!\n");
}
Expand Down Expand Up @@ -190,4 +212,12 @@ public RpcSerializeProtocol getSerializeProtocol() {
public void setSerializeProtocol(RpcSerializeProtocol serializeProtocol) {
this.serializeProtocol = serializeProtocol;
}

public int getEchoApiPort() {
return echoApiPort;
}

public void setEchoApiPort(int echoApiPort) {
this.echoApiPort = echoApiPort;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (C) 2017 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.netty.resolver;

import com.newlandframework.rpc.core.AbilityDetailProvider;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest;

import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:ApiEchoHandler.java
* @description:ApiEchoHandler功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2017/5/2
*/
public class ApiEchoHandler extends ChannelInboundHandlerAdapter {
private static final String CONTENT_TYPE = "Content-Type";
private static final String CONTENT_LENGTH = "Content-Length";
private static final String CONNECTION = "Connection";
private static final String KEEP_ALIVE = "keep-alive";

public ApiEchoHandler() {
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
HttpRequest req = (HttpRequest) msg;
AbilityDetailProvider provider = new AbilityDetailProvider();
byte[] content = provider.listAbilityDetail(true).toString().getBytes();
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(content));
response.headers().set(CONTENT_TYPE, "text/html");
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
response.headers().set(CONNECTION, KEEP_ALIVE);
ctx.write(response);
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2017 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.netty.resolver;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslContext;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:ApiEchoInitializer.java
* @description:ApiEchoInitializer功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2017/5/2
*/
public class ApiEchoInitializer extends ChannelInitializer<SocketChannel> {

private final SslContext sslCtx;

public ApiEchoInitializer(SslContext sslCtx) {
this.sslCtx = sslCtx;
}

@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast(new HttpServerCodec());
p.addLast(new ApiEchoHandler());
}
}

Loading

0 comments on commit 3d93f9d

Please sign in to comment.