jvr/client/client.cc

126 lines
2.6 KiB
C++

#include <iostream>
#include <vr.h>
#include <vrp.h>
#include <fstream>
#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;
bool endd = false;
char block[BLK_SIZE];
void vr_accept(const char *data, size_t len)
{
cout << "[APPLICATION] VR_ACCEPT: " << len << " block " << num_block << endl;
if(len == BLK_SIZE)
{
for(int i = 0; i < len; i++)
{
assert(data[i] == 6);
}
num_block++;
} else
{
if (num_block == TRANSFER_BLK)
{
cout << "SUCCESS!" << endl;
}
else {
cout << "ERROR!" << endl;
}
endd = true;
}
}
int main(int argc, char* argv[])
{
if (argc != 3)
{
cout << "invalid arguments." << endl;
return -1;
}
string arg(argv[1]);
vr_config *conf = parse_config_file(arg);
vr_init(*conf, stoi(argv[2]));
if (stoi(argv[2]) == 0)
{
memset(block, 6, BLK_SIZE);
for (uint32_t i = 0; i < TRANSFER_BLK; i++)
{
cout << "appending block " << i << endl;
vr_append(block, BLK_SIZE);
}
vr_append(block, 10);
vr_append(block, 10);
}
while(!endd)
{
}
// string each;
// while(!cin.eof())
// {
// cin >> each;
// vr_append(each.c_str(), each.length() + 1);
// }
return 0;
}