Basic protocol done

This commit is contained in:
Nikola Petrov 2025-01-24 20:57:44 +01:00
parent 4da7788a15
commit 2998c187d1
4 changed files with 73 additions and 55 deletions

View File

@ -4,6 +4,7 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <filesystem>
int main()
{
@ -20,26 +21,53 @@ int main()
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;
size_t needed_gen = 0;
TcpSocket::recvt(sock, &needed_gen, sizeof(needed_gen));
char filename[20];
int ret = sprintf(filename, "gen%04ld.bin", needed_gen);
idf.close();
idf.open(filename);
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;
}
std::vector<NetUnit> net;
net.resize(NUM_PER_GEN);
needed_gen = message.data;
idf.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit));
TcpSocket::sendt(sock, net.data(), NUM_PER_GEN * sizeof(NetUnit));
int ret = sprintf(filename, "gen%04ld.bin", needed_gen);
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);

View File

@ -13,32 +13,13 @@ typedef std::vector<NetList> UserList;
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:
void call(int sock, sockaddr_in newSocketInfo)
{
std::cout << "new User" << std::endl;
uint64_t conf, id;
Message message;
TcpSocket::recvt(sock, &conf, sizeof(conf));
TcpSocket::recvt(sock, &id, sizeof(id));
@ -58,38 +39,34 @@ void call(int sock, sockaddr_in newSocketInfo)
auto found = data.find(id);
if (found == data.end())
{
printf("notfound\n");
UserList list;
NetList netList = generate_first(id);
list.push_back(netList);
data.insert(std::pair<uint64_t, UserList>(id, list));
found = data.find(id);
}
found = data.find(id);
// repete point
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++)
while (true)
{
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;
printf("Not same\n");
break;
}
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("same\n");
printf("id: %ld\n", id);
printf("id: %ld, size: %ld\n", id, found->second.size());
std::cout << "del USER" << std::endl;
TcpSocket::closet(sock);

View File

@ -1,3 +1,16 @@
#include <cinttypes>
constexpr uint64_t StartHeader = 1737720524UL;
constexpr uint64_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 <vector>
#define NUM_PER_GEN 10
#define NUM_PER_GEN 16
#define NUM_OF_MUT 1
enum Liked