-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.cc
More file actions
149 lines (103 loc) · 3.33 KB
/
router.cc
File metadata and controls
149 lines (103 loc) · 3.33 KB
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
#include "router.hh"
#include "helper.hh"
#include "main.hh"
#define HOP_LIMIT 10
Router::Router(NetSocket *socket, bool nf)
{
sock = socket;
noForward = nf;
}
void
Router::processRumor(const QVariantMap& rumor,
const QHostAddress& sender,
const quint16 port)
{
//qDebug() << rumor;
QString origin = rumor["Origin"].toString();
quint32 seqNo = rumor["SeqNo"].toInt();
if (origin != me){
bool neworg = !routingTable.contains(origin);
if (neworg || currHighest[origin] < seqNo){
currHighest[origin] = seqNo;
routingTable[origin] = QPair<QHostAddress, quint16>(sender, port);
}
else if (rumor.contains("LastIP") && rumor.contains("LastPort")){
if (seqNo == currHighest[origin]){
QHostAddress holeIP(rumor["LastIP"].toInt());
quint16 holePort = rumor["LastPort"].toInt();
routingTable[origin] = QPair<QHostAddress, quint16>(holeIP, holePort);
}
}
if(neworg){
emit newOrigin(origin);
}
}
}
void
Router::sendMessage(const QString& message,const QString& destination)
{
QVariantMap messageMap;
messageMap["Dest"] = destination;
messageMap["ChatText"] = message;
messageMap["HopLimit"] = HOP_LIMIT;
messageMap["Origin"] = me;
QByteArray arr = Helper::SerializeMap(messageMap);
if (routingTable.contains(destination)){
QPair<QHostAddress, quint16> dest = routingTable[destination];
// qDebug() << "Sending: " << message;
//qDebug() << dest.first << ":" << dest.second;
sock->writeDatagram(arr, dest.first, dest.second);
}
}
void
Router::sendMap(const QMap<QString, QVariant>& msg, const QString& destination)
{
QMap<QString, QVariant> real_msg;
real_msg = msg;
real_msg.insert("Dest", destination);
real_msg.insert("HopLimit", HOP_LIMIT);
real_msg.insert("Origin", me);
if (destination == me){
if (msg.contains("Paxos"))
emit toPaxos(real_msg);
}
QByteArray arr = Helper::SerializeMap(real_msg);
if (routingTable.contains(destination)){
qDebug() << real_msg;
QPair<QHostAddress, quint16> dest = routingTable[destination];
sock->writeDatagram(arr, dest.first, dest.second);
}
//qDebug() << msg;
}
void
Router::receiveMessage(QVariantMap& msg)
{
//qDebug() << "Received: " << msg;
QString destination = msg["Dest"].toString();
int hopLimit = msg["HopLimit"].toInt();
if (destination == me){
if (msg.contains("ChatText"))
emit privateMessage(msg["ChatText"].toString(), msg["Origin"].toString());
else if (msg.contains("BlockRequest")){
//qDebug() << "Got block request, sending to dispatcher";
emit blockRequest(msg);
}
else if ((msg.contains("BlockReply") && msg.contains("Data")) ||
(msg.contains("SearchReply") && msg.contains("MatchNames") && msg.contains("MatchIDs"))){
//qDebug() << "Got reply, sending to filerequests";
emit toFileRequests(msg);
}
else if (msg.contains("Paxos")){
qDebug() << "Got paxos message, sending to paxos";
emit toPaxos(msg);
}
}
else if (!noForward && hopLimit > 0){
if (--hopLimit != 0){
msg["HopLimit"] = hopLimit;
QPair<QHostAddress, quint16> dest = routingTable[destination];
QByteArray arr = Helper::SerializeMap(msg);
sock->writeDatagram(arr, dest.first, dest.second);
}
}
}