-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathorocos_cpp.cpp
194 lines (162 loc) · 6.29 KB
/
orocos_cpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "orocos_cpp.hpp"
#include <orocos_cpp/CorbaNameService.hpp>
#include <rtt/transports/corba/TaskContextServer.hpp>
#include <lib_config/YAMLConfiguration.hpp>
#include <boost/lexical_cast.hpp>
#include "PluginHelper.hpp"
namespace orocos_cpp {
//Taken from /media/wirkus/Data/development/rock-runtime/tools/orocos.rb/ext/rorocos/corba_name_service_client.cc
CosNaming::NamingContext_var getNameService(const std::string name_service_ip, const std::string name_service_port)
{
if(CORBA::is_nil(RTT::corba::ApplicationServer::orb))
throw std::runtime_error("Corba is not initialized. Call Orocos.initialize first.");
CosNaming::NamingContext_var rootContext;
// Obtain reference to Root POA.
CORBA::Object_var obj_poa = RTT::corba::ApplicationServer::orb->resolve_initial_references("RootPOA");
PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj_poa);
if(CORBA::is_nil(root_poa))
throw std::runtime_error("Failed to narrow poa context.");
// activate poa manager
root_poa->the_POAManager()->activate();
// Obtain reference to NameServiceClient
CORBA::Object_var obj;
if(!name_service_ip.empty())
{
std::string temp("corbaloc::");
temp = temp + name_service_ip;
if(!name_service_port.empty())
temp = temp + ":" + name_service_port;
temp = temp +"/NameService";
obj = RTT::corba::ApplicationServer::orb->string_to_object(temp.c_str());
}
else
obj = RTT::corba::ApplicationServer::orb->resolve_initial_references("NameService");
try{
rootContext = CosNaming::NamingContext::_narrow(obj.in());
}catch(CORBA::TRANSIENT& ex){
std::cerr << "CORBA::TRANSIENT received while trying to obtain reference to NameService Client" << std::endl;
}
if(CORBA::is_nil(rootContext)){
std::cerr << "Failed to narrow NameService context." << std::endl;
return nullptr;
}
return rootContext;
}
bool validateNameServiceClient(std::string hostname, std::string port=""){
return getNameService(hostname, port);
}
bool set_env(std::string var, std::string value, bool overwrite=true){
if( setenv(var.c_str(), value.c_str(), overwrite) !=0 )
{
fprintf(stderr,"putenv failed\n");
return false;
}
return true;
}
bool set_corba_ns_host(std::string hostname_or_ip){
return set_env("ORBInitRef", "NameService=corbaname::"+hostname_or_ip);
}
bool setMaxMessageSize(size_t bytes){
return set_env("ORBgiopMaxMsgSize", std::to_string(bytes));
}
bool initializeCORBA(int argc, char**argv, std::string host="", size_t max_message_size=DEFAULT_OROCOS_MAX_MESSAGE_SIZE)
{
//Set set CORBA nameserver
if(!host.empty()){
set_corba_ns_host(host);
}
//Set CORBA max message size only if it was not set by the user
setMaxMessageSize(max_message_size);
//Do the initialization
bool orb_st = RTT::corba::ApplicationServer::InitOrb(argc, argv);
if(!orb_st){
std::cerr << "\nError initializing CORBA application server" <<std::endl;
return false;
}
if(!validateNameServiceClient(host, "")){
std::cerr << "\nCould not connect to name service '"<<host<<"'" <<std::endl;
return false;
}
return true;
}
static std::string default_oro_log_file_path(){
return "orocos-"+boost::lexical_cast<std::string>(getpid())+".log";
}
bool OrocosCpp::initialize(const OrocosCppConfig& config, bool quiet)
{
bool st;
//Init CORBA
if(config.init_corba){
if(!quiet) std::cout << "Initializing CORBA.. " << std::endl;
st = initializeCORBA(0, {}, config.corba_host, config.max_message_size);
if(!st){
std::cerr << "Failed to initialze CORBA" << std::endl;
return false;
}
}
//Init PkgConfig Registry
if(!quiet) std::cout << "\nLoading Rock-packages.." << std::endl;
package_registry = PkgConfigRegistry::initialize(config.package_initialization_whitelist, config.load_all_packages);
if(!package_registry){
std::cerr << "Error initializing Rock-packages" <<std::endl;
return false;
}
// Set orocos log file
if(config.oro_log_file_path == ""){
set_env("ORO_LOGFILE", default_oro_log_file_path(), true);
}else{
set_env("ORO_LOGFILE", config.oro_log_file_path, true);
}
//Init Bundle
bundle.reset(new Bundle()); //We want a valid pointer also if we don't initialize the bundle
if(config.init_bundle){
if(!quiet) std::cout << "\nInitializing Bundle.." << std::endl;
st = bundle->initialize(config.load_task_configs);
if(!st){
std::cerr << "Error during initialization of Bundle" << std::endl;
return false;
}
if(config.create_log_folder){
if(config.oro_log_file_path != ""){
set_env("ORO_LOGFILE", bundle->getLogDirectory()+"/orocos-"+boost::lexical_cast<std::string>(getpid())+".log", true);
}
}
}
//Load Typekits
if(config.load_typekits){
if(!quiet) std::cout << "\nLoading Typekits.." << std::endl;
for(std::string tkn : package_registry->getRegisteredTypekitNames())
{
try{
PluginHelper::loadTypekitAndTransports(tkn);
}catch(std::runtime_error& ex){
std::cerr << ex.what() << std::endl;
}
}
}
//Init Type Registry
type_registry = TypeRegistryPtr(new TypeRegistry(package_registry));
if(config.init_type_registry){
if(!quiet) std::cout << "\nLoading Type Registry.." << std::endl;
st = type_registry->loadTypeRegistries();
if(!st){
std::cerr << "Error during initialization of TypeRegistry" << std::endl;
return false;
}
}
if(!quiet) std::cout << "\nOrocosCPP initialization complete!"<<std::endl;
return true;
}
RTT::corba::TaskContextProxy *OrocosCpp::getTaskContext(std::string name)
{
return RTT::corba::TaskContextProxy::Create(name);
}
bool OrocosCpp::loadAllTypekitsForModel(std::string packageOrTaskModelName)
{
return orocos_cpp::PluginHelper::loadAllTypekitsForModel(packageOrTaskModelName);
}
std::string OrocosCpp::applyStringVariableInsertions(const std::string &cnd_yaml)
{
return libConfig::YAMLConfigParser::applyStringVariableInsertions(cnd_yaml);
}
}