aboutsummaryrefslogtreecommitdiff
path: root/works/life/computer-network-experiment/IO.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'works/life/computer-network-experiment/IO.cpp')
-rw-r--r--works/life/computer-network-experiment/IO.cpp81
1 files changed, 0 insertions, 81 deletions
diff --git a/works/life/computer-network-experiment/IO.cpp b/works/life/computer-network-experiment/IO.cpp
deleted file mode 100644
index 5d3fe12..0000000
--- a/works/life/computer-network-experiment/IO.cpp
+++ /dev/null
@@ -1,81 +0,0 @@
-#include "IO.h"
-
-#include <folly/CancellationToken.h>
-
-#include <mutex>
-#include <ostream>
-#include <thread>
-#include <type_traits>
-
-folly::MPMCQueue<Output> output_queue(100);
-
-namespace {
-folly::CancellationSource cancellation_source;
-std::thread io_thread;
-}
-
-void PrintOutput(const Output &output) {
- std::basic_ostream<Char> *stream;
-
- switch (output.type) {
- case OutputType::Error:
- stream = &error_stream;
- break;
- default:
- stream = &output_stream;
- break;
- }
-
- switch (output.color) {
- case OutputColor::Normal:
- (*stream) << output.message;
- break;
- case OutputColor::Green:
- (*stream) << CRUT("\x1b[32m") << output.message << CRUT("\x1b[39m")
- << std::flush;
- break;
- case OutputColor::Red:
- (*stream) << CRUT("\x1b[31m") << output.message << CRUT("\x1b[39m")
- << std::flush;
- break;
- case OutputColor::Yellow:
- (*stream) << CRUT("\x1b[33m") << output.message << CRUT("\x1b[39m")
- << std::flush;
- break;
- }
-}
-
-String ReadInputLine() {
- String line;
- std::getline(input_stream, line);
- return line;
-}
-
-void IOThread() {
- while (true) {
- if (cancellation_source.isCancellationRequested()) {
- while (true) {
- Output output;
- if (output_queue.readIfNotEmpty(output)) {
- PrintOutput(output);
- } else {
- return;
- }
- }
- }
-
- Output output;
- while (output_queue.readIfNotEmpty(output))
- PrintOutput(output);
-
- PrintOutput({CRUT("> ")});
- OnInputLine(ReadInputLine());
- }
-}
-
-void SignalAndWaitForOutputThreadStop() {
- cancellation_source.requestCancellation();
- io_thread.join();
-}
-
-void StartIOThread() { io_thread = std::thread(IOThread); }