diff --git a/Rules.top b/Rules.top index a287fc1..c4b8bd4 100644 --- a/Rules.top +++ b/Rules.top @@ -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: diff --git a/client/Rules.mk b/client/Rules.mk index 7966951..239aefa 100644 --- a/client/Rules.mk +++ b/client/Rules.mk @@ -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 diff --git a/client/client.cc b/client/client.cc index d03bc21..0b0d2d0 100644 --- a/client/client.cc +++ b/client/client.cc @@ -1,57 +1,11 @@ #include #include -#include #include +#include #include using namespace std; -// taken from https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c -vector split(const string& str, const string& delim) -{ - vector 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 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) { diff --git a/inc/vr.h b/inc/vr.h index 44a8d10..a6bd81b 100644 --- a/inc/vr.h +++ b/inc/vr.h @@ -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 \ No newline at end of file diff --git a/mk/cca.mk b/mk/cca.mk new file mode 100644 index 0000000..8fe1468 --- /dev/null +++ b/mk/cca.mk @@ -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)) diff --git a/server/server.cc b/server/server.cc index c55dd3c..319bc0e 100644 --- a/server/server.cc +++ b/server/server.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -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 split(const string& str, const string& delim) +{ + vector 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 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); +}