Somewhat production ready now. It's a bit slow, with 14 clients hammering on the server, the CPU reaches 80% ussage, and 16MB memory footprint.

This commit is contained in:
2025-05-16 21:31:04 +02:00
parent 18581ed9ac
commit 14279b5c06
13 changed files with 279 additions and 72 deletions
+8 -3
View File
@@ -10,9 +10,9 @@ using namespace std;
template <typename T>
class ConcurrentQueue {
private:
std::queue<T> queue_;
mutable std::mutex mutex_;
std::condition_variable condition_;
queue<T> queue_;
mutable mutex mutex_;
condition_variable condition_;
public:
// Apparently, if you have a mutex in a class, you can't copy or assign the class to any other class.
@@ -49,6 +49,11 @@ public:
lock_guard<mutex> lock(mutex_);
return queue_.empty();
}
int size() {
lock_guard<mutex> lock(mutex_);
return queue_.size();
}
};
#endif
+13
View File
@@ -0,0 +1,13 @@
#ifndef CRAWLER_H
#define CRAWLER_H
#include <string>
using namespace std;
struct Crawler {
string user_agent;
int links_pressed;
};
#endif
+3 -1
View File
@@ -4,7 +4,9 @@
enum data_type {
HTML,
CSS,
IMAGE
IMAGE,
TEXT,
IP
};
enum image_type {
+34
View File
@@ -0,0 +1,34 @@
//
// Created by skingging on 5/16/25.
//
#ifndef METRICSEXPORTER_H
#define METRICSEXPORTER_H
#include <memory>
#include <unordered_map>
#include "../include/Crawler.h"
using namespace std;
struct MetricsExporter {
static void serve(shared_ptr<unordered_map<uint32_t, Crawler>> crawler);
static void process_request(int client_fd);
static void send_data(int client_fd, const string& data);
};
const string HTML_RESPONSE_HEADER_lol =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Transfer-Encoding: chunked\r\n"
//"Cache-control: max-age=12000\r\n"
"Connection: close\r\n\r\n";
const string BEGINNING =
"<!DOCTYPE html>\n<html><head>\r\n"
"<title>Drip</title>\r\n"
"</head><body>\r\n";
const string END = "</body></html>\r\n";
#endif
+2 -2
View File
@@ -11,7 +11,7 @@ using namespace std;
class ServerUtils {
public:
static void serve(shared_ptr<ConcurrentQueue<Track>> t_test);
static void serve(shared_ptr<ConcurrentQueue<Track>> cq_track);
private:
static void process_request(int client_fd);
static void send_header(int client_fd, data_type type);
@@ -27,7 +27,7 @@ const string HTML_RESPONSE_HEADER =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Transfer-Encoding: chunked\r\n"
"Cache-control: max-age=12000\r\n"
//"Cache-control: max-age=12000\r\n"
"Connection: close\r\n\r\n";
const string CSS_RESPONSE_HEADER =
+3 -2
View File
@@ -4,13 +4,14 @@
#include <unordered_map>
#include "../include/ConcurrentQueue.h"
#include "../include/Crawler.h"
#include "../include/Track.h"
using namespace std;
struct TrackerUtils {
static void track(const shared_ptr<ConcurrentQueue<Track>>& t_test);
static void print(unordered_map<string, int> tracks);
static void track(const shared_ptr<ConcurrentQueue<Track>>& s_track, const shared_ptr<unordered_map<uint32_t, Crawler>>& crawler);
static void print();
};
#endif
+1
View File
@@ -15,6 +15,7 @@ struct WordUtils {
static string load_file(const string& path);
static vector<string> split_string(const string& input, data_type type);
static string extract_url(const string& input);
static string extract_user_agent(const string& input);
static bool contains_image(const string& input);
static string extract_image_name(const string& input);
static string create_tag(const unordered_map<string, unordered_map<string, int>>& word_frequencies, const char& hash);