Begin work on AsyncFileReader demo

This commit is contained in:
Alex Hultman
2018-09-23 20:32:47 +02:00
parent 7f3753e9db
commit 2fb87b67c7
5 changed files with 140 additions and 113 deletions
+82
View File
@@ -0,0 +1,82 @@
#include <map>
#include <cstring>
#include <fstream>
#include <sstream>
#include <iostream>
/* This is just a very simple and inefficient demo of async responses,
* please do roll your own variant or use a database or Node.js's async
* features instead of this really bad demo */
struct AsyncFileReader {
private:
/* The cache we have in memory for this file */
std::string cache;
int cacheOffset = 0;
/* The pending async file read (yes we only support one pending read) */
std::function<void(std::string_view)> pendingReadCb;
int fileSize;
std::ifstream fin;
uWS::Loop *loop;
public:
/* Construct a demo async. file reader for fileName */
AsyncFileReader(std::string fileName) {
fin.open(fileName, std::ios::binary);
// get fileSize
fin.seekg(0, fin.end);
fileSize = fin.tellg();
//std::cout << "File size is: " << fileSize << std::endl;
// cache up 1 mb!
cache.resize(1024 * 1024);
//std::cout << "Caching 1 MB at offset = " << 0 << std::endl;
fin.seekg(0, fin.beg);
fin.read(cache.data(), cache.length());
cacheOffset = 0;
// get loop for thread
loop = uWS::Loop::defaultLoop();
}
/* Returns any data already cached for this offset */
std::string_view peek(int offset) {
/* Did we hit the cache? */
if (offset >= cacheOffset && ((offset - cacheOffset) < cache.length())) {
/* Cache hit */
//std::cout << "Cache hit!" << std::endl;
return std::string_view(cache.data() + offset - cacheOffset, cache.length() - offset + cacheOffset);
} else {
/* Cache miss */
//std::cout << "Cache miss!" << std::endl;
return std::string_view(nullptr, 0);
}
}
/* Asynchronously request more data at offset */
void request(int offset, std::function<void(std::string_view)> cb) {
// std::async this
//std::cout << "Caching 1 MB at offset = " << offset << std::endl;
fin.seekg(offset, fin.beg);
fin.read(cache.data(), cache.length());
cacheOffset = offset;
// Loop.defer this
cb(std::string_view(cache.data(), cache.length()));
}
/* Abort any pending async. request */
void abort() {
}
int getFileSize() {
return fileSize;
}
};
-49
View File
@@ -1,49 +0,0 @@
#include <experimental/filesystem>
#include <map>
#include <fstream>
struct FileCache {
private:
std::map<std::string_view, std::string_view> cache;
void cacheFile(std::string file, std::string root) {
std::cout << "Caching file: " << file << std::endl;
std::ifstream fin(root + file, std::ios::binary);
std::ostringstream oss;
oss << fin.rdbuf();
if (file == "/index.html") {
file = "/";
}
char *cachedFile = (char *) malloc(oss.str().size());
memcpy(cachedFile, oss.str().data(), oss.str().size());
char *key = (char *) malloc(file.length());
memcpy(key, file.data(), file.length());
std::cout << "Size: " << oss.str().size() << std::endl;
cache[std::string_view(key, file.length())] = std::string_view(cachedFile, oss.str().size());
}
public:
FileCache(std::string root) {
for(auto &p : std::experimental::filesystem::recursive_directory_iterator(root)) {
cacheFile(p.path().string().substr(root.length()), root);
}
}
std::string_view getFile(std::string_view file) {
auto it = cache.find(file);
if (it == cache.end()) {
//std::cout << "Did not find file: " << file << std::endl;
return "<h1>Nope!</h1>";
} else {
//std::cout << "Did find file: " << file << std::endl;
return it->second;
}
}
};