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 <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; uint64_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 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 <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