add client to app

This commit is contained in:
Nikola Petrov 2025-01-26 22:51:32 +01:00
parent cb743d15d9
commit f4c5b1ad9a
6 changed files with 109 additions and 14 deletions

View File

@ -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)

View File

@ -6,4 +6,5 @@ namespace DnaStore
void saveData(DnaManagerData *data);
void saveVec(DnaManagerData *data);
void saveGen(DnaManagerData *data);
void sync();
} // namespace DnaStore

View File

@ -1,14 +1,21 @@
#include <cstdio>
#include <ctime>
#include <string>
#include <fstream>
#include <filesystem>
#include <thread>
#include "sys.hpp"
#include "DnaStore.hpp"
#include "TcpSocket.hpp"
#include "NetConst.hpp"
#include <raylib.h>
#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<NetUnit> 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);
}

View File

@ -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<NetUnit> net;

View File

@ -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);
}

View File

@ -3,7 +3,7 @@
#include <vector>
#define NUM_PER_GEN 16
#define NUM_OF_MUT 1
#define NUM_OF_MUT 3
enum Liked
{