Compare commits

...

2 Commits

Author SHA1 Message Date
477af8b063 Update values to fit DB 2025-01-26 00:51:47 +01:00
2998c187d1 Basic protocol done 2025-01-24 20:57:44 +01:00
7 changed files with 79 additions and 63 deletions

View File

@ -14,12 +14,12 @@ void DnaStore::load(DnaManagerData *data)
{ {
if (sys::fileExists(ID_FILE_NAME)) if (sys::fileExists(ID_FILE_NAME))
{ {
sys::loadDataFromFile(ID_FILE_NAME, &data->id, sizeof(uint64_t)); sys::loadDataFromFile(ID_FILE_NAME, &data->id, sizeof(int64_t));
} }
else else
{ {
data->id = time(nullptr); data->id = time(nullptr);
sys::saveDataToFile(ID_FILE_NAME, &data->id, sizeof(uint64_t)); sys::saveDataToFile(ID_FILE_NAME, &data->id, sizeof(int64_t));
} }
if (sys::fileExists(DATA_FILE_NAME)) if (sys::fileExists(DATA_FILE_NAME))

View File

@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <filesystem>
int main() int main()
{ {
@ -20,26 +21,53 @@ int main()
uint64_t ID = 0; uint64_t ID = 0;
idf.read((char *)&ID, sizeof(ID)); idf.read((char *)&ID, sizeof(ID));
idf.close();
printf("id: %ld\n", ID); printf("id: %ld\n", ID);
TcpSocket::sendt(sock, &StartHeader, sizeof(StartHeader)); TcpSocket::sendt(sock, &StartHeader, sizeof(StartHeader));
TcpSocket::sendt(sock, &ID, sizeof(ID)); TcpSocket::sendt(sock, &ID, sizeof(ID));
Message message;
size_t needed_gen = 0; size_t needed_gen = 0;
TcpSocket::recvt(sock, &needed_gen, sizeof(needed_gen));
char filename[20]; char filename[20];
int ret = sprintf(filename, "gen%04ld.bin", needed_gen);
idf.close(); while (true)
idf.open(filename); {
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;
}
std::vector<NetUnit> net; needed_gen = message.data;
net.resize(NUM_PER_GEN);
idf.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit)); int ret = sprintf(filename, "gen%04ld.bin", needed_gen);
TcpSocket::sendt(sock, net.data(), NUM_PER_GEN * sizeof(NetUnit)); filename[ret] = 0;
if (!std::filesystem::exists(filename))
{
message.mess = Mess::RES_NO;
TcpSocket::sendt(sock, &message, sizeof(Message));
break;
}
idf.open(filename, std::ios_base::binary | std::ios_base::in);
std::vector<NetUnit> net;
net.resize(NUM_PER_GEN);
idf.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); TcpSocket::closet(sock);

View File

@ -13,32 +13,13 @@ typedef std::vector<NetList> UserList;
std::map<uint64_t, UserList> data; std::map<uint64_t, UserList> data;
NetList generate_first(uint64_t id)
{
std::vector<NetUnit> ret;
ret.resize(NUM_PER_GEN);
uint128 randSeed = mrand::getState(id);
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
Dna dna;
DNA::newDna(&dna, &randSeed);
ret[i].hash = mrand::computeCRC32(&dna, sizeof(Dna));
ret[i].liked = Liked::tbd;
ret[i].index = i;
}
return ret;
}
// When a new client connected: // When a new client connected:
void call(int sock, sockaddr_in newSocketInfo) void call(int sock, sockaddr_in newSocketInfo)
{ {
std::cout << "new User" << std::endl; std::cout << "new User" << std::endl;
uint64_t conf, id; int64_t conf, id;
Message message;
TcpSocket::recvt(sock, &conf, sizeof(conf)); TcpSocket::recvt(sock, &conf, sizeof(conf));
TcpSocket::recvt(sock, &id, sizeof(id)); TcpSocket::recvt(sock, &id, sizeof(id));
@ -58,38 +39,34 @@ void call(int sock, sockaddr_in newSocketInfo)
auto found = data.find(id); auto found = data.find(id);
if (found == data.end()) if (found == data.end())
{ {
printf("notfound\n");
UserList list; UserList list;
NetList netList = generate_first(id);
list.push_back(netList);
data.insert(std::pair<uint64_t, UserList>(id, list)); data.insert(std::pair<uint64_t, UserList>(id, list));
found = data.find(id);
} }
found = data.find(id);
// repete point while (true)
size_t gen = found->second.size() - 1;
TcpSocket::sendt(sock, &gen, sizeof(size_t));
NetList list;
list.resize(NUM_PER_GEN);
TcpSocket::recvt(sock, list.data(), NUM_PER_GEN * sizeof(NetUnit));
for (size_t i = 0; i < NUM_PER_GEN; i++)
{ {
if (found->second[0][i].hash != list[i].hash) size_t gen = found->second.size();
message.mess = Mess::REQ_SEND_GEN;
message.data = gen;
TcpSocket::sendt(sock, &message, sizeof(Message));
TcpSocket::recvt(sock, &message, sizeof(Message));
if (message.mess == Mess::RES_NO)
{ {
found->second[0][i].liked != list[i].liked; break;
printf("Not same\n");
} }
NetList list;
list.resize(NUM_PER_GEN);
TcpSocket::recvt(sock, list.data(), NUM_PER_GEN * sizeof(NetUnit));
found->second.push_back(list);
} }
// generate the next generation hash form the new info and repete form repete point printf("id: %ld, size: %ld\n", id, found->second.size());
printf("same\n");
printf("id: %ld\n", id);
std::cout << "del USER" << std::endl; std::cout << "del USER" << std::endl;
TcpSocket::closet(sock); TcpSocket::closet(sock);

View File

@ -1,3 +1,16 @@
#include <cinttypes> #include <cinttypes>
constexpr uint64_t StartHeader = 1737720524UL; constexpr int64_t StartHeader = 1737720524UL;
enum Mess
{
RES_OK,
RES_NO,
REQ_SEND_GEN,
};
struct Message
{
Mess mess;
uint32_t data;
};

View File

@ -2,7 +2,7 @@
#include <list> #include <list>
#include <vector> #include <vector>
#define NUM_PER_GEN 10 #define NUM_PER_GEN 16
#define NUM_OF_MUT 1 #define NUM_OF_MUT 1
enum Liked enum Liked
@ -32,7 +32,7 @@ struct DnaManagerData
{ {
int generation; int generation;
uint128 randSeed; uint128 randSeed;
uint64_t id; int64_t id;
int queued; int queued;
int showed; int showed;
std::vector<Dna> vector; std::vector<Dna> vector;

View File

@ -10,7 +10,7 @@ struct uint128
namespace mrand namespace mrand
{ {
uint128 getState(unsigned long long seed); uint128 getState(unsigned long seed);
float getFloat(uint128 *state); float getFloat(uint128 *state);
int getValue(int min, int max, uint128 *state); int getValue(int min, int max, uint128 *state);
unsigned int computeCRC32(void *data, int dataSize); unsigned int computeCRC32(void *data, int dataSize);

View File

@ -34,10 +34,8 @@ uint64_t rprand_splitmix64(uint64_t &rprand_seed)
namespace mrand namespace mrand
{ {
uint128 getState(unsigned long long seed) uint128 getState(unsigned long rprand_seed)
{ {
uint64_t rprand_seed = (uint64_t)seed; // Set SplitMix64 seed for further use
uint128 rprand_state; uint128 rprand_state;
// To generate the Xoshiro128** state, we use SplitMix64 generator first // To generate the Xoshiro128** state, we use SplitMix64 generator first