Skip to content
Closed
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
102 changes: 82 additions & 20 deletions rcljava/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,58 @@ find_package(rmw REQUIRED)
find_package(rmw_implementation_cmake REQUIRED)

if (ANDROID)
find_host_package(Java COMPONENTS Development)
# https://cmake.org/cmake/help/v3.0/module/FindJava.html?highlight=java
find_host_package(Java 1.6 COMPONENTS Development)
else()
find_package(Java COMPONENTS Development)
# https://cmake.org/cmake/help/v3.0/module/FindJava.html?highlight=java
find_package(Java 1.6 COMPONENTS Development)

# https://cmake.org/cmake/help/v3.0/module/FindJNI.html?highlight=jni
find_package(JNI REQUIRED)
endif()

# https://cmake.org/cmake/help/v3.0/module/UseJava.html?highlight=java
include (UseJava)

### Definitions ###
set(CMAKE_JAVA_COMPILE_FLAGS "-source" "1.6" "-target" "1.6")

if(NOT WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra")
endif()

find_jar(JUNIT_JAR
NAMES
junit
PATHS
/usr/share/java
VERSIONS
4
)

# Bug in find_jar for Junit4 : https://sourceforge.net/p/rdkit/mailman/message/35301727/
set(JUNIT_JAR "/usr/share/java/junit4.jar")

set(${PROJECT_NAME}_sources
"src/main/java/org/ros2/rcljava/Node.java"
"src/main/java/org/ros2/rcljava/RCLJava.java"
"src/main/java/org/ros2/rcljava/Publisher.java"
"src/main/java/org/ros2/rcljava/Subscription.java"
"src/main/java/org/ros2/rcljava/Consumer.java"
"src/main/java/org/ros2/rcljava/Log.java"
"src/main/java/org/ros2/rcljava/QoSProfile.java"
"src/main/java/org/ros2/rcljava/NativeUtils.java"
"src/main/java/org/ros2/rcljava/exception/ImplementationAlreadyImportedException.java"
"src/main/java/org/ros2/rcljava/exception/InvalidRCLJAVAImplementation.java"
"src/main/java/org/ros2/rcljava/exception/NoImplementationAvailableException.java"
"src/main/java/org/ros2/rcljava/exception/NotInitializedException.java"

"src/test/java/org/ros2/rcljava/test/NodeTest.java"
"src/test/java/org/ros2/rcljava/test/RCLJavaTest.java"
"src/test/java/org/ros2/rcljava/test/RmwTest.java"
)

### Macro ###
macro(target)
if(NOT "${target_suffix} " STREQUAL " ")
get_rcl_information("${rmw_implementation}" "rcl${target_suffix}")
Expand All @@ -31,15 +69,19 @@ macro(target)
set(_jni_classes "RCLJava;Node;Publisher")

foreach(_jni_class ${_jni_classes})

set(_target_name "${PROJECT_NAME}${_jni_class}${target_suffix}")

add_library(
${_target_name}
SHARED src/main/cpp/org_ros2_rcljava_${_jni_class}.cpp
SHARED
src/main/cpp/org_ros2_rcljava_${_jni_class}.cpp
)

target_compile_definitions(
${_target_name}
PRIVATE
"RMW_IMPLEMENTATION_SUFFIX=${target_suffix}"
)
target_compile_definitions(${_target_name}
PRIVATE "RMW_IMPLEMENTATION_SUFFIX=${target_suffix}")

if(WIN32)
set(_build_types ";_DEBUG;_MINSIZEREL;_RELEASE;_RELWITHDEBINFO")
Expand All @@ -48,19 +90,22 @@ macro(target)
endif()

foreach(_build_type ${_build_types})
set_target_properties(${_target_name} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY${_build_type} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}"
RUNTIME_OUTPUT_DIRECTORY${_build_type} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}"
set_target_properties(
${_target_name}
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY${_build_type} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}"
RUNTIME_OUTPUT_DIRECTORY${_build_type} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}"
)
endforeach()

