docs: begin to setup main structure and write BlockSS
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,6 +1,7 @@
|
||||
/.devcontainer
|
||||
/.vscode
|
||||
/maintenance/test
|
||||
/maintenance/demo
|
||||
.clang-format
|
||||
/frontend/Web/node_modules
|
||||
/frontend/client/node_modules
|
||||
|
@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.15.2)
|
||||
project(BookStore)
|
||||
file(GLOB_RECURSE main_src backend/src/*.cpp)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/backend/include)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/external)
|
||||
add_executable(code ${main_src})
|
||||
add_subdirectory(maintenance/test)
|
||||
include(maintenance/test/ctest_config)
|
64
backend/include/bs-utility.h
Normal file
64
backend/include/bs-utility.h
Normal file
@ -0,0 +1,64 @@
|
||||
#ifndef PROTECTOR_UTILITY_H
|
||||
#define PROTECTOR_UTILITY_H
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <exception>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
class FatalError : public std::exception {
|
||||
public:
|
||||
FatalError(const char *__message, int __code)
|
||||
: message(__message), code(__code) {}
|
||||
|
||||
const char *what() const noexcept override { return message.c_str(); }
|
||||
const int GetCode() const noexcept { return code; }
|
||||
|
||||
private:
|
||||
std::string message;
|
||||
int code;
|
||||
};
|
||||
class BlockingStringStream {
|
||||
public:
|
||||
BlockingStringStream() {}
|
||||
|
||||
// Declaration of major std::stringstream interfaces
|
||||
template <typename T>
|
||||
BlockingStringStream &operator<<(const T &val);
|
||||
|
||||
template <typename T>
|
||||
BlockingStringStream &operator>>(T &val);
|
||||
BlockingStringStream &getline(std::string &str, char delim = '\n');
|
||||
std::stringstream internalStream;
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
};
|
||||
// Implementation of operator<<
|
||||
template <typename T>
|
||||
BlockingStringStream &BlockingStringStream::operator<<(const T &val) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
if (internalStream.peek() == EOF) internalStream.clear();
|
||||
internalStream << val;
|
||||
}
|
||||
condition.notify_one();
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Implementation of operator>>
|
||||
template <typename T>
|
||||
BlockingStringStream &BlockingStringStream::operator>>(T &val) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
|
||||
// Wait until data is available
|
||||
condition.wait(lock, [this] { return internalStream.peek() != EOF; });
|
||||
|
||||
internalStream >> val;
|
||||
|
||||
return *this;
|
||||
}
|
||||
#endif // PROTECTOR_UTILITY_H
|
@ -0,0 +1,5 @@
|
||||
#ifndef PROTECTOR_BUILDTIN_CLI_H
|
||||
#define PROTECTOR_BUILDTIN_CLI_H
|
||||
#include<string>
|
||||
void BookStoreMain(bool,std::string);
|
||||
#endif //PROTECTOR_BUILDTIN_CLI_H
|
@ -0,0 +1,4 @@
|
||||
#ifndef PROTECTOR_ENGINE_H
|
||||
#define PROTECTOR_ENGINE_H
|
||||
|
||||
#endif // PROTECTOR_ENGINE_H
|
@ -0,0 +1,4 @@
|
||||
#ifndef PROTECTOR_SCHEDULE_H
|
||||
#define PROTECTOR_SCHEDULE_H
|
||||
|
||||
#endif // PROTECTOR_SCHEDULE_H
|
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