Commit 8066bddd authored by Nihal's avatar Nihal

initial commit

parents
# 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++ kv 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 kv.
cmake_minimum_required(VERSION 3.5.1)
project(HelloWorld C CXX)
include(/home/sherrinford/grpc/examples/cpp/cmake/common.cmake)
# Proto file
get_filename_component(hw_proto "kv.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)
# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/kv.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/kv.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/kv.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/kv.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}")
# 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
client server)
add_executable(${_target} "${_target}.cc")
target_link_libraries(${_target}
hw_grpc_proto
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF})
endforeach()
File added
1)To run to project do the following :
$ mkdir -p cmake/build
$ pushd cmake/build
$ cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR ../..
$ make -j
2)server.config is to placed in cmake/build in the following manner (or Copy server.config to cmake/build) :
50051 //LISTENING_PORT
LRU //CACHE_REPLACEMENT_TYPE
15 //CACHE_SIZE
10 //THREAD_POOL_SIZE
For example :
50051
LRU
15
10
3)For using Batch Mode Place batch.txt file in cmake/build , It may contain Command in the following (case sensitive Manner):
Get key
Put key value
Del key
4)For using Iterative mode , type the commands in case sensetive manner :
Get key
Put key value
Del key
https://github.com/raviprakashgiri/Distributed-Service-using-GRPC
https://github.com/Mityuha/grpc_async
https://github.com/mtrebi/thread-pool/blob/master/README.md#thread-pool
https://grpc.io/docs/languages/cpp/async/
\ No newline at end of file
#include <pthread.h>
#include<bits/stdc++.h>
#include "storage.h"
using namespace std;
class cache
{
public:
struct entry{
string key;
string value;
bool valid;
bool modified;
int last_used=-1;
int frequency=0;
pthread_mutex_t lock;
};
const int KEY_SIZE= 256;
const int VAL_SIZE =256;
map<string,entry*>cache;
long long int current_capacity;
long long unsigned int counter;
// bool delete_key(string);
// void find_new_slot();
string get_entry(string key){
if(cache.find(key)!=cache.end()){
return cache[key]->value;
}
return "KEY NOT EXIST";
}
void initialize_cache(int size_){
storage_init();
current_capacity=size_;
counter=0;
}
bool put_into_cache(string key1,string value2,int code){
if(key1.length()>256 || value2.length()>256 || key1.length()==0 ||value2.length()==0 ){
return 0;
}
struct entry * temp=(struct entry*)malloc(sizeof(struct entry));
int k;
pthread_mutex_lock(&(temp->lock));
temp->key=key1;
temp->value =value2;
temp->valid=1;
temp->modified=0;
if(code=1)
temp->last_used=counter++;
else
temp->frequency++;
if(current_capacity<=0){
find_new_slot(code);
current_capacity++;
}
cache[key1]=temp;
current_capacity--;
counter++;
file_put(toCharArray(key1),toCharArray(value2));
pthread_mutex_unlock(&(temp->lock));
return 1;
}
void find_new_slot(int code){
cout<<"Problem here"<<endl;
string lowest;
int last_mod=INT_MAX;
for(map<string,entry*>::const_iterator it=cache.begin();it!=cache.end();++it){
if(code==1){
if(it->second->last_used<last_mod){
lowest=it->first;
last_mod=it->second->last_used;
}
}
else{
if(it->second->frequency<last_mod){
lowest=it->first;
last_mod=it->second->frequency;
}
}
}
entry* temp = cache[lowest];
cout<<"DELETEING ENTRY KEY : "<<lowest<<endl;
file_del(toCharArray(lowest));
cache.erase(lowest);
// free(temp);
}
void print_cache(){
for(map<string,entry*>::const_iterator it=cache.begin();it!=cache.end();++it)
cout<<it->first<<" "<<it->second->value<<endl;
}
bool delete_key(string key){
// cout<<"DELETEING ENTRY KEY : "<<key<<endl;
auto it = cache.find(key);
if(it==cache.end())
return false;
pthread_mutex_lock(&(it->second->lock));
file_del(toCharArray(it->first));
cache.erase(it->first);
pthread_mutex_unlock(&(it->second->lock));
// free(it->second);
return true;
}
};
#include <pthread.h>
#include<bits/stdc++.h>
using namespace std;
class cache
{
public:
struct entry{
string key;
string value;
bool valid;
bool modified;
int frequency=0;
pthread_mutex_t lock;
};
const int KEY_SIZE= 256;
const int VAL_SIZE =256;
const int max_size =3;
map<string,entry*>cache;
long long int current_capacity;
long long unsigned int counter;
// bool delete_key(string);
// void find_new_slot();
string get_entry(string key){
if(cache.find(key)!=cache.end()){
cache[key]->frequency++;
return cache[key]->value;
}
return "Entry Not Found";
}
void initialize_cache(){
current_capacity=max_size;
counter=0;
}
bool put_into_cache(string key1,string value2){
if(key1.length()>256 || value2.length()>256){
return false;
}
struct entry * temp=(struct entry*)malloc(sizeof(struct entry));
int k;
pthread_mutex_lock(&(temp->lock));
temp->key=key1;
temp->value =value2;
temp->valid=1;
temp->modified=0;
temp->frequency++;
if(current_capacity<=0){
find_new_slot();
current_capacity++;
}
cache[key1]=temp;
current_capacity--;
counter++;
pthread_mutex_unlock(&(temp->lock));
return true;
}
void find_new_slot(){
string lowest;
int last_mod=INT_MAX;
for(map<string,entry*>::const_iterator it=cache.begin();it!=cache.end();++it){
if(it->second->frequency<last_mod){
lowest=it->first;
last_mod=it->second->frequency;
}
}
pthread_mutex_lock(&(cache[lowest]->lock));
entry* temp = cache[lowest];
cout<<"DELETEING ENTRY KEY : "<<lowest<<endl;
cache.erase(lowest);
pthread_mutex_unlock(&(cache[lowest]->lock));
// free(temp);
}
void print_cache(){
for(map<string,entry*>::const_iterator it=cache.begin();it!=cache.end();++it)
cout<<it->first<<" "<<it->second->value<<endl;
}
bool delete_key(string key){
cout<<"DELETEING ENTRY KEY : "<<key<<endl;
auto it = cache.find(key);
if(it==cache.end())
return false;
pthread_mutex_lock(&(it->second->lock));
cache.erase(it->first);
pthread_mutex_unlock(&(it->second->lock));
// free(it->second);
return true;
}
};
#ifndef METEO_GRPC_CLIENT_H
#define METEO_GRPC_CLIENT_H
#include <functional>
#include <stdexcept>
#include <bits/stdc++.h>
#include <memory>
#include <fstream>
#include <iostream>
#include <cmath>
#include "assert.h"
#include <grpc++/grpc++.h>
#include <thread>
#include "kv.grpc.pb.h"
using grpc::Channel;
using grpc::ChannelArguments;
using grpc::ClientAsyncResponseReader;
using grpc::ClientContext;
using grpc::ClientAsyncReader;
using grpc::ClientAsyncWriter;
using grpc::ClientAsyncReaderWriter;
using grpc::CompletionQueue;
using grpc::Status;
using helloworld::GetRequest;
using helloworld::GetReply;
using helloworld::PutRequest;
using helloworld::PutReply;
using helloworld::DelRequest;
using helloworld::DelReply;
using helloworld::KeyValueStore;
#include <chrono>
using namespace std::chrono;
using namespace std;
time_t start;
time_t end;
class AbstractAsyncClientCall
{
public:
enum CallStatus { PROCESS, FINISH, DESTROY };
explicit AbstractAsyncClientCall():callStatus(PROCESS){}
virtual ~AbstractAsyncClientCall(){}
ClientContext context;
Status status;
CallStatus callStatus ;
virtual void Proceed(bool = true) = 0;
};
class AsyncClientCall: public AbstractAsyncClientCall
{
std::unique_ptr< ClientAsyncResponseReader<GetReply> > responder;
public:
GetReply reply;
AsyncClientCall(const GetRequest& request, CompletionQueue& cq_, std::unique_ptr<KeyValueStore::Stub>& stub_):AbstractAsyncClientCall()
{
// std::cout << "Get Key" << std::endl;
responder = stub_->AsyncGetValue(&context, request, &cq_);
responder->Finish(&reply, &status, (void*)this);
callStatus = PROCESS ;
}
virtual void Proceed(bool ok = true) override
{
if(callStatus == PROCESS)
{
// Verify that the request was completed successfully. Note that "ok"
// corresponds solely to the request for updates introduced by Finish().
GPR_ASSERT(ok);
if(status.ok())
{
std::cout << "Value received: " <<reply.value()<<"Status:"<<reply.status() << std::endl;
// time(&end);
}
delete this;
}
}
};
class AsyncClientCall1M : public AbstractAsyncClientCall
{
std::unique_ptr< ClientAsyncResponseReader<PutReply> > responder;
public:
PutReply reply;
AsyncClientCall1M(const PutRequest& request, CompletionQueue& cq_, std::unique_ptr<KeyValueStore::Stub>& stub_):AbstractAsyncClientCall()
{
// std::cout << "Put Key" << std::endl;
responder = stub_->AsyncPutKValue(&context, request, &cq_);
responder->Finish(&reply, &status, (void*)this);
callStatus = PROCESS ;
}
virtual void Proceed(bool ok = true) override
{
if(callStatus == PROCESS)
{
// Verify that the request was completed successfully. Note that "ok"
// corresponds solely to the request for updates introduced by Finish().
GPR_ASSERT(ok);
if(status.ok())
{
std::cout << "Message received: " <<reply.message() <<"Status:"<<reply.status()<< std::endl;
// time(&end);
}
delete this;
}
}
};
class AsyncClientCallMM : public AbstractAsyncClientCall
{
std::unique_ptr< ClientAsyncResponseReader<DelReply> > responder;
public:
DelReply reply;
AsyncClientCallMM(const DelRequest& request, CompletionQueue& cq_, std::unique_ptr<KeyValueStore::Stub>& stub_):AbstractAsyncClientCall()
{
// std::cout << "Delete Key" << std::endl;
responder = stub_->AsyncDelKValue(&context, request, &cq_);
responder->Finish(&reply, &status, (void*)this);
callStatus = PROCESS ;
}
virtual void Proceed(bool ok = true) override
{
if(callStatus == PROCESS)
{
// Verify that the request was completed successfully. Note that "ok"
// corresponds solely to the request for updates introduced by Finish().
GPR_ASSERT(ok);
if(status.ok())
{
std::cout << "Message received: " <<reply.message()<<"Status:"<<reply.status() << std::endl;
// time(&end);
}
delete this;
}
}
};
class KVClient
{
public:
explicit KVClient(std::shared_ptr<Channel> channel)
:stub_(KeyValueStore::NewStub(channel))
{}
void GetValue(const std::string& key) {
// Data we are sending to the server.
GetRequest request;
request.set_key(key); // Assembles the client's payload and sends it to the server.
new AsyncClientCall(request, cq_, stub_);
}
void PutKValue(const std::string& key,const std::string& value)
{
// Data we are sending to the server.
PutRequest request;
request.set_key(key); // Assembles the client's payload and sends it to the server.
request.set_value(value);
new AsyncClientCall1M(request, cq_, stub_);
}
void DelKValue(const std::string& key)
{
// Data we are sending to the server.
DelRequest request;
request.set_key(key); // Assembles the client's payload and sends it to the server.
new AsyncClientCallMM(request, cq_, stub_);
}
void AsyncCompleteRpc()
{
void* got_tag;
bool ok = false;
while(cq_.Next(&got_tag, &ok))
{
AbstractAsyncClientCall* call = static_cast<AbstractAsyncClientCall*>(got_tag);
call->Proceed(ok);
}
std::cout << "Completion queue is shutting down." << std::endl;
}
private:
// Out of the passed in Channel comes the stub, stored here, our view of the
// server's exposed services.
std::unique_ptr<KeyValueStore::Stub> stub_;
// The producer-consumer queue we use to communicate asynchronously with the
// gRPC runtime.
CompletionQueue cq_;
};
// for string delimiter
vector<string> split (string s, string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;
while ((pos_end = s.find (delimiter, pos_start)) != string::npos) {
token = s.substr (pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back (token);
}
res.push_back (s.substr (pos_start));
return res;
}
int main(int argc, char* argv[])
{
ChannelArguments args;
// Set the load balancing policy for the channel.
args.SetLoadBalancingPolicyName("round_robin");
KVClient client(grpc::CreateCustomChannel("localhost:50051", grpc::InsecureChannelCredentials(),args));
std::thread thread_ = std::thread(&KVClient::AsyncCompleteRpc, &client);
std::string userInput;
int mode;
cout<<"1.Batch Mode 2.Interactive mode"<<endl;
cin>>mode;
if(mode==1)
{
fstream newfile;
newfile.open("batch.txt",ios::in); //open a file to perform read operation using file object
if (newfile.is_open())
{ //checking whether the file is open
string tp;
// cout<<"Reached here"<<endl;
while(getline(newfile, tp))
{ //read data from file object and put it into string.
std::string delimiter = " ";
std::vector<std::string> v = split (tp, delimiter);
string func=v.front();
if(!func.compare("Get"))
{
v.erase(v.begin());
string key=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.GetValue(key);
}
else if(!func.compare("Put"))
{
v.erase(v.begin());
string key=v.front();
v.erase(v.begin());
string value=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.PutKValue(key,value);
}
else if(!func.compare("Del"))
{
v.erase(v.begin());
string key=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.DelKValue(key);
}
}
newfile.close(); //close the file object.
}
}
else if(mode==2)
{
std::cout<<"1.Get Key 2.Put Key Value 3.Del Key"<<std::endl;
std::cout << "Press control-c to quit" << std::endl << std::endl;
while(1)
{
getline(std::cin,userInput);
std::string delimiter = " ";
std::vector<std::string> v = split (userInput, delimiter);
string func=v.front();
if(!func.compare("Get"))
{
v.erase(v.begin());
string key=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.GetValue(key);
}
else if(!func.compare("Put"))
{
v.erase(v.begin());
string key=v.front();
v.erase(v.begin());
string value=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.PutKValue(key,value);
}
else if(!func.compare("Del"))
{
v.erase(v.begin());
string key=v.front();
time(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(false);
client.DelKValue(key);
} }
}
thread_.join();
}
#endif
// 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 helloworld;
// The greeting service definition.
service KeyValueStore {
// Sends a greeting
rpc GetValue (GetRequest) returns (GetReply) {}
rpc PutKValue (PutRequest) returns (PutReply) {}
rpc DelKValue (DelRequest) returns (DelReply) {}
}
// The request message containing the key.
message GetRequest {
string key = 1;
}
// The response message containing the value of corresponding key
message GetReply {
string value = 1;
int32 status=2;
}
// The request message containing the key and value.
message PutRequest {
string key = 1;
string value = 2;
}
// The response message containing message after adding key-value pair
message PutReply {
string message = 1;
int32 status=2;
}
// The request message containing the key and value.
message DelRequest {
string key = 1;
}
// The response message containing message after removing key-value pair
message DelReply {
string message = 1;
int32 status=2;
}
#include<stdlib.h>
#include<semaphore.h>
#include <stdint.h>
#include<unistd.h>
#include<pthread.h>
#include<stdio.h>
#include<iostream>
#include<fcntl.h>
#include<sys/stat.h>
#include <limits.h>
#include <sys/types.h>
#include<string.h>
#include<unistd.h>
int *fds ,setSize=4,*readCounters,readercount = 0;;
sem_t *mutex,*readingLock,x,y;
pthread_t tid,writerthreads[100],readerthreads[100];
unsigned modulusIndex( char *num, size_t size, unsigned divisor) {
unsigned rem = 0,temp = 0, i;
while(1)
{
if(i>=divisor)
break;
temp=num[i]%divisor;
rem += temp;
i++;
}
unsigned ans = rem%divisor;
return ans;
}
int file_search(char *key, char *value, int index) {
// Setting pointer to the start of file
lseek(fds[index], 0, SEEK_SET);
char fkey[256],fval[256];