This repository has been archived by the owner on Nov 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask10.cpp
188 lines (164 loc) · 5.11 KB
/
task10.cpp
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
#include <boost/asio.hpp>
#include <iostream>
#include <fstream>
using boost::asio::ip::tcp;
class Node {
public:
Node(int nodeNumber) {
this->nodeNumber = nodeNumber;
}
// Initialize the node withe the given routing table
// Note that the algorithm assumes an identical routing table on all nodes.
void setup(char const *routingTableName) {
// Setup the networking system
ioService = new boost::asio::io_service();
readRoutingTable(routingTableName);
try {
acceptor = new tcp::acceptor(*ioService, routingTable[nodeNumber]);
} catch (std::exception &) {
std::cout << "error listening on " << routingTable[nodeNumber] << std::endl;
throw;
}
}
void run() {
std::cout << "NodeNumber: " << nodeNumber << std::endl;
int amountOfNodes = routingTable.size();
int sum = 0;
//------------------------------------
//calculate receives
int tester = 1;
int receives = 0;
while((nodeNumber & tester) == 0 && (nodeNumber + tester) < amountOfNodes){
++receives;
tester = tester << 1;
}
while((nodeNumber & tester) == 0){
tester = tester << 1;
}
std::cout << "receives: " << receives << std::endl;
//------------------------------------
if(nodeNumber == 0) {
int input = 0;
printf("x[0]: ");
std::cin >> sum;
for(int i(1); i < amountOfNodes; ++i){
std::cout << "x[" << i << "]: ";
std::cin.clear();
std::cin >> input;
sendDataToNode(input, i);
}
} else {
std::cout << "send to node: " << (nodeNumber - tester) << std::endl;
sum = receiveData();
std::cout << "received: " << sum << std::endl;
}
while(receives){
int r = receiveData();
std::cout << "r: " << r << std::endl;
sum += r;
std::cout << --receives << "receives left " << std::endl;
}
if(nodeNumber == 0){
std::cout << "sum: " << sum << std::endl;
}
else
{
sendDataToNode(sum, nodeNumber - tester);
}
std::cin.clear();
int e;
std::cin >> e;
}
private:
// Sends the given data to the given target node and waits
// until the data is received by the remote endpoint.
void sendDataToNode(int x, int targetNodeNumber) {
std::stringstream dataStream;
dataStream << x;
try {
tcp::socket socket(*ioService);
socket.connect(routingTable[targetNodeNumber]);
socket.send(boost::asio::buffer(dataStream.str()));
socket.shutdown(tcp::socket::shutdown_both);
socket.close();
} catch (std::exception &) {
std::cout << "error sending data to " << targetNodeNumber << ": " << routingTable[targetNodeNumber] << std::endl;
throw;
}
}
// Waits until data from any incoming node is received and
// returns the received data.
// Note that this functions accepts data from any endpoint even if
// this endpoint is not entered in the routing table.
int receiveData() {
int x;
char buffer[32];
tcp::socket socket(*ioService);
try {
acceptor->accept(socket);
socket.receive(boost::asio::buffer(buffer, 32));
socket.shutdown(tcp::socket::shutdown_both);
socket.close();
std::stringstream dataStream(buffer);
dataStream >> x;
return x;
} catch (std::exception &) {
std::cout << "error receiving data." << std::endl;
throw;
}
}
// Reads the routing table from the given file.
// The number of nodes envolved in the calculation is derived from
// the routing table.
void readRoutingTable(char const *routingTableName) {
char line[1024];
std::ifstream file;
std::string ipAddress, port;
int nodeNumber = 0;
tcp::resolver resolver(*ioService);
file.open(routingTableName);
while (!file.eof()) {
file.getline(line, 1024);
std::stringstream lineStream(line);
// ignore empty lines
if (lineStream.str().length() == 0) break;
// ignore lines that start with '#'
if (lineStream.str().at(0) == '#') break;
lineStream >> ipAddress;
lineStream >> port;
std::cout << nodeNumber << "= " << ipAddress << ":" << port << std::endl;
tcp::resolver::query query(tcp::v4(), ipAddress, port);
try {
routingTable.push_back(*resolver.resolve(query));
} catch (std::exception &cause) {
std::cout << nodeNumber << ": " << cause.what() << std::endl;
}
++nodeNumber;
}
}
// Node topology data
int nodeNumber;
// network data
boost::asio::io_service *ioService;
std::vector<tcp::endpoint> routingTable;
tcp::acceptor *acceptor;
};
int main(int argc, char const *argv[]) {
int nodeNumber = 0;
char const *routingTableName = "routingTable.txt";
if (argc > 1) {
std::stringstream argumentStream(argv[1]);
argumentStream >> nodeNumber;
}
if (argc > 2) {
routingTableName = argv[2];
}
try {
Node node(nodeNumber);
node.setup(routingTableName);
node.run();
} catch (std::exception &cause) {
std::cout << cause.what() << std::endl;
}
return 0;
}