CustusX  18.04
An IGT application
cxOpenIGTLinkStreamerService.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 
14 #include <QFileInfo>
15 
16 #include "cxNetworkHandler.h"
17 #include "cxLogger.h"
18 #include "cxProfile.h"
19 #include "cxTrackingService.h"
21 
22 namespace cx
23 {
24 
26  mConnection(networkHandler),
27  mTrackingService(trackingService),
28  mStartedTrackingAndOpenIGTLinkFromHere(false)
29 {
31 
32  connect(mConnection.get(), &NetworkHandler::connected, mStreamer.get(), &OpenIGTLinkStreamer::receivedConnected);
33  connect(mConnection.get(), &NetworkHandler::disconnected, mStreamer.get(), &OpenIGTLinkStreamer::receivedDisconnected);
34  connect(mConnection.get(), &NetworkHandler::image, mStreamer.get(), &OpenIGTLinkStreamer::receivedImage);
35 
36  connect(mStreamer.get(), &OpenIGTLinkStreamer::stoppedStreaming, this, &OpenIGTLinkStreamerService::stopTrackingAndOpenIGTLinkClientIfStartedFromThisObject);
37 }
38 
40 {
41  this->stopTrackingAndOpenIGTLinkClientIfStartedFromThisObject();
42 }
43 
45 {
46  return "OpenIGTLink 3 streamer";
47 }
48 
50 {
51  return OPENIGTLINK3_STREAMER;
52 }
53 
54 std::vector<PropertyPtr> OpenIGTLinkStreamerService::getSettings(QDomElement root)
55 {
56  std::vector<PropertyPtr> retval;
57  retval.push_back(this->getIPOption(root));
58  retval.push_back(this->getStreamPortOption(root));
59  retval.push_back(this->trackAndStream(root));
60 
61  return retval;
62 }
63 
65 {
66  this->startTracking(root);
67  return mStreamer;
68 }
69 
71 {
72  this->stopTrackingAndOpenIGTLinkClientIfStartedFromThisObject();
73 }
74 
75 void OpenIGTLinkStreamerService::stopTrackingAndOpenIGTLinkClientIfStartedFromThisObject()
76 {
77  if(mStartedTrackingAndOpenIGTLinkFromHere)
78  {
79  mConnection->disconnectFromServer();
80 
81  OpenIGTLinkTrackingSystemServicePtr trackingSystemService = this->getOpenIGTLinkTrackingSystemService();
82  if(trackingSystemService)
83  trackingSystemService->setState(Tool::tsNONE);
84 
85  mStartedTrackingAndOpenIGTLinkFromHere = false;
86  }
87 }
88 
89 void OpenIGTLinkStreamerService::startTracking(QDomElement root)
90 {
91  if(this->trackAndStream(root)->getValue())
92  {
93  mStartedTrackingAndOpenIGTLinkFromHere = true;
94  this->configureTracking(root);
95  // Trying to connect will cause several tests to fail,
96  // because ClientSocket::ConnectToServer in OpenIGTLink/OpenIGTLink/Source/igtlClientSocket.cxx
97  // will print error messages when it cannot connect.
98  // Default value for trackAndStream (named start_tracking in settings xml file,
99  // and defined as OPENIGTLINK3_STREAMER_START_TRACKING in code) is therefore set to false.
100  mConnection->requestConnectToServer(this->getIPOption(root)->getValue().toStdString(),
101  int(this->getStreamPortOption(root)->getValue()));
102  }
103 }
104 
105 void OpenIGTLinkStreamerService::configureTracking(QDomElement root)
106 {
107  Q_UNUSED(root);
108  OpenIGTLinkTrackingSystemServicePtr trackingSystemService = this->getOpenIGTLinkTrackingSystemService();
109  if(trackingSystemService)
110  {
111  QFileInfo fileInfo(profile()->getToolConfigFilePath());
112  trackingSystemService->setConfigurationFile(fileInfo.filePath());//Set full path to current tool config file
113  trackingSystemService->setState(Tool::tsCONFIGURED);
114  }
115 }
116 
117 OpenIGTLinkTrackingSystemServicePtr OpenIGTLinkStreamerService::getOpenIGTLinkTrackingSystemService()
118 {
119  std::vector<TrackingSystemServicePtr> trackingSystems = mTrackingService->getTrackingSystems();
120  for (unsigned i = 0; i < trackingSystems.size(); ++i)
121  {
122  OpenIGTLinkTrackingSystemServicePtr trackingSystemService = boost::dynamic_pointer_cast<OpenIGTLinkTrackingSystemService>(trackingSystems[i]);
123  if(trackingSystemService)
124  return trackingSystemService;
125  }
127 }
128 
129 BoolPropertyBasePtr OpenIGTLinkStreamerService::trackAndStream(QDomElement root)
130 {
131  BoolPropertyPtr retval;
132  // Default value need to be false to prevent tests from failing
133  retval = BoolProperty::initialize(OPENIGTLINK3_STREAMER_START_TRACKING, "Also Start OpenIGTLink client and Tracking",
134  "Combined functionality: \n"
135  "Run both tracking and streaming over OpenIGTLink",
136  false, root);
137  retval->setGroup("Connection");
138 
139  return retval;
140 }
141 
142 StringPropertyBasePtr OpenIGTLinkStreamerService::getIPOption(QDomElement root)
143 {
144  StringPropertyPtr retval;
145  QString defaultValue = "127.0.0.1";
146  retval = StringProperty::initialize(OPENIGTLINK3_STREAMER_IP, "Address", "TCP/IP Address",
147  defaultValue, root);
148  retval->setGroup("Connection");
149  return retval;
150 }
151 
152 DoublePropertyBasePtr OpenIGTLinkStreamerService::getStreamPortOption(QDomElement root)
153 {
154  DoublePropertyPtr retval;
155  retval = DoubleProperty::initialize("ip_port", "Port", "TCP/IP Port (default 18944)",
156  18944, DoubleRange(1024, 49151, 1), 0, root);
157  retval->setGuiRepresentation(DoublePropertyBase::grSPINBOX);
158  retval->setAdvanced(true);
159  retval->setGroup("Connection");
160  return retval;
161 }
162 
163 } //namespace cx
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
boost::shared_ptr< class TrackingService > TrackingServicePtr
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:32
boost::shared_ptr< class NetworkHandler > NetworkHandlerPtr
boost::shared_ptr< OpenIGTLinkStreamer > OpenIGTLinkStreamerPtr
configured with basic info
Definition: cxTool.h:75
boost::shared_ptr< class StringProperty > StringPropertyPtr
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
boost::shared_ptr< class BoolPropertyBase > BoolPropertyBasePtr
boost::shared_ptr< class DoublePropertyBase > DoublePropertyBasePtr
not available
Definition: cxTool.h:74
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
boost::shared_ptr< class DoubleProperty > DoublePropertyPtr
boost::shared_ptr< class OpenIGTLinkTrackingSystemService > OpenIGTLinkTrackingSystemServicePtr
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
boost::shared_ptr< class BoolProperty > BoolPropertyPtr
boost::shared_ptr< class Streamer > StreamerPtr
void image(ImagePtr image)
Namespace for all CustusX production code.