-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.hh
206 lines (152 loc) · 5.87 KB
/
main.hh
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef PEERSTER_MAIN_HH
#define PEERSTER_MAIN_HH
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QTextEdit>
#include <QLineEdit>
#include <QUdpSocket>
#include <QKeyEvent>
#include <QHostInfo>
#include <QUuid>
#include <QTimer>
#include <QGroupBox>
#include <QTabWidget>
#include <QListWidget>
#include <QHBoxLayout>
#include <QVariant>
#include <QAction>
#include <QButtonGroup>
#include <QHeaderView>
#include <QPushButton>
#include <QWidget>
#include <QVector>
#include <QQueue>
#include <QTime>
#include <QHash>
#include <QSet>
#include <QtCrypto>
#include <QFileDialog>
#include <QTableWidget>
#include <QLabel>
#include "peer.hh"
class NetSocket : public QUdpSocket {
Q_OBJECT
public:
NetSocket();
bool bind(); // Bind this socket to a Peerster-specific default port.
private:
int myPortMin, myPortMax;
};
class ChatDialog : public QDialog {
Q_OBJECT
public:
ChatDialog();
NetSocket *sock;
QString origin;
//const QString origin = (QHostInfo::localHostName().split('.')[0] + "-" +
// (QUuid::createUuid().toString()).remove(QRegExp("\\{|\\}|\\-")));
public slots:
void receiveMessage();
void gotChatReturnPressed();
void gotPeerReturnPressed();
void flipCoin(); // Flip coin or rumor mongering time out
void antiEntropyTimeout();
void sendRoutingMessage(); // send Routing Message to all neighbor periodically
void lookUpHost(QHostInfo host);
void gotPrivateMsgEntered(QString, QString);
void openPrivateDialog(QListWidgetItem *item);
void gotAddFilePressed();
void gotSearchFilePressed();
void directDownloadFile();
void floodPeriodically();
void clickToDownload(QListWidgetItem *item);
private:
// GUI
QWidget *horizontalLayoutWidget;
QHBoxLayout *horizontalLayout;
QGroupBox *leftPanel;
QTabWidget *rightPanel;
QWidget *peerTab;
QWidget *recentTab;
QWidget *fileTab;
QWidget *downloadTab;
QWidget *searchTab;
QLineEdit *peerEdit;
QListWidget *peerDisplay;
QListWidget *recentDisplay;
QPushButton *peerBtn;
QPushButton *addFileBtn;
QFileDialog *fileDialog;
QTextEdit *textview; // Msg Display
QTextEdit *textline; // Msg Input
QTableWidget *fileView; // Msg Input
QGroupBox *bottomSection;
QTabWidget *bottomPanel;
QLineEdit *ddMeta; // Direct download Metafile
QLineEdit *ddName; // Direct download File name
QLineEdit *ddFrom; // Direct download from origin
QPushButton *ddBtn; // Direct download button
QLineEdit *searchFile;
QPushButton *searchBtn;
QListWidget *searchResult;
// Local Data
//QMap<QString, QMap<quint16, QString> > messageMap; // History Message Database, <Origin, <SeqNo, text>>
QMap<QString, QMap<quint16, QVariantMap> > messageMap; // History Message Database, <Origin, <SeqNo, text>>
QMap<QString, QVariant> statusMap; // Local Status Map, <Origin, SeqNo>
QVariantMap mongeringMsg;// Most recent mongering rumor
bool isShift; // If shift key is hold or not
QTimer *antiEntropyTimer;
QTimer *routingRumorTimer;
QTimer *searchTimer;
QMap<QString, Peer *> peerMap;
QQueue<QString> peerInputQueue; // To handle the concurrency of add peer actions.
QHash<QString, QPair<QHostAddress, quint16> > routingTable; // Next-hop routing table
bool noForward;
QHash<QByteArray, QByteArray> fileBlockHash; // <Block Hash, Block File>
QHash<QByteArray, QVariantMap> fileDisplay;
QHash<QByteArray, QVariantMap> metafileList; // <Metafile Hash, Meta File Map>
QHash<QByteArray, QSet<QByteArray>> downloadingFile;
QHash<QByteArray, QVariantMap> downloadFileBlock; // <Block Hash, <"block": Block File, "belong": Metafile Hash>>
QHash<QByteArray, QVariantMap> downloadMetafile; // <Metafile Hash, Meta File Map>
QMap<QString, QVariant> searchStatus; // <<"Budget", uint>, <"Search", QString>
QSet<QByteArray> searchResultSet;
// Functions
bool eventFilter(QObject *obj, QEvent *ev); // If press enter to send msg or not
void receiveStatusMessage(QMap<QString, QVariant> senderStatus, QHostAddress address, quint16 port);
void receiveRumorMessage(QMap<QString, QVariant> rumor, QHostAddress address, quint16 port);
void receivePrivateMessage(QMap<QString, QVariant> privateMsg, QHostAddress address, quint16 port);
void receiveBlockRequest(QMap<QString, QVariant> request);
void receiveBlockReply(QMap<QString, QVariant> reply);
//void mongerRumor(QString chatText, QString origin, quint16 seqNo, QHostAddress address = QHostAddress::LocalHost,
// quint16 port = 0);
void sendMessage(QMap<QString, QVariant> msg, QHostAddress address, quint16 port);
void sendRumorMessage(QMap<QString, QVariant> rumor, QHostAddress address = QHostAddress::LocalHost,
quint16 port = 0);
void sendStatus(QMap<QString, QVariant> statusMap, QHostAddress address, quint16 port);
void sendPrivateMessage(QMap<QString, QVariant> privateMsg, QHostAddress address, quint16 port);
Peer *pickNeighbors();
void addPeer(QHostAddress address, quint16 port, QString host);
void insertRoutingTable(QString origin, QHostAddress address, quint16 port);
void receiveSearchRequest(QMap<QString, QVariant> reply);
void receiveSearchReply(QMap<QString, QVariant> reply);
void floodSearchRequest(QMap<QString, QVariant> request);
void insertFileView(QString name, QString size, QByteArray hash, QString status, QString origin);
};
class PrivateDialog : public QDialog {
Q_OBJECT
friend class ChatDialog;
public:
PrivateDialog() = default;
explicit PrivateDialog(QString origin);
public slots:
void gotSendPressed();
signals:
void privateMsgSent(QString origin, QString text);
private:
QString origin;
QGroupBox *groupBox;
QTextEdit *textEdit;
QPushButton *sendBtn;
};
#endif // PEERSTER_MAIN_HH