gen library

This commit is contained in:
BuildTools 2019-04-10 11:26:17 -04:00
parent 8df27ccfb8
commit 2dadfc7544
6 changed files with 92 additions and 58 deletions

View File

@ -11,14 +11,21 @@ include $(dir)/Rules.mk
dir := client
include $(dir)/Rules.mk
LDFLAGS_TMP := -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
LDFLAGS_TMP := -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed \
-ldl \
.PHONY: all
all: out/vrsrv
all: out/vr.a out/vrclient out/vr.h
out/vrsrv: $(OBJ)
out/vr.h: inc/vr.h
cp inc/vr.h out/vr.h
out/vr.a: $(OBJ)
ar rcs $@ $^
out/vrclient: $(OBJ_C) out/vr.a
$(LINK)
clean:

View File

@ -8,6 +8,6 @@ CFLAGS_$(MOD):=$(addprefix -I, $(OUT)/proto/)
SRC_$(d) := client.cc
# DON'T TOUCH ANYTHING ELSE
include $(MK)/cc.mk
include $(MK)/cca.mk
include $(MK)/epilogue.mk

View File

@ -1,57 +1,11 @@
#include <iostream>
#include <vr.h>
#include <vrp.h>
#include <fstream>
#include <cstring>
#include <cassert>
using namespace std;
// taken from https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
vector<string> split(const string& str, const string& delim)
{
vector<string> tokens;
size_t prev = 0, pos = 0;
do
{
pos = str.find(delim, prev);
if (pos == string::npos) pos = str.length();
string token = str.substr(prev, pos-prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return tokens;
}
vr_config* parse_config_file(string& path)
{
uint32_t f;
string line;
ifstream infile(path);
getline(infile, line);
f = stoi(line);
vr_config *config = new vr_config(f);
while(getline(infile, line))
{
vr_role role;
vector<string> vec = split(line, " ");
assert(vec.size() == 3);
if(vec.at(0) == "p")
{
role = PRIMARY;
} else {
role = REPLICA;
}
config->add_config(role, vec.at(1), stoi(vec.at(2)));
cout << "added config: " << (role == PRIMARY ? "PRIMARY" : "REPLICA") << " " << vec.at(1) << ":" << stoi(vec.at(2)) << endl;
}
return config;
}
#define TRANSFER_BLK (2048)
#define BLK_SIZE (4096)
int num_block = 0;
@ -89,9 +43,7 @@ int main(int argc, char* argv[])
return -1;
}
string arg(argv[1]);
vr_config *conf = parse_config_file(arg);
vr_init(*conf, stoi(argv[2]));
vr_init(argv[1], stoi(argv[2]));
if (stoi(argv[2]) == 0)
{

View File

@ -1,9 +1,15 @@
#pragma once
#include "vrp.h"
#ifdef __cplusplus
extern "C" {
#endif
void vr_append(const char *data, size_t len);
void vr_accept(const char *data, size_t len);
void vr_init(vr_config& config, uint32_t config_index);
void vr_init(const char* config_file_path, uint32_t config_index);
#ifdef __cplusplus
}
#endif

12
mk/cca.mk Normal file
View File

@ -0,0 +1,12 @@
OBJ_$(d) := $(OBJ_$(d)) $(addprefix $(OUT)/$(d)/, $(SRC_$(d):.cc=.o))
$(OUT)/$(d)/%.o: MOD:=$(MOD)
$(OUT)/$(d)/%.o: d:=$(d)
$(OBJ_$(d)): $(OUT)/$(d)/%.o: $(d)/%.cc
$(MKDIR)
$(COMP)
OBJ_C := $(OBJ_C) $(OBJ_$(d))
-include $(DEP_$(d))

View File

@ -4,6 +4,7 @@
#include <sstream>
#include <cstring>
#include <chrono>
#include <fstream>
#include <cstdio>
#include <unistd.h>
@ -485,7 +486,7 @@ void commit_proc()
}
}
void vr_append(const char *_data, size_t len)
extern "C" void vr_append(const char *_data, size_t len)
{
uint32_t op_num = g_state->op_num + 1;
prepare_req req;
@ -553,8 +554,64 @@ void vr_init_thrd(vr_config *config, uint32_t config_index)
server->Wait();
}
void vr_init(vr_config &config, uint32_t config_index)
static void _vr_init(vr_config &config, uint32_t config_index)
{
server_thrd = thread(vr_init_thrd, &config, config_index);
while(!init){}
}
// taken from https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
vector<string> split(const string& str, const string& delim)
{
vector<string> tokens;
size_t prev = 0, pos = 0;
do
{
pos = str.find(delim, prev);
if (pos == string::npos) pos = str.length();
string token = str.substr(prev, pos-prev);
if (!token.empty()) tokens.push_back(token);
prev = pos + delim.length();
}
while (pos < str.length() && prev < str.length());
return tokens;
}
vr_config* parse_config_file(string& path)
{
uint32_t f;
string line;
ifstream infile(path);
getline(infile, line);
f = stoi(line);
vr_config *config = new vr_config(f);
while(getline(infile, line))
{
vr_role role;
vector<string> vec = split(line, " ");
assert(vec.size() == 3);
if(vec.at(0) == "p")
{
role = PRIMARY;
} else {
role = REPLICA;
}
config->add_config(role, vec.at(1), stoi(vec.at(2)));
cout << "Read config: " << (role == PRIMARY ? "PRIMARY" : "REPLICA") << " " << vec.at(1) << ":" << stoi(vec.at(2)) << endl;
}
return config;
}
extern "C" void vr_init(const char* path, uint32_t config_index)
{
vr_config* config;
string _path(path);
config = parse_config_file(_path);
_vr_init(*config, config_index);
}