IceProxyFinder.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Mirko Waechter ( mirko.waechter at kit dot edu)
18* @date 2014
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "IceProxyFinder.h"
24
25#include <QLineEdit>
26#include <QtGui>
27
28#include <Ice/Initialize.h>
29#include <IceStorm/IceStorm.h>
30
35
36#include <ArmarXGui/libraries/ArmarXGuiBase/ui_IceProxyFinder.h>
37
38namespace armarx
39{
46
48 QWidget(parent), ui(new ::Ui::IceProxyFinder), impl(new Impl)
49 {
50 setupUi();
51 }
52
54 {
55 if (impl->refreshProxyListTask)
56 {
57 impl->refreshProxyListTask->stop(true);
58 }
59 delete ui;
60 }
61
62 void
63 IceProxyFinderBase::setIceManager(IceManagerPtr icemanager, bool fetchProxies)
64 {
65 this->impl->icemanager = icemanager;
66 if (fetchProxies)
67 {
69 }
70 }
71
72 QString
74 {
75 return ui->cbProxyName->currentText();
76 }
77
78 void
80 {
81 ui->cbProxyName->setEditText(proxyName);
82 }
83
84 void
86 {
87 impl->defaultItem = proxyName;
88 }
89
90 void
91 IceProxyFinderBase::setSearchMask(const QString& searchMask)
92 {
93 ui->edtSearchMask->setText(searchMask);
94 }
95
96 void
98 {
99 ui->edtSearchMask->setVisible(show);
100 ui->labelMask->setVisible(show);
101 }
102
103 void
105 {
106 ui->labelMask->setVisible(show);
107 ui->labelProxy->setVisible(show);
108 }
109
110 ::Ui::IceProxyFinder*
112 {
113 return ui;
114 }
115
116 QWidget*
118 {
119 return ui->cbProxyName;
120 }
121
122 QStringList
124 {
125 if (searchMask.length() == 0)
126 {
127 searchMask = "*";
128 }
129
130 Ice::StringSeq proxyList;
131 QStringList qProxyList;
132 if (!impl->icemanager)
133 {
134 throw LocalException("IceManager must not be NULL");
135 }
136 auto searchMasks = Split(searchMask.toStdString(), "|");
137 for (auto& subSearchMasks : searchMasks)
138 {
139 if (impl->icemanager && impl->icemanager->getIceGridSession())
140 {
141
142 IceGridAdminPtr iceGridSession = impl->icemanager->getIceGridSession();
143 IceGrid::AdminPrx admin = iceGridSession->getAdmin();
144 Ice::AsyncResultPtr localBegin_getAllObjectInfos =
145 admin->begin_getAllObjectInfos(subSearchMasks);
146 while (!localBegin_getAllObjectInfos->isCompleted())
147 {
148 if (impl->refreshProxyListTask->isStopped())
149 {
150 return qProxyList;
151 }
152 usleep(10000);
153 }
154 IceGrid::ObjectInfoSeq objects =
155 admin->end_getAllObjectInfos(localBegin_getAllObjectInfos);
156 Ice::StringSeq tempProxyList;
157
158 for (auto& object : objects)
159 {
160 // if objects are hangig we might get connection refused
161 try
162 {
163 if (impl->refreshProxyListTask->isStopped())
164 {
165 return qProxyList;
166 }
167 std::string typedName = getTypedProxyName(object.proxy);
168 if (!typedName.empty())
169 {
170 tempProxyList.push_back(typedName);
171 }
172 }
173 catch (...)
174 {
175 }
176 }
177
178
179 proxyList.insert(proxyList.end(), tempProxyList.begin(), tempProxyList.end());
180 }
181 }
182
183
184 for (unsigned int i = 0; i < proxyList.size(); ++i)
185 {
186 qProxyList << QString::fromStdString(proxyList.at(i));
187 }
188 qProxyList.removeDuplicates();
189 return qProxyList;
190 }
191
192 void
194 {
195 if (impl->refreshProxyListTask && impl->refreshProxyListTask->isRunning())
196 {
197 return;
198 }
199
200 ui->btnRefresh->setEnabled(false);
201 ui->btnRefresh->setToolTip("Refreshing proxy list with mask '" + ui->edtSearchMask->text() +
202 "'. Please wait...");
203 impl->refreshProxyListTask =
205 impl->refreshProxyListTask->start();
206 }
207
208 void
213
214 void
216 {
217 ui->cbProxyName->clear();
218 if (not impl->defaultItem.isEmpty())
219 {
220 ui->cbProxyName->addItem(impl->defaultItem);
221 ui->cbProxyName->setCurrentIndex(0);
222 }
223 int i = 0;
224
225 for (; i < proxyList.size(); ++i)
226 {
227 if (proxyList.at(i) != impl->defaultItem)
228 {
229 ui->cbProxyName->addItem(proxyList.at(i));
230 }
231 }
232
233 ui->btnRefresh->setEnabled(true);
234 ui->btnRefresh->setToolTip(
235 "Press to refetch all available Kinematic Units with the given mask");
236 }
237
238 void
240 {
241 try
242 {
243 // Try to get the object proxy "objPrx" represented by "proxyName"
244 IceGrid::AdminPrx admin = this->impl->icemanager->getIceGridSession()->getAdmin();
245 Ice::Identity objectIceId = Ice::stringToIdentity(proxyName.toStdString());
246 Ice::ObjectPrx objPrx = admin->getObjectInfo(objectIceId).proxy;
247
248 // Try to ping the proxy (it could still be a stale entry in IceGrid)
249 objPrx->ice_ping();
250
251 // If this point is reached, the object represented by "proxyName" is valid and reachable via Ice - that is, the signal can be emitted
252 emit validProxySelected(proxyName);
253 }
254 catch (...)
255 {
256 // pass: Any exception triggered in the try-block indicates that "proxyName" is not a valid indicator for an object known to Ice
257 }
258 }
259
260 void
262 {
263 auto result = getProxyNameList(ui->edtSearchMask->text());
264 if (!impl->refreshProxyListTask->isStopped())
265 {
267 }
268 }
269
270 void
272 {
273 this->ui->setupUi(this);
274
275 this->connect(this,
276 SIGNAL(updateProxyListComboBoxSignal(QStringList)),
277 this,
278 SLOT(updateProxyListComboBox(QStringList)));
279
280 // These signals should trigger an update of the proxy list
281 this->connect(
282 this->ui->btnRefresh, SIGNAL(clicked()), this, SLOT(triggerProxyListUpdate()));
283 this->connect(this->ui->edtSearchMask,
284 SIGNAL(textEdited(QString)),
285 this,
286 SLOT(triggerProxyListUpdate()));
287 this->connect(this,
289 this,
290 SLOT(triggerProxyListUpdate()));
291
292 // If the combobox changes in any way (text manually updated or new item selected) it should be evaluated whether
293 // the "validProxySelected" signal should be emitted
294 this->connect(this->ui->cbProxyName,
295 SIGNAL(editTextChanged(QString)),
296 this,
297 SLOT(evaluateValidProxySelectedSignal(QString)));
298 this->connect(this->ui->cbProxyName,
299 SIGNAL(currentIndexChanged(QString)),
300 this,
301 SLOT(evaluateValidProxySelectedSignal(QString)));
302 }
303
305 {
306 ui->labelProxy->setText("Topic");
307 }
308
309 QStringList
310 IceTopicFinder::getProxyNameList(QString searchMask) const
311 {
312 if (searchMask.length() == 0)
313 {
314 searchMask = "*";
315 }
316
317 IceStorm::TopicDict topicList;
318
319 if (!impl->icemanager)
320 {
321 throw LocalException("IceManager must not be NULL");
322 }
323
324 if (impl->icemanager && impl->icemanager->getIceGridSession())
325 {
326 topicList = impl->icemanager->getTopicManager()->retrieveAll();
327 }
328
329 QStringList qProxyList;
330
331 for (auto& e : topicList)
332 {
333 auto name = QString::fromStdString(e.first);
334 QRegExp rex(searchMask, Qt::CaseInsensitive, QRegExp::Wildcard);
335
336 if (name.contains(rex))
337 {
338 qProxyList << name;
339 }
340 }
341
342 return qProxyList;
343 }
344
345} // namespace armarx
void updateProxyListComboBoxSignal(const QStringList &proxyList)
IceProxyFinderBase(QWidget *parent=0)
void showSearchMaskField(bool show)
QStringList getProxyNameListBase(QString searchMask) const
virtual QStringList getProxyNameList(QString searchMask) const =0
void setDefaultSelectedProxy(const QString &proxyName)
void setDefaultSelectedProxyAsItem(const QString &proxyName)
void evaluateValidProxySelectedSignal(const QString &proxyName)
void triggerProxyListComboBoxUpdateSignal()
void validProxySelected(const QString &proxyName)
Ui::IceProxyFinder * getUi()
std::unique_ptr< Impl > impl
void setIceManager(IceManagerPtr icemanager, bool fetchProxies=true)
void setSearchMask(const QString &searchMask)
void comboBoxTextChanged(QString currentText)
virtual std::string getTypedProxyName(Ice::ObjectPrx const &current) const
QString getSelectedProxyName() const
void updateProxyListComboBox(const QStringList &proxyList)
Ui::IceProxyFinder * ui
Widget to conveniently retrieve a proxy instance name of a specific interface type (the template para...
IceTopicFinder(QWidget *parent=0)
QStringList getProxyNameList(QString searchMask) const override
IceUtil::Handle< RunningTask< T > > pointer_type
Shared pointer type for convenience.
ArmarX Headers.
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< IceGridAdmin > IceGridAdminPtr
Definition ArmarXFwd.h:48
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceUtil::Handle< IceManager > IceManagerPtr
IceManager smart pointer.
Definition ArmarXFwd.h:39
RunningTask< IceProxyFinderBase >::pointer_type refreshProxyListTask