sncp优化

This commit is contained in:
redkale
2023-02-08 17:57:28 +08:00
parent 530bf8078e
commit 3ec4f9f96d
25 changed files with 2033 additions and 730 deletions

View File

@@ -0,0 +1,97 @@
/*
*
*/
package org.redkale.test.sncp;
import java.io.File;
import java.nio.channels.CompletionHandler;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.redkale.net.sncp.SncpAsyncHandler;
/**
*
* @author zhangjx
*/
public class SncpHandlerTest {
private boolean main;
public static void main(String[] args) throws Throwable {
SncpHandlerTest test = new SncpHandlerTest();
test.main = true;
test.run();
}
@Test
public void run() throws Exception {
SncpAsyncHandler.createHandler(CompletionHandler.class, new CompletionHandler() {
@Override
public void completed(Object result, Object attachment) {
if (main) {
System.out.println("handler result: " + result + ", attachment: " + attachment);
}
}
@Override
public void failed(Throwable exc, Object attachment) {
}
}).completed(1, 2);
SncpAsyncHandler.createHandler(ITestHandler1.class, new CompletionHandler() {
@Override
public void completed(Object result, Object attachment) {
System.out.println("handler1 result: " + result + ", attachment: " + attachment);
}
@Override
public void failed(Throwable exc, Object attachment) {
}
}).completed("name", "/user/");
SncpAsyncHandler.createHandler(ITestHandler2.class, new CompletionHandler() {
@Override
public void completed(Object result, Object attachment) {
System.out.println("handler2 result: " + result + ", attachment: " + attachment);
}
@Override
public void failed(Throwable exc, Object attachment) {
}
}).completed("aaa", "bbb");
SncpAsyncHandler.createHandler(ITestHandler3.class, new CompletionHandler() {
@Override
public void completed(Object result, Object attachment) {
System.out.println("handler3 result: " + result + ", attachment: " + attachment);
}
@Override
public void failed(Throwable exc, Object attachment) {
}
}).completed("key1", "val1");
}
public static abstract class ITestHandler1 implements CompletionHandler<String, File> {
@Override
public abstract void completed(String result, File attachment);
}
public static interface IClose<T> {
public void close(T val);
}
public static interface ITestHandler2 extends CompletionHandler<String, String>, IClose<File> {
}
public static interface ITestHandler3 extends CompletionHandler<String, String>, Map<String, String> {
}
}

View File

