NorMIT-nav  18.04
An IGT application
cxSocket.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 
12 
13 #include "cxSocket.h"
14 
15 #include <QTcpSocket>
16 #include "cxLogger.h"
17 #include <QTcpServer>
18 #include <QNetworkInterface>
19 
20 namespace cx
21 {
22 
23 //void SingleConnectionTcpServer::setSocket(QPointer<Socket> socket)
24 //{
25 // mSocket = socket;
26 //}
27 
29  QTcpServer(parent)
30 {
31 }
32 
33 
34 void SingleConnectionTcpServer::incomingConnection(qintptr socketDescriptor)
35 {
36  emit incoming(socketDescriptor);
37 }
38 
39 //---------------------------------------------------------
40 //---------------------------------------------------------
41 //---------------------------------------------------------
42 
44  mInfo(info),
45  mSocket(socket)
46 {
47  connect(mSocket, &QTcpSocket::connected, this, &SocketClientConnector::internalConnected);
48  connect(mSocket, &QTcpSocket::disconnected, this, &SocketClientConnector::internalDisconnected);
49 }
50 
52 {
53 
54 }
55 
57 {
58  CX_LOG_INFO() << "Trying to connect to " << mInfo.getDescription();
60  mSocket->connectToHost(mInfo.host, mInfo.port);
61 }
62 
64 {
65  mSocket->close();
66 // this->stopListen();
67 }
68 
70 {
71  if (mSocket->state() == QAbstractSocket::ConnectedState)
72  return scsCONNECTED;
73  if (mSocket->state() == QAbstractSocket::UnconnectedState)
74  return scsINACTIVE;
75  return scsCONNECTING;
76 }
77 
78 void SocketClientConnector::internalConnected()
79 {
80  CX_LOG_SUCCESS() << "Connected to " << mInfo.getDescription();
81  emit stateChanged(this->getState());
82 }
83 
84 void SocketClientConnector::internalDisconnected()
85 {
86  CX_LOG_SUCCESS() << "Disconnected";
87  this->stateChanged(this->getState());
88 }
89 
90 
91 //---------------------------------------------------------
92 //---------------------------------------------------------
93 //---------------------------------------------------------
94 
96  mInfo(info),
97  mSocket(socket)
98 {
99 
100 }
101 
103 {
104 
105 }
106 
108 {
109  this->startListen();
110 }
111 
113 {
114  this->stopListen();
115 }
116 
118 {
119  if (mSocket->state() == QAbstractSocket::ConnectedState)
120  return scsCONNECTED;
121  if (mServer->isListening())
122  return scsLISTENING;
123  if (mSocket->state() == QAbstractSocket::UnconnectedState)
124  return scsINACTIVE;
125  return scsCONNECTING;
126 }
127 
128 bool SocketServerConnector::startListen()
129 {
130  if (!mServer)
131  {
132  mServer = new SingleConnectionTcpServer(this);
133  connect(mServer.data(), &SingleConnectionTcpServer::incoming, this, &SocketServerConnector::incomingConnection);
134 // mServer->setSocket(mSocket);
135  }
137 
138  bool started = mServer->listen(QHostAddress::Any, mInfo.port);
139 
140  if (started)
141  {
142  CX_LOG_INFO() << QString("Server address: %1").arg(this->getAllServerHostnames().join(", "));
143  CX_LOG_INFO() << QString("Server is listening to port %1").arg(mServer->serverPort());
144  }
145  else
146  {
147  CX_LOG_INFO() << QString("Server failed to start. Error: %1").arg(mServer->errorString());
148  }
149 
150  emit stateChanged(this->getState());
151  return started;
152 }
153 
154 void SocketServerConnector::stopListen()
155 {
156  mSocket->close();
157 
158  if (mServer && mServer->isListening())
159  {
160  CX_LOG_INFO() << QString("Server stopped listening to port %1").arg(mServer->serverPort());
161  mServer->close();
162  emit stateChanged(this->getState());
163  }
164 }
165 
166 void SocketServerConnector::incomingConnection(qintptr socketDescriptor)
167 {
168  CX_LOG_INFO() << "Server: Incoming connection...";
169 
170  if (this->mSocket->state() == QAbstractSocket::ConnectedState)
171  {
172  reportError("Incoming connection request rejected: The server can only handle a single connection.");
173  return;
174  }
175 
176  int success = mSocket->setSocketDescriptor(socketDescriptor, QAbstractSocket::ConnectedState);
177  QString clientName = mSocket->localAddress().toString();
178  report("Connected to "+clientName+". Session started." + qstring_cast(success));
179 
180  emit stateChanged(this->getState());
181 }
182 
183 QStringList SocketServerConnector::getAllServerHostnames()
184 {
185  QStringList addresses;
186 
187  foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
188  {
189  if (interface.flags().testFlag(QNetworkInterface::IsRunning))
190  foreach (QNetworkAddressEntry entry, interface.addressEntries())
191  {
192  if ( interface.hardwareAddress() != "00:00:00:00:00:00"
193  && entry.ip().toString() != "127.0.0.1"
194  && entry.ip().toString().contains(".") )
195  addresses << QString("%1: %2").arg(interface.name()).arg(entry.ip().toString());
196  }
197  }
198 
199  return addresses;
200 }
201 
202 
203 
204 }//namespace cx
QString qstring_cast(const T &val)
void incoming(qintptr socketDescriptor)
CX_SOCKETCONNECTION_STATE
virtual ~SocketClientConnector()
Definition: cxSocket.cpp:51
void reportError(QString msg)
Definition: cxLogger.cpp:71
SingleConnectionTcpServer(QObject *parent)
Definition: cxSocket.cpp:28
#define CX_LOG_INFO
Definition: cxLogger.h:96
virtual void activate()
Definition: cxSocket.cpp:56
SocketServerConnector(SocketConnection::ConnectionInfo info, QTcpSocket *socket)
Definition: cxSocket.cpp:95
virtual ~SocketServerConnector()
Definition: cxSocket.cpp:102
void incomingConnection(qintptr socketDescriptor)
Definition: cxSocket.cpp:34
#define CX_LOG_SUCCESS
Definition: cxLogger.h:97
virtual CX_SOCKETCONNECTION_STATE getState()
Definition: cxSocket.cpp:69
void stateChanged(CX_SOCKETCONNECTION_STATE)
void report(QString msg)
Definition: cxLogger.cpp:69
SocketClientConnector(SocketConnection::ConnectionInfo info, QTcpSocket *socket)
Definition: cxSocket.cpp:43
virtual CX_SOCKETCONNECTION_STATE getState()
Definition: cxSocket.cpp:117
virtual void deactivate()
Definition: cxSocket.cpp:112
virtual void deactivate()
Definition: cxSocket.cpp:63
virtual void activate()
Definition: cxSocket.cpp:107
Namespace for all CustusX production code.