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 
38 namespace armarx
39 {
41  {
44  QString defaultItem;
45  };
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
210  {
212  }
213 
214  void
215  IceProxyFinderBase::updateProxyListComboBox(const QStringList& proxyList)
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  {
266  emit updateProxyListComboBoxSignal(result);
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
armarx::IceProxyFinderBase::~IceProxyFinderBase
~IceProxyFinderBase() override
Definition: IceProxyFinder.cpp:53
armarx::IceProxyFinderBase::Impl::defaultItem
QString defaultItem
Definition: IceProxyFinder.cpp:44
armarx::IceProxyFinderBase::setSearchMask
void setSearchMask(const QString &searchMask)
Definition: IceProxyFinder.cpp:91
armarx::IceProxyFinderBase::getUi
Ui::IceProxyFinder * getUi()
Definition: IceProxyFinder.cpp:111
armarx::IceProxyFinderBase::updateProxyList
void updateProxyList()
Definition: IceProxyFinder.cpp:261
armarx::IceTopicFinder::getProxyNameList
QStringList getProxyNameList(QString searchMask) const override
Definition: IceProxyFinder.cpp:310
armarx::IceProxyFinderBase::updateProxyListComboBoxSignal
void updateProxyListComboBoxSignal(const QStringList &proxyList)
LocalException.h
armarx::IceProxyFinderBase::setIceManager
void setIceManager(IceManagerPtr icemanager, bool fetchProxies=true)
Definition: IceProxyFinder.cpp:63
armarx::IceProxyFinderBase
The IceProxyFinderBase class provides a convenient way to query online proxies in the ice network,...
Definition: IceProxyFinder.h:52
armarx::IceProxyFinderBase::Impl
Definition: IceProxyFinder.cpp:40
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
armarx::IceProxyFinderBase::triggerProxyListComboBoxUpdateSignal
void triggerProxyListComboBoxUpdateSignal()
RunningTask.h
armarx::IceProxyFinderBase::IceProxyFinderBase
IceProxyFinderBase(QWidget *parent=0)
Definition: IceProxyFinder.cpp:47
armarx::IceProxyFinderBase::comboBoxTextChanged
void comboBoxTextChanged(QString currentText)
Definition: IceProxyFinder.cpp:209
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
armarx::IceProxyFinderBase::getTypedProxyName
virtual std::string getTypedProxyName(Ice::ObjectPrx const &current) const
Definition: IceProxyFinder.h:74
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::IceProxyFinderBase::setupUi
void setupUi()
Definition: IceProxyFinder.cpp:271
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
armarx::IceProxyFinderBase::Impl::refreshProxyListTask
RunningTask< IceProxyFinderBase >::pointer_type refreshProxyListTask
Definition: IceProxyFinder.cpp:43
IceProxyFinder.h
IceManager.h
armarx::IceTopicFinder::IceTopicFinder
IceTopicFinder(QWidget *parent=0)
Definition: IceProxyFinder.cpp:304
IceGridAdmin.h
armarx::IceProxyFinderBase::evaluateValidProxySelectedSignal
void evaluateValidProxySelectedSignal(const QString &proxyName)
Definition: IceProxyFinder.cpp:239
armarx::IceProxyFinderBase::setDefaultSelectedProxy
void setDefaultSelectedProxy(const QString &proxyName)
Definition: IceProxyFinder.cpp:79
armarx::IceProxyFinderBase::getProxyNameList
virtual QStringList getProxyNameList(QString searchMask) const =0
armarx::IceProxyFinderBase::getSelectedProxyName
QString getSelectedProxyName() const
Definition: IceProxyFinder.cpp:73
armarx::IceProxyFinderBase::impl
std::unique_ptr< Impl > impl
Definition: IceProxyFinder.h:97
armarx::IceProxyFinderBase::updateProxyListComboBox
void updateProxyListComboBox(const QStringList &proxyList)
Definition: IceProxyFinder.cpp:215
armarx::IceProxyFinderBase::getProxyNameComboBox
QWidget * getProxyNameComboBox()
Definition: IceProxyFinder.cpp:117
IceUtil::Handle< IceManager >
armarx::IceProxyFinder
Definition: StatechartViewerController.h:49
armarx::IceProxyFinderBase::Impl::icemanager
IceManagerPtr icemanager
Definition: IceProxyFinder.cpp:42
armarx::IceProxyFinderBase::validProxySelected
void validProxySelected(const QString &proxyName)
armarx::IceProxyFinderBase::showSearchMaskField
void showSearchMaskField(bool show)
Definition: IceProxyFinder.cpp:97
armarx::IceProxyFinderBase::triggerProxyListUpdate
void triggerProxyListUpdate()
Definition: IceProxyFinder.cpp:193
armarx::IceProxyFinderBase::ui
Ui::IceProxyFinder * ui
Definition: IceProxyFinder.h:96
armarx::IceProxyFinderBase::showLabels
void showLabels(bool show)
Definition: IceProxyFinder.cpp:104
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::IceProxyFinderBase::getProxyNameListBase
QStringList getProxyNameListBase(QString searchMask) const
Definition: IceProxyFinder.cpp:123
armarx::IceProxyFinderBase::setDefaultSelectedProxyAsItem
void setDefaultSelectedProxyAsItem(const QString &proxyName)
Definition: IceProxyFinder.cpp:85