Commit 31ac490a authored by mayankkakad's avatar mayankkakad

server adding, updating fingertables done (errors resolved)

parent 26eb00cd
......@@ -72,9 +72,37 @@ public:
getAddressResponder.Finish(info,Status::OK,this);
}
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;
fout.open(SERVERS,ios::app);
fout<<info.address()<<endl;
fout.open(SERVERS);
for(int i=0;i<count;i++)
fout<<addresses[i]<<endl;
fout.close();
null.set_nothing(0);
cout<<info.address()<<endl;
......
......@@ -113,36 +113,42 @@ public:
//cout<<"Getting my finger table"<<endl;
fin.open(FINGER_TABLE);
int nums=0;
for(int i=0;i<16;i++) {
do {
string temp;
getline(fin,temp);
if(temp=="null")
if(temp=="null"||temp.size()==0)
break;
nums++;
fingers[i]=stoi(temp);
}
fingers[nums++]=stoi(temp);
}while(fin);
fin.close();
int node=-1;
int next=-1;
//cout<<"Looking for the possible successor and predecessor"<<endl;
for(int i=0;i<nums;i++) {
if(i>0&&fingers[i]<fingers[i-1]) {
node=fingers[i-1];
next=fingers[i];
break;
}
if(fingers[i]>id||i==nums-1) {
if(i==nums-1&&fingers[i]>id) {
int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<id&&my_id>=id) {
node=fingers[nums-1];
next=my_id;
}
else if(nums>0&&my_id<id&&fingers[0]>=id) {
node=my_id;
next=fingers[0];
}
else {
for(int i=0;i<nums;i++) {
if(i>0&&fingers[i-1]<id&&fingers[i]>=id) {
node=fingers[i-1];
next=fingers[i];
break;
}
else if(i==nums-1)
node=fingers[i];
else {
else if(i>0&&fingers[i]<fingers[i-1]&&fingers[i]>id) {
node=fingers[i-1];
next=fingers[i];
break;
}
else if(i==nums-1) {
node=fingers[i];
next=my_id;
break;
}
break;
}
}
if(next!=-1) {
......@@ -155,13 +161,25 @@ public:
x.set_id(next);
Id y;
//cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
stub->GETPREDECESSOR(&context,x,&y);
if(y.id()<id) {
int mypred;
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<<"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_predaddress("0.0.0.0:"+to_string(y.id()));
successorInfo.set_predaddress("0.0.0.0:"+to_string(mypred));
}
else {
//cout<<"No it is not. We will ask the possible predecessor to find the successor of new node"<<endl;
......@@ -187,51 +205,9 @@ public:
}
else {
//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;
successorInfo.set_succaddress("0.0.0.0:"+to_string(node));
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<<"Only one node present right now, that is me"<<endl;
successorInfo.set_succaddress("0.0.0.0:"+to_string(my_id));
successorInfo.set_predaddress("0.0.0.0:"+to_string(my_id));
}
//cout<<"Sending the successor and predecessor back to the new server"<<endl;
newResponder.Finish(successorInfo,Status::OK,this);
......@@ -268,73 +244,65 @@ public:
for(int i=0;i<16;i++) {
string temp;
getline(fin,temp);
if(temp=="null")
if(temp=="null"||temp.size()==0)
break;
fingers[i]=stoi(temp);
}
fin.close();
int node=-1;
int next=-1;
for(int i=0;i<nums;i++) {
if(i>0&&fingers[i]>fingers[i-1]) {
node=fingers[i-1];
next=fingers[i];
break;
}
if(fingers[i]>idtofind||i==nums-1) {
if(i==nums-1&&fingers[i]>idtofind) {
int my_id=stoi(params.find("LISTENING_PORT")->second);
if(nums>0&&fingers[nums-1]<idtofind&&my_id>=idtofind) {
node=fingers[nums-1];
next=my_id;
}
else if(nums>0&&my_id<idtofind&&fingers[0]>=idtofind) {
node=my_id;
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];
next=fingers[i];
break;
}
else if(i==nums-1)
node=fingers[i];
else {
else if(i>0&&fingers[i]<fingers[i-1]&&fingers[i]>idtofind) {
node=fingers[i-1];
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));
shared_ptr<Channel> channel=grpc::CreateChannel(target_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub;
stub=KeyValueServices::NewStub(channel);
ClientContext context;
Id x;
x.set_id(next);
Id y;
//cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
stub->GETPREDECESSOR(&context,x,&y);
if(y.id()<idtofind) {
//cout<<"Yes it is. We found the successor"<<endl;
//cout<<"Successor: "<<y.id()<<endl;
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());
}
string target_address("0.0.0.0:"+to_string(next));
shared_ptr<Channel> channel=grpc::CreateChannel(target_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub;
stub=KeyValueServices::NewStub(channel);
ClientContext context;
Id x;
x.set_id(next);
Id y;
//cout<<"asking the possible successor whether its predecessor is less than the new node id"<<endl;
stub->GETPREDECESSOR(&context,x,&y);
if(y.id()==node) {
//cout<<"Yes it is. We found the successor"<<endl;
//cout<<"Successor: "<<y.id()<<endl;
idvar2.set_id(x.id());
}
else {
if(node<stoi(params.find("LISTENING_PORT")->second))
idvar2.set_id(node);
else {
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;
x.set_id(idtofind);
stub->GETSUCCESSOR(&context1,x,&y);
idvar2.set_id(y.id());
}
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());
}
getSuccessorResponder.Finish(idvar2,Status::OK,this);
}
......@@ -384,25 +352,37 @@ public:
int my_id=stoi(params.find("LISTENING_PORT")->second);
int prev_entry=my_id;
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++) {
int next_entry=(my_id+(1<<i))%(1<<16);
if(prev_entry<my_id&&next_entry>=my_id)
break;
for(int j=0;j<num;j++) {
if(ids[j]==my_id)
continue;
if(ids[j]>=next_entry) {
fout<<ids[j]<<endl;
continue;
if(curr!=count&&next_entry>ids[num-1]&&my_index!=0) {
fout<<ids[0]<<endl;
if(fingernodes[curr]!=ids[0]) {
for(int j=0;j<count;j++)
if(fingernodes[j]==ids[0])
curr=j;
}
if(j==num-1)
fout<<"null"<<endl;
}
prev_entry=next_entry;
}
while(i<16) {
fout<<"null"<<endl;
i++;
else {
while(curr<count&&next_entry>fingernodes[curr])
curr++;
}
if(curr<count&&fingernodes[curr]>=next_entry)
fout<<fingernodes[curr]<<endl;
if(curr==count)
fout<<"null"<<endl;
}
fout.close();
Null n;
......@@ -623,25 +603,37 @@ void updateAllFingerTables() {
int my_id=stoi(params.find("LISTENING_PORT")->second);
int prev_entry=my_id;
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++) {
int next_entry=(my_id+(1<<i))%(1<<16);
if(prev_entry<my_id&&next_entry>=my_id)
break;
for(int j=0;j<num;j++) {
if(ids[j]==my_id)
continue;
if(ids[j]>=next_entry) {
fout<<ids[j]<<endl;
continue;
if(curr!=count&&next_entry>ids[num-1]&&my_index!=0) {
fout<<ids[0]<<endl;
if(fingernodes[curr]!=ids[0]) {
for(int j=0;j<count;j++)
if(fingernodes[j]==ids[0])
curr=j;
}
if(j==num-1)
fout<<"null"<<endl;
}
prev_entry=next_entry;
}
while(i<16) {
fout<<"null"<<endl;
i++;
else {
while(curr<count&&next_entry>fingernodes[curr])
curr++;
}
if(curr<count&&fingernodes[curr]>next_entry)
fout<<fingernodes[curr]<<endl;
if(curr==count)
fout<<"null"<<endl;
}
fout.close();
ClientContext context2;
......@@ -701,6 +693,8 @@ void register_server_DNS(string my_address) {
fout<<successor<<endl;
fout<<predecessor<<endl;
fout.close();
//cout<<"Successor: "<<successor<<endl;
//cout<<"Predecessor: "<<predecessor<<endl;
//cout<<"Stored neighbours info"<<endl;
channel=grpc::CreateChannel(successor,grpc::InsecureChannelCredentials());
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