Classified ads
nodemodel.h
Go to the documentation of this file.
1 /* -*-C++-*- -*-coding: utf-8-unix;-*-
2  Classified Ads is Copyright (c) Antti Jarvinen 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 #ifndef CLASSIFIED_NODEMODEL_H
21 #define CLASSIFIED_NODEMODEL_H
22 #include <QSqlDatabase>
23 #include <QObject>
24 #include "../mcontroller.h" // because enum from there is needed
26 #include "../net/connection.h"
27 #include <QHostAddress>
28 #include <QPair>
29 #include "datamodelbase.h"
30 
31 class QSqlDatabase ;
32 class QMutex ;
33 class Hash ;
34 class QSslCertificate ;
35 class QSslKey ;
36 class Model ;
37 
45 class NodeModel : public ModelBase,
47  Q_OBJECT
48 
49 public:
50  NodeModel(MController *aController, const Model &aModel) ;
51  ~NodeModel() ;
52  virtual Hash& nodeFingerPrint();
53  virtual int listenPortOfThisNode() ;
55  virtual void setListenPortOfThisNode(int port) ;
57  virtual const QSslCertificate& nodeCert() const ;
59  virtual const QSslKey& nodeKey() const ;
69  virtual QByteArray* getNextItemToSend(Connection& aConnection) ;
70 
76  virtual Node* nodeByHash(const Hash& aHash) ;
85  virtual QList<QPair<QHostAddress,int> > getHotAddresses() ;
97  QList<Node *>* getHotNodes(int aMaxNodes) ;
121  virtual bool nodeGreetingReceived(Node& aNode ,
122  bool aWasInitialGreeting = false ) ;
137  virtual QList<Node *>* getNodesAfterHash(const Hash& aHash,
138  unsigned aMaxNodes,
139  int aMaxInactivityMinutes = -1 ) ;
144  bool updateNodeLastConnectTimeInDb(Node& aNode) ;
149  virtual bool updateNodeLastMutualConnectTimeInDb(const Hash& aNodeFp,
150  quint32 aTime ) ;
160  virtual QList<Node *>* getNodesBeforeHash(const Hash& aHash,
161  unsigned aMaxNodes) ;
170  virtual void closeOldestInactiveConnection() ;
175  QString dataDir() ;
187  virtual void addNodeFromBroadcast(const Hash& aNodeFingerPrint,
188  const QHostAddress& aAddr,
189  int aPort ) ;
196  virtual void addNodeToConnectionWishList(Node* aNode) ;
202  virtual bool addNodeToConnectionWishList(const Hash& aNode) ;
203 
209  virtual Node* nextConnectionWishListItem() ;
214  virtual bool isNodeAlreadyConnected(const Node& aNode) const ;
219  virtual bool isNodeAlreadyConnected(const Hash& aHash) const ;
244  virtual Hash bucketEndHash(const Hash& aFingerPrintOfNodeAsking) ;
246  virtual void setDnsName(QString aName) ;
248  virtual QString getDnsName() ;
249 protected:
250  bool insertNodeToDb(Node& aNode) ;
256  bool updateNodeInDb(Node& aNode) ;
258 private:
260  bool saveSslCertToDb(const QByteArray& aCert) ;
261  bool saveSslKeyToDb(const QByteArray& aKey) ;
262  bool loadSslCertFromDb() ;
263  bool loadSslKeyFromDb() ;
265  void countNodes() ;
266  Q_IPV6ADDR ipv6AddrFromUints(quint32 aLeastSignificant,
267  quint32 aLessSignificant,
268  quint32 aMoreSignificant,
269  quint32 aMostSignificant ) const ;
270 signals:
272  const QString& aExplanation) ;
273 private:
276  QSslCertificate* iThisNodeCert ;
277  QSslKey* iThisNodeKey ;
278  QList<QPair<QHostAddress,int> > iHotAddresses ;
279  const Model& iModel ;
280  QString iDataDir ;
281  QList<Node *> iConnectionWishList ;
282 } ;
283 #endif
NodeModel(MController *aController, const Model &aModel)
virtual const QSslCertificate & nodeCert() const
bool insertNodeToDb(Node &aNode)
bool openOrCreateSSLCertificate()
Node is a peer in network.
Definition: node.h:39
bool saveSslCertToDb(const QByteArray &aCert)
virtual Node * nextConnectionWishListItem()
bool saveSslKeyToDb(const QByteArray &aKey)
virtual const QSslKey & nodeKey() const
virtual Hash bucketEndHash(const Hash &aFingerPrintOfNodeAsking)
const Model & iModel
Definition: nodemodel.h:279
virtual void setDnsName(QString aName)
virtual Node * nodeByHash(const Hash &aHash)
virtual bool updateNodeLastMutualConnectTimeInDb(const Hash &aNodeFp, quint32 aTime)
QString iDataDir
Definition: nodemodel.h:280
virtual int listenPortOfThisNode()
bool loadSslKeyFromDb()
QSslCertificate * iThisNodeCert
Definition: nodemodel.h:276
virtual QByteArray * getNextItemToSend(Connection &aConnection)
QList< Node * > iConnectionWishList
Definition: nodemodel.h:281
virtual void closeOldestInactiveConnection()
Hash is class that carries 160-bit digest.
Definition: hash.h:37
CAErrorSituation
Definition: mcontroller.h:48
virtual QString getDnsName()
virtual void addNodeToConnectionWishList(Node *aNode)
virtual bool nodeGreetingReceived(Node &aNode, bool aWasInitialGreeting=false)
QList< QPair< QHostAddress, int > > iHotAddresses
Definition: nodemodel.h:278
virtual void addNodeFromBroadcast(const Hash &aNodeFingerPrint, const QHostAddress &aAddr, int aPort)
Pure-virtual interface class for controller. This is spammed across parties needing access to applica...
Definition: mcontroller.h:39
void countNodes()
virtual QList< Node * > * getNodesAfterHash(const Hash &aHash, unsigned aMaxNodes, int aMaxInactivityMinutes=-1)
datamodel-parts common part. this is inherited and contains common funcs
Definition: datamodelbase.h:38
bool loadSslCertFromDb()
M of the MVC pattern. Handles permanent storage.
Definition: model.h:47
virtual QList< Node * > * getNodesBeforeHash(const Hash &aHash, unsigned aMaxNodes)
QString dataDir()
virtual void setListenPortOfThisNode(int port)
void error(MController::CAErrorSituation aError, const QString &aExplanation)
Q_IPV6ADDR ipv6AddrFromUints(quint32 aLeastSignificant, quint32 aLessSignificant, quint32 aMoreSignificant, quint32 aMostSignificant) const
This is node-specific part of the datamodel.
Definition: nodemodel.h:45
Hash * iFingerPrintOfThisNode
Definition: nodemodel.h:275
virtual QList< QPair< QHostAddress, int > > getHotAddresses()
virtual Hash & nodeFingerPrint()
QList< Node * > * getHotNodes(int aMaxNodes)
virtual bool isNodeAlreadyConnected(const Node &aNode) const
void deleteOldestConnectedNode()
Pure-virtual interface of nodemodel.
Definition: mnodemodelprotocolinterface.h:38
bool updateNodeInDb(Node &aNode)
Class that represents a network connection.
Definition: connection.h:41
MController * iController
Definition: nodemodel.h:274
QSslKey * iThisNodeKey
Definition: nodemodel.h:277
void retrieveListOfHotConnections()
bool updateNodeLastConnectTimeInDb(Node &aNode)