Skip to content

Commit 84b28ff

Browse files
authored
Merge pull request #6 from esteve/services
Added support for clients and services
2 parents 5369ef6 + f829395 commit 84b28ff

21 files changed

+1143
-40
lines changed

rcljava/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ macro(target)
3232
get_rcl_information("${rmw_implementation}" "rcl${target_suffix}")
3333
endif()
3434

35-
set(_jni_classes "RCLJava;Node;Publisher")
35+
set(_jni_classes "RCLJava;Node;Publisher;Client")
3636

3737
foreach(_jni_class ${_jni_classes})
3838

@@ -80,11 +80,17 @@ macro(target)
8080
endmacro()
8181

8282
set(${PROJECT_NAME}_sources
83+
"src/main/java/org/ros2/rcljava/BiConsumer.java"
84+
"src/main/java/org/ros2/rcljava/Client.java"
8385
"src/main/java/org/ros2/rcljava/Consumer.java"
8486
"src/main/java/org/ros2/rcljava/Node.java"
8587
"src/main/java/org/ros2/rcljava/Publisher.java"
88+
"src/main/java/org/ros2/rcljava/RCLFuture.java"
8689
"src/main/java/org/ros2/rcljava/RCLJava.java"
90+
"src/main/java/org/ros2/rcljava/RMWRequestId.java"
91+
"src/main/java/org/ros2/rcljava/Service.java"
8792
"src/main/java/org/ros2/rcljava/Subscription.java"
93+
"src/main/java/org/ros2/rcljava/TriConsumer.java"
8894
)
8995

9096
add_jar("${PROJECT_NAME}_jar"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2016 Esteve Fernandez <esteve@apache.org>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/* DO NOT EDIT THIS FILE - it is machine generated */
16+
#include <jni.h>
17+
/* Header for class org_ros2_rcljava_Client */
18+
19+
#ifndef ORG_ROS2_RCLJAVA_CLIENT_H_
20+
#define ORG_ROS2_RCLJAVA_CLIENT_H_
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
/*
26+
* Class: org_ros2_rcljava_Client
27+
* Method: nativeSendClientRequest
28+
* Signature: (JJJJLjava/lang/Object;)V
29+
*/
30+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_Client_nativeSendClientRequest
31+
(JNIEnv *, jclass, jlong, jlong, jlong, jlong, jobject);
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
36+
#endif // ORG_ROS2_RCLJAVA_CLIENT_H_

rcljava/include/org_ros2_rcljava_Node.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreatePublisherHandle
3737
JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateSubscriptionHandle
3838
(JNIEnv *, jclass, jlong, jclass, jstring);
3939

40+
/*
41+
* Class: org_ros2_rcljava_Node
42+
* Method: nativeCreateServiceHandle
43+
* Signature: (JLjava/lang/Class;Ljava/lang/String;)J
44+
*/
45+
JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateServiceHandle
46+
(JNIEnv *, jclass, jlong, jclass, jstring);
47+
48+
/*
49+
* Class: org_ros2_rcljava_Node
50+
* Method: nativeCreateClientHandle
51+
* Signature: (JLjava/lang/Class;Ljava/lang/String;)J
52+
*/
53+
JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateClientHandle
54+
(JNIEnv *, jclass, jlong, jclass, jstring);
55+
4056
#ifdef __cplusplus
4157
}
4258
#endif

