IceGridViewer.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 Manfred Kroehnert ( Manfred.Kroehnert at kit dot edu)
18 * @date 2013
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "IceGridViewer.h"
24 #include <ArmarXGui/gui-plugins/SystemStateMonitorPlugin/ui_IceGridViewer.h>
25 #include "NodeObserver.h"
26 #include "model/NodeInfoModel.h"
27 #include "model/ServerInfoModel.h"
28 #include "model/AdapterInfoModel.h"
29 
30 #include <ArmarXGui/gui-plugins/SystemStateMonitorPlugin/ui_IceGridNodeView.h>
32 
33 #include <Ice/ObjectAdapter.h>
34 
35 #include <algorithm>
36 
37 
38 namespace armarx
39 {
41  ui(new Ui::IceGridViewer),
42  ADAPTER_NAME("IceGridNodeObserverGui")
43  {
44  ui->setupUi(getWidget());
45 
46  qRegisterMetaType<QVector<int>>("QVector<int>");
47  qRegisterMetaType<IceGrid::ServerDynamicInfo>("IceGrid::ServerDynamicInfo");
48  connect(ui->gridNodeSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(selectGridNode(int)));
49 
50  Ui::IceGridNodeView* content = new Ui::IceGridNodeView();
51  content->setupUi(ui->contentWidget);
52  nodeInfoModel = new NodeInfoModel;
53  content->NodeInfo->setModel(nodeInfoModel);
54  serverInfoModel = new ServerInfoModel;
55  content->ServerInfo->setModel(serverInfoModel);
56  adapterInfoModel = new AdapterInfoModel;
57  content->AdapterInfo->setModel(adapterInfoModel);
58 
59  connect(serverInfoModel, SIGNAL(serverInfoChanged(IceGrid::ServerDynamicInfo)), this, SLOT(serverInfoChanged(IceGrid::ServerDynamicInfo)));
60 
61  selectorModel = new QStringListModel;
62  ui->gridNodeSelector->setModel(selectorModel);
63  }
64 
65 
67  {
68  delete ui;
69  }
70 
71 
72  void IceGridViewer::loadSettings(QSettings* settings)
73  {
74  }
75 
76 
77  void IceGridViewer::saveSettings(QSettings* settings)
78  {
79  }
80 
81 
83  {
84  // create new IceGridAdmin which also creates a new AdminSession
85  // Observers set on this new session won't interfer with the default observers
86  // used for tracking ManagedIceObject dependencies
87  iceGridAdmin = IceGridAdmin::Create(getIceManager()->getCommunicator(), ADAPTER_NAME);
88  // remove default observers which are not required and used here
89  iceGridAdmin->removeObservers();
90  }
91 
92 
94  {
95  IceGrid::AdminSessionPrx adminSession = iceGridAdmin->adminSession();
96 
97  QtNodeObserverPtr observer = new QtNodeObserver(this);
98  Ice::ObjectPrx objectProxy = iceGridAdmin->registerObjectWithNewAdapter(observer, ADAPTER_NAME, observerAdapter);
99  IceGrid::NodeObserverPrx nodeObserver = IceGrid::NodeObserverPrx::checkedCast(objectProxy);
100 
101  adminSession->setObservers(NULL, NULL, NULL, NULL, NULL);
102  adminSession->setObservers(NULL,
103  nodeObserver,
104  NULL,
105  NULL,
106  NULL);
107  }
108 
110  {
111  // stop updating the AdminSession so it can be removed
112  iceGridAdmin->stop();
113  // remove observers so the IceGridAmin does not hold any more references to them
114  iceGridAdmin->adminSession()->setObservers(NULL, NULL, NULL, NULL, NULL);
115  // destroy the object adapter to remove the observer objects
116  observerAdapter->destroy();
117  }
118 
119 
120  void IceGridViewer::gridNodeListUpdate(const IceGrid::NodeDynamicInfoSeq& nodeSeq)
121  {
122  QString lastSelectionText = ui->gridNodeSelector->currentText();
123  {
124  std::unique_lock guard(gridNodeListMutex);
125  gridNodeList = nodeSeq;
126  QStringList nodeNames;
127 
128  for (size_t i = 0; i < gridNodeList.size(); ++i)
129  {
130  nodeNames.append(QString::fromStdString(gridNodeList[i].info.name));
131  }
132 
133  selectorModel->setStringList(nodeNames);
134  }
135 
136  int index = ui->gridNodeSelector->findText(lastSelectionText);
137  index = std::max(0, index);
138 
139  ui->gridNodeSelector->setCurrentIndex(index);
141  }
142 
143 
144  void IceGridViewer::gridNodeListAdd(const IceGrid::NodeDynamicInfo& node)
145  {
146  std::unique_lock guard(gridNodeListMutex);
147  QStringList currentEntries = selectorModel->stringList();
148  currentEntries.append(QString::fromStdString(node.info.name));
149  gridNodeList.push_back(node);
150  selectorModel->setStringList(currentEntries);
151  }
152 
153 
154  void IceGridViewer::gridNodeListRemove(const QString& node)
155  {
156  QString lastSelectionText = ui->gridNodeSelector->currentText();
157  {
158  std::unique_lock guard(gridNodeListMutex);
159 
160  QStringList currentEntries = selectorModel->stringList();
161  currentEntries.removeOne(node);
162 
163  for (IceGrid::NodeDynamicInfoSeq::iterator nodeInfo = gridNodeList.begin(); nodeInfo != gridNodeList.end(); nodeInfo++)
164  {
165  if (nodeInfo->info.name == node.toStdString())
166  {
167  nodeInfo = gridNodeList.erase(nodeInfo);
168  }
169  }
170 
171  selectorModel->setStringList(currentEntries);
172 
173  }
174  int index = std::max<int>(0, ui->gridNodeSelector->findText(lastSelectionText));
175  ui->gridNodeSelector->setCurrentIndex(index);
177  }
178 
179 
180  void IceGridViewer::updateServerInfo(const std::string& nodeName, const IceGrid::ServerDynamicInfo& serverUpdateInfo)
181  {
182  {
183  std::unique_lock guard(gridNodeListMutex);
184  IceGrid::NodeDynamicInfoSeq::iterator nodeEntry =
185  std::find_if(gridNodeList.begin(), gridNodeList.end(), [nodeName](IceGrid::NodeDynamicInfo nodeInfo)
186  {
187  return nodeName == nodeInfo.info.name;
188  });
189 
190  if (nodeEntry == gridNodeList.end())
191  {
192  return;
193  }
194 
195  IceGrid::ServerDynamicInfoSeq::iterator serverEntry =
196  std::find_if(nodeEntry->servers.begin(), nodeEntry->servers.end(), [serverUpdateInfo](IceGrid::ServerDynamicInfo serverInfo)
197  {
198  return serverUpdateInfo.id == serverInfo.id;
199  });
200 
201  if (serverEntry == nodeEntry->servers.end())
202  {
203  nodeEntry->servers.push_back(serverUpdateInfo);
204  }
205  else
206  {
207  if (serverUpdateInfo.pid == 0)
208  {
209  nodeEntry->servers.erase(serverEntry);
210  }
211  else
212  {
213  (*serverEntry) = serverUpdateInfo;
214  }
215  }
216  }
217 
218  selectGridNode(ui->gridNodeSelector->currentIndex());
219  }
220 
221 
222  void IceGridViewer::updateAdapterInfo(const std::string& nodeName, const IceGrid::AdapterDynamicInfo& adapterUpdateInfo)
223  {
224  {
225  std::unique_lock guard(gridNodeListMutex);
226  IceGrid::NodeDynamicInfoSeq::iterator nodeEntry =
227  std::find_if(gridNodeList.begin(), gridNodeList.end(), [nodeName](IceGrid::NodeDynamicInfo nodeInfo)
228  {
229  return nodeName == nodeInfo.info.name;
230  });
231 
232  if (nodeEntry == gridNodeList.end())
233  {
234  return;
235  }
236 
237  IceGrid::AdapterDynamicInfoSeq::iterator adapterEntry =
238  std::find_if(nodeEntry->adapters.begin(), nodeEntry->adapters.end(), [adapterUpdateInfo](IceGrid::AdapterDynamicInfo adapterInfo)
239  {
240  return adapterUpdateInfo.id == adapterInfo.id;
241  });
242 
243  if (adapterEntry == nodeEntry->adapters.end())
244  {
245  nodeEntry->adapters.push_back(adapterUpdateInfo);
246  }
247  else
248  {
249  if (!adapterUpdateInfo.proxy)
250  {
251  nodeEntry->adapters.erase(adapterEntry);
252  }
253  else
254  {
255  (*adapterEntry) = adapterUpdateInfo;
256  }
257  }
258  }
259 
260  selectGridNode(ui->gridNodeSelector->currentIndex());
261  }
262 
263 
264  void IceGridViewer::serverInfoChanged(IceGrid::ServerDynamicInfo serverInfo)
265  {
266  iceGridAdmin->getAdmin()->enableServer(serverInfo.id, serverInfo.enabled);
267  }
268 
269 
271  {
272  if (index >= (int)gridNodeList.size() || index < 0)
273  {
274  return;
275  }
276 
277  std::unique_lock guard(gridNodeListMutex);
278  nodeInfoModel->setData(gridNodeList[index].info);
279  serverInfoModel->setData(gridNodeList[index].servers);
280  adapterInfoModel->setData(gridNodeList[index].adapters);
281  }
282 }
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:353
armarx::IceGridViewer::updateServerInfo
void updateServerInfo(const std::string &nodeName, const IceGrid::ServerDynamicInfo &serverUpdateInfo)
Definition: IceGridViewer.cpp:180
AdapterInfoModel::setData
bool setData(IceGrid::AdapterDynamicInfoSeq newInfo)
Definition: AdapterInfoModel.h:128
armarx::IceGridViewer::onInitComponent
void onInitComponent() override
Definition: IceGridViewer.cpp:82
index
uint8_t index
Definition: EtherCATFrame.h:59
IceGridViewer.h
QtNodeObserver
Definition: NodeObserver.h:27
armarx::IceGridViewer::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: IceGridViewer.cpp:109
armarx::IceGridViewer
Definition: IceGridViewer.h:58
NodeInfoModel::setData
bool setData(IceGrid::NodeInfo newInfo)
Definition: NodeInfoModel.h:169
armarx::IceGridViewer::~IceGridViewer
~IceGridViewer() override
Definition: IceGridViewer.cpp:66
ServerInfoModel
Definition: ServerInfoModel.h:33
NodeObserver.h
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
ServerInfoModel.h
armarx::IceGridViewer::gridNodeListUpdate
void gridNodeListUpdate(const IceGrid::NodeDynamicInfoSeq &nodeSeq)
Definition: IceGridViewer.cpp:120
max
T max(T t1, T t2)
Definition: gdiam.h:48
IceGridAdmin.h
AdapterInfoModel.h
armarx::IceGridViewer::onConnectComponent
void onConnectComponent() override
Definition: IceGridViewer.cpp:93
armarx::IceGridViewer::gridNodeListAdd
void gridNodeListAdd(const IceGrid::NodeDynamicInfo &node)
Definition: IceGridViewer.cpp:144
armarx::IceGridViewer::IceGridViewer
IceGridViewer()
Definition: IceGridViewer.cpp:40
NodeInfoModel.h
NodeInfoModel
Definition: NodeInfoModel.h:30
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::IceGridViewer::selectGridNode
void selectGridNode(int index)
Definition: IceGridViewer.cpp:270
AdapterInfoModel
Definition: AdapterInfoModel.h:30
armarx::IceGridViewer::serverInfoChanged
void serverInfoChanged(IceGrid::ServerDynamicInfo serverInfo)
Definition: IceGridViewer.cpp:264
ServerInfoModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition: ServerInfoModel.h:197
armarx::IceGridViewer::gridNodeListRemove
void gridNodeListRemove(const QString &node)
Definition: IceGridViewer.cpp:154
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
armarx::IceGridViewer::saveSettings
void saveSettings(QSettings *settings) override
Saves the manager models.
Definition: IceGridViewer.cpp:77
armarx::IceGridViewer::loadSettings
void loadSettings(QSettings *settings) override
Load stored manager models.
Definition: IceGridViewer.cpp:72
armarx::IceGridViewer::updateAdapterInfo
void updateAdapterInfo(const std::string &nodeName, const IceGrid::AdapterDynamicInfo &adapterUpdateInfo)
Definition: IceGridViewer.cpp:222
armarx::ManagedIceObject::getCommunicator
Ice::CommunicatorPtr getCommunicator() const
Definition: ManagedIceObject.cpp:431
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28