Skip to content

Commit 22c44e6

Browse files
committed
feat: project 09
1 parent 1db7bea commit 22c44e6

33 files changed

+1429
-5
lines changed

common/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ function(setupGTest target)
2222
gtest_discover_tests(${target})
2323
endfunction()
2424

25-
26-
function(setupCPack target)
27-
install(TARGETS ${target} RUNTIME DESTINATION bin)
28-
25+
function(setupDebPkg)
2926
set(CPACK_GENERATOR DEB)
3027
set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
3128
set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
3229
set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
3330
set(CPACK_PACKAGE_CONTACT j0tunn@ya.ru)
3431
include(CPack) # generates target `package`
3532
endfunction()
33+
34+
function(setupCPack target)
35+
install(TARGETS ${target} RUNTIME DESTINATION bin)
36+
setupDebPkg()
37+
endfunction()

projects/09/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../common/CMakeLists.txt)
4+
5+
project(async VERSION ${PROJECT_VERSION})
6+
7+
add_library(async SHARED
8+
async.cpp
9+
bulk_reader.cpp
10+
stream_logger.cpp
11+
file_logger.cpp
12+
reader_state.cpp
13+
thread_logger.cpp
14+
)
15+
16+
add_executable(async_cli
17+
main.cpp
18+
)
19+
target_link_libraries(async_cli async)
20+
21+
# Package
22+
install(TARGETS async LIBRARY DESTINATION lib)
23+
install(TARGETS async_cli RUNTIME DESTINATION bin)
24+
setupDebPkg()

projects/09/async.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "async.h"
2+
#include "bulk_reader.h"
3+
#include "stream_logger.h"
4+
#include "file_logger.h"
5+
#include "thread_logger.h"
6+
#include <filesystem>
7+
#include <string>
8+
9+
using namespace std;
10+
11+
namespace async {
12+
13+
class Handle {
14+
public:
15+
Handle(size_t bulkSize)
16+
: reader_(bulkSize)
17+
, consoleLogger_(1, []() { return new StreamLogger(cout); })
18+
, fileLogger_(2, []() { return new FileLogger(filesystem::current_path()); })
19+
{
20+
reader_.addFlushObserver(&consoleLogger_);
21+
reader_.addFlushObserver(&fileLogger_);
22+
}
23+
24+
~Handle() {
25+
reader_.eof();
26+
}
27+
28+
void addCmd(const string& cmd) {
29+
reader_.addCmd(cmd);
30+
}
31+
32+
private:
33+
BulkReader reader_;
34+
ThreadLogger consoleLogger_;
35+
ThreadLogger fileLogger_;
36+
};
37+
38+
Handle* connect(size_t bulkSize) {
39+
return new Handle(bulkSize);
40+
}
41+
42+
void receive(Handle* h, const char *data, size_t size) {
43+
h->addCmd(string(data, size));
44+
}
45+
46+
void disconnect(Handle* h) {
47+
delete h;
48+
}
49+
50+
}

projects/09/async.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
5+
namespace async {
6+
7+
class Handle;
8+
9+
Handle* connect(std::size_t bulkSize);
10+
void receive(Handle* h, const char *data, std::size_t size);
11+
void disconnect(Handle* h);
12+
13+
}

projects/09/bulk_reader.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
#include "bulk_reader.h"
3+
4+
using namespace std;
5+
6+
BulkReader::BulkReader(unsigned int bulkSize)
7+
: state_(new AutoModeState([this](unique_ptr<ReaderState>&& newState) { this->setNewState(move(newState)); }, bulkSize))
8+
{}
9+
10+
void BulkReader::addCmd(const string& cmd) {
11+
if (cmd == "{") {
12+
state_->startBulk();
13+
return;
14+
}
15+
16+
if (cmd == "}") {
17+
state_->finishBulk();
18+
return;
19+
}
20+
21+
state_->addCmd(cmd);
22+
}
23+
24+
void BulkReader::eof() {
25+
state_->eof();
26+
}
27+
28+
void BulkReader::setNewState(unique_ptr<ReaderState>&& newState) {
29+
notifyFlush_(state_->getBulk());
30+
state_ = move(newState);
31+
}
32+
33+
void BulkReader::addFlushObserver(IFlushObserver* observer) {
34+
observers_.push_back(observer);
35+
}
36+
37+
void BulkReader::notifyFlush_(const vector<Command>& bulk) {
38+
for (auto observer : observers_) {
39+
observer->onFlush(bulk);
40+
}
41+
}

projects/09/bulk_reader.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include <memory>
6+
#include "flush_observer.h"
7+
#include "command.h"
8+
#include "reader_state.h"
9+
10+
class BulkReader {
11+
public:
12+
explicit BulkReader(unsigned int bulkSize);
13+
void addCmd(const std::string& cmd);
14+
void eof();
15+
16+
void addFlushObserver(IFlushObserver* observer);
17+
18+
private:
19+
void notifyFlush_(const std::vector<Command>& bulk);
20+
void setNewState(std::unique_ptr<ReaderState>&& state);
21+
22+
std::vector<IFlushObserver*> observers_;
23+
std::unique_ptr<ReaderState> state_;
24+
};

projects/09/command.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <ctime>
5+
6+
struct Command {
7+
explicit Command(const std::string& cmd)
8+
: val(cmd)
9+
, timestamp(std::time(0))
10+
{}
11+
12+
Command(const Command&) = default;
13+
14+
std::string val;
15+
std::time_t timestamp;
16+
};

projects/09/file_logger.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <sstream>
2+
#include <fstream>
3+
#include <thread>
4+
#include "file_logger.h"
5+
#include "stream_logger.h"
6+
7+
using namespace std;
8+
9+
FileLogger::FileLogger(const filesystem::path& dir)
10+
: logDir_(dir)
11+
{}
12+
13+
void FileLogger::onFlush(const vector<Command>& bulk) {
14+
if (bulk.size() == 0) {
15+
return;
16+
}
17+
18+
stringstream fileName;
19+
fileName << "bulk" << bulk[0].timestamp << "_" << this_thread::get_id() << ".log";
20+
21+
ofstream out(logDir_ / fileName.str());
22+
StreamLogger subLogger(out);
23+
subLogger.onFlush(bulk);
24+
}

projects/09/file_logger.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <filesystem>
5+
#include "flush_observer.h"
6+
7+
class FileLogger : public IFlushObserver {
8+
public:
9+
FileLogger(const std::filesystem::path& dir);
10+
void onFlush(const std::vector<Command>& bulk) override;
11+
12+
private:
13+
const std::filesystem::path logDir_;
14+
};

projects/09/flush_observer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include "command.h"
5+
6+
class IFlushObserver {
7+
public:
8+
virtual ~IFlushObserver() = default;
9+
virtual void onFlush(const std::vector<Command>& bulk) = 0;
10+
};

0 commit comments

Comments
 (0)