-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.cpp
395 lines (352 loc) · 13.2 KB
/
server.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "include/serversocket.hpp"
#include "include/clientsocket.hpp"
#include "include/socketexception.hpp"
#include "commons.h"
#include <iostream>
#include <thread>
#include <string>
#include <vector>
#include <unistd.h>
#include <poll.h>
auto phase_itr = phases.begin();
// A handy little struct to keep track of the order of the toSockets and fromSockets
struct RequestingServerDetails {
int to_idx, from_idx;
};
class Server {
public:
int m_designation;
ServerSocket m_controllerSocket;
ServerSocket m_serverSocket;
ServerSocket m_peerFromSockets[NUM_SERVERS];
ClientSocket m_peerToSockets[NUM_SERVERS];
std::vector<int> m_peers;
ObjectX SendInfo;
std::vector<ObjectX> SetP;
int M = 0;
int N;
Server(int port, int designation): m_serverSocket(port) {
ServerSocket new_socket;
m_designation = designation;
m_serverSocket.accept(new_socket);
std::cout << "Connected to controller" << std::endl;
m_controllerSocket = new_socket;
SendInfo.server_id = designation;
SendInfo.VN = 1;
SendInfo.RU = NUM_SERVERS;
SendInfo.DS = 0;
}
void controllerCommand(){
int endphase_flag = 0;
while (!endphase_flag) {
int* controllerMsg = new int;
int bytes_read;
do {
bytes_read = m_controllerSocket.recv(controllerMsg, sizeof(int));
} while(bytes_read==0);
switch(*controllerMsg) {
case NONE: {
none();
}
break;
case START_PHASE: {
std::cout << "\n\n\nReceived a Phase Start message from controller\n";
phase();
}
break;
case UPDATE: {
std::cout << "\n\n\nReceived UPDATE on object X from controller\n";
update();
reply();
}
break;
case END_PHASE: {
endphase_flag = 1;
std::cout << "\n\n\nReceived END_PHASE from controller\n";
}
break;
}
//std::cout << "VN:" << SendInfo.VN << "\tRU:" << SendInfo.RU << "\tDS:" << SendInfo.DS << std::endl;
}
}
void phase() {
// Socket connection sever
m_peers.clear();
SetP.clear();
for (ServerSocket peer_socket: m_peerFromSockets) {
if (peer_socket.is_valid()) {
try {
peer_socket.close();
} catch (SocketException& e) {}
}
}
for (ClientSocket peer_socket: m_peerToSockets) {
if (peer_socket.is_valid()) {
try {
peer_socket.close();
} catch (SocketException &e) {}
}
}
//Form connections
std::string current_phase = *phase_itr;
std::string separator = ":";
std::size_t partition_end;
std::size_t partition_start = 0;
do {
partition_end = current_phase.find(separator, partition_start);
std::size_t nchars = partition_end - partition_start;
std::string partition = current_phase.substr(partition_start, nchars);
std::cout << "Partition is " << partition << std::endl;
partition_start = partition_end + 1;
// Am I in this partition?
auto designation = std::to_string(m_designation);
if (partition.find(designation) != std::string::npos) {
// Who are my neighbors?
for (int peer: partition) {
peer -= '0';
if (peer != m_designation) {
m_peers.push_back(peer);
}
}
std::cout << "My peers are:";
for (int peer: m_peers)
std::cout << " " << peer;
std::cout << std::endl;
//Listen for connections from other servers
auto connFromUtil = [=]() {
for (int i = 0; i < m_peers.size(); i++) {
ServerSocket new_socket;
m_serverSocket.accept(new_socket);
std::cout << "Found a connection...\n";
m_peerFromSockets[i] = new_socket;
}
};
std::thread connFromThread = std::thread(connFromUtil);
sleep(1);
//Connect to other servers
auto connToUtil = [=]() {
for (int peer: m_peers) {
m_peerToSockets[peer] = ClientSocket();
std::string ip = servers[peer].ip;
int port = servers[peer].port;
std::cout << "IP: " << ip << "\tPort: " << port << std::endl;
m_peerToSockets[peer].connect(ip, port);
std::cout << "Connected to the server\n";
}
};
std::thread connToThread = std::thread(connToUtil);
connToThread.join();
connFromThread.join();
break;
}
} while(partition_end != std::string::npos);
phase_itr++;
sleep(1);
// Inform the controller that the partitioning is complete.
int* success_msg_buffer = new int;
*success_msg_buffer = 1;
m_controllerSocket.send(success_msg_buffer, sizeof(int));
//std::cout << "Sent the partitioning confirmation to the controller.\n";
}
void none() {
RequestingServerDetails request_details = listenForVoteReq();
sendVote(request_details.to_idx);
listenForUpdate(request_details.from_idx);
}
RequestingServerDetails listenForVoteReq() {
struct pollfd pfds[m_peers.size()];
for(int i = 0; i < m_peers.size(); i++) {
pfds[i].fd = m_peerFromSockets[i].get_file_desc();
pfds[i].events = POLLIN;
}
int ret = 0;
int recv_socket_idx = -1;
RequestingServerDetails request_deets = {-1, -1};
int cycle=0;
do {
ret = poll(pfds, m_peers.size(), 100);
if (ret) {
for (int j = 0; j < m_peers.size(); j++) {
if (pfds[j].revents & POLLIN) {
auto *ListenToReqbuf = new ObjectX;
int bytes_read = m_peerFromSockets[j].recv((ObjectX *) ListenToReqbuf, sizeof(ObjectX));
if (bytes_read < 0)
std::cerr << "Error receiving Vote Request from server " << m_peers[j] << std::endl;
else {
if (bytes_read >= sizeof(ObjectX)) {
recv_socket_idx = ListenToReqbuf->server_id;
request_deets.from_idx = j;
request_deets.to_idx = recv_socket_idx;
//std::cout << "Vote details: " << request_deets.from_idx << "\t" << request_deets.to_idx << std::endl;
std::cout << "Received a vote request from server " << recv_socket_idx << std::endl;
break;
}
}
}
}
}
cycle++;
} while(cycle<4 && recv_socket_idx==-1);
return request_deets;
}
void sendVote(int requesting_socket) {
if (requesting_socket != -1) {
m_peerToSockets[requesting_socket].send(&SendInfo, sizeof(ObjectX));
std::cout << "Sent Vote to peer " << requesting_socket << std::endl;
}
}
void listenForUpdate(int requesting_socket) {
if (requesting_socket != -1) {
ObjectX *update_buffer = new ObjectX;
m_peerFromSockets[requesting_socket].recv(update_buffer, sizeof(ObjectX));
if (update_buffer->VN == -1)
std::cout << "Update was aborted" << std::endl;
else {
SendInfo = *update_buffer;
SendInfo.server_id = m_designation;
std::cout << "ObjectX updated to - VN:" << SendInfo.VN << "\tRU:" << SendInfo.RU
<< "\tDS:" << SendInfo.DS << std::endl;
}
}
else {
std::cout << "No update received for this round\n";
}
}
void reply() {
int reply_buf = 1;
m_controllerSocket.send(&reply_buf, sizeof(reply_buf));
std::cout << "Replied to controller" << std::endl;
}
void update() {
SetP.clear();
sendVoteReq();
getVotes();
int ret_dist = isDistinguished();
if(ret_dist) {
catchUp();
doUpdate();
}
else
abort();
}
void sendVoteReq() {
for (auto peer_idx: m_peers) {
m_peerToSockets[peer_idx].send(&SendInfo, sizeof(ObjectX));
}
std::cout << "Sent Vote Request to peers\n";
}
void getVotes() {
for (int i=0; i<m_peers.size(); i++) {
auto* recv_buffer = new ObjectX;
try {
m_peerFromSockets[i].recv(recv_buffer, sizeof(ObjectX));
} catch (SocketException&) { std::perror("getVotes"); }
std::cout << "Server:" << recv_buffer->server_id <<
" VN:" << recv_buffer->VN <<
" DS:" << recv_buffer->DS <<
" RU:" << recv_buffer-> RU << std::endl;
//Store the votes in an array
SetP.push_back(*recv_buffer);
}
SetP.push_back(SendInfo);
}
int isDistinguished() {
int is_Distinguished_flag = 0;
for(auto site: SetP) {
if (site.VN > M)
M = site.VN;
}
std::cout << "Latest version number is " << M << std::endl;
std::vector<ObjectX> setI;
for(auto site: SetP) {
if(site.VN == M) {
setI.push_back(site);
}
}
//Find N
N = setI[0].RU;
int cardI = setI.size();
std::cout << "card(I):" << cardI << " N:" << N << std::endl;
//Check if server is part of distinguished partition
if(cardI > (N/2)) {
is_Distinguished_flag = 1;
}
else if (N%2==0 && cardI==N/2) {
for(auto site: SetP) {
if(site.server_id == SendInfo.DS) {
is_Distinguished_flag = 1;
break;
}
}
}
return is_Distinguished_flag;
}
void catchUp() {
if(SendInfo.VN < M) {
std::cout << "Server has outdated version of X" << std::endl;
SendInfo.VN = M;
}
else
std::cout << "Server has latest version of X" << std::endl;
}
void doUpdate() {
SendInfo.VN = M+1;
SendInfo.RU = SetP.size(); // Including the server that wants to perform the update.
int min_server_id = SendInfo.server_id;
for(auto site: SetP) {
min_server_id = min_server_id < site.server_id ? min_server_id : site.server_id;
}
SendInfo.DS = min_server_id;
for (auto peer_id: m_peers) {
m_peerToSockets[peer_id].send(&SendInfo, sizeof(ObjectX));
}
std::cout << "Updated X" <<std::endl;
}
void abort() {
ObjectX* abort_info = new ObjectX;
abort_info->VN = -1;
abort_info->DS = -1;
abort_info->RU = -1;
abort_info->server_id = m_designation;
for (auto peer_id: m_peers) {
m_peerToSockets[peer_id].send(abort_info, sizeof(ObjectX));
}
std::cout << "Aborted" << std::endl;
}
void close() {
sleep(1);
for (ServerSocket peer_socket: m_peerFromSockets) {
if (peer_socket.is_valid()) {
try {
peer_socket.close();
} catch (SocketException& e) {}
}
}
for (ClientSocket peer_socket: m_peerToSockets) {
if (peer_socket.is_valid()) {
try {
peer_socket.close();
} catch (SocketException& e) {}
}
}
try {
m_controllerSocket.close();
} catch (SocketException& e) {}
try {
m_serverSocket.close();
} catch (SocketException& e) {}
}
};
int main(int argc, char** argv) {
if (argc < 3) {
std::cout << "Args: <server_port> <server_designation>\n";
return 1;
}
int port, designation;
port = std::atoi(argv[1]);
designation = std::atoi(argv[2]);
Server server(port, designation);
server.controllerCommand();
server.close();
return 0;
}