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
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 #ifndef CLASSIFIED_NODEMODEL_H
22 #define CLASSIFIED_NODEMODEL_H
23 #include <QSqlDatabase>
24 #include <QObject>
25 #include "../mcontroller.h" // because enum from there is needed
27 #include "../net/connection.h"
28 #include <QHostAddress>
29 #include <QPair>
30 #include "datamodelbase.h"
31 
32 class QSqlDatabase ;
33 class QMutex ;
34 class Hash ;
35 class QSslCertificate ;
36 class QSslKey ;
37 class Model ;
38 
46 class NodeModel : public ModelBase,
48  Q_OBJECT
49 
50 public:
51  NodeModel(MController *aController, const Model &aModel) ;
52  ~NodeModel() ;
53  virtual Hash& nodeFingerPrint();
54  virtual int listenPortOfThisNode() ;
56  virtual void setListenPortOfThisNode(int port) ;
58  virtual const QSslCertificate& nodeCert() const ;
60  virtual const QSslKey& nodeKey() const ;
70  virtual QByteArray* getNextItemToSend(Connection& aConnection) ;
71 
77  virtual Node* nodeByHash(const Hash& aHash) ;
86  virtual QList<HostConnectQueueItem> getHotAddresses() ;
98  QList<Node *>* getHotNodes(int aMaxNodes) ;
122  virtual bool nodeGreetingReceived(Node& aNode ,
123  bool aWasInitialGreeting = false ) ;
138  virtual QList<Node *>* getNodesAfterHash(const Hash& aHash,
139  unsigned aMaxNodes,
140  int aMaxInactivityMinutes = -1 ) ;
145  bool updateNodeLastConnectTimeInDb(Node& aNode) ;
150  virtual bool updateNodeLastMutualConnectTimeInDb(const Hash& aNodeFp,
151  quint32 aTime ) ;
161  virtual QList<Node *>* getNodesBeforeHash(const Hash& aHash,
162  unsigned aMaxNodes) ;
171  virtual void closeOldestInactiveConnection() ;
176  QString dataDir() ;
188  virtual void addNodeFromBroadcast(const Hash& aNodeFingerPrint,
189  const QHostAddress& aAddr,
190  int aPort ) ;
198  virtual bool addNodeToConnectionWishList(Node* aNode) ;
204  virtual bool addNodeToConnectionWishList(const Hash& aNode) ;
205 
211  virtual Node* nextConnectionWishListItem() ;
216  virtual bool isNodeAlreadyConnected(const Node& aNode) const ;
221  virtual bool isNodeAlreadyConnected(const Hash& aHash) const ;
246  virtual Hash bucketEndHash(const Hash& aFingerPrintOfNodeAsking) ;
248  virtual void setDnsName(QString aName) ;
250  virtual QString getDnsName() ;
254  virtual void offerNodeToRecentlyFailedList(const Hash& aFailedNodeHash) ;
255 protected:
256  bool insertNodeToDb(Node& aNode) ;
262  bool updateNodeInDb(Node& aNode) ;
267  void timerEvent(QTimerEvent *event);
268 private:
270  bool saveSslCertToDb(const QByteArray& aCert) ;
271  bool saveSslKeyToDb(const QByteArray& aKey) ;
272  bool loadSslCertFromDb() ;
273  bool loadSslKeyFromDb() ;
275  void countNodes() ;
276  Q_IPV6ADDR ipv6AddrFromUints(quint32 aLeastSignificant,
277  quint32 aLessSignificant,
278  quint32 aMoreSignificant,
279  quint32 aMostSignificant ) const ;
280  void removeNodeFromRecentlyFailedList(const Hash& aConnectedHostFingerPrint);
281 signals:
283  const QString& aExplanation) ;
284 private:
287  QSslCertificate* iThisNodeCert ;
288  QSslKey* iThisNodeKey ;
289  QList<HostConnectQueueItem> iHotAddresses ;
290  const Model& iModel ;
291  QString iDataDir ;
292  QList<Node *> iConnectionWishList ;
298  QList<QPair<Hash,unsigned> > iRecentlyFailedNodes ;
299  int iTimerId ;
300 } ;
301 #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:40
bool saveSslCertToDb(const QByteArray &aCert)
virtual Node * nextConnectionWishListItem()
bool saveSslKeyToDb(const QByteArray &aKey)
virtual const QSslKey & nodeKey() const
virtual Hash bucketEndHash(const Hash &aFingerPrintOfNodeAsking)
QList< HostConnectQueueItem > iHotAddresses
Definition: nodemodel.h:289
const Model & iModel
Definition: nodemodel.h:290
virtual void setDnsName(QString aName)
virtual Node * nodeByHash(const Hash &aHash)
virtual bool updateNodeLastMutualConnectTimeInDb(const Hash &aNodeFp, quint32 aTime)
QString iDataDir
Definition: nodemodel.h:291
virtual int listenPortOfThisNode()
bool loadSslKeyFromDb()
QSslCertificate * iThisNodeCert
Definition: nodemodel.h:287
virtual QByteArray * getNextItemToSend(Connection &aConnection)
QList< Node * > iConnectionWishList
Definition: nodemodel.h:292
virtual void closeOldestInactiveConnection()
Hash is class that carries 160-bit digest.
Definition: hash.h:38
CAErrorSituation
Definition: mcontroller.h:52
virtual QString getDnsName()
virtual bool nodeGreetingReceived(Node &aNode, bool aWasInitialGreeting=false)
void timerEvent(QTimerEvent *event)
virtual void addNodeFromBroadcast(const Hash &aNodeFingerPrint, const QHostAddress &aAddr, int aPort)
virtual QList< HostConnectQueueItem > getHotAddresses()
Pure-virtual interface class for controller. This is spammed across parties needing access to applica...
Definition: mcontroller.h:43
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:39
bool loadSslCertFromDb()
M of the MVC pattern. Handles permanent storage.
Definition: model.h:48
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:46
Hash * iFingerPrintOfThisNode
Definition: nodemodel.h:286
virtual Hash & nodeFingerPrint()
QList< Node * > * getHotNodes(int aMaxNodes)
virtual bool isNodeAlreadyConnected(const Node &aNode) const
void removeNodeFromRecentlyFailedList(const Hash &aConnectedHostFingerPrint)
void deleteOldestConnectedNode()
int iTimerId
Definition: nodemodel.h:299
QList< QPair< Hash, unsigned > > iRecentlyFailedNodes
Definition: nodemodel.h:298
Pure-virtual interface of nodemodel.
Definition: mnodemodelprotocolinterface.h:39
bool updateNodeInDb(Node &aNode)
Class that represents a network connection.
Definition: connection.h:43
MController * iController
Definition: nodemodel.h:285
QSslKey * iThisNodeKey
Definition: nodemodel.h:288
virtual void offerNodeToRecentlyFailedList(const Hash &aFailedNodeHash)
void retrieveListOfHotConnections()
virtual bool addNodeToConnectionWishList(Node *aNode)
bool updateNodeLastConnectTimeInDb(Node &aNode)