@@ -163,6 +163,7 @@ public class SncpTest {
conf.addValue("host", "0.0.0.0");
conf.addValue("port", "" + port);
conf.addValue("protocol", protocol);
conf.addValue("maxbody", "" + (100 * 1024 * 1024));
SncpServer server = new SncpServer(null, System.currentTimeMillis(), conf, factory);
Set<InetSocketAddress> set = new LinkedHashSet<>();
if (port2 > 0) {
@@ -203,6 +204,7 @@ public class SncpTest {
conf.addValue("host", "0.0.0.0");
conf.addValue("port", "" + (port2 < 10 ? 0 : port2));
conf.addValue("protocol", protocol);
conf.addValue("maxbody", "" + (100 * 1024 * 1024));
SncpServer server = new SncpServer(null, System.currentTimeMillis(), conf, factory);
Set<InetSocketAddress> set = new LinkedHashSet<>();
set.add(new InetSocketAddress(myhost, port));

View File

@@ -0,0 +1,146 @@
/*
*
*/
package org.redkale.test.sncp;
import java.lang.reflect.Method;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.CompletableFuture;
import org.redkale.annotation.ResourceType;
import org.redkale.convert.bson.*;
import org.redkale.net.sncp.SncpDynServlet.SncpActionServlet;
import org.redkale.net.sncp.*;
import org.redkale.service.Service;
import org.redkale.test.util.TestBean;
import org.redkale.util.Uint128;
/**
*
* @author zhangjx
*/
public interface TestService extends Service {
public boolean change(TestBean bean, String name, int id);
public void insert(BooleanHandler handler, TestBean bean, String name, int id);
public void update(long show, short v2, CompletionHandler<Boolean, TestBean> handler, TestBean bean, String name, int id);
public CompletableFuture<String> changeName(TestBean bean, String name, int id);
@ResourceType(TestService.class)
public static class TestServiceImpl implements TestService {
@Override
public boolean change(TestBean bean, String name, int id) {
return false;
}
@Override
public void insert(BooleanHandler handler, TestBean bean, String name, int id) {
}
@Override
public void update(long show, short v2, CompletionHandler<Boolean, TestBean> handler, TestBean bean, String name, int id) {
}
@Override
public CompletableFuture<String> changeName(TestBean bean, String name, int id) {
return null;
}
}
public static class BooleanHandler implements CompletionHandler<Boolean, TestBean> {
@Override
public void completed(Boolean result, TestBean attachment) {
}
@Override
public void failed(Throwable exc, TestBean attachment) {
}
}
public static class DynActionTestService_change extends SncpActionServlet {
public DynActionTestService_change(String resourceName, Class resourceType, Service service, Uint128 serviceid, Uint128 actionid, final Method method) {
super(resourceName, resourceType, service, serviceid, actionid, method);
}
@Override
public void action(SncpRequest request, SncpResponse response) throws Throwable {
BsonConvert convert = request.getBsonConvert();
BsonReader in = request.getBsonReader();
TestBean arg1 = convert.convertFrom(paramTypes[1], in);
String arg2 = convert.convertFrom(paramTypes[2], in);
int arg3 = convert.convertFrom(paramTypes[3], in);
TestService serviceObj = (TestService) service();
Object rs = serviceObj.change(arg1, arg2, arg3);
response.finish(boolean.class, rs);
}
}
public static class DynActionTestService_insert extends SncpActionServlet {
public DynActionTestService_insert(String resourceName, Class resourceType, Service service, Uint128 serviceid, Uint128 actionid, final Method method) {
super(resourceName, resourceType, service, serviceid, actionid, method);
}
@Override
public void action(SncpRequest request, SncpResponse response) throws Throwable {
BsonConvert convert = request.getBsonConvert();
BsonReader in = request.getBsonReader();
BooleanHandler arg0 = response.getParamAsyncHandler();
convert.convertFrom(CompletionHandler.class, in);
TestBean arg1 = convert.convertFrom(paramTypes[2], in);
String arg2 = convert.convertFrom(paramTypes[3], in);
int arg3 = convert.convertFrom(paramTypes[4], in);
TestService serviceObj = (TestService) service();
serviceObj.insert(arg0, arg1, arg2, arg3);
response.finishVoid();
}
}
public static class DynActionTestService_update extends SncpActionServlet {
public DynActionTestService_update(String resourceName, Class resourceType, Service service, Uint128 serviceid, Uint128 actionid, final Method method) {
super(resourceName, resourceType, service, serviceid, actionid, method);
}
@Override
public void action(SncpRequest request, SncpResponse response) throws Throwable {
BsonConvert convert = request.getBsonConvert();
BsonReader in = request.getBsonReader();
long a1 = convert.convertFrom(paramTypes[1], in);
short a2 = convert.convertFrom(paramTypes[2], in);
CompletionHandler a3 = response.getParamAsyncHandler();
convert.convertFrom(CompletionHandler.class, in);
TestBean arg1 = convert.convertFrom(paramTypes[4], in);
String arg2 = convert.convertFrom(paramTypes[5], in);
int arg3 = convert.convertFrom(paramTypes[6], in);
TestService serviceObj = (TestService) service();
serviceObj.update(a1, a2, a3, arg1, arg2, arg3);
response.finishVoid();
}
}
public static class DynActionTestService_changeName extends SncpActionServlet {
public DynActionTestService_changeName(String resourceName, Class resourceType, Service service, Uint128 serviceid, Uint128 actionid, final Method method) {
super(resourceName, resourceType, service, serviceid, actionid, method);
}
@Override
public void action(SncpRequest request, SncpResponse response) throws Throwable {
BsonConvert convert = request.getBsonConvert();
BsonReader in = request.getBsonReader();
TestBean arg1 = convert.convertFrom(paramTypes[1], in);
String arg2 = convert.convertFrom(paramTypes[2], in);
int arg3 = convert.convertFrom(paramTypes[3], in);
TestService serviceObj = (TestService) service();
CompletableFuture future = serviceObj.changeName(arg1, arg2, arg3);
response.finishFuture(paramHandlerResultType, future);
}
}
}

View File

@@ -5,7 +5,6 @@
*/
package org.redkale.test.sncp;
import org.redkale.net.sncp.*;
import org.redkale.annotation.ResourceType;
/**
@@ -15,6 +14,5 @@ import org.redkale.annotation.ResourceType;
@ResourceType(SncpTestIService.class)
public class _DynLocalSncpTestService extends SncpTestServiceImpl {
private SncpOldClient _redkale_client;
}

View File

@@ -0,0 +1,74 @@
/*
*
*/
package org.redkale.test.util;
import java.io.File;
import java.lang.reflect.*;
import java.nio.channels.CompletionHandler;
import org.junit.jupiter.api.*;
import org.redkale.util.TypeToken;
/**
*
* @author zhangjx
*/
public class TypeTokenTest {
private boolean main;
public static void main(String[] args) throws Throwable {
TypeTokenTest test = new TypeTokenTest();
test.main = true;
test.run();
test.run2();
}
@Test
public void run() throws Exception {
Class serviceType = Service1.class;
Method method = serviceType.getMethod("test", String.class, CompletionHandler.class);
Type handlerType = TypeToken.getGenericType(method.getGenericParameterTypes()[1], serviceType);
Type resultType = null;
if (handlerType instanceof Class) {
resultType = Object.class;
} else if (handlerType instanceof ParameterizedType) {
resultType = TypeToken.getGenericType(((ParameterizedType) handlerType).getActualTypeArguments()[0], handlerType);
}
if (!main) {
Assertions.assertEquals(resultType, String.class);
}
System.out.println("resultType = " + resultType);
}
@Test
public void run2() throws Exception {
Class serviceType = Service2.class;
Method method = serviceType.getMethod("test", String.class, CompletionHandler.class);
Type handlerType = TypeToken.getGenericType(method.getGenericParameterTypes()[1], serviceType);
Type resultType = null;
if (handlerType instanceof Class) {
resultType = Object.class;
} else if (handlerType instanceof ParameterizedType) {
resultType = TypeToken.getGenericType(((ParameterizedType) handlerType).getActualTypeArguments()[0], handlerType);
}
if (!main) {
Assertions.assertEquals(resultType, File.class);
}
System.out.println("resultType = " + resultType);
}
public static abstract class Service1 {
public abstract void test(String name, CompletionHandler<String, Integer> handler);
}
public static abstract class IService2<T> {
public abstract void test(String name, CompletionHandler<T, Integer> handler);
}
public static abstract class Service2 extends IService2<File> {
}
}