ament_target_dependencies(${_target_name}
"rcl${target_suffix}"
)

target_include_directories(${_target_name}
target_include_directories(
${_target_name}
PUBLIC
${JNI_INCLUDE_DIRS}
${JNI_INCLUDE_DIRS}
)

if(NOT rosidl_generate_interfaces_SKIP_INSTALL)
Expand All @@ -76,28 +121,44 @@ macro(target)
LIBRARY DESTINATION lib
)
endif()

endforeach()

endmacro()

set(${PROJECT_NAME}_sources
"src/main/java/org/ros2/rcljava/Node.java"
"src/main/java/org/ros2/rcljava/RCLJava.java"
"src/main/java/org/ros2/rcljava/Publisher.java"
"src/main/java/org/ros2/rcljava/Subscription.java"
"src/main/java/org/ros2/rcljava/Consumer.java"
)

### Main ###
add_jar("${PROJECT_NAME}_jar"
SOURCES
"${${PROJECT_NAME}_sources}"
INCLUDE_JARS
"${JUNIT_JAR}"
OUTPUT_NAME
"${PROJECT_NAME}"
)

set(TEST_COMMAND ${Java_JAVA_EXECUTABLE} ${JVMARGS} -classpath ${JUNIT_JAR}:${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/java/${PROJECT_NAME}.jar -Djava.library.path=${CMAKE_INSTALL_PREFIX}/lib/ org.junit.runner.JUnitCore)

# add_test(
# NAME RmwTest
# COMMAND ${TEST_COMMAND} org.ros2.rcljava.test.RmwTest
# )

add_test(
NAME RCLJavaTest
COMMAND ${TEST_COMMAND} org.ros2.rcljava.test.RCLJavaTest
)

# add_test(
# NAME NodeTest
# COMMAND ${TEST_COMMAND} org.ros2.rcljava.test.NodeTest
# )

## loop for all RMW target.
call_for_each_rmw_implementation(target)

### Testing ###
if(AMENT_ENABLE_TESTING)
enable_testing()

find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()

Expand All @@ -106,6 +167,7 @@ if(AMENT_ENABLE_TESTING)
)
endif()

### Install ###
if(NOT rosidl_generate_interfaces_SKIP_INSTALL)
set(_install_jar_dir "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}")
get_property(_jar_file TARGET "${PROJECT_NAME}_jar" PROPERTY "JAR_FILE")
Expand Down
5 changes: 5 additions & 0 deletions rcljava/src/main/java/org/ros2/rcljava/Consumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
package org.ros2.rcljava;

/**
*
*
* @author Esteve Fernandez <[email protected]>
*/
public interface Consumer<T> {
void accept(T t);
}
36 changes: 36 additions & 0 deletions rcljava/src/main/java/org/ros2/rcljava/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2016 Open Source Robotics Foundation, Inc.
*
* 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.
*/
package org.ros2.rcljava;

import org.ros2.rcljava.exception.NoImplementationAvailableException;

/**
* Not define in ROS2.
*
* @author Mickael Gaillard
*/
public class Log {

public Log() { }

public void fatal(Exception e) {
throw new NoImplementationAvailableException(e);
}

public void info(String string) {
throw new NoImplementationAvailableException(new Exception(string));
}

}
34 changes: 34 additions & 0 deletions rcljava/src/main/java/org/ros2/rcljava/NativeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.ros2.rcljava;

import java.util.Vector;

