NorMIT-nav  22.09
An IGT application
cxSocketConnection.h
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 #ifndef CXSOCKETCONNECTION_H
13 #define CXSOCKETCONNECTION_H
14 
15 #include <QPointer>
16 #include <QTcpServer>
17 #include <QMutex>
18 #include "cxEnumConverter.h"
19 #include "boost/shared_ptr.hpp"
20 
21 namespace cx {
23 {
29 };
30 } // namespace cx
31 
33 
37 #define assertRunningInObjectThread() \
38 { \
39  if (QThread::currentThread()!=this->thread()) \
40  { \
41  CX_LOG_ERROR() \
42  << QString("Method should be called in the object's thread [%1] only, current thread = [%2]") \
43  .arg(this->thread()->objectName()) \
44  .arg(QThread::currentThread()->objectName()); \
45  } \
46 } \
47 
48 
49 
50 namespace cx {
51 
52 typedef boost::shared_ptr<class SocketConnector> SocketConnectorPtr;
53 
54 class cxResource_EXPORT SocketConnection : public QObject
55 {
56  Q_OBJECT
57 public:
58  explicit SocketConnection(QObject *parent = 0);
59 
60  struct cxResource_EXPORT ConnectionInfo
61  {
62  QString role;
63  QString protocol;
64  QString host;
65  int port;
66 
67  bool operator==(const ConnectionInfo& rhs) const;
68  bool isServer() const;
69  bool isClient() const;
70  bool isLocalhostConnection() const;
71  QString getDescription() const;
72  };
73 
78  ConnectionInfo getConnectionInfo();
79  CX_SOCKETCONNECTION_STATE getState();
80  virtual void setConnectionInfo(ConnectionInfo info);
81 
82  virtual void requestConnect();
83  virtual void requestDisconnect();
84 
85 public slots:
86  bool sendData(const char* data, qint64 maxSize);
87 
88 signals:
89  void connectionInfoChanged();
90  void stateChanged(CX_SOCKETCONNECTION_STATE status);
91  void connected();
92  void disconnected();
93  void error();
94 
95 private slots:
96  void internalConnected();
97  void internalDisconnected();
98  void internalError(QAbstractSocket::SocketError socketError);
99  virtual void internalDataAvailable() = 0;
100 
101 protected:
102  virtual void setProtocol(QString protocolname) = 0;
103  SocketConnectorPtr createConnector(ConnectionInfo info);
104  bool socketIsConnected();
105  bool enoughBytesAvailableOnSocket(int bytes) const;
106  bool socketReceive(void *packPointer, int packSize) const;
107  QStringList getAllServerHostnames();
108  void setCurrentConnectionInfo();
109  void stateChange(CX_SOCKETCONNECTION_STATE newState);
110  CX_SOCKETCONNECTION_STATE computeState();
111 
112  QTcpSocket* mSocket;
113 
117 
118 protected:
120 };
121 
122 } //namespace cx
123 
124 #endif // CXSOCKETCONNECTION_H
cx::scsCOUNT
@ scsCOUNT
Definition: cxSocketConnection.h:28
cx::SocketConnection::mConnector
SocketConnectorPtr mConnector
Definition: cxSocketConnection.h:119
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::SocketConnection::mCurrentState
CX_SOCKETCONNECTION_STATE mCurrentState
Definition: cxSocketConnection.h:114
cx::SocketConnection::ConnectionInfo::protocol
QString protocol
Definition: cxSocketConnection.h:63
SNW_DECLARE_ENUM_STRING_CONVERTERS
SNW_DECLARE_ENUM_STRING_CONVERTERS(cxResource_EXPORT, cx, CX_SOCKETCONNECTION_STATE)
cx::SocketConnection::mNextConnectionInfo
ConnectionInfo mNextConnectionInfo
info to be used for the next connect(), mutexed.
Definition: cxSocketConnection.h:116
cx::SocketConnection::ConnectionInfo::host
QString host
Definition: cxSocketConnection.h:64
cx::SocketConnection::ConnectionInfo
Definition: cxSocketConnection.h:60
cx::scsLISTENING
@ scsLISTENING
Definition: cxSocketConnection.h:26
cx::scsINACTIVE
@ scsINACTIVE
Definition: cxSocketConnection.h:24
cx::SocketConnection
Definition: cxSocketConnection.h:54
cxEnumConverter.h
cx::scsCONNECTED
@ scsCONNECTED
Definition: cxSocketConnection.h:25
cx::SocketConnection::mNextConnectionInfoMutex
QMutex mNextConnectionInfoMutex
Definition: cxSocketConnection.h:115
cx::SocketConnection::ConnectionInfo::role
QString role
Definition: cxSocketConnection.h:62
cx::SocketConnection::mSocket
QTcpSocket * mSocket
Definition: cxSocketConnection.h:112
cx::scsCONNECTING
@ scsCONNECTING
Definition: cxSocketConnection.h:27
cx::operator==
bool operator==(const RegistrationTransform &lhs, const RegistrationTransform &rhs)
Definition: cxRegistrationTransform.cpp:151
cx::SocketConnectorPtr
boost::shared_ptr< class SocketConnector > SocketConnectorPtr
Definition: cxSocketConnection.h:52
cx::CX_SOCKETCONNECTION_STATE
CX_SOCKETCONNECTION_STATE
Definition: cxSocketConnection.h:22
cx::SocketConnection::ConnectionInfo::port
int port
Definition: cxSocketConnection.h:65