NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 #ifndef CXSOCKETCONNECTION_H
34 #define CXSOCKETCONNECTION_H
35 
36 #include <QPointer>
37 #include <QTcpServer>
38 #include <QMutex>
39 #include "cxEnumConverter.h"
40 #include "boost/shared_ptr.hpp"
41 
42 namespace cx {
44 {
50 };
51 } // namespace cx
52 
54 
58 #define assertRunningInObjectThread() \
59 { \
60  if (QThread::currentThread()!=this->thread()) \
61  { \
62  CX_LOG_ERROR() \
63  << QString("Method should be called in the object's thread [%1] only, current thread = [%2]") \
64  .arg(this->thread()->objectName()) \
65  .arg(QThread::currentThread()->objectName()); \
66  } \
67 } \
68 
69 
70 
71 namespace cx {
72 
73 typedef boost::shared_ptr<class SocketConnector> SocketConnectorPtr;
74 
75 class cxResource_EXPORT SocketConnection : public QObject
76 {
77  Q_OBJECT
78 public:
79  explicit SocketConnection(QObject *parent = 0);
80 
81  struct cxResource_EXPORT ConnectionInfo
82  {
83  QString role;
84  QString protocol;
85  QString host;
86  int port;
87 
88  bool operator==(const ConnectionInfo& rhs) const;
89  bool isServer() const;
90  bool isClient() const;
91  bool isLocalhostConnection() const;
92  QString getDescription() const;
93  };
94 
99  ConnectionInfo getConnectionInfo();
100  CX_SOCKETCONNECTION_STATE getState();
101  virtual void setConnectionInfo(ConnectionInfo info);
102 
103  virtual void requestConnect();
104  virtual void requestDisconnect();
105 
106 public slots:
107  bool sendData(const char* data, qint64 maxSize);
108 
109 signals:
110  void connectionInfoChanged();
111  void stateChanged(CX_SOCKETCONNECTION_STATE status);
112  void connected();
113  void disconnected();
114  void error();
115 
116 private slots:
117  void internalConnected();
118  void internalDisconnected();
119  void internalError(QAbstractSocket::SocketError socketError);
120  virtual void internalDataAvailable() = 0;
121 
122 protected:
123  virtual void setProtocol(QString protocolname) = 0;
124  SocketConnectorPtr createConnector(ConnectionInfo info);
125  bool socketIsConnected();
126  bool enoughBytesAvailableOnSocket(int bytes) const;
127  bool socketReceive(void *packPointer, int packSize) const;
128  QStringList getAllServerHostnames();
129  void setCurrentConnectionInfo();
130  void stateChange(CX_SOCKETCONNECTION_STATE newState);
131  CX_SOCKETCONNECTION_STATE computeState();
132 
133  QTcpSocket* mSocket;
134 
138 
139 protected:
141 };
142 
143 } //namespace cx
144 
145 #endif // CXSOCKETCONNECTION_H
CX_SOCKETCONNECTION_STATE
ConnectionInfo mNextConnectionInfo
info to be used for the next connect(), mutexed.
CX_SOCKETCONNECTION_STATE mCurrentState
SNW_DECLARE_ENUM_STRING_CONVERTERS(cx, CX_SOCKETCONNECTION_STATE)
boost::shared_ptr< class SocketConnector > SocketConnectorPtr
bool operator==(const RegistrationTransform &lhs, const RegistrationTransform &rhs)
SocketConnectorPtr mConnector