CustusX  16.5.0-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxSocketConnection.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 
33 #include "cxSocketConnection.h"
34 #include <QTcpServer>
35 #include <QTcpSocket>
36 #include <QNetworkInterface>
37 #include <QThread>
38 
39 #include "cxSocket.h"
40 #include "cxLogger.h"
41 
43 {
44  "inactive",
45  "connected",
46  "listening",
47  "connecting"
48 }
50 
51 
52 
53 namespace cx
54 {
55 
56 
58  {
59  return (this->role == rhs.role)
60  && (this->protocol == rhs.protocol)
61  && (this->host == rhs.host)
62  && (this->port == rhs.port);
63  }
64 
65  bool SocketConnection::ConnectionInfo::isServer() const { return role.toLower()=="server"; }
66  bool SocketConnection::ConnectionInfo::isClient() const { return !this->isServer(); }
67  bool SocketConnection::ConnectionInfo::isLocalhostConnection() const { return host.toLower().contains("localhost"); }
68 
69  QString SocketConnection::ConnectionInfo::getDescription() const
70  {
71  QString postfix;
72  QString name = host;
73  if (isServer())
74  name = "listen";
75  if (!protocol.isEmpty())
76  postfix = QString("[%1]").arg(protocol);
77  return QString("%1:%2%3").arg(name).arg(port).arg(postfix);
78  }
79 
83 
84 
85 SocketConnection::SocketConnection(QObject *parent) :
86  QObject(parent)
87 {
89  qRegisterMetaType<boost::function<void()> >("boost::function<void()>");
90  qRegisterMetaType<CX_SOCKETCONNECTION_STATE>("CX_SOCKETCONNECTION_STATE");
91 
92  mNextConnectionInfo.host = "localhost";
93  mNextConnectionInfo.port = 18944;
94  mNextConnectionInfo.role = "client";
95 
96  mSocket = new QTcpSocket(this);
97  connect(mSocket, &QTcpSocket::connected, this, &SocketConnection::internalConnected);
98  connect(mSocket, &QTcpSocket::disconnected, this, &SocketConnection::internalDisconnected);
99  connect(mSocket, &QTcpSocket::readyRead, this, &SocketConnection::internalDataAvailable);
100 
101  //see http://stackoverflow.com/questions/26062397/qt-connect-function-signal-disambiguation-using-lambdas
102  void (QTcpSocket::* errorOverloaded)(QAbstractSocket::SocketError) = &QTcpSocket::error;
103  connect(mSocket, errorOverloaded, this, &SocketConnection::error);
104  connect(mSocket, errorOverloaded, this, &SocketConnection::internalError);
105 }
106 
108 {
109  QMutexLocker locker(&mNextConnectionInfoMutex);
110  return mNextConnectionInfo;
111 }
112 
114 {
115  QMutexLocker locker(&mNextConnectionInfoMutex);
116  if (info==mNextConnectionInfo)
117  return;
118  mNextConnectionInfo = info;
119  locker.unlock();
120 
121  emit connectionInfoChanged();
122 }
123 
125 {
126  if (mCurrentState==newState)
127  return;
128 
129  if (newState==scsCONNECTED)
130  emit connected();
131  else if (mCurrentState==scsCONNECTED)
132  emit disconnected();
133 
134  mCurrentState = newState;
135 
137 }
138 
140 {
141  return mCurrentState;
142 }
143 
145 {
147 
148  ConnectionInfo info = this->getConnectionInfo();
149  mConnector = this->createConnector(info);
150  this->setProtocol(info.protocol);
151  mConnector->activate();
152 }
153 
155 {
156  SocketConnectorPtr retval;
157 
158  if (info.isClient())
159  retval.reset(new SocketClientConnector(info, mSocket));
160  else
161  retval.reset(new SocketServerConnector(info, mSocket));
162 
163  connect(retval.get(), &SocketConnector::stateChanged, this, &SocketConnection::stateChange);
164  return retval;
165 }
166 
168 {
170 
171  if (mConnector)
172  {
173  mConnector->deactivate();
174  mConnector.reset();
175  }
176  mSocket->close();
177 }
178 
179 bool SocketConnection::sendData(const char *data, qint64 maxSize)
180 {
181  if(!this->socketIsConnected())
182  return false;
183  qint64 writtenBytes = mSocket->write(data, maxSize);
184  if(writtenBytes != maxSize)
185  return false;
186  return true;
187 }
188 
189 void SocketConnection::internalConnected()
190 {
191 // CX_LOG_SUCCESS() << "Connected to " << mCurrentConnectionInfo.getDescription();
192  this->stateChange(this->computeState());
193 }
194 
195 void SocketConnection::internalDisconnected()
196 {
197 // CX_LOG_SUCCESS() << "Disconnected";
198  this->stateChange(this->computeState());
199 }
200 
201 void SocketConnection::internalError(QAbstractSocket::SocketError socketError)
202 {
203  CX_LOG_ERROR() << QString("[%1] Socket error [code=%2]: %3")
204  .arg(mSocket->peerName())
205  .arg(QString::number(socketError))
206  .arg(mSocket->errorString());
207 
208  this->stateChange(this->computeState());
209 }
210 
212 {
213  return mSocket->state() == QAbstractSocket::ConnectedState;
214 }
215 
217 {
218  bool enoughBytes = mSocket->bytesAvailable() >= bytes;
219  if(!enoughBytes)
220  CX_LOG_DEBUG() << "Want " << bytes << " but only "<< mSocket->bytesAvailable() << " are available on the socket atm.";
221  return enoughBytes;
222 }
223 
224 bool SocketConnection::socketReceive(void *packPointer, int packSize) const
225 {
226  if(!this->enoughBytesAvailableOnSocket(packSize))
227  return false;
228 
229  char* charPointer = reinterpret_cast<char*>(packPointer);
230  int r = mSocket->read(charPointer, packSize);
231  if(r <= 0)
232  {
233  CX_LOG_ERROR() << "Error when receiving data from socket.";
234  return false;
235  }
236  return true;
237 }
238 
240 {
241  if (mConnector)
242  return mConnector->getState();
243  return scsINACTIVE;
244 }
245 
246 
247 } //namespace cx
virtual void setConnectionInfo(ConnectionInfo info)
thread-safe
CX_SOCKETCONNECTION_STATE
bool sendData(const char *data, qint64 maxSize)
not thread-safe
SNW_DEFINE_ENUM_STRING_CONVERTERS_BEGIN(cx, CX_SOCKETCONNECTION_STATE, scsCOUNT)
bool socketReceive(void *packPointer, int packSize) const
bool enoughBytesAvailableOnSocket(int bytes) const
CX_SOCKETCONNECTION_STATE getState()
thread-safe
ConnectionInfo mNextConnectionInfo
info to be used for the next connect(), mutexed.
void stateChange(CX_SOCKETCONNECTION_STATE newState)
CX_SOCKETCONNECTION_STATE mCurrentState
void stateChanged(CX_SOCKETCONNECTION_STATE status)
virtual void requestDisconnect()
not thread-safe: use invoke
ConnectionInfo getConnectionInfo()
thread-safe
virtual void requestConnect()
not thread-safe: use invoke
#define CX_LOG_ERROR
Definition: cxLogger.h:114
SocketConnectorPtr createConnector(ConnectionInfo info)
virtual void setProtocol(QString protocolname)=0
boost::shared_ptr< class SocketConnector > SocketConnectorPtr
void stateChanged(CX_SOCKETCONNECTION_STATE)
bool operator==(const RegistrationTransform &lhs, const RegistrationTransform &rhs)
#define CX_LOG_DEBUG
Definition: cxLogger.h:110
#define assertRunningInObjectThread()
CX_SOCKETCONNECTION_STATE computeState()
SocketConnectorPtr mConnector
SNW_DEFINE_ENUM_STRING_CONVERTERS_END(cx, ORIENTATION_TYPE, otCOUNT)