Add sqlite and start work on comunication protocol

This commit is contained in:
2025-01-24 15:57:23 +01:00
parent a226d0f156
commit 4da7788a15
9 changed files with 266 additions and 31 deletions

8
server/inc/sql.hpp Normal file
View File

@@ -0,0 +1,8 @@
namespace sql
{
void init();
void shutdown();
}

View File

@@ -1,9 +1,9 @@
#include "TcpSocket.hpp"
#include "NetConst.hpp"
#include "values/DnaManager.hpp"
#include <iostream>
using namespace std;
char mes[] = "Hello Server!";
#include <fstream>
#include <vector>
int main()
{
@@ -15,24 +15,31 @@ int main()
return 0;
}
char tempBuffer[AS_DEFAULT_BUFFER_SIZE + 1];
ssize_t messageLength;
std::ifstream idf("ID.bin", std::ios_base::binary);
// You should do an input loop, so the program won't terminate immediately
string input;
getline(cin, input);
while (input != "exit")
{
TcpSocket::sendt(sock, input.data(), input.size());
messageLength = TcpSocket::recvt(sock, tempBuffer, AS_DEFAULT_BUFFER_SIZE);
if (messageLength <= 0)
{
break;
}
tempBuffer[messageLength] = 0;
printf("%s\n", tempBuffer);
getline(cin, input);
}
uint64_t ID = 0;
idf.read((char *)&ID, sizeof(ID));
printf("id: %ld\n", ID);
TcpSocket::sendt(sock, &StartHeader, sizeof(StartHeader));
TcpSocket::sendt(sock, &ID, sizeof(ID));
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);
std::vector<NetUnit> net;
net.resize(NUM_PER_GEN);
idf.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit));
TcpSocket::sendt(sock, net.data(), NUM_PER_GEN * sizeof(NetUnit));
TcpSocket::closet(sock);

View File

@@ -1,28 +1,103 @@
#include "TcpSocket.hpp"
#include "NetConst.hpp"
#include "sql.hpp"
#include "values/DnaManager.hpp"
#include <iostream>
using namespace std;
#include <map>
// use pthread rw_lock to lock db so you can safy clone .db file
typedef std::vector<NetUnit> NetList;
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;
char tempBuffer[AS_DEFAULT_BUFFER_SIZE + 1];
ssize_t messageLength;
while ((messageLength = TcpSocket::recvt(sock, tempBuffer, AS_DEFAULT_BUFFER_SIZE)) > 0)
uint64_t conf, id;
TcpSocket::recvt(sock, &conf, sizeof(conf));
TcpSocket::recvt(sock, &id, sizeof(id));
if (conf != StartHeader)
{
tempBuffer[messageLength] = '\0';
TcpSocket::sendt(sock, tempBuffer, messageLength);
TcpSocket::closet(sock);
return;
}
if (id == 0)
{
printf("ID ERROR\n");
return;
}
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);
// 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++)
{
if (found->second[0][i].hash != list[i].hash)
{
found->second[0][i].liked != list[i].liked;
printf("Not same\n");
}
}
// generate the next generation hash form the new info and repete form repete point
printf("same\n");
printf("id: %ld\n", id);
std::cout << "del USER" << std::endl;
TcpSocket::closet(sock);
}
int main()
{
sql::init();
// Bind the server to a port.
int err = TcpSocket::listent("0.0.0.0", 8888, call);
if (err < 0)

77
server/src/sql.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include "sql.hpp"
#include <cstdio>
#include <cstdlib>
#include <sqlite3.h>
const char create_table[] = "CREATE TABLE IF NOT EXISTS prim_table ( ID INTEGER PRIMARY KEY, GEN INTEGER, HASH INTEGER, POS INTEGER, LIKED INTEGER);";
const char create_index[] = "CREATE INDEX IF NOT EXISTS idx_id ON prim_table (ID);";
constexpr char DB_NAME[] = "data.db";
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
for (i = 0; i < argc; i++)
{
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
void sql::init()
{
sqlite3_initialize();
sqlite3 *db;
char *zErrMsg = 0;
int rc;
/* Open database */
rc = sqlite3_open(DB_NAME, &db);
if (rc)
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return;
}
else
{
fprintf(stdout, "Opened database successfully\n");
}
/* Execute SQL statement */
rc = sqlite3_exec(db, create_table, callback, 0, &zErrMsg);
if (rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Table created successfully\n");
}
/* Execute SQL statement */
rc = sqlite3_exec(db, create_index, callback, 0, &zErrMsg);
if (rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Index created successfully\n");
}
sqlite3_close(db);
}
void sql::shutdown()
{
sqlite3_shutdown();
}