rcljava/include/org_ros2_rcljava_RCLJava.h

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#ifndef ORG_ROS2_RCLJAVA_RCLJAVA_H_
2020
#define ORG_ROS2_RCLJAVA_RCLJAVA_H_
21+
2122
#ifdef __cplusplus
2223
extern "C" {
2324
#endif
@@ -53,6 +54,14 @@ JNIEXPORT jstring JNICALL Java_org_ros2_rcljava_RCLJava_nativeGetRMWIdentifier
5354
JNIEXPORT jboolean JNICALL Java_org_ros2_rcljava_RCLJava_nativeOk
5455
(JNIEnv *, jclass);
5556

57+
/*
58+
* Class: org_ros2_rcljava_RCLJava
59+
* Method: nativeShutdown
60+
* Signature: ()V
61+
*/
62+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeShutdown
63+
(JNIEnv *, jclass);
64+
5665
/*
5766
* Class: org_ros2_rcljava_RCLJava
5867
* Method: nativeGetZeroInitializedWaitSet
@@ -64,10 +73,10 @@ JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_RCLJava_nativeGetZeroInitializedWa
6473
/*
6574
* Class: org_ros2_rcljava_RCLJava
6675
* Method: nativeWaitSetInit
67-
* Signature: (JIII)V
76+
* Signature: (JIIIII)V
6877
*/
6978
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetInit
70-
(JNIEnv *, jclass, jlong, jint, jint, jint);
79+
(JNIEnv *, jclass, jlong, jint, jint, jint, jint, jint);
7180

7281
/*
7382
* Class: org_ros2_rcljava_RCLJava
@@ -103,13 +112,70 @@ JNIEXPORT jobject JNICALL Java_org_ros2_rcljava_RCLJava_nativeTake
103112

104113
/*
105114
* Class: org_ros2_rcljava_RCLJava
106-
* Method: nativeShutdown
107-
* Signature: ()V
115+
* Method: nativeWaitSetClearServices
116+
* Signature: (J)V
108117
*/
109-
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeShutdown
110-
(JNIEnv *, jclass);
118+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetClearServices
119+
(JNIEnv *, jclass, jlong);
120+
121+
/*
122+
* Class: org_ros2_rcljava_RCLJava
123+
* Method: nativeWaitSetAddService
124+
* Signature: (JJ)V
125+
*/
126+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetAddService
127+
(JNIEnv *, jclass, jlong, jlong);
128+
129+
/*
130+
* Class: org_ros2_rcljava_RCLJava
131+
* Method: nativeWaitSetClearClients
132+
* Signature: (J)V
133+
*/
134+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetClearClients
135+
(JNIEnv *, jclass, jlong);
136+
137+
/*
138+
* Class: org_ros2_rcljava_RCLJava
139+
* Method: nativeWaitSetAddClient
140+
* Signature: (JJ)V
141+
*/
142+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeWaitSetAddClient
143+
(JNIEnv *, jclass, jlong, jlong);
144+
145+
/*
146+
* Class: org_ros2_rcljava_RCLJava
147+
* Method: nativeTakeRequest
148+
* Signature: (JJJLjava/lang/Object;)Ljava/lang/Object;
149+
*/
150+
JNIEXPORT jobject JNICALL Java_org_ros2_rcljava_RCLJava_nativeTakeRequest
151+
(JNIEnv *, jclass, jlong, jlong, jlong, jobject);
152+
153+
/*
154+
* Class: org_ros2_rcljava_RCLJava
155+
* Method: nativeSendServiceResponse
156+
* Signature: (JLjava/lang/Object;JJLjava/lang/Object;)V
157+
*/
158+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeSendServiceResponse
159+
(JNIEnv *, jclass, jlong, jobject, jlong, jlong, jobject);
160+
161+
/*
162+
* Class: org_ros2_rcljava_RCLJava
163+
* Method: nativeSendClientRequest
164+
* Signature: (JJLjava/lang/Object;JJ)V
165+
*/
166+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_RCLJava_nativeSendClientRequest
167+
(JNIEnv *, jclass, jlong, jlong, jlong, jlong, jobject);
168+
169+
/*
170+
* Class: org_ros2_rcljava_RCLJava
171+
* Method: nativeTakeResponse
172+
* Signature: (JJJLjava/lang/Object;)Ljava/lang/Object;
173+
*/
174+
JNIEXPORT jobject JNICALL Java_org_ros2_rcljava_RCLJava_nativeTakeResponse
175+
(JNIEnv *, jclass, jlong, jlong, jlong, jobject);
111176

112177
#ifdef __cplusplus
113178
}
114179
#endif
180+
115181
#endif // ORG_ROS2_RCLJAVA_RCLJAVA_H_
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 Esteve Fernandez <esteve@apache.org>
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <jni.h>
16+
17+
#include <cassert>
18+
#include <cstdlib>
19+
#include <string>
20+
21+
#include "rmw/rmw.h"
22+
#include "rcl/error_handling.h"
23+
#include "rcl/rcl.h"
24+
#include "rcl/node.h"
25+
#include "rosidl_generator_c/message_type_support.h"
26+
27+
#include "rcljava_common/exceptions.h"
28+
#include "rcljava_common/signatures.h"
29+
30+
#include "org_ros2_rcljava_Client.h"
31+
32+
JNIEXPORT void JNICALL Java_org_ros2_rcljava_Client_nativeSendClientRequest(JNIEnv * env, jclass,
33+
jlong client_handle, jlong sequence_number, jlong jrequest_from_java_converter_handle,
34+
jlong jrequest_to_java_converter_handle, jobject jrequest_msg)
35+
{
36+
assert(client_handle != 0);
37+
assert(jrequest_from_java_converter_handle != 0);
38+
assert(jrequest_to_java_converter_handle != 0);
39+
assert(jrequest_msg != nullptr);
40+
41+
rcl_client_t * client = reinterpret_cast<rcl_client_t *>(client_handle);
42+
43+
convert_from_java_signature convert_from_java =
44+
reinterpret_cast<convert_from_java_signature>(jrequest_from_java_converter_handle);
45+
46+
void * request_msg = convert_from_java(jrequest_msg, nullptr);
47+
48+
rcl_ret_t ret = rcl_send_request(client, request_msg, &sequence_number);
49+
50+
if (ret != RCL_RET_OK) {
51+
rcljava_throw_exception(
52+
env, "java/lang/IllegalStateException",
53+
"Failed to send request from a client: " + std::string(rcl_get_error_string_safe()));
54+
}
55+
}

