Commit e94cec16 authored by Mayank Manoj's avatar Mayank Manoj

Merge branch 'mayank' into 'master'

Mayank

See merge request !2
parents 40295e4f b69e736e
......@@ -20,6 +20,9 @@ public:
virtual void pushAll() {
return;
}
virtual string getKeyValuePairs(int id) {
return "This will never run";
}
};
class storageLRU : public memoryManagement {
......@@ -57,6 +60,10 @@ public:
void pushAll() {
mycache.pushAll();
}
string getKeyValuePairs(int id) {
return "keyvaluepairs";
}
};
class storageLFU : public memoryManagement {
......@@ -93,4 +100,8 @@ public:
void pushAll() {
mycache.pushAll();
}
string getKeyValuePairs(int id) {
return "keyvaluepairs";
}
};
\ No newline at end of file
......@@ -42,7 +42,7 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}")
# Targets (client|server)
foreach(_target
client client_test server)
client client_test server dns)
add_executable(${_target} "${_target}.cpp"
${hw_proto_srcs}
${hw_grpc_srcs})
......
......@@ -13,6 +13,8 @@ using keyvaluestore::Value;
using keyvaluestore::KeyValue;
using keyvaluestore::ReqStatus;
using keyvaluestore::KeyValueServices;
using keyvaluestore::Info;
using keyvaluestore::Null;
std::map<std::string, std::string> params;
std::string config_filename = "../config";
......@@ -150,7 +152,15 @@ void parse(std::string& str, std::string& cmd, std::string& key, std::string& va
}
void RunClient() {
std::string target_address("0.0.0.0:"+params.find("LISTENING_PORT")->second);
std::string dns_address("0.0.0.0:1234");
std::shared_ptr<Channel> channel=grpc::CreateChannel(dns_address, grpc::InsecureChannelCredentials());
std::unique_ptr<KeyValueServices::Stub> stub=KeyValueServices::NewStub(channel);
Null null;
null.set_nothing(0);
Info info;
ClientContext context;
Status status=stub->GETADDRESS(&context,null,&info);
std::string target_address(info.address());
// Instantiates the client
KeyValueServicesClient client(
// Channel from which RPCs are made - endpoint is the target_address
......
#include <bits/stdc++.h>
#include <grpcpp/grpcpp.h>
#include<fstream>
#include "keyvaluestore.grpc.pb.h"
#define SERVERS "serverlist.txt"
using namespace std;
using grpc::Server;
using grpc::ServerAsyncResponseWriter;
using grpc::ServerBuilder;
using grpc::ServerCompletionQueue;
using grpc::ServerContext;
using grpc::Status;
using grpc::Channel;
using grpc::ClientContext;
using keyvaluestore::KeyValueServices;
using keyvaluestore::Info;
using keyvaluestore::Null;
using keyvaluestore::Addresses;
ServerBuilder builder;
KeyValueServices::AsyncService service;
std::unique_ptr<Server> server;
enum RequestType {
GETADDRESS,
ADDADDRESS,
UPDATEFINGERTABLES,
GETSERVERS
};
class DNSData {
public:
DNSData(KeyValueServices::AsyncService *service, ServerCompletionQueue *cq, RequestType reqType) : service(service), cq(cq), getAddressResponder(&context), addAddressResponder(&context),updateFingerTablesResponder(&context),getServersResponder(&context), status(CREATE), reqType(reqType) {
Proceed();
}
void Proceed() {
if (status == CREATE) {
status = PROCESS;
if(reqType==GETADDRESS)
service->RequestGETADDRESS(&context, &null, &getAddressResponder, cq, cq, this);
else if(reqType==ADDADDRESS)
service->RequestADDADDRESS(&context, &info, &addAddressResponder, cq, cq, this);
else if(reqType==UPDATEFINGERTABLES)
service->RequestUPDATEFINGERTABLES(&context,&null,&updateFingerTablesResponder,cq,cq,this);
else
service->RequestGETSERVERS(&context,&null,&getServersResponder,cq,cq,this);
}
else if (status == PROCESS) {
new DNSData(service, cq, reqType);
if(reqType==GETADDRESS) {
ifstream fin;
int size=0;
map<int,string> servers;
fin.open(SERVERS);
do {
string temp;
getline(fin,temp);
if(temp.size()==0)
break;
servers[size++]=temp;
}while(fin);
fin.close();
if(size==0)
info.set_address("null");
else {
int x=rand()%size;
info.set_address(servers.find(x)->second);
}
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);
for(int i=0;i<count;i++)
fout<<addresses[i]<<endl;
fout.close();
null.set_nothing(0);
cout<<info.address()<<endl;
addAddressResponder.Finish(null,Status::OK,this);
}
else if(reqType==UPDATEFINGERTABLES){
ifstream fin;
int index=0;
map<int,string> servers;
fin.open(SERVERS);
do {
string temp;
getline(fin,temp);
if(temp.size()==0)
break;
servers[index++]=temp;
}while(fin);
fin.close();
string addressarr="";
for(int i=0;i<index;i++)
addressarr+=servers[i]+";";
for(int i=0;i<index;i++) {
string target_address(servers[i]);
shared_ptr<Channel> channel=grpc::CreateChannel(target_address, grpc::InsecureChannelCredentials());
unique_ptr<KeyValueServices::Stub> stub;
stub=KeyValueServices::NewStub(channel);
Null null;
ClientContext cont;
Addresses addr;
addr.set_addresses(addressarr);
addr.set_servers(index);
stub->UPDATETABLE(&cont,addr,&null);
}
updateFingerTablesResponder.Finish(null,Status::OK,this);
}
else {
ifstream fin;
int index=0;
map<int,string> servers;
fin.open(SERVERS);
do {
string temp;
getline(fin,temp);
if(temp.size()==0)
break;
servers[index++]=temp;
}while(fin);
fin.close();
string addressarr="";
for(int i=0;i<index;i++)
addressarr+=servers[i]+";";
addr1.set_addresses(addressarr);
addr1.set_servers(index);
getServersResponder.Finish(addr1,Status::OK,this);
}
status = FINISH;
}
else {
GPR_ASSERT(status == FINISH);
delete this;
}
}
private:
KeyValueServices::AsyncService *service;
ServerCompletionQueue *cq;
ServerContext context;
Null null;
Info info;
Addresses addr1;
ServerAsyncResponseWriter<Info> getAddressResponder;
ServerAsyncResponseWriter<Null> addAddressResponder;
ServerAsyncResponseWriter<Null> updateFingerTablesResponder;
ServerAsyncResponseWriter<Addresses> getServersResponder;
enum CallStatus {
CREATE,
PROCESS,
FINISH
};
CallStatus status;
RequestType reqType;
};
int main(int argc,char **argv) {
srand(time(0));
string server_address("0.0.0.0:1234");
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
unique_ptr<ServerCompletionQueue> comp_queue=builder.AddCompletionQueue();
server = builder.BuildAndStart();
cout<<"DNS SERVER COMES UP SUCCESSFULLY"<<endl;
new DNSData(&service,comp_queue.get(),GETADDRESS);
new DNSData(&service,comp_queue.get(),ADDADDRESS);
new DNSData(&service,comp_queue.get(),UPDATEFINGERTABLES);
new DNSData(&service,comp_queue.get(),GETSERVERS);
void *tag;
bool ok;
while(true) {
GPR_ASSERT(comp_queue->Next(&tag,&ok));
GPR_ASSERT(ok);
static_cast<DNSData *>(tag)->Proceed();
}
}
\ No newline at end of file
......@@ -8,9 +8,18 @@ service KeyValueServices {
rpc GET(Key) returns (Value) {}
rpc PUT(KeyValue) returns (ReqStatus) {}
rpc DEL(Key) returns (ReqStatus) {}
rpc NEW(Info) returns (SuccessorInfo) {}
rpc INFORMSUCCESSOR(Info) returns (KeyValues) {}
rpc INFORMPREDECESSOR(Info) returns (Null) {}
rpc GETADDRESS(Null) returns (Info) {}
rpc ADDADDRESS(Info) returns (Null) {}
rpc UPDATEFINGERTABLES(Null) returns (Null) {}
rpc GETSUCCESSOR(Id) returns (Id) {}
rpc GETPREDECESSOR(Id) returns (Id) {}
rpc UPDATETABLE(Addresses) returns (Null) {}
rpc GETSERVERS(Null) returns (Addresses) {}
}
message Key {
string key = 1;
}
......@@ -30,3 +39,30 @@ message ReqStatus {
int32 status = 1;
string error = 2;
}
message Info {
string address = 1;
}
message SuccessorInfo {
string succaddress = 1;
string predaddress = 2;
}
message KeyValues {
string keys = 1;
string values = 2;
}
message Null {
int32 nothing = 1;
}
message Id {
int64 id = 1;
}
message Addresses {
string addresses = 1;
int64 servers = 2;
}
\ No newline at end of file
This diff is collapsed.
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