jni.cc
Go to the documentation of this file.
1 // Copyright 2017 Intermodalics All Rights Reserved.
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 #include <string>
15 
16 #include <jni.h>
17 
18 #include <glog/logging.h>
20 #include <nodelet/loader.h>
21 #include <pluginlib/class_loader.h>
22 #include <ros/ros.h>
23 
24 #include "tango_helper.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 JNIEXPORT jboolean JNICALL
32  JNIEnv* env, jclass /*class*/, jobject binder) {
33  return tango_helper::SetBinder(env, binder);
34 }
35 
36 JNIEXPORT jboolean JNICALL
38  JNIEnv* env, jclass /*class*/, jobject activity) {
39  return tango_helper::IsTangoVersionOk(env, activity);
40 }
41 
42 JNIEXPORT jint JNICALL
44  JNIEnv* env, jobject /*obj*/, jstring master_uri_value,
45  jstring host_ip_value, jstring node_name_value,
46  jobjectArray remapping_objects_value) {
47  const char* master_uri = env->GetStringUTFChars(master_uri_value, NULL);
48  const char* host_ip = env->GetStringUTFChars(host_ip_value, NULL);
49  const char* node_name = env->GetStringUTFChars(node_name_value, NULL);
50  int argc = 3;
51  std::string master_uri_string("__master:=" + std::string(master_uri));
52  std::string host_ip_string("__ip:=" + std::string(host_ip));
53  const std::string node_name_string(node_name);
54  char* argv[] = {"/", &master_uri_string[0], &host_ip_string[0]};
55 
56  env->ReleaseStringUTFChars(master_uri_value, master_uri);
57  env->ReleaseStringUTFChars(host_ip_value, host_ip);
58  env->ReleaseStringUTFChars(node_name_value, node_name);
59 
60  std::map<std::string, std::string> remappings;
61  if (remapping_objects_value == NULL || env->GetArrayLength(remapping_objects_value) == 0) {
62  LOG(INFO) << "No remapping to be done.";
63  } else {
64  int remappingStringCount = env->GetArrayLength(remapping_objects_value);
65  for (int i = 0; i < remappingStringCount; ++i) {
66  jstring remap_arg_value = (jstring) (env->GetObjectArrayElement(remapping_objects_value, i));
67  const char* remap_arg = env->GetStringUTFChars(remap_arg_value, NULL);
68  // Parse remapping argument to extract old and new names.
69  // According to ROS doc, the syntax for remapping arguments is: old_name:=new_name.
70  // See http://wiki.ros.org/Remapping%20Arguments.
71  std::string remap_arg_string = std::string(remap_arg);
72  std::string delimiter = ":=";
73  size_t delimiter_position = remap_arg_string.find(delimiter);
74  if (delimiter_position == std::string::npos) {
75  LOG(ERROR) << "Invalid remapping argument: " << remap_arg << ". The correct syntax is old_name:=new_name.";
76  return 1;
77  }
78  std::string remap_old_name = remap_arg_string.substr(0, delimiter_position);
79  remap_arg_string.erase(0, delimiter_position + delimiter.length());
80  std::string remap_new_name = remap_arg_string;
81  remappings.insert(std::pair<std::string, std::string>(remap_old_name, remap_new_name));
82  LOG(INFO) << "Remapping " << remap_old_name << " to " << remap_new_name;
83  env->ReleaseStringUTFChars(remap_arg_value, remap_arg);
84  }
85  }
86 
87  ros::init(argc, argv, node_name_string.c_str());
88  nodelet::Loader loader;
89  std::vector<std::string> nodelet_argv;
90 
91  LOG(INFO) << "Start loading nodelets.";
92  const bool result = loader.load("/tango", "tango_ros_native/TangoRosNodelet", remappings, nodelet_argv);
93  if (!result) {
94  LOG(ERROR) << "Problem loading Tango ROS nodelet!";
95  return 1;
96  }
97  LOG(INFO) << "Finished loading nodelets.";
98 
99  // Check that all necessary plugins are available.
100  pluginlib::ClassLoader<image_transport::PublisherPlugin> image_transport_pub_loader("image_transport", "image_transport::PublisherPlugin");
101  if (!image_transport_pub_loader.isClassAvailable("image_transport/raw_pub")) {
102  LOG(ERROR) << "Plugin image_transport/raw_pub is not available.";
103  return 1;
104  }
105  if (!image_transport_pub_loader.isClassAvailable("image_transport/compressed_pub")) {
106  LOG(ERROR) << "Plugin image_transport/compressed_pub is not available.";
107  return 1;
108  }
109 
111  spinner.start();
113  return 0;
114 }
115 
116 JNIEXPORT jint JNICALL
118  JNIEnv* /*env*/, jobject /*obj*/) {
119  return 0;
120 }
121 
122 #ifdef __cplusplus
123 }
124 #endif
JNIEXPORT jint JNICALL Java_eu_intermodalics_nodelet_1manager_TangoNodeletManager_execute(JNIEnv *env, jobject, jstring master_uri_value, jstring host_ip_value, jstring node_name_value, jobjectArray remapping_objects_value)
Definition: jni.cc:43
bool IsTangoVersionOk(JNIEnv *env, jobject activity)
Definition: tango_helper.cc:21
std::string master_uri
std::string host_ip
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void spinner()
bool SetBinder(JNIEnv *env, jobject binder)
Definition: tango_helper.cc:33
JNIEXPORT jint JNICALL Java_eu_intermodalics_nodelet_1manager_TangoNodeletManager_shutdown(JNIEnv *, jobject)
Definition: jni.cc:117
JNIEXPORT jboolean JNICALL Java_eu_intermodalics_nodelet_1manager_TangoInitializationHelper_isTangoVersionOk(JNIEnv *env, jclass, jobject activity)
Definition: jni.cc:37
JNIEXPORT jboolean JNICALL Java_eu_intermodalics_nodelet_1manager_TangoInitializationHelper_setBinderTangoService(JNIEnv *env, jclass, jobject binder)
Definition: jni.cc:31
virtual bool isClassAvailable(const std::string &lookup_name)
ROSCPP_DECL void waitForShutdown()


TangoRosStreamer
Author(s):
autogenerated on Mon Jun 10 2019 15:37:54