This commit is contained in:
Qing Wang
2020-10-21 22:11:49 +08:00
parent f4c9b02908
commit 90e834c960
8 changed files with 74 additions and 142 deletions
@@ -4,25 +4,25 @@ import java.util.concurrent.CompletableFuture;
public interface AsyncRpcFunction {
// CompletableFuture<Object> invoke();
//
// <Arg1Type>
// CompletableFuture<Object> invoke(Arg1Type arg1);
CompletableFuture<Object> invoke(Class returnClz);
<ReturnType, Arg1Type, Arg2Type>
<Arg1Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1);
<Arg1Type, Arg2Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2);
// <Arg1Type, Arg2Type, Arg3Type>
// CompletableFuture<Object> invoke(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3);
//
//
// <Arg1Type, Arg2Type, Arg3Type, Arg4Type>
// CompletableFuture<Object> invoke(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4);
//
// <Arg1Type, Arg2Type, Arg3Type, Arg4Type, Arg5Type>
// CompletableFuture<Object> invoke(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4, Arg5Type arg5);
//
// <Arg1Type, Arg2Type, Arg3Type, Arg4Type, Arg5Type, Arg6Type>
// CompletableFuture<Object> invoke(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4, Arg5Type arg5, Arg6Type arg6);
<Arg1Type, Arg2Type, Arg3Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3);
<Arg1Type, Arg2Type, Arg3Type, Arg4Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4);
<Arg1Type, Arg2Type, Arg3Type, Arg4Type, Arg5Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4, Arg5Type arg5);
<Arg1Type, Arg2Type, Arg3Type, Arg4Type, Arg5Type, Arg6Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4, Arg5Type arg5, Arg6Type arg6);
}
@@ -13,47 +13,47 @@ public class AsyncRpcFunctionImpl implements AsyncRpcFunction {
this.funcName = funcName;
}
// public CompletableFuture<Object> invoke() {
// return internalInvoke(new Object[0]);
// }
//
// public <Arg1Type> CompletableFuture<Object> invoke(Arg1Type arg1) {
// Object[] args = new Object[] {arg1};
// return internalInvoke(args);
// }
public CompletableFuture<Object> invoke(Class returnClz) {
return internalInvoke(returnClz, new Object[0]);
}
public <ReturnType, Arg1Type, Arg2Type>
public <Arg1Type> CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1) {
Object[] args = new Object[] {arg1};
return internalInvoke(returnClz, args);
}
public <Arg1Type, Arg2Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2) {
Object[] args = new Object[] {arg1, arg2};
return internalInvoke(returnClz, args);
}
// public <Arg1Type, Arg2Type, Arg3Type>
// CompletableFuture<Object> invoke(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3) {
// Object[] args = new Object[] {arg1, arg2, arg3};
// return internalInvoke(args);
// }
//
//
// public <Arg1Type, Arg2Type, Arg3Type, Arg4Type>
// CompletableFuture<Object> invoke(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4) {
// Object[] args = new Object[] {arg1, arg2, arg3, arg4};
// return internalInvoke(args);
// }
//
// public <Arg1Type, Arg2Type, Arg3Type, Arg4Type, Arg5Type>
// CompletableFuture<Object> invoke(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4, Arg5Type arg5) {
// Object[] args = new Object[] {arg1, arg2, arg3, arg4, arg5};
// return internalInvoke(args);
// }
//
// public <Arg1Type, Arg2Type, Arg3Type, Arg4Type, Arg5Type, Arg6Type>
// CompletableFuture<Object> invoke(Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4, Arg5Type arg5, Arg6Type arg6) {
// Object[] args = new Object[] {arg1, arg2, arg3, arg4, arg5, arg6};
// return internalInvoke(args);
// }
public <Arg1Type, Arg2Type, Arg3Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3) {
Object[] args = new Object[] {arg1, arg2, arg3};
return internalInvoke(returnClz, args);
}
private <ReturnType> CompletableFuture<Object> internalInvoke(Class returnClz, Object[] args) {
public <Arg1Type, Arg2Type, Arg3Type, Arg4Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4) {
Object[] args = new Object[] {arg1, arg2, arg3, arg4};
return internalInvoke(returnClz, args);
}
public <Arg1Type, Arg2Type, Arg3Type, Arg4Type, Arg5Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4, Arg5Type arg5) {
Object[] args = new Object[] {arg1, arg2, arg3, arg4, arg5};
return internalInvoke(returnClz, args);
}
public <Arg1Type, Arg2Type, Arg3Type, Arg4Type, Arg5Type, Arg6Type>
CompletableFuture<Object> invoke(Class returnClz, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3, Arg4Type arg4, Arg5Type arg5, Arg6Type arg6) {
Object[] args = new Object[] {arg1, arg2, arg3, arg4, arg5, arg6};
return internalInvoke(returnClz, args);
}
private CompletableFuture<Object> internalInvoke(Class returnClz, Object[] args) {
return rpcClient.invoke(returnClz, funcName, args);
}
}
@@ -39,7 +39,7 @@ public class NativeRpcClient implements RpcClient {
return new AsyncRpcFunctionImpl(this, funcName);
}
public <ReturnType> CompletableFuture<Object> invoke(Class returnClz, String funcName, Object[] args) {
public CompletableFuture<Object> invoke(Class returnClz, String funcName, Object[] args) {
if (rpcClientPointer == -1) {
throw new RuntimeException("no init");
}
@@ -78,13 +78,12 @@ public class NativeRpcClient implements RpcClient {
* The callback that will be invoked once the reuslt of rpc request received.
* Note that this method will be invoked in JNI.
*/
private void onResultReceived(long requestId, byte[] encodedReturnValueBytes) throws IOException {
private void onResultReceived(long requestId, byte[] encodedReturnValue) throws IOException {
// if (requestId not is local_cache) {//error}
// System.out.println("result is " + codec.myDecodeInt(encodedReturnValueBytes));
synchronized (this) {
final Class<?> returnClz = localFutureReturnTypenameCache.get(requestId);
CompletableFuture<Object> future = localFutureCache.get(requestId);
Object o = codec.decodeReturnValue(returnClz, encodedReturnValueBytes);
Object o = codec.decodeReturnValue(returnClz, encodedReturnValue);
future.complete(o);
}
}
@@ -1,40 +0,0 @@
package org.restrpc.client;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
public class RestFuture<T> {
private Class<T> metaType;
private T value;
public RestFuture(Class<T> metaType) {
this.metaType = metaType;
}
private byte[] encodedData;
private static Codec codec = new Codec();
public RestFuture() {
}
void complete(Integer value) {
this.value = (T) value;
}
void complete(Long value) {
this.value = (T) value;
}
// ...
// TODO(qwang): Add uncheck!!!
public T get() throws IOException {
// // TODO(qwang): This can be cached.
// return (T) codec.decodeSingleValue(metaType.getTypeName(), encodedData);
return null;
}
}
@@ -8,7 +8,7 @@ public interface RpcClient {
AsyncRpcFunction asyncFunc(String funcName);
<ReturnType> CompletableFuture<Object> invoke(Class returnClz, String funcName, Object[] args);
CompletableFuture<Object> invoke(Class returnClz, String funcName, Object[] args);
void close();
}
@@ -1,19 +1,12 @@
package org.restrpc.test;
import org.msgpack.core.MessageBufferPacker;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;
import org.restrpc.client.Codec;
import org.restrpc.client.NativeRpcClient;
import org.restrpc.client.RestFuture;
import org.restrpc.client.RpcClient;
import org.testng.annotations.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class BasicClientTest {
@@ -21,7 +14,7 @@ public class BasicClientTest {
public void testBasic() throws IOException, InterruptedException, ExecutionException {
RpcClient rpcClient = new NativeRpcClient();
rpcClient.connect("127.0.0.1:9000");
CompletableFuture<Object> future = rpcClient.asyncFunc("echo").invoke(String.class, 2, 3);
CompletableFuture<Object> future = rpcClient.asyncFunc("add").invoke(Integer.class, 2, 3);
System.out.println("The result of add(2, 3) is " + future.get());
}
@@ -47,21 +40,4 @@ public class BasicClientTest {
Object[] args = new Object[] {2, 3};
byte[] bs = codec.encode("add", args);
}
@Test
public void testDecode() throws IOException {
// CompletableFuture<?> i = new CompletableFuture<Integer>() {};
// System.out.println(i.getClass().getDeclare);
//
// OutputStream os = new ByteArrayOutputStream();
// MessageBufferPacker messagePacker = MessagePack.newDefaultBufferPacker();
// messagePacker.packArrayHeader(3);
// messagePacker.packString("add");
// messagePacker.packInt(2);
// messagePacker.packInt(3);
// byte[] bs1 = messagePacker.toByteArray();
//
// MessageUnpacker messageUnpacker = MessagePack.newDefaultUnpacker(bs1);
// messageUnpacker.unpackInt();
}
}