Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion rcljava/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ macro(target)
get_rcl_information("${rmw_implementation}" "rcl${target_suffix}")
endif()

set(_jni_classes "RCLJava;Node;Publisher")
set(_jni_classes "RCLJava;Node;Publisher;Client")

foreach(_jni_class ${_jni_classes})

Expand Down Expand Up @@ -80,11 +80,17 @@ macro(target)
endmacro()

set(${PROJECT_NAME}_sources
"src/main/java/org/ros2/rcljava/BiConsumer.java"
"src/main/java/org/ros2/rcljava/Client.java"
"src/main/java/org/ros2/rcljava/Consumer.java"
"src/main/java/org/ros2/rcljava/Node.java"
"src/main/java/org/ros2/rcljava/Publisher.java"
"src/main/java/org/ros2/rcljava/RCLFuture.java"
"src/main/java/org/ros2/rcljava/RCLJava.java"
"src/main/java/org/ros2/rcljava/RMWRequestId.java"
"src/main/java/org/ros2/rcljava/Service.java"
"src/main/java/org/ros2/rcljava/Subscription.java"
"src/main/java/org/ros2/rcljava/TriConsumer.java"
)

add_jar("${PROJECT_NAME}_jar"
Expand Down
36 changes: 36 additions & 0 deletions rcljava/include/org_ros2_rcljava_Client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2016 Esteve Fernandez <[email protected]>
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_ros2_rcljava_Client */

#ifndef ORG_ROS2_RCLJAVA_CLIENT_H_
#define ORG_ROS2_RCLJAVA_CLIENT_H_

#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_ros2_rcljava_Client
* Method: nativeSendClientRequest
* Signature: (JJJJLjava/lang/Object;)V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_Client_nativeSendClientRequest
(JNIEnv *, jclass, jlong, jlong, jlong, jlong, jobject);
#ifdef __cplusplus
}
#endif

#endif // ORG_ROS2_RCLJAVA_CLIENT_H_
16 changes: 16 additions & 0 deletions rcljava/include/org_ros2_rcljava_Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreatePublisherHandle
JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateSubscriptionHandle
(JNIEnv *, jclass, jlong, jclass, jstring);

/*
* Class: org_ros2_rcljava_Node
* Method: nativeCreateServiceHandle
* Signature: (JLjava/lang/Class;Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateServiceHandle
(JNIEnv *, jclass, jlong, jclass, jstring);

/*
* Class: org_ros2_rcljava_Node
* Method: nativeCreateClientHandle
* Signature: (JLjava/lang/Class;Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateClientHandle
(JNIEnv *, jclass, jlong, jclass, jstring);

#ifdef __cplusplus
}
#endif
Expand Down
78 changes: 72 additions & 6 deletions rcljava/include/org_ros2_rcljava_RCLJava.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#ifndef ORG_ROS2_RCLJAVA_RCLJAVA_H_
#define ORG_ROS2_RCLJAVA_RCLJAVA_H_

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -53,6 +54,14 @@ JNIEXPORT jstring JNICALL Java_org_ros2_rcljava_RCLJava_nativeGetRMWIdentifier
JNIEXPORT jboolean JNICALL Java_org_ros2_rcljava_RCLJava_nativeOk
(JNIEnv *, jclass);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeShutdown
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeShutdown
(JNIEnv *, jclass);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeGetZeroInitializedWaitSet
Expand All @@ -64,10 +73,10 @@ JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_RCLJava_nativeGetZeroInitializedWa
/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeWaitSetInit
* Signature: (JIII)V
* Signature: (JIIIII)V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetInit
(JNIEnv *, jclass, jlong, jint, jint, jint);
(JNIEnv *, jclass, jlong, jint, jint, jint, jint, jint);

/*
* Class: org_ros2_rcljava_RCLJava
Expand Down Expand Up @@ -103,13 +112,70 @@ JNIEXPORT jobject JNICALL Java_org_ros2_rcljava_RCLJava_nativeTake

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeShutdown
* Signature: ()V
* Method: nativeWaitSetClearServices
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeShutdown
(JNIEnv *, jclass);
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetClearServices
(JNIEnv *, jclass, jlong);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeWaitSetAddService
* Signature: (JJ)V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetAddService
(JNIEnv *, jclass, jlong, jlong);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeWaitSetClearClients
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetClearClients
(JNIEnv *, jclass, jlong);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeWaitSetAddClient
* Signature: (JJ)V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetAddClient
(JNIEnv *, jclass, jlong, jlong);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeTakeRequest
* Signature: (JJJLjava/lang/Object;)Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL Java_org_ros2_rcljava_RCLJava_nativeTakeRequest
(JNIEnv *, jclass, jlong, jlong, jlong, jobject);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeSendServiceResponse
* Signature: (JLjava/lang/Object;JJLjava/lang/Object;)V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeSendServiceResponse
(JNIEnv *, jclass, jlong, jobject, jlong, jlong, jobject);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeSendClientRequest
* Signature: (JJLjava/lang/Object;JJ)V
*/
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeSendClientRequest
(JNIEnv *, jclass, jlong, jlong, jlong, jlong, jobject);

