Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxNetworkConnectionHandle.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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include "cxSender.h"
36 #include "cxTime.h"
37 #include <QThread>
38 
39 #include "cxPlusProtocol.h"
40 #include "cxCustusProtocol.h"
41 #include "cxRASProtocol.h"
42 #include "igtl_header.h"
45 #include "cxXmlOptionItem.h"
46 #include "cxProfile.h"
47 #include "cxStringProperty.h"
48 #include "cxDoubleProperty.h"
49 #include "cxNetworkConnection.h"
50 
51 namespace cx
52 {
53 
54 
56 {
57  mThread.reset(new QThread());
58  mThread->setObjectName(threadname);
59  mClient.reset(new NetworkConnection(threadname));
60  connect(mClient.get(), &NetworkConnection::connectionInfoChanged, this, &NetworkConnectionHandle::onConnectionInfoChanged);
61  mClient->moveToThread(mThread.get());
62 
63  mOptions = options.descend(mClient->getUid());
64 
65  mIp = this->createIpOption();
66  mPort = this->createPortOption();
67  mProtocols = this->createDialectOption();
68  mRole = this->createRoleOption();
69 
70  this->onPropertiesChanged();
71 
72  mThread->start();
73 }
74 
75 void NetworkConnectionHandle::onPropertiesChanged()
76 {
78  info.role = mRole->getValue();
79  info.host = mIp->getValue();
80  info.port = mPort->getValue();
81  info.protocol = mProtocols->getValue();
82 
83  mClient->setConnectionInfo(info);
84 }
85 
86 void NetworkConnectionHandle::onConnectionInfoChanged()
87 {
88  NetworkConnection::ConnectionInfo info = mClient->getConnectionInfo();
89  mRole->setValue(info.role);
90  mIp->setValue(info.host);
91  mPort->setValue(info.port);
92  mProtocols->setValue(info.protocol);
93 }
94 
96 {
97  mThread->quit();
98  mThread->wait();
99 
100  mClient.reset();
101  // thread-delete implicitly at end.
102 }
103 
105 {
106  return mClient.get();
107 }
108 
109 StringPropertyBasePtr NetworkConnectionHandle::createDialectOption()
110 {
111  StringPropertyPtr retval;
112  QStringList dialectnames;
113  if(mClient)
114  dialectnames = mClient->getAvailableDialects();
115  retval = StringProperty::initialize("protocol", "Connect to", "Protocol to use during connection.",
116  mClient->getConnectionInfo().protocol,
117  dialectnames, mOptions.getElement());
118  retval->setValueRange(dialectnames);
119  retval->setGroup("Connection");
120  connect(retval.get(), &Property::changed, this, &NetworkConnectionHandle::onPropertiesChanged);
121  return retval;
122 }
123 
124 StringPropertyBasePtr NetworkConnectionHandle::createIpOption()
125 {
126  StringPropertyPtr retval;
127  retval = StringProperty::initialize("address", "Address", "Network Address",
128  mClient->getConnectionInfo().host,
129  mOptions.getElement());
130  retval->setGroup("Connection");
131  connect(retval.get(), &Property::changed, this, &NetworkConnectionHandle::onPropertiesChanged);
132  return retval;
133 }
134 
135 
136 DoublePropertyBasePtr NetworkConnectionHandle::createPortOption()
137 {
138  DoublePropertyPtr retval;
139  int defval = mClient->getConnectionInfo().port;
140  retval = DoubleProperty::initialize("port", "Port",
141  "Network Port (default "+QString::number(defval)+")",
142  defval,
143  DoubleRange(1024, 49151, 1), 0,
144  mOptions.getElement());
145  retval->setGuiRepresentation(DoublePropertyBase::grSPINBOX);
146  retval->setAdvanced(true);
147  retval->setGroup("Connection");
148  connect(retval.get(), &Property::changed, this, &NetworkConnectionHandle::onPropertiesChanged);
149  return retval;
150 }
151 
152 StringPropertyBasePtr NetworkConnectionHandle::createRoleOption()
153 {
154  StringPropertyPtr retval;
155  QStringList values = QStringList() << "client" << "server";
156  retval = StringProperty::initialize("role", "Role",
157  "Act as client or server in the network connection",
158  mClient->getConnectionInfo().role,
159  values, mOptions.getElement());
160  retval->setGroup("Connection");
161  connect(retval.get(), &Property::changed, this, &NetworkConnectionHandle::onPropertiesChanged);
162  return retval;
163 }
164 
165 }//namespace cx
QDomElement getElement()
return the current element
boost::shared_ptr< class StringProperty > StringPropertyPtr
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
boost::shared_ptr< class DoublePropertyBase > DoublePropertyBasePtr
NetworkConnectionHandle(QString threadname, XmlOptionFile options)
void changed()
emit when the underlying data value is changed: The user interface will be updated.
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
boost::shared_ptr< class DoubleProperty > DoublePropertyPtr
The NetworkConnection class handles incoming OpenIGTLink packages.
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
Helper class for xml files used to store ssc/cx data.
XmlOptionFile descend(QString element) const
step one level down in the xml tree