Compare commits
3 Commits
9a37e8b581
...
609958bc69
Author | SHA1 | Date | |
---|---|---|---|
609958bc69 | |||
2c9e13baaf | |||
9edd3fc156 |
@ -57,6 +57,7 @@ target_link_libraries(app ${CMAKE_BINARY_DIR}/raylib/lib/libraylib.a)
|
||||
add_executable(server
|
||||
server/src/server.cpp
|
||||
server/src/sql.cpp
|
||||
server/src/checker.cpp
|
||||
|
||||
shared/src/TcpSocket.cpp
|
||||
shared/src/values/Dna.cpp
|
||||
@ -65,10 +66,4 @@ add_executable(server
|
||||
)
|
||||
# Add include directories
|
||||
target_include_directories(server PRIVATE server/inc shared/inc ${CMAKE_BINARY_DIR}/sqlite/)
|
||||
target_link_libraries(server PRIVATE pthread dl m ${CMAKE_BINARY_DIR}/sqlite-prefix/libsqlite3.a)
|
||||
|
||||
add_executable(client
|
||||
server/src/client.cpp
|
||||
shared/src/TcpSocket.cpp
|
||||
)
|
||||
target_include_directories(client PRIVATE server/inc shared/inc)
|
||||
target_link_libraries(server PRIVATE pthread dl m ${CMAKE_BINARY_DIR}/sqlite-prefix/libsqlite3.a)
|
3
server/inc/checker.hpp
Normal file
3
server/inc/checker.hpp
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
|
||||
void checker();
|
@ -1,18 +1,42 @@
|
||||
#include <cinttypes>
|
||||
|
||||
|
||||
const char create_table[] = "CREATE TABLE IF NOT EXISTS prim_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, GEN INTEGER, HASH INTEGER, POS INTEGER, LIKED INTEGER);";
|
||||
const char create_table[] = "CREATE TABLE IF NOT EXISTS prim_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, GEN INTEGER, HASH INTEGER, POS INTEGER, LIKED INTEGER, CHECKED INTEGER);";
|
||||
|
||||
const char create_index[] = "CREATE INDEX IF NOT EXISTS idx_usrer_id ON prim_table (USER_ID);";
|
||||
|
||||
const char insert_data[] = "INSERT INTO prim_table (USER_ID, GEN, HASH, POS, LIKED) VALUES(?, ?, ?, ?, ?);";
|
||||
const char insert_data[] = "INSERT INTO prim_table (USER_ID, GEN, HASH, POS, LIKED, CHECKED) VALUES(?, ?, ?, ?, ?, 0);";
|
||||
|
||||
const char max_gen[] = "SELECT MAX(GEN) FROM prim_table WHERE USER_ID = ?;";
|
||||
|
||||
const char get_unchecked[] = "SELECT USER_ID FROM prim_table WHERE CHECKED = 0 GROUP BY USER_ID;";
|
||||
|
||||
const char get_gen[] = "SELECT HASH, POS, LIKED FROM prim_table WHERE USER_ID = ? AND GEN = ? ORDER BY POS ASC;";
|
||||
|
||||
const char remove_gen[] = "DELETE FROM prim_table WHERE USER_ID = ? AND GEN >= ?;";
|
||||
|
||||
const char set_checked[] = "UPDATE prim_table SET CHECKED = 1 WHERE USER_ID = ? AND GEN = ?;";
|
||||
|
||||
constexpr char DB_NAME[] = "data.db";
|
||||
|
||||
struct sqlite3;
|
||||
struct sqlite3_stmt;
|
||||
|
||||
#define SQL_DONE 101
|
||||
#define SQL_NULL 5
|
||||
|
||||
namespace sql
|
||||
{
|
||||
void init();
|
||||
void shutdown();
|
||||
|
||||
int open(const char *filename, sqlite3 **ppDb);
|
||||
int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail);
|
||||
int bind_int64(sqlite3_stmt *pStmt, int column, int64_t value);
|
||||
int step(sqlite3_stmt *pStmt);
|
||||
int column_count(sqlite3_stmt *pStmt);
|
||||
int column_type(sqlite3_stmt *pStmt, int column);
|
||||
int64_t column_int64(sqlite3_stmt *pStmt, int column);
|
||||
int finalize(sqlite3_stmt *pStmt);
|
||||
int reset(sqlite3_stmt *pStmt);
|
||||
int close(sqlite3 *db);
|
||||
const char *errmsg(sqlite3 *db);
|
||||
}
|
||||
|
112
server/src/checker.cpp
Normal file
112
server/src/checker.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
|
||||
#include "sql.hpp"
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include "values/DnaManager.hpp"
|
||||
|
||||
void checker()
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
sqlite3 *db;
|
||||
int rc = sql::open(DB_NAME, &db);
|
||||
if (rc)
|
||||
{
|
||||
fprintf(stderr, "Can't open database: %s\n", sql::errmsg(db));
|
||||
}
|
||||
|
||||
sqlite3_stmt *get_unchecked_stmt;
|
||||
sql::prepare_v2(db, get_unchecked, -1, &get_unchecked_stmt, NULL);
|
||||
|
||||
sqlite3_stmt *get_gen_stmt;
|
||||
sql::prepare_v2(db, get_gen, -1, &get_gen_stmt, NULL);
|
||||
|
||||
sqlite3_stmt *remove_gen_stmt;
|
||||
sql::prepare_v2(db, remove_gen, -1, &remove_gen_stmt, NULL);
|
||||
|
||||
sqlite3_stmt *set_checked_stmt;
|
||||
sql::prepare_v2(db, set_checked, -1, &set_checked_stmt, NULL);
|
||||
|
||||
while (true)
|
||||
{
|
||||
int64_t user_id = 0;
|
||||
if (sql::step(get_unchecked_stmt) != SQL_DONE)
|
||||
{
|
||||
int type = sql::column_type(get_unchecked_stmt, 0);
|
||||
if (type != SQL_NULL)
|
||||
{
|
||||
user_id = sql::column_int64(get_unchecked_stmt, 0);
|
||||
}
|
||||
}
|
||||
sql::reset(get_unchecked_stmt);
|
||||
if (user_id == 0)
|
||||
{
|
||||
std::this_thread::sleep_for(60s);
|
||||
continue;
|
||||
}
|
||||
|
||||
DnaManagerData data;
|
||||
data.id = user_id;
|
||||
data.randSeed = mrand::getState(data.id);
|
||||
data.queued = 0;
|
||||
data.showed = 0;
|
||||
data.generation = 0;
|
||||
data.vector.resize(NUM_PER_GEN);
|
||||
|
||||
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
||||
{
|
||||
DNA::newDna(&data.vector[i], &data.randSeed);
|
||||
}
|
||||
bool found_err = false;
|
||||
while (found_err != true)
|
||||
{
|
||||
sql::bind_int64(get_gen_stmt, 1, data.id);
|
||||
sql::bind_int64(get_gen_stmt, 2, data.generation);
|
||||
|
||||
bool new_gen = false;
|
||||
while (sql::step(get_gen_stmt) != SQL_DONE)
|
||||
{
|
||||
int64_t hash = sql::column_int64(get_gen_stmt, 0);
|
||||
int64_t pos = sql::column_int64(get_gen_stmt, 1);
|
||||
int64_t liked = sql::column_int64(get_gen_stmt, 2);
|
||||
UiUnit unit = DnaManager::next(&data);
|
||||
int64_t cal_hash = mrand::computeCRC32(unit.dna, sizeof(Dna));
|
||||
if ((unit.index != pos) || (hash != cal_hash))
|
||||
{
|
||||
found_err = true;
|
||||
sql::bind_int64(remove_gen_stmt, 1, data.id);
|
||||
sql::bind_int64(remove_gen_stmt, 2, data.generation);
|
||||
sql::step(remove_gen_stmt);
|
||||
sql::reset(remove_gen_stmt);
|
||||
break;
|
||||
}
|
||||
unit.liked = (Liked)liked;
|
||||
new_gen = DnaManager::like(unit, &data);
|
||||
}
|
||||
|
||||
if (!found_err)
|
||||
{
|
||||
sql::bind_int64(set_checked_stmt, 1, data.id);
|
||||
sql::bind_int64(set_checked_stmt, 2, data.generation);
|
||||
sql::step(set_checked_stmt);
|
||||
sql::reset(set_checked_stmt);
|
||||
}
|
||||
if (!new_gen)
|
||||
{
|
||||
found_err = true;
|
||||
sql::bind_int64(remove_gen_stmt, 1, data.id);
|
||||
sql::bind_int64(remove_gen_stmt, 2, data.generation);
|
||||
sql::step(remove_gen_stmt);
|
||||
sql::reset(remove_gen_stmt);
|
||||
}
|
||||
else
|
||||
{
|
||||
DnaManager::newGen(&data);
|
||||
}
|
||||
|
||||
sql::reset(get_gen_stmt);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
#include "TcpSocket.hpp"
|
||||
#include "NetConst.hpp"
|
||||
#include "values/DnaManager.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
int main()
|
||||
{
|
||||
int sock = TcpSocket::connectt("petrovv.com", 8888);
|
||||
|
||||
if (sock < 0)
|
||||
{
|
||||
printf("Error %d", sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::ifstream idf("ID.bin", 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;
|
||||
|
||||
size_t needed_gen = 0;
|
||||
char filename[20];
|
||||
|
||||
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(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;
|
||||
}
|
||||
|
||||
std::ifstream sfile(filename, 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);
|
||||
|
||||
return 0;
|
||||
}
|
@ -2,11 +2,11 @@
|
||||
#include "NetConst.hpp"
|
||||
#include "sql.hpp"
|
||||
#include "values/DnaManager.hpp"
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include "checker.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <thread>
|
||||
|
||||
// use pthread rw_lock to lock db so you can safy clone .db file
|
||||
|
||||
@ -40,30 +40,30 @@ void call(int sock, sockaddr_in newSocketInfo)
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
int rc = sqlite3_open(DB_NAME, &db);
|
||||
int rc = sql::open(DB_NAME, &db);
|
||||
if (rc)
|
||||
{
|
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||
fprintf(stderr, "Can't open database: %s\n", sql::errmsg(db));
|
||||
ok = false;
|
||||
}
|
||||
|
||||
sqlite3_prepare_v2(db, max_gen, -1, &stmt, NULL);
|
||||
sqlite3_bind_int64(stmt, 1, id);
|
||||
sql::prepare_v2(db, max_gen, -1, &stmt, NULL);
|
||||
sql::bind_int64(stmt, 1, id);
|
||||
int64_t gen = 0;
|
||||
while (sqlite3_step(stmt) != SQLITE_DONE)
|
||||
while (sql::step(stmt) != SQL_DONE)
|
||||
{
|
||||
int num_cols = sqlite3_column_count(stmt);
|
||||
int num_cols = sql::column_count(stmt);
|
||||
|
||||
int type = sqlite3_column_type(stmt, 0);
|
||||
int type = sql::column_type(stmt, 0);
|
||||
|
||||
if (type == SQLITE_NULL)
|
||||
if (type == SQL_NULL)
|
||||
break;
|
||||
gen = sqlite3_column_int64(stmt, 0);
|
||||
gen = sql::column_int64(stmt, 0);
|
||||
gen++;
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
sql::finalize(stmt);
|
||||
|
||||
sqlite3_prepare_v2(db, insert_data, -1, &stmt, NULL);
|
||||
sql::prepare_v2(db, insert_data, -1, &stmt, NULL);
|
||||
|
||||
while (ok)
|
||||
{
|
||||
@ -85,21 +85,21 @@ void call(int sock, sockaddr_in newSocketInfo)
|
||||
|
||||
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);
|
||||
sqlite3_bind_int64(stmt, 4, list[i].index);
|
||||
sqlite3_bind_int64(stmt, 5, list[i].liked);
|
||||
sqlite3_step(stmt);
|
||||
sqlite3_reset(stmt);
|
||||
sql::bind_int64(stmt, 1, id);
|
||||
sql::bind_int64(stmt, 2, gen);
|
||||
sql::bind_int64(stmt, 3, list[i].hash);
|
||||
sql::bind_int64(stmt, 4, list[i].index);
|
||||
sql::bind_int64(stmt, 5, list[i].liked);
|
||||
sql::step(stmt);
|
||||
sql::reset(stmt);
|
||||
}
|
||||
|
||||
gen++;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
sql::finalize(stmt);
|
||||
|
||||
sqlite3_close(db);
|
||||
sql::close(db);
|
||||
printf("del\n");
|
||||
TcpSocket::closet(sock);
|
||||
}
|
||||
@ -107,6 +107,7 @@ void call(int sock, sockaddr_in newSocketInfo)
|
||||
int main()
|
||||
{
|
||||
sql::init();
|
||||
std::thread t(checker);
|
||||
// Bind the server to a port.
|
||||
int err = TcpSocket::listent("0.0.0.0", 8888, call);
|
||||
if (err < 0)
|
||||
|
@ -57,4 +57,52 @@ void sql::init()
|
||||
void sql::shutdown()
|
||||
{
|
||||
sqlite3_shutdown();
|
||||
}
|
||||
|
||||
namespace sql
|
||||
{
|
||||
int open(const char *filename, sqlite3 **ppDb)
|
||||
{
|
||||
return sqlite3_open(filename, ppDb);
|
||||
}
|
||||
int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail)
|
||||
{
|
||||
return sqlite3_prepare_v2(db, zSql, nByte, pStmt, pzTail);
|
||||
}
|
||||
int bind_int64(sqlite3_stmt *pStmt, int column, int64_t value)
|
||||
{
|
||||
return sqlite3_bind_int64(pStmt, column, value);
|
||||
}
|
||||
int step(sqlite3_stmt *pStmt)
|
||||
{
|
||||
return sqlite3_step(pStmt);
|
||||
}
|
||||
int column_count(sqlite3_stmt *pStmt)
|
||||
{
|
||||
return sqlite3_column_count(pStmt);
|
||||
}
|
||||
int column_type(sqlite3_stmt *pStmt, int column)
|
||||
{
|
||||
return sqlite3_column_type(pStmt, column);
|
||||
}
|
||||
int64_t column_int64(sqlite3_stmt *pStmt, int column)
|
||||
{
|
||||
return sqlite3_column_int64(pStmt, column);
|
||||
}
|
||||
int finalize(sqlite3_stmt *pStmt)
|
||||
{
|
||||
return sqlite3_finalize(pStmt);
|
||||
}
|
||||
int reset(sqlite3_stmt *pStmt)
|
||||
{
|
||||
return sqlite3_reset(pStmt);
|
||||
}
|
||||
int close(sqlite3 *db)
|
||||
{
|
||||
return sqlite3_close(db);
|
||||
}
|
||||
const char *errmsg(sqlite3 *db)
|
||||
{
|
||||
return sqlite3_errmsg(db);
|
||||
}
|
||||
}
|
16
shared/inc/values/Similarity.hpp
Normal file
16
shared/inc/values/Similarity.hpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include "Dna.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace Similarity
|
||||
{
|
||||
// float euclidean_distance(Dna *d1, Dna *d2); direct distance betwen vector. wont give 0 and 1
|
||||
// float dot_product(Dna *d1, Dna *d2); doent return betwen 0 to 1
|
||||
float cosine_similarity(Dna *d1, Dna *d2);
|
||||
float hamming_distance(Dna *d1, Dna *d2);
|
||||
float jaccard_index(Dna *d1, Dna *d2);
|
||||
float levenshtein_distance(Dna *d1, Dna *d2);
|
||||
// float needleman_wunsch(Dna *d1, Dna *d2); used for bioinformatics and aligment. Dont need its aligned alredy
|
||||
|
||||
typedef float(simil_func)(Dna *d1, Dna *d2);
|
||||
float calc_similarity(std::vector<Dna> vec, simil_func f);
|
||||
}
|
114
shared/src/values/Similarity.cpp
Normal file
114
shared/src/values/Similarity.cpp
Normal file
@ -0,0 +1,114 @@
|
||||
#include "values/Similarity.hpp"
|
||||
#include <cmath>
|
||||
|
||||
#define MATCH 1
|
||||
#define MISMATCH -1
|
||||
#define GAP -2
|
||||
|
||||
namespace Similarity
|
||||
{
|
||||
|
||||
float cosine_similarity(Dna *d1, Dna *d2)
|
||||
{
|
||||
uint8_t *d1a = (uint8_t *)d1;
|
||||
uint8_t *d2a = (uint8_t *)d2;
|
||||
|
||||
float mag1 = 0.0f;
|
||||
float mag2 = 0.0f;
|
||||
float dot_prod = 0.0f;
|
||||
for (size_t i = 0; i < sizeof(Dna); i++)
|
||||
{
|
||||
dot_prod += d1a[i] * d2a[i];
|
||||
mag1 += d1a[i] * d1a[i];
|
||||
mag2 += d2a[i] * d2a[i];
|
||||
}
|
||||
mag1 = sqrt(mag1);
|
||||
mag2 = sqrt(mag2);
|
||||
|
||||
return dot_prod / (mag1 * mag2);
|
||||
}
|
||||
|
||||
float hamming_distance(Dna *d1, Dna *d2)
|
||||
{
|
||||
uint8_t *d1a = (uint8_t *)d1;
|
||||
uint8_t *d2a = (uint8_t *)d2;
|
||||
float distance = 0;
|
||||
for (size_t i = 0; i < sizeof(Dna); i++)
|
||||
{
|
||||
if (d1a[i] != d2a[i])
|
||||
{
|
||||
distance++;
|
||||
}
|
||||
}
|
||||
return 1 - (distance / sizeof(Dna));
|
||||
}
|
||||
|
||||
float jaccard_index(Dna *d1, Dna *d2)
|
||||
{
|
||||
uint8_t *d1a = (uint8_t *)d1;
|
||||
uint8_t *d2a = (uint8_t *)d2;
|
||||
size_t intersection = 0;
|
||||
size_t union_size = sizeof(Dna) + sizeof(Dna);
|
||||
|
||||
for (size_t i = 0; i < sizeof(Dna); i++)
|
||||
{
|
||||
for (size_t j = 0; j < sizeof(Dna); j++)
|
||||
{
|
||||
if (d1a[i] == d2a[j])
|
||||
{
|
||||
intersection++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
union_size -= intersection;
|
||||
return (float)intersection / union_size;
|
||||
}
|
||||
|
||||
float levenshtein_distance(Dna *d1, Dna *d2)
|
||||
{
|
||||
auto min = [](uint8_t a, uint8_t b, uint8_t c) -> uint8_t
|
||||
{
|
||||
return (a < b ? (a < c ? a : c) : (b < c ? b : c));
|
||||
};
|
||||
|
||||
uint8_t *d1a = (uint8_t *)d1;
|
||||
uint8_t *d2a = (uint8_t *)d2;
|
||||
float matrix[sizeof(Dna) + 1][sizeof(Dna) + 1];
|
||||
for (size_t i = 0; i <= sizeof(Dna); i++)
|
||||
{
|
||||
matrix[i][0] = i;
|
||||
}
|
||||
for (size_t j = 0; j <= sizeof(Dna); j++)
|
||||
{
|
||||
matrix[0][j] = j;
|
||||
}
|
||||
for (size_t i = 1; i <= sizeof(Dna); i++)
|
||||
{
|
||||
for (size_t j = 1; j <= sizeof(Dna); j++)
|
||||
{
|
||||
uint8_t cost = (d1a[i - 1] == d2a[j - 1]) ? 0 : 1;
|
||||
matrix[i][j] = min(matrix[i - 1][j] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j - 1] + cost);
|
||||
}
|
||||
}
|
||||
float ld = matrix[sizeof(Dna)][sizeof(Dna)];
|
||||
return 1 - (ld / sizeof(Dna));
|
||||
}
|
||||
|
||||
float calc_similarity(std::vector<Dna> vec, simil_func f)
|
||||
{
|
||||
size_t num_pairs = (vec.size() * (vec.size() - 1)) / 2;
|
||||
|
||||
float total_similarity = 0.0;
|
||||
for (size_t i = 0; i < vec.size(); i++)
|
||||
{
|
||||
for (size_t j = i + 1; j < vec.size(); j++)
|
||||
{
|
||||
total_similarity += f(&vec[i], &vec[j]);
|
||||
}
|
||||
}
|
||||
float average_similarity = total_similarity / num_pairs;
|
||||
return average_similarity;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user