rcljava/src/main/cpp/org_ros2_rcljava_Node.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,81 @@ JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateSubscriptionHandl
102102
jlong jsubscription = reinterpret_cast<jlong>(subscription);
103103
return jsubscription;
104104
}
105+
106+
JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateServiceHandle(JNIEnv * env, jclass,
107+
jlong node_handle, jclass jservice_class, jstring jservice_name)
108+
{
109+
jmethodID mid = env->GetStaticMethodID(jservice_class, "getServiceTypeSupport", "()J");
110+
111+
assert(mid != NULL);
112+
113+
jlong jts = env->CallStaticLongMethod(jservice_class, mid);
114+
115+
assert(jts != 0);
116+
117+
const char * service_name_tmp = env->GetStringUTFChars(jservice_name, 0);
118+
119+
std::string service_name(service_name_tmp);
120+
121+
env->ReleaseStringUTFChars(jservice_name, service_name_tmp);
122+
123+
rcl_node_t * node = reinterpret_cast<rcl_node_t *>(node_handle);
124+
125+
rosidl_service_type_support_t * ts =
126+
reinterpret_cast<rosidl_service_type_support_t *>(jts);
127+
128+
rcl_service_t * service = static_cast<rcl_service_t *>(malloc(sizeof(rcl_service_t)));
129+
service->impl = NULL;
130+
rcl_service_options_t service_ops = rcl_service_get_default_options();
131+
132+
rcl_ret_t ret = rcl_service_init(service, node, ts, service_name.c_str(), &service_ops);
133+
134+
if (ret != RCL_RET_OK) {
135+
rcljava_throw_exception(
136+
env, "java/lang/IllegalStateException",
137+
"Failed to create service: " + std::string(rcl_get_error_string_safe()));
138+
return 0;
139+
}
140+
141+
jlong jservice = reinterpret_cast<jlong>(service);
142+
return jservice;
143+
}
144+
145+
JNIEXPORT jlong JNICALL Java_org_ros2_rcljava_Node_nativeCreateClientHandle(JNIEnv * env, jclass,
146+
jlong node_handle, jclass jservice_class, jstring jservice_name)
147+
{
148+
jmethodID mid = env->GetStaticMethodID(jservice_class, "getServiceTypeSupport", "()J");
149+
150+
assert(mid != NULL);
151+
152+
jlong jts = env->CallStaticLongMethod(jservice_class, mid);
153+
154+
assert(jts != 0);
155+
156+
const char * service_name_tmp = env->GetStringUTFChars(jservice_name, 0);
157+
158+
std::string service_name(service_name_tmp);
159+
160+
env->ReleaseStringUTFChars(jservice_name, service_name_tmp);
161+
162+
rcl_node_t * node = reinterpret_cast<rcl_node_t *>(node_handle);
163+
164+
rosidl_service_type_support_t * ts =
165+
reinterpret_cast<rosidl_service_type_support_t *>(jts);
166+
167+
rcl_client_t * client = static_cast<rcl_client_t *>(malloc(sizeof(rcl_client_t)));
168+
client->impl = NULL;
169+
rcl_client_options_t client_ops = rcl_client_get_default_options();
170+
171+
rcl_ret_t ret = rcl_client_init(client, node, ts, service_name.c_str(), &client_ops);
172+
173+
if (ret != RCL_RET_OK) {
174+
rcljava_throw_exception(
175+
env, "java/lang/IllegalStateException",
176+
"Failed to create client: " + std::string(rcl_get_error_string_safe()));
177+
return 0;
178+
}
179+
180+
jlong jclient = reinterpret_cast<jlong>(client);
181+
return jclient;
182+
}

0 commit comments

Comments
 (0)