Skip to content

Commit

Permalink
Support Java pojo arguments and return value.
Browse files Browse the repository at this point in the history
  • Loading branch information
jovany-wang committed Oct 23, 2020
1 parent 4530dc7 commit 0e6689d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 10 deletions.
16 changes: 11 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.1)
project(example)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11")
set(ASIO_STANDALONE 1)

SET(ENABLE_SSL OFF)

Expand All @@ -10,12 +11,17 @@ if (ENABLE_SSL)
message(STATUS "Use SSL")
endif()

find_package(Boost COMPONENTS system filesystem REQUIRED)
find_package(JNI REQUIRED)
#find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(
"/usr/local/include"
"../../include"
"../../third/msgpack/include"
)
# "/usr/local/include"
"../include"
"../jni"
${JNI_INCLUDE_DIRS}
# "/usr/local/opt/openjdk/include/"
"/Users/qingwang/workspace/opensource/rest_rpc/third/msgpack/include")
INCLUDE_DIRECTORIES(SYSTEM "/Users/qingwang/workspace/opensource/dousi/build/external/boost/src/boost_ep")
INCLUDE_DIRECTORIES(SYSTEM "/Users/qingwang/workspace/opensource/rest_rpc/third/msgpack/include")

add_executable(basic_server server/main.cpp)
add_executable(basic_client client/main.cpp)
Expand Down
5 changes: 5 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<artifactId>msgpack-core</artifactId>
<version>0.8.21</version>
</dependency>
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>jackson-dataformat-msgpack</artifactId>
<version>0.8.21</version>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
20 changes: 15 additions & 5 deletions java/src/main/java/org/restrpc/client/Codec.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.restrpc.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.msgpack.core.MessageBufferPacker;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;

import java.awt.print.PrinterGraphics;
import org.msgpack.jackson.dataformat.JsonArrayFormat;
import org.msgpack.jackson.dataformat.MessagePackFactory;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;

public class Codec {

Expand Down Expand Up @@ -39,13 +42,15 @@ public byte[] encode(String funcName, Object[] args) throws IOException {
messagePacker.packString((String) arg);
break;
default:
throw new RuntimeException("Unknown type: " + argTypeName);
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
messagePacker.writePayload(objectMapper.writeValueAsBytes(arg));
}
}
return messagePacker.toByteArray();
}

public Object decodeReturnValue(Class returnClz, byte[] encodedBytes) throws IOException {
public Object decodeReturnValue(Class<?> returnClz, byte[] encodedBytes) throws IOException {
if (returnClz == null) {
throw new RuntimeException("Internal bug.");
}
Expand All @@ -66,7 +71,12 @@ public Object decodeReturnValue(Class returnClz, byte[] encodedBytes) throws IOE
return messageUnpacker.unpackLong();
} else if (String.class.equals(returnClz)) {
return messageUnpacker.unpackString();
} else {
final int remainder = (int) (encodedBytes.length - messageUnpacker.getTotalReadBytes());
byte[] payload = messageUnpacker.readPayload(remainder);
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
return objectMapper.readValue(payload, returnClz);
}
throw new RuntimeException("Unknown type: " + returnClz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.restrpc.examples;

import org.restrpc.client.NativeRpcClient;
import org.restrpc.client.RpcClient;

import java.io.Serializable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

class Person implements Serializable {
private static final long serialVersionUID = 5989656920203532884L;
private int id;
private String name;
private int age;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Person {" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}

public class PassPojoAsArgumentExample {

public static void main(String[] args) throws InterruptedException, ExecutionException {
/**
* An example shows how we use this Java client to connect to
* the C++ RPC server and invoke the C++ RPC methods.
*
* First of all, we should run a C++ rpc server. In this example,
* we first run the `basic_server` which is written here:
* https://github.com/qicosmos/rest_rpc/blob/master/examples/server/main.cpp
*/
RpcClient rpcClient = new NativeRpcClient();
rpcClient.connect("127.0.0.1:9000");

{
Person p = new Person();
p.setId(10001);
p.setName("Jack");
p.setAge(22);
CompletableFuture<?> future = rpcClient.asyncFunc("get_person_name").invoke(String.class, p);
System.out.println("The result of get_person_name() is " + future.get());
}

{
CompletableFuture<?> future = rpcClient.asyncFunc("get_person").invoke(Person.class);
System.out.println("The result of get_person() is " + future.get());
}

rpcClient.close();
}
}

0 comments on commit 0e6689d

Please sign in to comment.