forked from avinashnarsale/Chandy-Lamport-snapshot-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.cc
236 lines (214 loc) · 7.47 KB
/
controller.cc
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
#include <iostream>
#include <fstream>
#include <string>
#include <mutex>
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <netdb.h>
#include <sstream>
#include <algorithm>
#include <ifaddrs.h>
#include <time.h>
#include <ctime>
#include <list>
#include <thread>
#include <chrono>
#include <random>
#include "bank.pb.h"
using namespace std;
BranchMessage branch_message;
InitBranch branch;
/*
Channel states can be zero and when SerializeToString is done protocol buffer for cpp
will loose its states to reduce allocation to handle this I used alternative logic
where I add dummy $1 into state while sending from branches and remove same after I receive at
controller side. Line#90.
*/
int retrive_snapshots(int snapshotID){
cout << "\nRetrieving snapshot for snapshot_id:" << snapshotID << "..."<< endl;
RetrieveSnapshot retriveSnap;
retriveSnap.set_snapshot_id(snapshotID);
branch_message.set_allocated_retrieve_snapshot(&retriveSnap);
string retrive_output;
if (!branch_message.SerializeToString(&retrive_output)) {
cerr << "Failed to write branch message." << endl;
return -1;
}else{
for (int i = 0; i < branch.all_branches_size(); i++) {
InitBranch_Branch branch_all = branch.all_branches(i);
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char buffer[4096] = {0};
//memset(&serv_addr, '0', sizeof(serv_addr));
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
printf("\nERROR: Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(branch_all.port());
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, branch_all.ip().c_str(), &serv_addr.sin_addr)<=0) {
printf("\nERROR: Invalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
printf("\nERROR: Connection Failed \n");
return -1;
}
send(sock, retrive_output.c_str(), retrive_output.size(), 0);
valread=read( sock, buffer, 4096);
if(valread<0){
cerr << "ERROR: In reading buffer." << endl;
return -1;
}else{
buffer[valread] = '\0';
BranchMessage return_bm;
return_bm.ParseFromString(buffer);
if(return_bm.has_return_snapshot()){
ReturnSnapshot returnSnap=return_bm.return_snapshot();
if(returnSnap.has_local_snapshot()){
ReturnSnapshot_LocalSnapshot localreturn=returnSnap.local_snapshot();
cout << "Snapshot of branch " << branch_all.name() << " :"<< endl;
cout << "Current Balance:" << localreturn.balance() << endl;
for (int i=0; i < localreturn.channel_state_size(); i++){
cout << "States of channel#:" << i << " = " << localreturn.channel_state(i)-1 << endl;
}
}
}else{
cerr << "ERROR: In receiving snapshot_id. " << endl;
return -1;
}
}
close(sock);
}
}
branch_message.release_retrieve_snapshot();
}
int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 3) {
cerr << "Usage: " << argv[0] << "./controller <balance> <branch_list>" << endl;
return -1;
}
ifstream infile(argv[2]);
string line;
int number_of_branches=0;
while (getline(infile, line))
{
number_of_branches++;
istringstream iss(line);
string in_branch_name, in_ip, in_port;
if (!(iss >> in_branch_name >> in_ip >> in_port)) { break; }
InitBranch_Branch* branch_input=branch.add_all_branches();
branch_input->set_name(in_branch_name);
branch_input->set_ip(in_ip);
branch_input->set_port(stoi(in_port));
}
infile.close();
if(number_of_branches>0){
branch.set_balance(atoi(argv[1])/number_of_branches);
}else{
cerr << "ERROR: No Branch exist, cannot split balance. " << endl;
return -1;
}
string output;
branch_message.set_allocated_init_branch(&branch);
if (!branch_message.SerializeToString(&output)) {
cerr << "ERROR: Failed to write branch message." << endl;
return -1;
}
cout << "Initial balance for all Branches: " << branch.balance() << endl;
int i = 4;
for (int i = 0; i < branch.all_branches_size(); i++) {
InitBranch_Branch branch_all = branch.all_branches(i);
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
//memset(&serv_addr, '0', sizeof(serv_addr));
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
printf("\nERROR: Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(branch_all.port());
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, branch_all.ip().c_str(), &serv_addr.sin_addr)<=0) {
printf("\nERROR: Invalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
printf("\nERROR: Connection Failed \n");
return -1;
}
send(sock, output.c_str() , output.size(), 0);
cout << "Branch: " << branch_all.name() << " added successfully. " << endl;
close(sock);
branch_all.clear_name();
branch_all.clear_ip();
branch_all.clear_port();
}
branch_message.release_init_branch();
//sleep time for initialization
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
int snapID=1;
while(1)
{
cout << "Initializing of snapshot(#"<<snapID<< ")" << endl;
InitSnapshot initSnapshot;
initSnapshot.set_snapshot_id(snapID);
branch_message.set_allocated_init_snapshot(&initSnapshot);
string insnap_output;
if (!branch_message.SerializeToString(&insnap_output)) {
cerr << "Failed to write branch message." << endl;
return -1;
}else{
std::random_device seeder;
std::mt19937 engine(seeder());
std::uniform_int_distribution<int> dist(0,branch.all_branches_size()-1);
int random_branch_id = dist(engine);
InitBranch_Branch branch_all = branch.all_branches(random_branch_id);
cout << "Selected random branch: " << branch_all.name() << " for initialization of snapshot" << endl;
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
//memset(&serv_addr, '0', sizeof(serv_addr));
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
printf("\nERROR: Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(branch_all.port());
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, branch_all.ip().c_str(), &serv_addr.sin_addr)<=0) {
printf("\nERROR: Invalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
printf("\nERROR: Connection Failed \n");
return -1;
}
send(sock, insnap_output.c_str(), insnap_output.size(), 0);
close(sock);
}
branch_message.release_init_snapshot();
std::this_thread::sleep_for(std::chrono::milliseconds(9000));
// retrive snapshot
retrive_snapshots(snapID);
snapID++;
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}