-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCorbaNameService.cpp
More file actions
207 lines (175 loc) · 6.21 KB
/
Copy pathCorbaNameService.cpp
File metadata and controls
207 lines (175 loc) · 6.21 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "CorbaNameService.hpp"
#include <rtt/transports/corba/TaskContextProxy.hpp>
#include <rtt/transports/corba/TaskContextC.h>
#include <stdexcept>
#include <iostream>
using namespace orocos_cpp;
CorbaNameService::CorbaNameService(std::string name_service_ip, std::string name_service_port) : ip(name_service_ip), port(name_service_port)
{
}
bool CorbaNameService::initOrb()
{
if ( !CORBA::is_nil(orb) )
return false;
try {
int argc = 0;
char **argv = nullptr;
// First initialize the ORB, that will remove some arguments...
orb = CORBA::ORB_init (argc, const_cast<char**>(argv),
"omniORB4");
}
catch (CORBA::Exception &e) {
std::cout << "Orb Init : CORBA exception raised!" << std::endl;
std::cout << CORBA_EXCEPTION_INFO(e) << std::endl;
return false;
}
return true;
}
bool CorbaNameService::isConnected()
{
return !CORBA::is_nil(orb);
}
bool CorbaNameService::connect()
{
if(CORBA::is_nil(orb))
{
if(!initOrb())
{
throw std::runtime_error("CorbaNameService::Error, failed to initialize CORBA Orb");
}
}
// NameService
try {
if(ip.empty())
{
rootObj = orb->resolve_initial_references("NameService");
}
else{
std::string temp("corbaloc::");
temp = temp + ip;
if(!port.empty())
temp = temp + ":" + port;
temp = temp +"/NameService";
rootObj = orb->string_to_object(temp.c_str());
}
} catch (...) {
return false;
}
try {
rootContext = CosNaming::NamingContext::_narrow(rootObj);
} catch (...) {
return false;
}
if (CORBA::is_nil(rootContext)) {
std::string err("TaskContextProxy could not acquire NameService.");
std::cerr << err << std::endl;
throw std::runtime_error(err);
}
return true;
}
std::vector< std::string > CorbaNameService::getRegisteredTasks()
{
if(CORBA::is_nil(orb))
{
throw std::runtime_error("CorbaNameService::Error, called getRegisteredTasks() without connection " );
}
CosNaming::Name server_name;
server_name.length(1);
server_name[0].id = CORBA::string_dup("TaskContexts");
std::vector<std::string> task_names;
CosNaming::BindingList_var binding_list;
CosNaming::BindingIterator_var binding_it;
// get all available task names from the name server
CORBA::Object_var control_tasks_var = rootContext->resolve(server_name);
CosNaming::NamingContext_var control_tasks = CosNaming::NamingContext::_narrow (control_tasks_var);
if (CORBA::is_nil(control_tasks))
return task_names;
control_tasks->list(0, binding_list, binding_it);
if (CORBA::is_nil(binding_it))
return task_names;
// iterate over all task names
while(binding_it->next_n(10, binding_list))
{
CosNaming::BindingList list = binding_list.in();
for (unsigned int i = 0; i < list.length(); ++i)
{
std::string name = list[i].binding_name[0].id.in();
CosNaming::Name serverName;
serverName.length(2);
serverName[0].id = CORBA::string_dup("TaskContexts");
serverName[1].id = CORBA::string_dup( name.c_str() );
try {
//verify that the object really exists, and is not just some leftover from a crash
// Get object reference
CORBA::Object_var task_object = rootContext->resolve(serverName);
RTT::corba::CTaskContext_var mtask = RTT::corba::CTaskContext::_narrow (task_object.in ());
//This is a hack: orocosrb-tasks can make the program execution stall (wait forever),
//if they are ghost and it is tried to connect to them via getName,
//isActive or so. this is true at least in the case where they are retrieved from remote.
//This hack omits all orocosrb tasks, because I don't kow about a better way
//to omit these tasks.
if(name.find("orocosrb") != std::string::npos )
continue;
// force connect to object.
CORBA::String_var nm = mtask->getName();
task_names.push_back(name);
}
catch (...)
{
}
}
}
return task_names;
}
bool CorbaNameService::isRegistered(const std::string& taskName)
{
if(CORBA::is_nil(orb))
{
throw std::runtime_error("CorbaNameService::Error, called getTaskContext() without connection " );
}
CosNaming::Name serverName;
serverName.length(2);
serverName[0].id = CORBA::string_dup("TaskContexts");
serverName[1].id = CORBA::string_dup( taskName.c_str() );
try {
// Get object reference
CORBA::Object_var task_object = rootContext->resolve(serverName);
if(CORBA::is_nil(task_object))
return false;
RTT::corba::CTaskContext_var mtask = RTT::corba::CTaskContext::_narrow (task_object.in ());
if ( CORBA::is_nil( mtask ) ) {
return false;
}
// force connect to object.
//this needs to be done. If not, we may return a ghost task
CORBA::String_var nm = mtask->getName();
} catch (...)
{
return false;
}
return true;
}
RTT::TaskContext* CorbaNameService::getTaskContext(const std::string& taskName)
{
if(CORBA::is_nil(orb))
{
throw std::runtime_error("CorbaNameService::Error, called getTaskContext() without connection " );
}
CosNaming::Name serverName;
serverName.length(2);
serverName[0].id = CORBA::string_dup("TaskContexts");
serverName[1].id = CORBA::string_dup( taskName.c_str() );
// Get object reference
CORBA::Object_var task_object = rootContext->resolve(serverName);
CORBA::String_var s = orb->object_to_string(task_object);
RTT::TaskContext *ret = nullptr;
try
{
ret = RTT::corba::TaskContextProxy::Create(s.in(), true);;
}
catch (...)
{
std::cout << "Ghost " << taskName << std::endl;
}
return ret;
}