ready to write the database commands

This commit is contained in:
2025-10-25 17:05:45 -05:00
parent 9124b24d40
commit 138ab70eff
3 changed files with 26 additions and 9 deletions

View File

@@ -14,7 +14,7 @@
#include <httplib.h> #include <httplib.h>
namespace socket_manager { namespace socket_manager {
void start_daemon(std::uint16_t port){ void start_daemon(std::uint16_t port, std::shared_ptr<duckdb::Connection> connection ){
int server_socket = socket(AF_INET, SOCK_STREAM, 0); int server_socket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in server_address; sockaddr_in server_address;
server_address.sin_family = AF_INET; server_address.sin_family = AF_INET;
@@ -28,13 +28,13 @@ namespace socket_manager {
while(true){ while(true){
int client_socket = accept(server_socket, nullptr,nullptr); int client_socket = accept(server_socket, nullptr,nullptr);
spdlog::info("new socket created"); spdlog::info("new socket created");
std::thread client_handling_thread = std::thread{handle_client, client_socket}; std::thread client_handling_thread = std::thread{handle_client, client_socket, connection};
client_handling_thread.detach(); client_handling_thread.detach();
spdlog::debug("new thread spun up, returning to main loop"); spdlog::debug("new thread spun up, returning to main loop");
} }
} }
void handle_client(int client_socket){ void handle_client(int client_socket, std::shared_ptr<duckdb::Connection> connection ){
std::vector buffer = std::vector<char>{}; std::vector buffer = std::vector<char>{};
serializable::HandshakeConnection prelim_handshake = serializable::HandshakeConnection{.payload = 123}; serializable::HandshakeConnection prelim_handshake = serializable::HandshakeConnection{.payload = 123};
nlohmann::json to_dump_handshake = prelim_handshake; nlohmann::json to_dump_handshake = prelim_handshake;
@@ -47,7 +47,7 @@ void handle_client(int client_socket){
close(client_socket); close(client_socket);
spdlog::debug("Connection closed gracefully {}", client_socket); spdlog::debug("Connection closed gracefully {}", client_socket);
} }
void start_http_server(std::uint16_t port){ void start_http_server(std::uint16_t port, std::shared_ptr<duckdb::Connection> connection ){
spdlog::info("Starting HTTP server on {}", port); spdlog::info("Starting HTTP server on {}", port);
httplib::Server server = httplib::Server{}; httplib::Server server = httplib::Server{};
std::vector<char> pixel = other_general_utils::read_file_bytes("1x1.png"); std::vector<char> pixel = other_general_utils::read_file_bytes("1x1.png");
@@ -82,3 +82,9 @@ void start_http_server(std::uint16_t port){
server.listen("0.0.0.0", port); server.listen("0.0.0.0", port);
} }
} }
namespace database_utils {
void initialize_db(std::shared_ptr<duckdb::Connection> connection){
}
}

View File

@@ -2,10 +2,14 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <cstdint> #include <cstdint>
#include <netinet/in.h> #include <netinet/in.h>
#include <duckdb.hpp>
namespace socket_manager { namespace socket_manager {
void handle_client(int client_socket); void handle_client(int client_socket, std::shared_ptr<duckdb::Connection> connection );
void start_daemon(std::uint16_t port); void start_daemon(std::uint16_t port, std::shared_ptr<duckdb::Connection> connection );
void start_http_server(std::uint16_t port); void start_http_server(std::uint16_t port, std::shared_ptr<duckdb::Connection> connection );
} }
namespace database_utils {
void initialize_db(std::shared_ptr<duckdb::Connection> connection);
}

View File

@@ -1,11 +1,15 @@
#include "duckdb/main/connection.hpp"
#include "duckdb/main/database.hpp"
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include <cstdint> #include <cstdint>
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <memory>
#include <toml++/toml.hpp> #include <toml++/toml.hpp>
#include <unistd.h> #include <unistd.h>
#include "daemon-utils/socket-manager.hpp" #include "daemon-utils/socket-manager.hpp"
#include <duckdb.hpp>
// This is the *daemon* // This is the *daemon*
int main() { int main() {
@@ -25,10 +29,13 @@ int main() {
spdlog::debug("Path collected: {}", http_server_fqdn); spdlog::debug("Path collected: {}", http_server_fqdn);
spdlog::debug("Port collected for inter-process communication {}", inter_comm_port); spdlog::debug("Port collected for inter-process communication {}", inter_comm_port);
std::thread socket_management_root = std::thread{socket_manager::start_daemon, inter_comm_port}; duckdb::DuckDB duck_in_memory_database = duckdb::DuckDB{nullptr};
std::shared_ptr<duckdb::Connection> connection = std::make_shared<duckdb::Connection>(duck_in_memory_database);
std::thread socket_management_root = std::thread{socket_manager::start_daemon, inter_comm_port, connection};
socket_management_root.detach(); socket_management_root.detach();
spdlog::info("socket management thread started and detached"); spdlog::info("socket management thread started and detached");
std::thread http_server_root = std::thread{socket_manager::start_http_server, http_port}; std::thread http_server_root = std::thread{socket_manager::start_http_server, http_port, connection};
http_server_root.detach(); http_server_root.detach();
spdlog::info("http root thread has been started and detached."); spdlog::info("http root thread has been started and detached.");
while (true) { while (true) {