docs: begin to setup main structure and write BlockSS
This commit is contained in:
13
backend/src/bs-utility.cpp
Normal file
13
backend/src/bs-utility.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include "bs-utility.h"
|
||||
|
||||
BlockingStringStream &BlockingStringStream::getline(std::string &str,
|
||||
char delim) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
// Wait until data is available
|
||||
condition.wait(lock, [this] { return internalStream.peek() != EOF; });
|
||||
|
||||
std::getline(internalStream, str, delim);
|
||||
|
||||
return *this;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
#include "builtin-cli.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "schedule.h"
|
||||
#include "bs-utility.h"
|
||||
void BookStoreMain(bool is_server, std::string config_dir) {
|
||||
std::ios::sync_with_stdio(false);
|
||||
if (!is_server) {
|
||||
; // TODO: run as client
|
||||
} else {
|
||||
throw FatalError("Server mode is not implemented yet", 1);
|
||||
std::cin.tie(nullptr);
|
||||
std::cout.rdbuf(nullptr);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
#include "engine.h"
|
@ -1 +1,60 @@
|
||||
int main() { return 0; }
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "bs-utility.h"
|
||||
#include "builtin-cli.h"
|
||||
#include "clipp/clipp.h"
|
||||
void test() {
|
||||
BlockingStringStream bss;
|
||||
|
||||
std::thread writer([&bss]() {
|
||||
for (int i = 1; i <= 5; ++i) {
|
||||
bss << "Data " << i << '\n';
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
});
|
||||
|
||||
std::thread reader([&bss]() {
|
||||
std::string data;
|
||||
for (int i = 1; i <= 5; ++i) {
|
||||
// Use getline with string delimiter
|
||||
bss.getline(data, '\n');
|
||||
std::cout << "Received: " << data << std::endl;
|
||||
}
|
||||
});
|
||||
reader.join();
|
||||
writer.join();
|
||||
}
|
||||
int main(int argc, char **argv) {
|
||||
test();
|
||||
return 0;
|
||||
bool is_server = false;
|
||||
std::string config_dir = "";
|
||||
bool custom_config_dir = false;
|
||||
auto cli =
|
||||
(clipp::option("-s", "--server").set(is_server).doc("run as server"),
|
||||
clipp::option("-c", "--config")
|
||||
.set(custom_config_dir)
|
||||
.doc("use config directory") &
|
||||
clipp::value("config directory", config_dir));
|
||||
if (!clipp::parse(argc, argv, cli)) {
|
||||
std::cout << clipp::make_man_page(cli, argv[0]);
|
||||
return 0;
|
||||
}
|
||||
if (!custom_config_dir) config_dir = "./";
|
||||
try {
|
||||
BookStoreMain(is_server, config_dir);
|
||||
} catch (const FatalError &e) {
|
||||
std::cerr << "\e[7m\e[31m[Fatal Error] " << e.GetCode() << " : " << e.what()
|
||||
<< "\e[0m" << std::endl;
|
||||
return e.GetCode();
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "\e[7m\e[31m[other std::exception] " << e.what() << "\e[0m"
|
||||
<< std::endl;
|
||||
return 255;
|
||||
} catch (...) {
|
||||
std::cerr << "\e[7m\e[31m[Unknown Exception]\e[0m" << std::endl;
|
||||
return 255;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
#include "schedule.h"
|
||||
#include "engine.h"
|
Reference in New Issue
Block a user