From f201239152ef570bc7f94839d25fef0eacb39f48 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Sat, 6 Jul 2019 22:33:42 +0200 Subject: [PATCH] Added some way to configure the system --- Makefile | 2 +- src/izaro-storage.cpp | 194 +++++++++++++++++++++++++----------------- 2 files changed, 119 insertions(+), 77 deletions(-) diff --git a/Makefile b/Makefile index 5dfe392..66a0d59 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ TJS_SRC := #pool_allocator.cpp TinyJS.cpp TinyJS_Functions.cpp TinyJS_MathFunctions.cpp TinyJS_StringFunctions.cpp TinyJS_Threading.cpp TJS_OBJECTS := $(TJS_SRC:%.cpp=$(OBJ_DIR)/%.tjs.o) -OBJECTS := $(SRC:%.cpp=$(OBJ_DIR)/%.o) +OBJECTS := $(SRC:%.cpp=$(OBJ_DIR)/%.o) ./CommandEr/build/commander.o TEST_OBJECTS := $(SRC:%.cpp=$(OBJ_DIR)/%.test.o) TARGETNAME := $(TARGET:%.cpp=%) diff --git a/src/izaro-storage.cpp b/src/izaro-storage.cpp index 8a71510..daa0c0b 100644 --- a/src/izaro-storage.cpp +++ b/src/izaro-storage.cpp @@ -13,6 +13,7 @@ #include #include #include +#include "commander.hpp" int main( @@ -99,88 +100,129 @@ int main( db.delete_table.clear(); } #endif - database run_db(database::create("/tmp/", 4096)); - auto soc = socket(AF_INET, SOCK_DGRAM, 0); - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_port = htons(20450); - memset((void*)&addr.sin_addr, 0, sizeof(addr.sin_addr)); - bind(soc,(struct sockaddr*)&addr,sizeof(addr)); - - do{ - received_data recv; - sending_data reply; - sockaddr_in client; - socklen_t packet_sz; - size_t reply_size; - recvfrom( - soc, - (void*)&recv, - sizeof(received_data), - MSG_WAITFORONE, - (struct sockaddr*)&client, - &packet_sz - ); - - if(packet_sz < sizeof(db_op)) continue; - - switch (static_cast(recv.op)) - { - case db_op::version: - reply.rep_id = recv.rep_id; - reply.identifier.x = 1; - reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier.x); - break; - case db_op::read: - { + + CMD::commander cmd_args(argc, argv); + + std::string database_str = "/data/"; + + if(cmd_args.isFlagSet("-database")) + { + database_str = cmd_args.getFlagValue("-database"); + } + + size_t database_size = 4096; + + if(cmd_args.isFlagSet("-page-count")) + { + try{ + database_size = std::stoll(cmd_args.getFlagValue("-page-count")); + } catch (...) { + std::cerr << "Invalid page-count value" << std::endl; + return 1; + } + + } + + if(cmd_args.isFlagSet("-create")) + { + try{ + database creat(database::create(database_str, database_size)); + std::cout << "Created" << std::endl; + return 0; + } catch (...) { + std::cerr << "Creating of " << database_str << " of " << database_size << " pages failed" << std::endl; + return 1; + } + } + + try{ + + database run_db(database::open(database_str)); + auto soc = socket(AF_INET, SOCK_DGRAM, 0); + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(20450); + memset((void*)&addr.sin_addr, 0, sizeof(addr.sin_addr)); + bind(soc,(struct sockaddr*)&addr,sizeof(addr)); + + do{ + received_data recv; + sending_data reply; + sockaddr_in client; + socklen_t packet_sz; + size_t reply_size; + recvfrom( + soc, + (void*)&recv, + sizeof(received_data), + MSG_WAITFORONE, + (struct sockaddr*)&client, + &packet_sz + ); + + if(packet_sz < sizeof(db_op)) continue; + + switch (static_cast(recv.op)) + { + case db_op::version: reply.rep_id = recv.rep_id; - auto req = run_db.read(recv.identifier); - if(req.first != 0) + reply.identifier.x = 1; + reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier.x); + break; + case db_op::read: { - reply.identifier = recv.identifier; - reply.page = req.second; - reply_size = sizeof(reply); + reply.rep_id = recv.rep_id; + auto req = run_db.read(recv.identifier); + if(req.first != 0) + { + reply.identifier = recv.identifier; + reply.page = req.second; + reply_size = sizeof(reply); + } + else + { + reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); + } } - else + break; + case db_op::write: { - reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); + reply.rep_id = recv.rep_id; + try{ + run_db.write(recv.identifier, recv.page); + reply.identifier = recv.identifier; + } catch (...) { + + } + reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); } - } - break; - case db_op::write: - { - reply.rep_id = recv.rep_id; - try{ - run_db.write(recv.identifier, recv.page); + break; + case db_op::remove: + { + reply.rep_id = recv.rep_id; + run_db.remove(recv.identifier); reply.identifier = recv.identifier; - } catch (...) { - + reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); } - reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); - } - break; - case db_op::remove: - { - reply.rep_id = recv.rep_id; - run_db.remove(recv.identifier); - reply.identifier = recv.identifier; - reply_size = sizeof(reply.rep_id) + sizeof(reply.identifier); - } - break; - default: - std::cerr << "bad_request " << (uint32_t)static_cast(recv.op) << std::endl; - continue; - break; - } - sendto( - soc, - (void*)&reply, - reply_size, - 0, - (struct sockaddr*)&client, - (socklen_t)sizeof(client) - ); - //std::cerr << "reply to " << (uint32_t)static_cast(recv.op) << std::endl; - }while(true); + break; + default: + std::cerr << "bad_request " << (uint32_t)static_cast(recv.op) << std::endl; + continue; + break; + } + sendto( + soc, + (void*)&reply, + reply_size, + 0, + (struct sockaddr*)&client, + (socklen_t)sizeof(client) + ); + //std::cerr << "reply to " << (uint32_t)static_cast(recv.op) << std::endl; + }while(true); + } catch (...) { + std::cerr << "GoJDB crashed !" << std::endl; + return 1; + } }