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 <ArmarXGui/libraries/ArmarXGuiBase/ui_IceProxyFinder.h>
26 
31 
32 #include <IceStorm/IceStorm.h>
33 #include <Ice/Initialize.h>
34 
35 #include <QLineEdit>
36 #include <QtGui>
37 
38 namespace armarx
39 {
41  {
44  QString defaultItem;
45  };
46 
48  QWidget(parent),
49  ui(new ::Ui::IceProxyFinder),
50  impl(new Impl)
51  {
52  setupUi();
53  }
54 
56  {
57  if (impl->refreshProxyListTask)
58  {
59  impl->refreshProxyListTask->stop(true);
60  }
61  delete ui;
62  }
63 
64  void IceProxyFinderBase::setIceManager(IceManagerPtr icemanager, bool fetchProxies)
65  {
66  this->impl->icemanager = icemanager;
67  if (fetchProxies)
68  {
70  }
71  }
72 
74  {
75  return ui->cbProxyName->currentText();
76  }
77 
78  void IceProxyFinderBase::setDefaultSelectedProxy(const QString& proxyName)
79  {
80  ui->cbProxyName->setEditText(proxyName);
81  }
82 
84  {
85  impl->defaultItem = proxyName;
86  }
87 
88  void IceProxyFinderBase::setSearchMask(const QString& searchMask)
89  {
90  ui->edtSearchMask->setText(searchMask);
91  }
92 
94  {
95  ui->edtSearchMask->setVisible(show);
96  ui->labelMask->setVisible(show);
97  }
98 
100  {
101  ui->labelMask->setVisible(show);
102  ui->labelProxy->setVisible(show);
103  }
104 
105  ::Ui::IceProxyFinder* IceProxyFinderBase::getUi()
106  {
107  return ui;
108  }
109 
111  {
112  return ui->cbProxyName;
113  }
114 
115  QStringList IceProxyFinderBase::getProxyNameListBase(QString searchMask) const
116  {
117  if (searchMask.length() == 0)
118  {
119  searchMask = "*";
120  }
121 
122  Ice::StringSeq proxyList;
123  QStringList qProxyList;
124  if (!impl->icemanager)
125  {
126  throw LocalException("IceManager must not be NULL");
127  }
128  auto searchMasks = Split(searchMask.toStdString(), "|");
129  for (auto& subSearchMasks : searchMasks)
130  {
131  if (impl->icemanager && impl->icemanager->getIceGridSession())
132  {
133 
134  IceGridAdminPtr iceGridSession = impl->icemanager->getIceGridSession();
135  IceGrid::AdminPrx admin = iceGridSession->getAdmin();
136  Ice::AsyncResultPtr localBegin_getAllObjectInfos = admin->begin_getAllObjectInfos(subSearchMasks);
137  while (!localBegin_getAllObjectInfos->isCompleted())
138  {
139  if (impl->refreshProxyListTask->isStopped())
140  {
141  return qProxyList;
142  }
143  usleep(10000);
144  }
145  IceGrid::ObjectInfoSeq objects = admin->end_getAllObjectInfos(localBegin_getAllObjectInfos);
146  Ice::StringSeq tempProxyList;
147 
148  for (auto& object : objects)
149  {
150  // if objects are hangig we might get connection refused
151  try
152  {
153  if (impl->refreshProxyListTask->isStopped())
154  {
155  return qProxyList;
156  }
157  std::string typedName = getTypedProxyName(object.proxy);
158  if (!typedName.empty())
159  {
160  tempProxyList.push_back(typedName);
161  }
162  }
163  catch (...)
164  {
165  }
166  }
167 
168 
169  proxyList.insert(proxyList.end(), tempProxyList.begin(), tempProxyList.end());
170  }
171  }
172 
173 
174 
175  for (unsigned int i = 0; i < proxyList.size(); ++i)
176  {
177  qProxyList << QString::fromStdString(proxyList.at(i));
178  }
179  qProxyList.removeDuplicates();
180  return qProxyList;
181  }
182 
184  {
185  if (impl->refreshProxyListTask && impl->refreshProxyListTask->isRunning())
186  {
187  return;
188  }
189 
190  ui->btnRefresh->setEnabled(false);
191  ui->btnRefresh->setToolTip("Refreshing proxy list with mask '" + ui->edtSearchMask->text() + "'. Please wait...");
193  impl->refreshProxyListTask->start();
194  }
195 
196  void IceProxyFinderBase::comboBoxTextChanged(QString currentText)
197  {
199  }
200 
201  void IceProxyFinderBase::updateProxyListComboBox(const QStringList& proxyList)
202  {
203  ui->cbProxyName->clear();
204  if (not impl->defaultItem.isEmpty())
205  {
206  ui->cbProxyName->addItem(impl->defaultItem);
207  ui->cbProxyName->setCurrentIndex(0);
208  }
209  int i = 0;
210 
211  for (; i < proxyList.size(); ++i)
212  {
213  if (proxyList.at(i) != impl->defaultItem)
214  {
215  ui->cbProxyName->addItem(proxyList.at(i));
216  }
217  }
218 
219  ui->btnRefresh->setEnabled(true);
220  ui->btnRefresh->setToolTip("Press to refetch all available Kinematic Units with the given mask");
221  }
222 
224  {
225  try
226  {
227  // Try to get the object proxy "objPrx" represented by "proxyName"
228  IceGrid::AdminPrx admin = this->impl->icemanager->getIceGridSession()->getAdmin();
229  Ice::Identity objectIceId = Ice::stringToIdentity(proxyName.toStdString());
230  Ice::ObjectPrx objPrx = admin->getObjectInfo(objectIceId).proxy;
231 
232  // Try to ping the proxy (it could still be a stale entry in IceGrid)
233  objPrx->ice_ping();
234 
235  // If this point is reached, the object represented by "proxyName" is valid and reachable via Ice - that is, the signal can be emitted
236  emit validProxySelected(proxyName);
237  }
238  catch (...)
239  {
240  // pass: Any exception triggered in the try-block indicates that "proxyName" is not a valid indicator for an object known to Ice
241  }
242  }
243 
245  {
246  auto result = getProxyNameList(ui->edtSearchMask->text());
247  if (!impl->refreshProxyListTask->isStopped())
248  {
249  emit updateProxyListComboBoxSignal(result);
250  }
251  }
252 
254  {
255  this->ui->setupUi(this);
256 
257  this->connect(this, SIGNAL(updateProxyListComboBoxSignal(QStringList)), this, SLOT(updateProxyListComboBox(QStringList)));
258 
259  // These signals should trigger an update of the proxy list
260  this->connect(this->ui->btnRefresh, SIGNAL(clicked()), this, SLOT(triggerProxyListUpdate()));
261  this->connect(this->ui->edtSearchMask, SIGNAL(textEdited(QString)), this, SLOT(triggerProxyListUpdate()));
262  this->connect(this, SIGNAL(triggerProxyListComboBoxUpdateSignal()), this, SLOT(triggerProxyListUpdate()));
263 
264  // If the combobox changes in any way (text manually updated or new item selected) it should be evaluated whether
265  // the "validProxySelected" signal should be emitted
266  this->connect(this->ui->cbProxyName, SIGNAL(editTextChanged(QString)), this, SLOT(evaluateValidProxySelectedSignal(QString)));
267  this->connect(this->ui->cbProxyName, SIGNAL(currentIndexChanged(QString)), this, SLOT(evaluateValidProxySelectedSignal(QString)));
268  }
269 
271  : IceProxyFinderBase(parent)
272  {
273  ui->labelProxy->setText("Topic");
274  }
275 
276  QStringList IceTopicFinder::getProxyNameList(QString searchMask) const
277  {
278  if (searchMask.length() == 0)
279  {
280  searchMask = "*";
281  }
282 
283  IceStorm::TopicDict topicList;
284 
285  if (!impl->icemanager)
286  {
287  throw LocalException("IceManager must not be NULL");
288  }
289 
290  if (impl->icemanager && impl->icemanager->getIceGridSession())
291  {
292  topicList = impl->icemanager->getTopicManager()->retrieveAll();
293  }
294 
295  QStringList qProxyList;
296 
297  for (auto& e : topicList)
298  {
299  auto name = QString::fromStdString(e.first);
300  QRegExp rex(searchMask, Qt::CaseInsensitive, QRegExp::Wildcard);
301 
302  if (name.contains(rex))
303  {
304  qProxyList << name;
305  }
306  }
307 
308  return qProxyList;
309  }
310 
311 }
armarx::IceProxyFinderBase::~IceProxyFinderBase
~IceProxyFinderBase() override
Definition: IceProxyFinder.cpp:55
armarx::IceProxyFinderBase::Impl::defaultItem
QString defaultItem
Definition: IceProxyFinder.cpp:44
armarx::IceProxyFinderBase::setSearchMask
void setSearchMask(const QString &searchMask)
Definition: IceProxyFinder.cpp:88
armarx::IceProxyFinderBase::getUi
Ui::IceProxyFinder * getUi()
Definition: IceProxyFinder.cpp:105
armarx::IceProxyFinderBase::updateProxyList
void updateProxyList()
Definition: IceProxyFinder.cpp:244
armarx::IceTopicFinder::getProxyNameList
QStringList getProxyNameList(QString searchMask) const override
Definition: IceProxyFinder.cpp:276
armarx::IceProxyFinderBase::updateProxyListComboBoxSignal
void updateProxyListComboBoxSignal(const QStringList &proxyList)
LocalException.h
armarx::IceProxyFinderBase::setIceManager
void setIceManager(IceManagerPtr icemanager, bool fetchProxies=true)
Definition: IceProxyFinder.cpp:64
armarx::IceProxyFinderBase
The IceProxyFinderBase class provides a convenient way to query online proxies in the ice network,...
Definition: IceProxyFinder.h:53
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:35
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:196
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
armarx::IceProxyFinderBase::getTypedProxyName
virtual std::string getTypedProxyName(Ice::ObjectPrx const &current) const
Definition: IceProxyFinder.h:75
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::IceProxyFinderBase::setupUi
void setupUi()
Definition: IceProxyFinder.cpp:253
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:58
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:270
IceGridAdmin.h
armarx::IceProxyFinderBase::evaluateValidProxySelectedSignal
void evaluateValidProxySelectedSignal(const QString &proxyName)
Definition: IceProxyFinder.cpp:223
armarx::IceProxyFinderBase::setDefaultSelectedProxy
void setDefaultSelectedProxy(const QString &proxyName)
Definition: IceProxyFinder.cpp:78
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:98
armarx::IceProxyFinderBase::updateProxyListComboBox
void updateProxyListComboBox(const QStringList &proxyList)
Definition: IceProxyFinder.cpp:201
armarx::IceProxyFinderBase::getProxyNameComboBox
QWidget * getProxyNameComboBox()
Definition: IceProxyFinder.cpp:110
IceUtil::Handle< IceManager >
armarx::IceProxyFinder
Definition: StatechartViewerController.h:51
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:93
armarx::IceProxyFinderBase::triggerProxyListUpdate
void triggerProxyListUpdate()
Definition: IceProxyFinder.cpp:183
armarx::IceProxyFinderBase::ui
Ui::IceProxyFinder * ui
Definition: IceProxyFinder.h:97
armarx::IceProxyFinderBase::showLabels
void showLabels(bool show)
Definition: IceProxyFinder.cpp:99
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::IceProxyFinderBase::getProxyNameListBase
QStringList getProxyNameListBase(QString searchMask) const
Definition: IceProxyFinder.cpp:115
armarx::IceProxyFinderBase::setDefaultSelectedProxyAsItem
void setDefaultSelectedProxyAsItem(const QString &proxyName)
Definition: IceProxyFinder.cpp:83