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 modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (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
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Classified Ads. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #ifndef CONNECTION_H
22 #define CONNECTION_H
23 #include <QTcpSocket> // for SocketError
24 #include <QHostAddress> // for ipv6 addr type
25 #include "protocol.h"
26 #include "node.h"
27 
28 class QSslSocket ;
29 class QByteArray ;
30 class MController ;
31 class Model ;
32 
41 class Connection : public QObject {
42  Q_OBJECT
43 public:
47  Open,
50  };
58  public:
60  virtual bool dataReceived(const QByteArray& aData,
61  Connection& aConnection ) = 0 ;
63  virtual void connectionClosed(Connection *aDeletee) = 0 ;
67  virtual void connectionReady(Connection *aBusinessEntity) = 0 ;
68  } ;
72  Connection(int aSocketDescriptor,
73  ConnectionObserver& aObserver,
74  Model& aModel,
75  MController& aController);
94  Connection(const QHostAddress& aAddr,
95  const int aPort,
96  ConnectionObserver &aObserver,
97  Model& aModel,
98  MController& aController,
99  const Hash& aFpOfNodeToTry ) ;
103  ~Connection() ;
107  static bool Ipv6AddressesEqual(const Q_IPV6ADDR& aAddr1,
108  const Q_IPV6ADDR& aAddr2) ;
118  Node* node() const {
119  return iNodeOfConnectedPeer ;
120  }
121 
126  void setNode(Node* aNode) {
127  iNodeOfConnectedPeer = aNode ;
128  }
129 
133  QHostAddress peerAddress() const ;
134 
147  return iConnectionState;
148  }
155  return iStageOfBucketFill ;
156  }
161  iStageOfBucketFill = aStage ;
162  }
163 
167  bool isInbound() const {
168  return iSocketIsIncoming ;
169  } ;
173  time_t getOpenTime() const {
174  return iSocketOpenTime ;
175  } ;
180  Hash getPeerHash() const ;
184  unsigned long bytesIn() const {
185  return iBytesIn ;
186  }
190  unsigned long bytesOut() const {
191  return iBytesOut ;
192  }
193 public slots:
194  void socketReady() ;
202  void run() ;
207  bool isInPrivateAddrSpace() const ;
208 signals:
209  void error(QTcpSocket::SocketError socketError);
210  void finished() ;
211  void blackListNetworkAddr(QHostAddress aAddr) ;
212 protected:
213  void performRead() ;
214  void readLoop() ;
228  void checkForBucketFill() ;
229 public:// these are public because datamodel access these, via lock()
241  QList <SendQueueItem> iSendQueue ;
251  QList<QByteArray *> iNextProtocolItemToSend ;
252 public:
253  bool iNeedsToRun ;
256 protected: // these are not public
264  QSslSocket *iSocket ;
269  QByteArray *iBytesRead ;
282  QHostAddress iAddrToConnect ;
284  const int iPortToConnect ;
286  const int iSocketDescriptor ;
288  const bool iSocketIsIncoming ;
316  quint32 iTimeOfBucketFill ;
323  const time_t iSocketOpenTime ;
325  unsigned long iBytesIn ;
327  unsigned long iBytesOut ;
328 } ;
329 #endif
void error(QTcpSocket::SocketError socketError)
Node * iNodeOfConnectedPeer
Definition: connection.h:280
bool isInbound() const
Definition: connection.h:167
const int iPortToConnect
Definition: connection.h:284
unsigned long bytesIn() const
Definition: connection.h:184
unsigned long iBytesOut
Definition: connection.h:327
ConnectionState connectionState() const
Definition: connection.h:146
Node is a peer in network.
Definition: node.h:39
void setStageOfBucketFill(ProtocolItemType aStage)
Definition: connection.h:160
QSslSocket * iSocket
Definition: connection.h:264
Hash iFpOfNodeWeTrying
Definition: connection.h:298
void runForIncomingConnections()
void finished()
quint32 iBytesExpectedInPacketBeingRead
Definition: connection.h:267
Hash iEndOfBucket
Definition: connection.h:321
void socketReady()
unsigned iNumberOfPacketsReceived
Definition: connection.h:255
ConnectionState iConnectionState
Definition: connection.h:257
const time_t iSocketOpenTime
Definition: connection.h:323
const Hash & fingerPrintOfNodeAttempted()
unsigned long iBytesIn
Definition: connection.h:325
ProtocolItemType iStageOfBucketFill
Definition: connection.h:312
int iInvocationsSinceLastByteReceived
Definition: connection.h:276
Hash is class that carries 160-bit digest.
Definition: hash.h:37
MController & iController
Definition: connection.h:290
const bool iSocketIsIncoming
Definition: connection.h:288
ConnectionObserver & iObserver
Definition: connection.h:259
Definition: connection.h:47
ConnectionState
Definition: connection.h:45
ProtocolItemType stageOfBucketFill() const
Definition: connection.h:154
QHostAddress iAddrToConnect
Definition: connection.h:282
Model & iModel
Definition: connection.h:265
ProtocolItemType
Definition: protocol.h:124
void blackListNetworkAddr(QHostAddress aAddr)
virtual bool dataReceived(const QByteArray &aData, Connection &aConnection)=0
Definition: connection.h:49
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:39
void readLoop()
QHostAddress peerAddress() const
Definition: connection.h:48
void run()
M of the MVC pattern. Handles permanent storage.
Definition: model.h:47
Interface for receiving network traffic.
Definition: connection.h:57
time_t iTimeOfLastActivity
Definition: connection.h:254
unsigned long bytesOut() const
Definition: connection.h:190
Connection(int aSocketDescriptor, ConnectionObserver &aObserver, Model &aModel, MController &aController)
QList< SendQueueItem > iSendQueue
Definition: connection.h:241
static bool Ipv6AddressesEqual(const Q_IPV6ADDR &aAddr1, const Q_IPV6ADDR &aAddr2)
quint32 iTimeOfBucketFill
Definition: connection.h:316
QList< QByteArray * > iNextProtocolItemToSend
Definition: connection.h:251
time_t getOpenTime() const
Definition: connection.h:173
Node * node() const
Definition: connection.h:118
void runForOutgoingConnections()
QByteArray * iBytesRead
Definition: connection.h:269
Hash getPeerHash() const
Definition: connection.h:46
void setNode(Node *aNode)
Definition: connection.h:126
Class that represents a network connection.
Definition: connection.h:41
bool isInPrivateAddrSpace() const
void checkForBucketFill()
bool iNeedsToRun
Definition: connection.h:253
const int iSocketDescriptor
Definition: connection.h:286