Classified ads
connection.h
Go to the documentation of this file.
1 /* -*-C++-*- -*-coding: utf-8-unix;-*-
2  Classified Ads is Copyright (c) Antti Järvinen 2013.
3 
4  This file is part of Classified Ads.
5 
6  Classified Ads is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  Classified Ads is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with Classified Ads; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 
22 #ifndef CONNECTION_H
23 #define CONNECTION_H
24 #include <QTcpSocket> // for SocketError
25 #include <QHostAddress> // for ipv6 addr type
26 #include "protocol.h"
27 #include "node.h"
28 
29 class QSslSocket ;
30 class QByteArray ;
31 class MController ;
32 class Model ;
33 class QSslError ;
34 
43 class Connection : public QObject {
44  Q_OBJECT
45 public:
49  Open,
52  };
60  public:
62  virtual bool dataReceived(const QByteArray& aData,
63  Connection& aConnection ) = 0 ;
65  virtual void connectionClosed(Connection *aDeletee) = 0 ;
69  virtual void connectionReady(Connection *aBusinessEntity) = 0 ;
70  } ;
74  Connection(int aSocketDescriptor,
75  ConnectionObserver& aObserver,
76  Model& aModel,
77  MController& aController);
96  Connection(const QHostAddress& aAddr,
97  const int aPort,
98  ConnectionObserver &aObserver,
99  Model& aModel,
100  MController& aController,
101  const Hash& aFpOfNodeToTry ) ;
105  ~Connection() ;
109  static bool Ipv6AddressesEqual(const Q_IPV6ADDR& aAddr1,
110  const Q_IPV6ADDR& aAddr2) ;
120  Node* node() const {
121  return iNodeOfConnectedPeer ;
122  }
123 
128  void setNode(Node* aNode) {
129  iNodeOfConnectedPeer = aNode ;
130  }
131 
135  QHostAddress peerAddress() const ;
136 
149  return iConnectionState;
150  }
157  return iStageOfBucketFill ;
158  }
163  iStageOfBucketFill = aStage ;
164  }
165 
169  bool isInbound() const {
170  return iSocketIsIncoming ;
171  } ;
175  time_t getOpenTime() const {
176  return iSocketOpenTime ;
177  } ;
182  Hash getPeerHash() const ;
186  unsigned long bytesIn() const {
187  return iBytesIn ;
188  }
192  unsigned long bytesOut() const {
193  return iBytesOut ;
194  }
200  bool forciblyCloseSocket() ;
205  bool isInPrivateAddrSpace() const ;
206 public slots:
210  void socketReady() ;
218  void run() ;
222  void encryptedBytesWritten ( qint64 written ) ;
226  void sslErrors ( const QList<QSslError> & errors ) ;
230  void socketError ( QAbstractSocket::SocketError socketError ) ;
234  void disconnected () ;
238  void readyRead () ;
239 signals:
240  void error(QTcpSocket::SocketError socketError);
241  void finished() ;
242  void blackListNetworkAddr(QHostAddress aAddr) ;
245  void connectionAttemptFailed(const Hash& aNodeHash) ;
246 protected:
247  void performRead() ;
248  void readLoop() ;
262  void checkForBucketFill() ;
266  void flushSocket() ;
270  void setupSocketSignals() ;
271 public:// these are public because datamodel access these, via lock()
283  QList <SendQueueItem> iSendQueue ;
293  QList<QByteArray *> iNextProtocolItemToSend ;
294 public:
295  bool iNeedsToRun ;
303  static const unsigned iMinutesBetweenBucketFill = 30 ;
304 protected: // these are not public
305  void msleep(int aMilliSeconds) ;
313  QSslSocket *iSocket ;
318  QByteArray *iBytesRead ;
331  QHostAddress iAddrToConnect ;
333  const int iPortToConnect ;
335  const int iSocketDescriptor ;
337  const bool iSocketIsIncoming ;
372  const time_t iSocketOpenTime ;
374  unsigned long iBytesIn ;
376  unsigned long iBytesOut ;
378  QHostAddress iPeerAddress ;
392 } ;
393 #endif
void error(QTcpSocket::SocketError socketError)
static const unsigned iMinutesBetweenBucketFill
Definition: connection.h:303
Node * iNodeOfConnectedPeer
Definition: connection.h:329
bool isInbound() const
Definition: connection.h:169
const int iPortToConnect
Definition: connection.h:333
unsigned long bytesIn() const
Definition: connection.h:186
unsigned long iBytesOut
Definition: connection.h:376
ConnectionState connectionState() const
Definition: connection.h:148
Node is a peer in network.
Definition: node.h:40
void setStageOfBucketFill(ProtocolItemType aStage)
Definition: connection.h:162
QSslSocket * iSocket
Definition: connection.h:313
Hash iFpOfNodeWeTrying
Definition: connection.h:347
void runForIncomingConnections()
void finished()
quint32 iBytesExpectedInPacketBeingRead
Definition: connection.h:316
Hash iEndOfBucket
Definition: connection.h:370
bool forciblyCloseSocket()
void socketReady()
unsigned iNumberOfPacketsReceived
Definition: connection.h:297
ConnectionState iConnectionState
Definition: connection.h:306
void encryptedBytesWritten(qint64 written)
const time_t iSocketOpenTime
Definition: connection.h:372
const Hash & fingerPrintOfNodeAttempted()
unsigned long iBytesIn
Definition: connection.h:374
ProtocolItemType iStageOfBucketFill
Definition: connection.h:361
int iInvocationsSinceLastByteReceived
Definition: connection.h:325
Hash is class that carries 160-bit digest.
Definition: hash.h:38
MController & iController
Definition: connection.h:339
const bool iSocketIsIncoming
Definition: connection.h:337
ConnectionObserver & iObserver
Definition: connection.h:308
Definition: connection.h:49
ConnectionState
Definition: connection.h:47
ProtocolItemType stageOfBucketFill() const
Definition: connection.h:156
QHostAddress iAddrToConnect
Definition: connection.h:331
Model & iModel
Definition: connection.h:314
ProtocolItemType
Definition: protocol.h:150
void blackListNetworkAddr(QHostAddress aAddr)
int iSleepBetweenSendOperations
Definition: connection.h:387
virtual bool dataReceived(const QByteArray &aData, Connection &aConnection)=0
Definition: connection.h:51
void performRead()
virtual void connectionReady(Connection *aBusinessEntity)=0
virtual void connectionClosed(Connection *aDeletee)=0
Pure-virtual interface class for controller. This is spammed across parties needing access to applica...
Definition: mcontroller.h:43
void msleep(int aMilliSeconds)
void readLoop()
QHostAddress peerAddress() const
Definition: connection.h:50
void run()
M of the MVC pattern. Handles permanent storage.
Definition: model.h:48
Interface for receiving network traffic.
Definition: connection.h:59
time_t iTimeOfLastActivity
Definition: connection.h:296
void flushSocket()
unsigned long bytesOut() const
Definition: connection.h:192
Connection(int aSocketDescriptor, ConnectionObserver &aObserver, Model &aModel, MController &aController)
QList< SendQueueItem > iSendQueue
Definition: connection.h:283
static bool Ipv6AddressesEqual(const Q_IPV6ADDR &aAddr1, const Q_IPV6ADDR &aAddr2)
quint32 iTimeOfBucketFill
Definition: connection.h:365
QList< QByteArray * > iNextProtocolItemToSend
Definition: connection.h:293
time_t getOpenTime() const
Definition: connection.h:175
Node * node() const
Definition: connection.h:120
void setupSocketSignals()
void runForOutgoingConnections()
QByteArray * iBytesRead
Definition: connection.h:318
void readyRead()
void socketError(QAbstractSocket::SocketError socketError)
Hash getPeerHash() const
Definition: connection.h:48
void setNode(Node *aNode)
Definition: connection.h:128
QHostAddress iPeerAddress
Definition: connection.h:378
Hash iPeerHash
Definition: connection.h:391
void disconnected()
int iBytesPendingWrite
Definition: connection.h:383
Class that represents a network connection.
Definition: connection.h:43
void connectionAttemptFailed(const Hash &aNodeHash)
void sslErrors(const QList< QSslError > &errors)
bool isInPrivateAddrSpace() const
void checkForBucketFill()
bool iNeedsToRun
Definition: connection.h:295
const int iSocketDescriptor
Definition: connection.h:335