Commit 8f3fe21b authored by Vishal's avatar Vishal

Update PA5/CMakeLists.txt, PA5/common.cmake, PA5/config.txt, PA5/helper.cc,...

Update PA5/CMakeLists.txt, PA5/common.cmake, PA5/config.txt, PA5/helper.cc, PA5/helper.h, PA5/keyvaluepackage.grpc.pb.cc, PA5/keyvaluepackage.grpc.pb.h, PA5/keyvaluepackage.pb.cc, PA5/keyvaluepackage.pb.h, PA5/keyvaluepackage.proto, PA5/KVclient.cc, PA5/KVserver.cc, PA5/KVServerClient.cc, PA5/KVServerClient.h files
parent 702859f4
cmake_minimum_required(VERSION 3.5.1)
project(HelloWorld C CXX)
include(common.cmake)
# Proto file
get_filename_component(hw_proto "keyvaluepackage.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)
# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/keyvaluepackage.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/keyvaluepackage.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/keyvaluepackage.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/keyvaluepackage.grpc.pb.h")
add_custom_command(
OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
COMMAND ${_PROTOBUF_PROTOC}
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
-I "${hw_proto_path}"
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
"${hw_proto}"
DEPENDS "${hw_proto}")
# Include generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/config.txt DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
# hw_grpc_proto
add_library(hw_grpc_proto
${hw_grpc_srcs}
${hw_grpc_hdrs}
${hw_proto_srcs}
${hw_proto_hdrs})
target_link_libraries(hw_grpc_proto
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
# Targets greeter_[async_](client|server)
foreach(_target
KVserver KVclient)
add_executable(${_target} "${_target}.cc" helper.cc KVServerClient.cc)
target_link_libraries(${_target}
hw_grpc_proto
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
endforeach()
#include <fstream>
#include "unistd.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpc/support/log.h>
#include <grpcpp/grpcpp.h>
#include <thread>
#include "keyvaluepackage.grpc.pb.h"
#include <chrono>
#include <ctime>
#include "KVServerClient.h"
using grpc::Channel;
using grpc::ClientAsyncResponseReader;
using grpc::ClientContext;
using grpc::CompletionQueue;
using grpc::Status;
using keyvaluepackage::KV;
using keyvaluepackage::GetKeyRequest;
using keyvaluepackage::Reply;
using keyvaluepackage::PutKeyRequest;
using keyvaluepackage::DelKeyRequest;
class GreeterClient {
public:
explicit GreeterClient(std::shared_ptr <Channel> channel)
: stub_(KV::NewStub(channel)) {}
void GET(const std::string& k)
{
GetKeyRequest request;
request.set_key(k);
AsyncClientCall* call = new AsyncClientCall;
call->response_reader = stub_->PrepareAsyncGET(&call->context,request,&cq_);
call->response_reader->StartCall();
call->response_reader->Finish(&call->reply,&call->status,(void *)call);
void* got_tag;
bool ok = false;
// Block until the next result is available in the completion queue "cq".
GPR_ASSERT(cq_.Next(&got_tag, &ok));
// The tag in this example is the memory location of the call object
call = static_cast<AsyncClientCall*>(got_tag);
// Verify that the request was completed successfully. Note that "ok"
// corresponds solely to the request for updates introduced by Finish().
GPR_ASSERT(ok);
if (call->status.ok()==false)
{
std::cout << "RPC failed" << std::endl;
} else
{
std::cout<<"Success"<<std::endl;
}
return;
}
void PUT(const std::string& k,const std::string& k2)
{
PutKeyRequest request;
request.set_key(k);
request.set_value(k2);
AsyncClientCall* call = new AsyncClientCall;
call->response_reader = stub_->PrepareAsyncPUT(&call->context,request,&cq_);
call->response_reader->StartCall();
call->response_reader->Finish(&call->reply,&call->status,(void *)call);
void* got_tag;
bool ok = false;
// Block until the next result is available in the completion queue "cq".
GPR_ASSERT(cq_.Next(&got_tag, &ok));
// The tag in this example is the memory location of the call object
call = static_cast<AsyncClientCall*>(got_tag);
// Verify that the request was completed successfully. Note that "ok"
// corresponds solely to the request for updates introduced by Finish().
GPR_ASSERT(ok);
if (call->status.ok()==false)
{
std::cout << "RPC failed" << std::endl;
} else
{
std::cout<<"Success"<<std::endl;
}
return;
}
void DEL(const std::string& k)
{
DelKeyRequest request;
request.set_key(k);
AsyncClientCall* call = new AsyncClientCall;
call->response_reader = stub_->PrepareAsyncDEL(&call->context,request,&cq_);
call->response_reader->StartCall();
call->response_reader->Finish(&call->reply,&call->status,(void *)call);
void* got_tag;
bool ok = false;
// Block until the next result is available in the completion queue "cq".
GPR_ASSERT(cq_.Next(&got_tag, &ok));
// The tag in this example is the memory location of the call object
call = static_cast<AsyncClientCall*>(got_tag);
// Verify that the request was completed successfully. Note that "ok"
// corresponds solely to the request for updates introduced by Finish().
GPR_ASSERT(ok);
if (call->status.ok()==false)
{
std::cout << "RPC failed" << std::endl;
} else
{
std::cout<<"Success"<<std::endl;
}
return;
}
void AsyncCompleteRpc() {
void* got_tag;
bool ok = false;
// Block until the next result is available in the completion queue "cq".
cq_.Next(&got_tag, &ok);
// The tag in this example is the memory location of the call object
AsyncClientCall* call = static_cast<AsyncClientCall*>(got_tag);
// Verify that the request was completed successfully. Note that "ok"
// corresponds solely to the request for updates introduced by Finish().
GPR_ASSERT(ok);
if (call->status.ok()==false)
{
std::cout << "RPC failed" << std::endl;
} else
{
std::cout<<"Success"<<std::endl;
}
}
private:
// struct for keeping state and data information
struct AsyncClientCall {
// Container for the data we expect from the server.
keyvaluepackage::Reply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
// Storage for the status of the RPC upon completion.
Status status;
std::unique_ptr<ClientAsyncResponseReader<keyvaluepackage::Reply>> response_reader;
};
// Out of the passed in Channel comes the stub, stored here, our view of the
// server's exposed services.
std::unique_ptr<KV::Stub> stub_;
// The producer-consumer queue we use to communicate asynchronously with the
// gRPC runtime.
CompletionQueue cq_;
};
void connectNxt(int requestType,std::string port,std::string key ,std::string value) {
GreeterClient greeter(grpc::CreateChannel(
"localhost:"+port, grpc::InsecureChannelCredentials()));
if(requestType==1)
{
greeter.GET(key);
} else if(requestType==2)
{
greeter.PUT(key,value);
} else if(requestType==3)
{
greeter.DEL(key);
}
}
#include "iostream"
void connectNxt(int requestType,std::string port,std::string key ,std::string value);
This diff is collapsed.
This diff is collapsed.
# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# cmake build file for C++ route_guide example.
# Assumes protobuf and gRPC have been installed using cmake.
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
# that automatically builds all the dependencies before building route_guide.
cmake_minimum_required(VERSION 3.5.1)
set (CMAKE_CXX_STANDARD 11)
if(MSVC)
add_definitions(-D_WIN32_WINNT=0x600)
endif()
find_package(Threads REQUIRED)
if(GRPC_AS_SUBMODULE)
# One way to build a projects that uses gRPC is to just include the
# entire gRPC project tree via "add_subdirectory".
# This approach is very simple to use, but the are some potential
# disadvantages:
# * it includes gRPC's CMakeLists.txt directly into your build script
# without and that can make gRPC's internal setting interfere with your
# own build.
# * depending on what's installed on your system, the contents of submodules
# in gRPC's third_party/* might need to be available (and there might be
# additional prerequisites required to build them). Consider using
# the gRPC_*_PROVIDER options to fine-tune the expected behavior.
#
# A more robust approach to add dependency on gRPC is using
# cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).
# Include the gRPC's cmake build (normally grpc source code would live
# in a git submodule called "third_party/grpc", but this example lives in
# the same repository as gRPC sources, so we just look a few directories up)
add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)
message(STATUS "Using gRPC via add_subdirectory.")
# After using add_subdirectory, we can now use the grpc targets directly from
# this build.
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
set(_REFLECTION grpc++_reflection)
if(CMAKE_CROSSCOMPILING)
find_program(_PROTOBUF_PROTOC protoc)
else()
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
endif()
set(_GRPC_GRPCPP grpc++)
if(CMAKE_CROSSCOMPILING)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else()
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
endif()
elseif(GRPC_FETCHCONTENT)
# Another way is to use CMake's FetchContent module to clone gRPC at
# configure time. This makes gRPC's source code available to your project,
# similar to a git submodule.
message(STATUS "Using gRPC via add_subdirectory (FetchContent).")
include(FetchContent)
FetchContent_Declare(
grpc
GIT_REPOSITORY https://github.com/grpc/grpc.git
# when using gRPC, you will actually set this to an existing tag, such as
# v1.25.0, v1.26.0 etc..
# For the purpose of testing, we override the tag used to the commit
# that's currently under test.
GIT_TAG vGRPC_TAG_VERSION_OF_YOUR_CHOICE)
FetchContent_MakeAvailable(grpc)
# Since FetchContent uses add_subdirectory under the hood, we can use
# the grpc targets directly from this build.
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
set(_REFLECTION grpc++_reflection)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
set(_GRPC_GRPCPP grpc++)
if(CMAKE_CROSSCOMPILING)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else()
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
endif()
else()
# This branch assumes that gRPC and all its dependencies are already installed
# on this system, so they can be located by find_package().
# Find Protobuf installation
# Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
set(_REFLECTION gRPC::grpc++_reflection)
if(CMAKE_CROSSCOMPILING)
find_program(_PROTOBUF_PROTOC protoc)
else()
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
endif()
# Find gRPC installation
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(_GRPC_GRPCPP gRPC::grpc++)
if(CMAKE_CROSSCOMPILING)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else()
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
endif()
endif()
5000
6000
LFU
1024
4
\ No newline at end of file
#include "helper.h"
#include "iostream"
#include <fstream>
int clientlistenserver=6000,chordserver=5000;
struct conf print(int i)
{
struct conf a;
std::string myText;
// Read from the text file
std::ifstream MyReadFile("config.txt");
// Use a while loop together with the getline() function to read the file line by line
getline(MyReadFile,myText);
a.chordserverport=std::stoi(myText);
getline(MyReadFile,myText);
a.clientlistenport=std::stoi(myText);
getline(MyReadFile,myText);
a.cache_replacement=myText;
getline(MyReadFile,myText);
a.cache_size=std::stoi(myText);
getline(MyReadFile,myText);
a.thread_pool=std::stoi(myText);
// Close the file
MyReadFile.close();
std::ofstream MyoutFile{"config.txt"};
// Use a while loop together with the getline() function to read the file line by line
if(i==1)
MyoutFile<<std::to_string(a.chordserverport+1)<< std::endl;
else
MyoutFile<<std::to_string(a.chordserverport)<< std::endl;
if(i==2)
MyoutFile<<std::to_string(a.clientlistenport+1)<< std::endl;
else
MyoutFile<<std::to_string(a.clientlistenport)<<std::endl;
MyoutFile<<a.cache_replacement<< std::endl;
MyoutFile<<std::to_string(a.cache_size)<< std::endl;
MyoutFile<<std::to_string(a.thread_pool)<< std::endl;
// Close the file
MyoutFile.close();
return a;
}
std::string hashValue(std::string str) {
std::size_t str_hash = std::hash < std::string > {}(str);
std::string ans;
int sums;
for (int i = 0; i < 4; ++i) {
sums=0;
for (int j = i*4; j <(i+1)*4 ; ++j) {
if((str_hash&(1<<j))>0)
sums+=(1<<(j-i*4));
}
switch (sums) {
case 0:
ans+='0';
break;
case 1:
ans+='1';
break;
case 2:
ans+='2';
break;
case 3:
ans+='3';
break;
case 4:
ans+='4';
break;
case 5:
ans+='5';
break;
case 6:
ans+='6';
break;
case 7:
ans+='7';
break;
case 8:
ans+='8';
break;
case 9:
ans+='9';
break;
case 10:
ans+='a';
break;
case 11:
ans+='b';
break;
case 12:
ans+='c';
break;
case 13:
ans+='d';
break;
case 14:
ans+='e';
break;
case 15:
ans+='f';
}
}
return ans;
}
std::string addintoHash(std::string myHash,int shift) {
int ans = 0;
for (int i = 0; i < 4; ++i) {
ans *= 16;
switch (myHash[i]) {
case '0':
ans += 0;
break;
case '1':
ans += 1;
break;
case '2':
ans += 2;
break;
case '3':
ans += 3;
break;
case '4':
ans += 4;
break;
case '5':
ans += 5;
break;
case '6':
ans += 6;
break;
case '7':
ans += 7;
break;
case '8':
ans += 8;
break;
case '9':
ans += 9;
break;
case 'a':
ans += 10;
break;
case 'b':
ans += 11;
break;
case 'c':
ans += 12;
break;
case 'd':
ans += 13;
break;
case 'e':
ans += 14;
break;
case 'f':
ans += 15;
}
}
ans+=shift;
std::string ans1;
int sums;
for (int i = 3; i >=0; --i) {
sums=0;
for (int j = i*4; j <(i+1)*4 ; ++j) {
if((ans&(1<<j))>0)
sums+=(1<<(j-i*4));
}
switch (sums) {
case 0:
ans1+='0';
break;
case 1:
ans1+='1';
break;
case 2:
ans1+='2';
break;
case 3:
ans1+='3';
break;
case 4:
ans1+='4';
break;
case 5:
ans1+='5';
break;
case 6:
ans1+='6';
break;
case 7:
ans1+='7';
break;
case 8:
ans1+='8';
break;
case 9:
ans1+='9';
break;
case 10:
ans1+='a';
break;
case 11:
ans1+='b';
break;
case 12:
ans1+='c';
break;
case 13:
ans1+='d';
break;
case 14:
ans1+='e';
break;
case 15:
ans1+='f';
}
}
return ans1;
}
struct fingerTable updateFingerTable(int maxPort,int myPort)
{
struct fingerTable fingeValue;
std::string hashMyPort= hashValue(std::to_string(myPort)),shiftsum,successor="z";
for (int i = 0; i < 16; ++i) {
shiftsum= addintoHash(hashMyPort,(1<<i));
for (int allserver = 5000; allserver <=maxPort ; ++allserver) {
if(hashMyPort< hashValue(std::to_string(allserver)))
{
successor=min(successor,hashValue(std::to_string(allserver)));
if(successor==hashValue(std::to_string(allserver)))
{
fingeValue.table[i]=allserver;
}
}
}
if(successor=="z")
{
for (int allserver = 5000; allserver <=maxPort ; ++allserver) {
if(min(successor, hashValue(std::to_string(allserver)))<successor)
{
successor=std::min(successor,hashValue(std::to_string(allserver)));
fingeValue.table[i]=allserver;
}
}
}
}
std::string lower="0";
for (int i = 5000; i <= maxPort ; ++i) {
if(hashMyPort > hashValue(std::to_string(i)))
{
lower= std::max(lower, hashValue(std::to_string(i)));
if(hashValue(std::to_string(i))==lower)
{
fingeValue.predecessor=i;
}
}
}
if(lower=="0")
{
for (int i = 5000; i <= maxPort ; ++i) {
lower= std::max(lower, hashValue(std::to_string(i)));
if(hashValue(std::to_string(i))==lower)
{
fingeValue.predecessor=i;
}
}
}
return fingeValue;
}
#include "iostream"
#include<bits/stdc++.h>
struct conf{
int cache_size,thread_pool,clientlistenport,chordserverport;
std::string cache_replacement;
};
std::string hashValue(std::string);
struct conf print(int);
struct fingerTable{
int predecessor;
int table[16];
};
struct fingerTable updateFingerTable(int ,int);
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package keyvaluepackage;
// The greeting service definition.
service KV {
// Sends a greeting
rpc GET (GetKeyRequest) returns (Reply) {}
rpc PUT (PutKeyRequest) returns (Reply) {}
rpc DEL (DelKeyRequest) returns (Reply){}
}
message GetKeyRequest{
string key=1;
}
message PutKeyRequest{
string key=1;
string value=2;
}
message DelKeyRequest{
string key=1;
}
message Reply{
string key=1;
string value=2;
}
// The request message containing the user's name.
//message HelloRequest {
// string name = 1;
//}
//message HelloWithSerial{
// string name=1;
// int32 seqNumber=2;
//}
//
//// The response message containing the greetings
//message HelloReply {
// string message = 1;
//}
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