/*
* Class: org_ros2_rcljava_RCLJava
* Method: nativeTakeResponse
* Signature: (JJJLjava/lang/Object;)Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL Java_org_ros2_rcljava_RCLJava_nativeTakeResponse
(JNIEnv *, jclass, jlong, jlong, jlong, jobject);

#ifdef __cplusplus
}
#endif

#endif // ORG_ROS2_RCLJAVA_RCLJAVA_H_
55 changes: 55 additions & 0 deletions rcljava/src/main/cpp/org_ros2_rcljava_Client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2016 Esteve Fernandez <[email protected]>
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

#include <jni.h>

#include <cassert>
#include <cstdlib>
#include <string>

#include "rmw/rmw.h"
#include "rcl/error_handling.h"
#include "rcl/rcl.h"
#include "rcl/node.h"
#include "rosidl_generator_c/message_type_support.h"

#include "rcljava_common/exceptions.h"
#include "rcljava_common/signatures.h"

#include "org_ros2_rcljava_Client.h"

JNIEXPORT void JNICALL Java_org_ros2_rcljava_Client_nativeSendClientRequest(JNIEnv * env, jclass,
jlong client_handle, jlong sequence_number, jlong jrequest_from_java_converter_handle,
jlong jrequest_to_java_converter_handle, jobject jrequest_msg)
{
assert(client_handle != 0);
assert(jrequest_from_java_converter_handle != 0);
assert(jrequest_to_java_converter_handle != 0);
assert(jrequest_msg != nullptr);

rcl_client_t * client = reinterpret_cast<rcl_client_t *>(client_handle);

convert_from_java_signature convert_from_java =
reinterpret_cast<convert_from_java_signature>(jrequest_from_java_converter_handle);

void * request_msg = convert_from_java(jrequest_msg, nullptr);

rcl_ret_t ret = rcl_send_request(client, request_msg, &sequence_number);

if (ret != RCL_RET_OK) {
rcljava_throw_exception(
env, "java/lang/IllegalStateException",
"Failed to send request from a client: " + std::string(rcl_get_error_string_safe()));
}
}
78 changes: 78 additions & 0 deletions rcljava/src/main/cpp/org_ros2_rcljava_Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,81 @@ JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateSubscriptionHandl
jlong jsubscription = reinterpret_cast<jlong>(subscription);
return jsubscription;
}

JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateServiceHandle(JNIEnv * env, jclass,
jlong node_handle, jclass jservice_class, jstring jservice_name)
{
jmethodID mid = env->GetStaticMethodID(jservice_class, "getServiceTypeSupport", "()J");

assert(mid != NULL);

jlong jts = env->CallStaticLongMethod(jservice_class, mid);

assert(jts != 0);

const char * service_name_tmp = env->GetStringUTFChars(jservice_name, 0);

std::string service_name(service_name_tmp);

env->ReleaseStringUTFChars(jservice_name, service_name_tmp);

rcl_node_t * node = reinterpret_cast<rcl_node_t *>(node_handle);

rosidl_service_type_support_t * ts =
reinterpret_cast<rosidl_service_type_support_t *>(jts);

rcl_service_t * service = static_cast<rcl_service_t *>(malloc(sizeof(rcl_service_t)));
service->impl = NULL;
rcl_service_options_t service_ops = rcl_service_get_default_options();

rcl_ret_t ret = rcl_service_init(service, node, ts, service_name.c_str(), &service_ops);

if (ret != RCL_RET_OK) {
rcljava_throw_exception(
env, "java/lang/IllegalStateException",
"Failed to create service: " + std::string(rcl_get_error_string_safe()));
return 0;
}

jlong jservice = reinterpret_cast<jlong>(service);
return jservice;
}

JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateClientHandle(JNIEnv * env, jclass,
jlong node_handle, jclass jservice_class, jstring jservice_name)
{
jmethodID mid = env->GetStaticMethodID(jservice_class, "getServiceTypeSupport", "()J");

assert(mid != NULL);

jlong jts = env->CallStaticLongMethod(jservice_class, mid);

assert(jts != 0);

const char * service_name_tmp = env->GetStringUTFChars(jservice_name, 0);

std::string service_name(service_name_tmp);

env->ReleaseStringUTFChars(jservice_name, service_name_tmp);

rcl_node_t * node = reinterpret_cast<rcl_node_t *>(node_handle);

rosidl_service_type_support_t * ts =
reinterpret_cast<rosidl_service_type_support_t *>(jts);

rcl_client_t * client = static_cast<rcl_client_t *>(malloc(sizeof(rcl_client_t)));
client->impl = NULL;
rcl_client_options_t client_ops = rcl_client_get_default_options();

rcl_ret_t ret = rcl_client_init(client, node, ts, service_name.c_str(), &client_ops);

if (ret != RCL_RET_OK) {
rcljava_throw_exception(
env, "java/lang/IllegalStateException",
"Failed to create client: " + std::string(rcl_get_error_string_safe()));
return 0;
}

jlong jclient = reinterpret_cast<jlong>(client);
return jclient;
}
Loading