File: networkconnectorengine.h

package info (click to toggle)
classified-ads 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,772 kB
  • sloc: cpp: 34,291; tcl: 1,175; xml: 64; makefile: 40
file content (164 lines) | stat: -rw-r--r-- 5,945 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*     -*-C++-*- -*-coding: utf-8-unix;-*-
  Classified Ads is Copyright (c) Antti Järvinen 2013-2018.

  This file is part of Classified Ads.

  Classified Ads is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  Classified Ads is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with Classified Ads; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/



#ifndef NETWORK_CONNECTOR_ENG_H
#define NETWORK_CONNECTOR_ENG_H

#include <QTcpSocket>
#include <QUdpSocket>
#include <QHostAddress>
#include <QPair>
#include "connection.h" // for ConnectionObserver
#include "../controller.h"
#include "../datamodel/model.h"

class ProtocolMessageParser ;

/**
 * @brief Class that will initiate 1 or more network connections.
 *
 * This may be notified about situation where network connection
 * might be in order.
 *
 * This will also initiate first connection to seed node,
 * notifying controller if failed.
 */
class NetworkConnectorEngine : public QThread {
    Q_OBJECT
public:
    /**
     * Constructor
     */
    NetworkConnectorEngine(Controller *aController,
                           Model *aModel,
                           Connection::ConnectionObserver &aObserver) ;
    /**
     * Destructor
     */
    ~NetworkConnectorEngine() ;
    /**
     * this class is a thread, thus run.
     *
     * this method has its own execution context so it won't
     * hurt the UI or the incoming connections.
     *
     * this will ask datamodel for nodes to connect and try to
     * arrange connections to given nodes, some will succeed
     * and some then will be less successful.
     */
    void run();
public slots:
    void threadIsDeleted() ;
    void blackListNetworkAddr(QHostAddress aAddr) ;
    void nodeConnectionAttemptStatus(Connection::ConnectionState aStatus,
                                     const Hash aHashOfAttemptedNode );
    /**
     * Slot called at thread finish. Contains cleanup operations. 
     */
    void aboutToFinish() ; 
signals:
    void error(QTcpSocket::SocketError socketError);
private:
    /** internal method for starting a new connection */
    void spawnNewConnection(const QHostAddress& aAddr,
                            const int aPort,
                            const Hash& aHashOfNodeToConnect) ;
    /** method for updating list of nodes to connect */
    void updateListOfNodesToConnect() ;
    /**
     *  prune from list self and nodes already connected.
     *  @param aListToPrune is list of nodes that check
     *  @return this will return true if there were at least one node
     *          that was already connected
     */
    bool pruneConnectionProspectList(QList<Node *>* aListToPrune) const ;
    /**
     * method for sending advertisement about node in ip protocl
     * broadcast packet
     */
    void sendBroadCast() ;
    /**
     * Method for checking if there is anything in datamodels
     * connection wishlist
     */
    bool tryServeWishListItem() ;
    /**
     * method for creating connections to nodes that are supposed to
     * store private messages destined to profiles whose private
     * keys we have in this node. This is supposed to work in
     * conjunction with @PrivMessageModel::fillBucket() that
     * it will then, in turn, send requests to said nodes about
     * private messages that we're supposed to receive.
     */
    void createConnectionsToNodesStoringPrivateMessages() ;
    /**
     * Sends query (poll) about private messages to given node.
     * Node must be connected.
     * This method polls not only private messages but also
     * profile comments - reason for this is simple: they
     * have the same bucket so as we're already making
     * commections to nodes storing private messaeges we'll
     * find the comments from same bucket.
     *
     * Naturally this only helps the profile operator as he'll
     * this way find his own comments ; for the rest of the
     * comment-reading crowd a separate mechanism needs to be in
     * place.
     */
    void sendPrivateMessagesPoll(const Hash& aNodeFingerPrint) ;
private: // methods:
    bool performDNSLookup(QHostAddress* aAddressToBeSet,
                          const QString& aFromDnsName,
                          bool aDoWeHaveIpv6) ;
public:
    bool iNeedsToRun ;
private: // data
    Controller *iController ;
    Model *iModel ;
    Connection::ConnectionObserver &iConnectionObserver ;
    QList<MNodeModelProtocolInterface::HostConnectQueueItem> iAddressesInOrderToConnect ;
    QUdpSocket* iBroadCastSocket ;
    time_t iTimeOfLastBroadCast ;
    /**
     * this is time when private messages were last polled for any
     * profile ; this is used for sequencing the polls so that
     * each profile gets decent and fair polling periods
     */
    time_t iTimeOfLastMsgPoll ;
    /**
     * this is timestamp of last message poll for individual
     * profile ; this is sent to remote nodes and the nodes are
     * asked to send messages that they've received after
     * the timestamp presented here
     */
    quint32 iTimeOfLastMsgPollForProfile ;
    /**
     * Addresses that we're supposed to not connect. This easily
     * gets at least addresses of self in a box that has
     * multiple network addresses (or multiple temporary
     * ipv6 addresses)
     */
    QList<QHostAddress> iAddressBlacklist ;
    Hash iProfileToPollPrivateMessages ;
    QList<Hash> iPrivateMessagePollWishList ;
} ;
#endif