public abstract class NativeUtils {

private static java.lang.reflect.Field LIBRARIES;

static {
try {
LIBRARIES = ClassLoader.class.getDeclaredField("loadedLibraryNames");
LIBRARIES.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}

@SuppressWarnings("unchecked")
public static String[] getLoadedLibraries(final ClassLoader loader) {
Vector<String> libraries = new Vector<String>();

try {
libraries = (Vector<String>) LIBRARIES.get(loader);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

return libraries.toArray(new String[] {});
}
}
100 changes: 78 additions & 22 deletions rcljava/src/main/java/org/ros2/rcljava/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,105 @@
package org.ros2.rcljava;

import java.lang.ref.WeakReference;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
* <h1>Node ROS2.</h1>
* <p></p>
* @author Esteve Fernandez <[email protected]>
* @author Mickael Gaillard <[email protected]>
*/
public class Node {
static {
try {
System.loadLibrary("rcljavaNode__" + RCLJava.getRMWIdentifier());
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
private static Logger logger = Logger.getLogger(RCLJava.LOG_NAME);

static {
RCLJava.loadLibrary("rcljavaNode__" + RCLJava.getRMWIdentifier());
}

private long nodeHandle;
private List<Subscription> subscriptions = new ArrayList<Subscription>();
/** Node handler */
private final long nodeHandle;

/** List of subscriptions */
private final List<Subscription<?>> subscriptions;

// Native call.
private static native <T> long nativeCreatePublisherHandle(
long nodeHandle, Class<T> cls, String topic);

private static native <T> long nativeCreateSubscriptionHandle(
long nodeHandle, Class<T> cls, String topic);

/**
* Constructor of Node.
* @param nodeHandle Handler to the node.
*/
public Node(long nodeHandle) {
this.nodeHandle = nodeHandle;
this.subscriptions = new ArrayList<Subscription<?>>();
}

private static native <T> long nativeCreatePublisherHandle(
long nodeHandle, Class<T> cls, String topic);
/**
* <h1>Create a new publisher.</h1>
*
* @param <T> Message definition.
* @param message Message class.
* @param topic Topic to publish.
* @param qos QOS profile.
* @return Publisher instance.
*/
public <T> Publisher<T> createPublisher(Class<T> message, String topic, QoSProfile qos) {
logger.fine("Create Publisher : " + topic);
long publisherHandle = Node.nativeCreatePublisherHandle(this.nodeHandle, message, topic);

private static native <T> long nativeCreateSubscriptionHandle(
long nodeHandle, Class<T> cls, String topic);
Publisher<T> publisher = new Publisher<T>(this.nodeHandle, publisherHandle, message, topic, qos);
RCLJava.publisherReferences.add(new WeakReference<Publisher<?>>(publisher));

public <T> Publisher<T> createPublisher(Class<T> cls, String topic) {
long publisherHandle = nativeCreatePublisherHandle(this.nodeHandle, cls, topic);
Publisher<T> publisher = new Publisher<T>(this.nodeHandle, publisherHandle);
RCLJava.publisherReferences.add(new WeakReference<Publisher>(publisher));
return publisher;
}

public <T> Subscription<T> createSubscription(Class<T> cls, String topic, Consumer<T> callback) {
long subscriptionHandle = nativeCreateSubscriptionHandle(this.nodeHandle, cls, topic);
/**
* <h1>Create a new Subscriber with callback.</h1>
*
* @param <T> Message definition.
* @param message Message Class
* @param topic Topic to subscribe.
* @param callback Function to call on recieve.
* @param qos QOS profile.
* @return
*/
public <T> Subscription<T> createSubscription(
Class<T> message,
String topic,
Consumer<T> callback,
QoSProfile qos) {
logger.fine("Create Subscription : " + topic);
long subscriptionHandle = Node.nativeCreateSubscriptionHandle(this.nodeHandle, message, topic);

Subscription<T> subscription = new Subscription<T>(this.nodeHandle, subscriptionHandle, cls, topic, callback);
Subscription<T> subscription = new Subscription<T>(
this.nodeHandle,
subscriptionHandle,
message,
topic,
callback,
qos);
this.subscriptions.add(subscription);
return subscription;
}

public List<Subscription> getSubscriptions() {
/**
* Get list of Subscriptions.
* @return ArrayList of Subscriptions
*/
public List<Subscription<?>> getSubscriptions() {
return this.subscriptions;
}

/**
* Release all Publisher ressource.
*/
public void dispose() {
//TODO Implement on JNI
}
}
Loading