Commit 31ac490a authored by mayankkakad's avatar mayankkakad

server adding, updating fingertables done (errors resolved)

parent 26eb00cd
...@@ -72,9 +72,37 @@ public: ...@@ -72,9 +72,37 @@ public:
getAddressResponder.Finish(info,Status::OK,this); getAddressResponder.Finish(info,Status::OK,this);
} }
else if(reqType==ADDADDRESS){ else if(reqType==ADDADDRESS){
ifstream fin;
fin.open(SERVERS);
int size=0;
map<int,string> svs;
do {
string temp;
getline(fin,temp);
if(temp.size()==0)
break;
svs[size++]=temp;
} while(fin);
fin.close();
string addresses[size+1];
int count=0;
string addtoadd=info.address();
int porttoadd=stoi(addtoadd.substr(addtoadd.find(':')+1));
bool flag=false;
for(int i=0;i<size;i++) {
int curr_port=stoi(svs[i].substr(svs[i].find(':')+1));
if(porttoadd<curr_port&&flag==false) {
addresses[count++]=addtoadd;
flag=true;
}
addresses[count++]=svs[i];
}
if(flag==false)
addresses[count++]=addtoadd;
ofstream fout; ofstream fout;
fout.open(SERVERS,ios::app); fout.open(SERVERS);
fout<<info.address()<<endl; for(int i=0;i<count;i++)
fout<<addresses[i]<<endl;
fout.close(); fout.close();
null.set_nothing(0); null.set_nothing(0);
cout<<info.address()<<endl; cout<<info.address()<<endl;
......
...@@ -113,36 +113,42 @@ public: ...@@ -113,36 +113,42 @@ public:
//cout<<"Getting my finger table"<<endl; //cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE); fin.open(FINGER_TABLE);
int nums=0; int nums=0;
for(int i=0;i<16;i++) { do {
string temp; string temp;
getline(fin,temp); getline(fin,temp);
if(temp=="null") if(temp=="null"||temp.size()==0)
break; break;
nums++; fingers[nums++]=stoi(temp);
fingers[i]=stoi(temp); }while(fin);
}
fin.close(); fin.close();
int node=-1; int node=-1;
int next=-1; int next=-1;
//cout<<"Looking for the possible successor and predecessor"<<endl; int my_id=stoi(params.find("LISTENING_PORT")->second);
for(int i=0;i<nums;i++) { if(nums>0&&fingers[nums-1]<id&&my_id>=id) {
if(i>0&&fingers[i]<fingers[i-1]) { node=fingers[nums-1];
node=fingers[i-1]; next=my_id;
next=fingers[i]; }
break; else if(nums>0&&my_id<id&&fingers[0]>=id) {
} node=my_id;
if(fingers[i]>id||i==nums-1) { next=fingers[0];
if(i==nums-1&&fingers[i]>id) { }
else {
for(int i=0;i<nums;i++) {
if(i>0&&fingers[i-1]<id&&fingers[i]>=id) {
node=fingers[i-1]; node=fingers[i-1];
next=fingers[i]; next=fingers[i];
break;
} }
else if(i==nums-1) else if(i>0&&fingers[i]<fingers[i-1]&&fingers[i]>id) {
node=fingers[i];
else {
node=fingers[i-1]; node=fingers[i-1];
next=fingers[i]; next=fingers[i];
break;
}
else if(i==nums-1) {
node=fingers[i];
next=my_id;
break;
} }
break;
} }
} }
if(next!=-1) { if(next!=-1) {
...@@ -155,13 +161,25 @@ public: ...@@ -155,13 +161,25 @@ public:
x.set_id(next); x.set_id(next);
Id y; Id y;
//cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl; //cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
stub->GETPREDECESSOR(&context,x,&y); int mypred;
if(y.id()<id) { if(next!=my_id) {
stub->GETPREDECESSOR(&context,x,&y);
mypred=y.id();
}
else {
fin.open(NEIGHBOURS);
string temp;
getline(fin,temp);
getline(fin,temp);
fin.close();
mypred=stoi(temp.substr(temp.find(':')+1));
}
if(mypred==node) {
//cout<<"Yes it is. So we found the successor and the predecessor"<<endl; //cout<<"Yes it is. So we found the successor and the predecessor"<<endl;
//cout<<"Successor: "<<x.id()<<endl; //cout<<"Successor: "<<x.id()<<endl;
//cout<<"Predecessor: "<<y.id()<<endl; //cout<<"Predecessor: "<<mypred<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(x.id())); successorInfo.set_succaddress("0.0.0.0:"+to_string(x.id()));
successorInfo.set_predaddress("0.0.0.0:"+to_string(y.id())); successorInfo.set_predaddress("0.0.0.0:"+to_string(mypred));
} }
else { else {
//cout<<"No it is not. We will ask the possible predecessor to find the successor of new node"<<endl; //cout<<"No it is not. We will ask the possible predecessor to find the successor of new node"<<endl;
...@@ -187,51 +205,9 @@ public: ...@@ -187,51 +205,9 @@ public:
} }
else { else {
//cout<<"We got no node with id greater than new node"<<endl; //cout<<"We got no node with id greater than new node"<<endl;
if(node==stoi(params.find("LISTENING_PORT")->second)) { //cout<<"Only one node present right now, that is me"<<endl;
//cout<<"Only one node present right now, that is me"<<endl; successorInfo.set_succaddress("0.0.0.0:"+to_string(my_id));
successorInfo.set_succaddress("0.0.0.0:"+to_string(node)); successorInfo.set_predaddress("0.0.0.0:"+to_string(my_id));
successorInfo.set_predaddress("0.0.0.0:"+to_string(node));
}
else if(node>stoi(params.find("LISTENING_PORT")->second)){
string tar_address("0.0.0.0:"+to_string(node));
shared_ptr<Channel> channel=grpc::CreateChannel(tar_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel);
ClientContext context1;
Id x,y,z;
x.set_id(id);
//cout<<"Asking the last node about the successor"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
if(y.id()!=stoi(params.find("LISTENING_PORT")->second)) {
string t_address("0.0.0.0:"+to_string(y.id()));
channel=grpc::CreateChannel(t_address, grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel);
ClientContext context2;
stub->GETPREDECESSOR(&context2,y,&z);
//cout<<"We got the successor and the predecessor"<<endl;
//cout<<"Successor: "<<y.id()<<endl;
//cout<<"Predecessor: "<<z.id()<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(y.id()));
successorInfo.set_predaddress("0.0.0.0:"+to_string(z.id()));
}
else {
//cout<<"We got the successor and the predecessor"<<endl;
//cout<<"Successor: "<<y.id()<<endl;
string pre;
ifstream fin;
fin.open(NEIGHBOURS);
getline(fin,pre);
getline(fin,pre);
fin.close();
int a=stoi(pre.substr(pre.find(':')+1));
//cout<<"Predecessor: "<<a<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(y.id()));
successorInfo.set_predaddress("0.0.0.0:"+to_string(a));
}
}
else {
successorInfo.set_succaddress("0.0.0.0:"+to_string(node));
successorInfo.set_predaddress("0.0.0.0:"+params.find("LISTENING_PORT")->second);
}
} }
//cout<<"Sending the successor and predecessor back to the new server"<<endl; //cout<<"Sending the successor and predecessor back to the new server"<<endl;
newResponder.Finish(successorInfo,Status::OK,this); newResponder.Finish(successorInfo,Status::OK,this);
...@@ -268,73 +244,65 @@ public: ...@@ -268,73 +244,65 @@ public:
for(int i=0;i<16;i++) { for(int i=0;i<16;i++) {
string temp; string temp;
getline(fin,temp); getline(fin,temp);
if(temp=="null") if(temp=="null"||temp.size()==0)
break; break;
fingers[i]=stoi(temp); fingers[i]=stoi(temp);
} }
fin.close(); fin.close();
int node=-1; int node=-1;
int next=-1; int next=-1;
for(int i=0;i<nums;i++) { int my_id=stoi(params.find("LISTENING_PORT")->second);
if(i>0&&fingers[i]>fingers[i-1]) { if(nums>0&&fingers[nums-1]<idtofind&&my_id>=idtofind) {
node=fingers[i-1]; node=fingers[nums-1];
next=fingers[i]; next=my_id;
break; }
} else if(nums>0&&my_id<idtofind&&fingers[0]>=idtofind) {
if(fingers[i]>idtofind||i==nums-1) { node=my_id;
if(i==nums-1&&fingers[i]>idtofind) { next=fingers[0];
}
else {
for(int i=0;i<nums;i++) {
if(i>0&&fingers[i-1]<idtofind&&fingers[i]>=idtofind) {
node=fingers[i-1]; node=fingers[i-1];
next=fingers[i]; next=fingers[i];
break;
} }
else if(i==nums-1) else if(i>0&&fingers[i]<fingers[i-1]&&fingers[i]>idtofind) {
node=fingers[i];
else {
node=fingers[i-1]; node=fingers[i-1];
next=fingers[i]; next=fingers[i];
break;
}
else if(i==nums-1) {
node=fingers[i];
next=my_id;
break;
} }
break;
} }
} }
if(next!=-1) { string target_address("0.0.0.0:"+to_string(next));
string target_address("0.0.0.0:"+to_string(next)); shared_ptr<Channel> channel=grpc::CreateChannel(target_address, grpc::InsecureChannelCredentials());
shared_ptr<Channel> channel=grpc::CreateChannel(target_address, grpc::InsecureChannelCredentials()); unique_ptr<KeyValueServices::Stub> stub;
unique_ptr<KeyValueServices::Stub> stub; stub=KeyValueServices::NewStub(channel);
stub=KeyValueServices::NewStub(channel); ClientContext context;
ClientContext context; Id x;
Id x; x.set_id(next);
x.set_id(next); Id y;
Id y; //cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
//cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl; stub->GETPREDECESSOR(&context,x,&y);
stub->GETPREDECESSOR(&context,x,&y); if(y.id()==node) {
if(y.id()<idtofind) { //cout<<"Yes it is. We found the successor"<<endl;
//cout<<"Yes it is. We found the successor"<<endl; //cout<<"Successor: "<<y.id()<<endl;
//cout<<"Successor: "<<y.id()<<endl; idvar2.set_id(x.id());
idvar2.set_id(y.id());
}
else {
string tar_address("0.0.0.0:"+to_string(node));
channel=grpc::CreateChannel(tar_address, grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel);
ClientContext context1;
x.set_id(idtofind);
//cout<<"No it is not. Asking the possible predecessor to find its successor"<<endl;
stub->GETSUCCESSOR(&context1,x,&y);
idvar2.set_id(y.id());
}
} }
else { else {
if(node<stoi(params.find("LISTENING_PORT")->second)) string tar_address("0.0.0.0:"+to_string(node));
idvar2.set_id(node); channel=grpc::CreateChannel(tar_address, grpc::InsecureChannelCredentials());
else { stub=KeyValueServices::NewStub(channel);
string tar_address("0.0.0.0:"+to_string(node)); ClientContext context1;
shared_ptr<Channel> channel=grpc::CreateChannel(tar_address, grpc::InsecureChannelCredentials()); x.set_id(idtofind);
unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel); //cout<<"No it is not. Asking the possible predecessor to find its successor"<<endl;
ClientContext context1; stub->GETSUCCESSOR(&context1,x,&y);
Id x,y; idvar2.set_id(y.id());
x.set_id(idtofind);
stub->GETSUCCESSOR(&context1,x,&y);
idvar2.set_id(y.id());
}
} }
getSuccessorResponder.Finish(idvar2,Status::OK,this); getSuccessorResponder.Finish(idvar2,Status::OK,this);
} }
...@@ -384,25 +352,37 @@ public: ...@@ -384,25 +352,37 @@ public:
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
int prev_entry=my_id; int prev_entry=my_id;
int i=0; int i=0;
int my_index=0;
for(i=0;i<num;i++)
if(ids[i]==my_id) {
my_index=i;
break;
}
int fingernodes[num-1];
int count=0;
for(i=my_index+1;i<num;i++)
fingernodes[count++]=ids[i];
for(i=0;i<my_index;i++)
fingernodes[count++]=ids[i];
int curr=0;
for(i=0;i<16;i++) { for(i=0;i<16;i++) {
int next_entry=(my_id+(1<<i))%(1<<16); int next_entry=(my_id+(1<<i))%(1<<16);
if(prev_entry<my_id&&next_entry>=my_id) if(curr!=count&&next_entry>ids[num-1]&&my_index!=0) {
break; fout<<ids[0]<<endl;
for(int j=0;j<num;j++) { if(fingernodes[curr]!=ids[0]) {
if(ids[j]==my_id) for(int j=0;j<count;j++)
continue; if(fingernodes[j]==ids[0])
if(ids[j]>=next_entry) { curr=j;
fout<<ids[j]<<endl;
continue;
} }
if(j==num-1)
fout<<"null"<<endl;
} }
prev_entry=next_entry; else {
} while(curr<count&&next_entry>fingernodes[curr])
while(i<16) { curr++;
fout<<"null"<<endl; }
i++; if(curr<count&&fingernodes[curr]>=next_entry)
fout<<fingernodes[curr]<<endl;
if(curr==count)
fout<<"null"<<endl;
} }
fout.close(); fout.close();
Null n; Null n;
...@@ -623,25 +603,37 @@ void updateAllFingerTables() { ...@@ -623,25 +603,37 @@ void updateAllFingerTables() {
int my_id=stoi(params.find("LISTENING_PORT")->second); int my_id=stoi(params.find("LISTENING_PORT")->second);
int prev_entry=my_id; int prev_entry=my_id;
int i=0; int i=0;
int my_index=0;
for(i=0;i<num;i++)
if(ids[i]==my_id) {
my_index=i;
break;
}
int fingernodes[num-1];
int count=0;
for(i=my_index+1;i<num;i++)
fingernodes[count++]=ids[i];
for(i=0;i<my_index;i++)
fingernodes[count++]=ids[i];
int curr=0;
for(i=0;i<16;i++) { for(i=0;i<16;i++) {
int next_entry=(my_id+(1<<i))%(1<<16); int next_entry=(my_id+(1<<i))%(1<<16);
if(prev_entry<my_id&&next_entry>=my_id) if(curr!=count&&next_entry>ids[num-1]&&my_index!=0) {
break; fout<<ids[0]<<endl;
for(int j=0;j<num;j++) { if(fingernodes[curr]!=ids[0]) {
if(ids[j]==my_id) for(int j=0;j<count;j++)
continue; if(fingernodes[j]==ids[0])
if(ids[j]>=next_entry) { curr=j;
fout<<ids[j]<<endl;
continue;
} }
if(j==num-1)
fout<<"null"<<endl;
} }
prev_entry=next_entry; else {
} while(curr<count&&next_entry>fingernodes[curr])
while(i<16) { curr++;
fout<<"null"<<endl; }
i++; if(curr<count&&fingernodes[curr]>next_entry)
fout<<fingernodes[curr]<<endl;
if(curr==count)
fout<<"null"<<endl;
} }
fout.close(); fout.close();
ClientContext context2; ClientContext context2;
...@@ -701,6 +693,8 @@ void register_server_DNS(string my_address) { ...@@ -701,6 +693,8 @@ void register_server_DNS(string my_address) {
fout<<successor<<endl; fout<<successor<<endl;
fout<<predecessor<<endl; fout<<predecessor<<endl;
fout.close(); fout.close();
//cout<<"Successor: "<<successor<<endl;
//cout<<"Predecessor: "<<predecessor<<endl;
//cout<<"Stored neighbours info"<<endl; //cout<<"Stored neighbours info"<<endl;
channel=grpc::CreateChannel(successor,grpc::InsecureChannelCredentials()); channel=grpc::CreateChannel(successor,grpc::InsecureChannelCredentials());
stub=KeyValueServices::NewStub(channel); stub=KeyValueServices::NewStub(channel);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment