diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d8e718..b0e35db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ add_executable(app shared/src/values/Dna.cpp shared/src/values/DnaManager.cpp shared/src/values/mrand.cpp + shared/src/TcpSocket.cpp ) # Add include directories target_include_directories(app PRIVATE app/inc shared/inc ${CMAKE_BINARY_DIR}/raylib/include) diff --git a/app/inc/DnaStore.hpp b/app/inc/DnaStore.hpp index bac51cd..045a999 100644 --- a/app/inc/DnaStore.hpp +++ b/app/inc/DnaStore.hpp @@ -6,4 +6,5 @@ namespace DnaStore void saveData(DnaManagerData *data); void saveVec(DnaManagerData *data); void saveGen(DnaManagerData *data); + void sync(); } // namespace DnaStore diff --git a/app/src/DnaStore.cpp b/app/src/DnaStore.cpp index 102a90f..f79560a 100644 --- a/app/src/DnaStore.cpp +++ b/app/src/DnaStore.cpp @@ -1,14 +1,21 @@ #include #include +#include +#include +#include +#include #include "sys.hpp" #include "DnaStore.hpp" +#include "TcpSocket.hpp" +#include "NetConst.hpp" #include #define ID_FILE_NAME "ID.bin" #define DATA_FILE_NAME "DATA.bin" #define VECTOR_FILE_NAME "VECTOR.bin" +#define GEN_FILE_PATTRN "gen%04d.bin" void DnaStore::load(DnaManagerData *data) { @@ -134,7 +141,92 @@ void DnaStore::saveGen(DnaManagerData *data) gen[i].liked = Liked::no; } - const char *fileName = TextFormat("gen%04d.bin", data->generation); + const char *fileName = TextFormat(GEN_FILE_PATTRN, data->generation); sys::saveDataToFile(fileName, gen.data(), sizeof(NetUnit) * NUM_PER_GEN); + sync(); +} + +void client(std::string prefix); + +void DnaStore::sync() +{ + const char *prefix = sys::transformFilePath(""); + std::string prefixs = prefix; + + std::thread t(client, prefixs); + t.detach(); +} + +void client(std::string prefix) +{ + + constexpr int extra_buff = 20; // len of 2**31 -> 2147483648 plus the pattern size + std::string buffer; + buffer.resize(prefix.size() + extra_buff); + + int sock = TcpSocket::connectt("petrovv.com", 8888); + + if (sock < 0) + { + // printf("Error %d", sock); + return; + } + + int ret = sprintf(buffer.data(), "%s/" ID_FILE_NAME, prefix.c_str()); + buffer.data()[ret] = 0; + std::ifstream idf(buffer, std::ios_base::binary); + + uint64_t ID = 0; + + idf.read((char *)&ID, sizeof(ID)); + idf.close(); + + // printf("id: %ld\n", ID); + + TcpSocket::sendt(sock, &StartHeader, sizeof(StartHeader)); + TcpSocket::sendt(sock, &ID, sizeof(ID)); + + Message message; + + int needed_gen = 0; + + while (true) + { + TcpSocket::recvt(sock, &message, sizeof(Message)); + if (message.mess != Mess::REQ_SEND_GEN) + { + message.mess = Mess::RES_NO; + TcpSocket::sendt(sock, &message, sizeof(Message)); + break; + } + + needed_gen = message.data; + + int ret = sprintf(buffer.data(), "%s/" GEN_FILE_PATTRN, prefix.c_str(), needed_gen); + buffer.data()[ret] = 0; + + if (!std::filesystem::exists(buffer)) + { + message.mess = Mess::RES_NO; + TcpSocket::sendt(sock, &message, sizeof(Message)); + break; + } + + std::ifstream sfile(buffer, std::ios_base::binary | std::ios_base::in); + + std::vector net; + net.resize(NUM_PER_GEN); + + sfile.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit)); + + message.mess = Mess::RES_OK; + message.data = NUM_PER_GEN * sizeof(NetUnit); + + TcpSocket::sendt(sock, &message, sizeof(Message)); + + TcpSocket::sendt(sock, net.data(), NUM_PER_GEN * sizeof(NetUnit)); + } + + TcpSocket::closet(sock); } diff --git a/server/src/client.cpp b/server/src/client.cpp index 0fe3033..da05266 100644 --- a/server/src/client.cpp +++ b/server/src/client.cpp @@ -8,7 +8,7 @@ int main() { - int sock = TcpSocket::connectt("localhost", 8888); + int sock = TcpSocket::connectt("petrovv.com", 8888); if (sock < 0) { @@ -53,7 +53,7 @@ int main() TcpSocket::sendt(sock, &message, sizeof(Message)); break; } - + std::ifstream sfile(filename, std::ios_base::binary | std::ios_base::in); std::vector net; diff --git a/server/src/server.cpp b/server/src/server.cpp index 0272dc2..e8fc3c3 100644 --- a/server/src/server.cpp +++ b/server/src/server.cpp @@ -13,8 +13,8 @@ // When a new client connected: void call(int sock, sockaddr_in newSocketInfo) { - printf("new User"); - + std::string add = TcpSocket::remoteAddress(newSocketInfo); + printf("new: %s\n", add.c_str()); int64_t conf, id; Message message; @@ -23,6 +23,7 @@ void call(int sock, sockaddr_in newSocketInfo) if (conf != StartHeader) { + printf("StartHeader ERROR\n"); TcpSocket::closet(sock); return; } @@ -51,14 +52,13 @@ void call(int sock, sockaddr_in newSocketInfo) while (sqlite3_step(stmt) != SQLITE_DONE) { int num_cols = sqlite3_column_count(stmt); - + int type = sqlite3_column_type(stmt, 0); - - if(type == SQLITE_NULL) break; + + if (type == SQLITE_NULL) + break; gen = sqlite3_column_int64(stmt, 0); gen++; - - } sqlite3_finalize(stmt); @@ -80,8 +80,9 @@ void call(int sock, sockaddr_in newSocketInfo) list.resize(NUM_PER_GEN); TcpSocket::recvt(sock, list.data(), NUM_PER_GEN * sizeof(NetUnit)); - - for(size_t i = 0; i < list.size(); i++){ + + for (size_t i = 0; i < list.size(); i++) + { sqlite3_bind_int64(stmt, 1, id); sqlite3_bind_int64(stmt, 2, gen); sqlite3_bind_int64(stmt, 3, list[i].hash); @@ -97,7 +98,7 @@ void call(int sock, sockaddr_in newSocketInfo) sqlite3_finalize(stmt); sqlite3_close(db); - printf("del USER"); + printf("del\n"); TcpSocket::closet(sock); } diff --git a/shared/inc/values/DnaManager.hpp b/shared/inc/values/DnaManager.hpp index 5676c29..e58f415 100644 --- a/shared/inc/values/DnaManager.hpp +++ b/shared/inc/values/DnaManager.hpp @@ -3,7 +3,7 @@ #include #define NUM_PER_GEN 16 -#define NUM_OF_MUT 1 +#define NUM_OF_MUT 3 enum Liked {