Commit c11b1fa7 authored by Vishal's avatar Vishal

Merge branch 'vishalm-master-patch-86039' into 'master'

Update PA5/chord.png, PA5/CMakeLists.txt, PA5/common.cmake, PA5/config.txt,...

See merge request !2
parents 1f38347a b5231467
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"
#include <vector>
void tokenize(std::string const &str, const char delim,
std::vector<std::string> &out)
{
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != std::string::npos)
{
end = str.find(delim, start);
out.push_back(str.substr(start, end - start));
}
}
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;
std::pair<int, int> search_key1(std::string file_name, std::string key) {
std::fstream myfile;
myfile.open(file_name, myfile.out| myfile.in);
int file_length = myfile.tellg();
// std::cout << file_length << '\n';
int num_records = file_length / 513;
char key_record[257];
char val_record[257];
key_record[256] = '\0';
val_record[256] = '\0';
char status[1];
std::string my_key;
std::string my_val;
int last_empty_record = -1;
bool flag = false;
myfile.seekg(0, myfile.beg);
int key_line_index = -1;
// std::cout << num_records;
for (int i = 0; i < num_records; i++) {
my_key = "";
my_val = "";
myfile.read(status, 1);
// myfile.seekg(1, myfile.cur);
myfile.read(key_record, 256);
// myfile.seekg(256, myfile.cur);
myfile.read(val_record, 256);
// myfile.seekg(256, myfile.cur);
for(int j = 0; j < 256; j++) {
if (key_record[j] == ' ') break;
// std::cout << key_record[j];
my_key += key_record[j];
}
for(int j = 0; j < 256; j++) {
if (val_record[j] == ' ') break;
my_val += val_record[j];
}
if (status[0] == '1') {
if (my_key == key) {
// found record.
// std::cout << "hahsdasd";
key_line_index = i;
break;
}
} else {
last_empty_record = i;
}
}
myfile.close();
return std::make_pair(key_line_index, last_empty_record);
}
std::string filename12;
void put_to_file1(std::string file_name, std::string key, std::string value) {
std::pair<int, int> search_result = search_key1(file_name, key);
char key_record[257];
char val_record[257];
key_record[256] = '\0';
val_record[256] = '\0';
char status[1];
std::string my_key;
std::string my_val;
std::fstream myfile;
myfile.open(file_name, myfile.out| myfile.in);
if (search_result.first != -1) {
// std::cout << "hoora";
// found the entry
myfile.seekg(search_result.first * 513 + 256 + 1, myfile.beg);
std::string padding(256-value.size(), ' ');
myfile << value.c_str();
// myfile.write(value.c_str(), value.size());
myfile << padding.c_str();
} else {
if (search_result.second != -1) {
myfile.seekg(search_result.second * 513, myfile.beg);
} else {
myfile.seekg(0, myfile.end);
}
status[0] = '1';
myfile << status[0];
// myfile.write(status, 1);
std::string padding_1(256-key.size(), ' ');
myfile << key.c_str();
// myfile.write(key.c_str(), key.size());
myfile << padding_1.c_str();
// myfile.write(padding_1.c_str(), padding_1.size());
std::string padding_2(256-value.size(), ' ');
myfile << value.c_str();
// myfile.write(value.c_str(), value.size());
myfile << padding_2.c_str();
// myfile.write(padding_2.c_str(), padding_2.size());
}
myfile.close();
}
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 if(k=="-2")
{
const char delim = ';';
std::vector<std::string> k1,v1;
tokenize(call->reply.key(), delim, k1);
tokenize(call->reply.value(),delim,v1);
for (int i = 0; i < k1.size(); ++i) {
if(k1.size()!=0)
{
// std::cout<<k1[i]<<" "<<v1[i]<<std::endl;
put_to_file1(filename12,k1[i],v1[i]);
}
}
std::cout<<"Receive data from Successor server"<<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;
}
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);
}
}
void DataTransfer(std::string port ,std::string value,std::string fname) {
filename12=fname;
GreeterClient greeter(grpc::CreateChannel(
"localhost:"+port, grpc::InsecureChannelCredentials()));
greeter.PUT("-2",value);
}
#include "iostream"
void connectNxt(int requestType,std::string port,std::string key ,std::string value);
void DataTransfer(std::string nxtPort,std::string value,std::string filename);
\ No newline at end of file
This diff is collapsed.
#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 "helper.h"
#include <chrono>
#include <ctime>
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;
std::fstream myfile;
std::chrono::time_point<std::chrono::system_clock> start, end;
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);
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 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 AsyncCompleteRpc() {
void* got_tag;
bool ok = false;
// Block until the next result is available in the completion queue "cq".
while (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())
{
std::cout<<call->reply.key()<<" "<<call->reply.value()<<std::endl;
}
else
std::cout << "RPC failed" << std::endl;
// Once we're complete, deallocate the call object.
delete call;
}
}
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_;
};
int main(int argc, char** argv) {
struct conf configData= print(2);
std::cout<<"Enter connecting port number"<<std::endl;
int p;
std::cin>>p;
GreeterClient greeter(grpc::CreateChannel(
"localhost:"+ std::to_string(p), grpc::InsecureChannelCredentials()));
std::thread thread = std::thread(&GreeterClient::AsyncCompleteRpc, &greeter);
std::string s1,s2,user;
std::string a;
//start
start = std::chrono::system_clock::now();
while (std::cin>>a)
{
// std::cout<<"1 For get"<<std::endl;
// std::cout<<"2 For put"<<std::endl;
// std::cout<<"3 For del"<<std::endl;
if(a=="1")
{
std::cin>>user;
greeter.GET(user);
} else if(a=="2")
{
std::cin>>s1>>s2;
greeter.PUT(s1,s2);
} else if(a=="3")
{
std::cin>>s1;
greeter.DEL(s1);
} else
{
std::cout<<"Please start a client again(Ctrl + C) and give a valid input"<<std::endl;
break;
}
}//10000 -> request
//end
//response =(end-start)/10000
//end-start= 10000/(end-start)
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
// std::cout << "finished computation at " << std::ctime(&end_time)
// << "elapsed time: " << elapsed_seconds.count() << "s\n";
int total=10000;
double throughput=total/(elapsed_seconds.count());
double response=elapsed_seconds.count()/total;
// std::cout<<throughput<<" "<<response<<std::endl;
thread.join();
return 0;
}
This diff is collapsed.
Vishal Mishra (213050066)
This project completed by me
1.create directory into CwD
mkdir cmake/build
change config.txt file to changing configuration of setting
make sure that these file present into cpp folder
2.change to directory
cd cmake/build
3. cmake -DCMAKE_PREFIX_PATH={{LOCATION OF Updated CMAKE}} ../..
4. MAKE
5. ./KVserver(for open server)
6. ./KVclient (for open client)
inform for client :-
key size must be 16 bit and it is represented into HEXADECIMAL Form started
form
0000 0001 ... ffff
1 for GET
2 for PUT
3 for DEL
Reference.
1. https://www.geeksforgeeks.org/chrono-in-c/
2. https://grpc.io/docs/languages/cpp/quickstart/
3. https://grpc.io/docs/languages/cpp/async/
4. https://medium.com/@andrewvetovitz/grpc-c-introduction-45a66ca9461f
# 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++)