Compare commits

..

No commits in common. "9ae95aef1cc3666706d9b1ed4e30d86916b9ede6" and "5fa84e866ff22fb67c1ef4f0767974bfd6f4a497" have entirely different histories.

12 changed files with 77 additions and 62 deletions

View File

@ -11,18 +11,16 @@ include_directories(
add_executable(park add_executable(park
main.cpp main.cpp
data.cpp data.cpp
headers/data.h headers/data.h
encrypt.cpp
headers/encrypt.h
Customer.cpp Customer.cpp
headers/Customer.h headers/Customer.h
Park_spot.cpp Park_spot.cpp
headers/Park_spot.h headers/Park_spot.h
Park_time.cpp Park_time.cpp
headers/Park_time.h headers/Park_time.h
encrypt.cpp
headers/encrypt.h
Query.cpp Query.cpp
headers/Query.h headers/Query.h
) )
@ -36,7 +34,7 @@ if (UNIX)
sqlite3 sqlite3
pthread pthread
dl dl
sodium libsodium
) )
elseif (MSYS OR MINGW) elseif (MSYS OR MINGW)
target_link_libraries(park target_link_libraries(park

View File

@ -2,7 +2,7 @@
// constructors // constructors
Customer::Customer(string name_, string password_, Verhicle_type verhicle_) Customer::Customer(string name_, string password_, Verhicle_type verhicle_)
: name{name_}, verhicle{verhicle_}, password{hash_password(password_)} { : name{name_}, verhicle{verhicle_}, password{hash_password(password)} {
id = auto_increment_db() + 1; id = auto_increment_db() + 1;
save_db(); save_db();
} }

View File

@ -3,12 +3,12 @@
// constructors // constructors
Park_spot::Park_spot() Park_spot::Park_spot()
: parked_customer{0}, id{auto_increment_db() + 1}, taken{false} { : parked{nullptr}, id{auto_increment_db() + 1}, taken{false} {
save_db(); save_db();
} }
Park_spot::Park_spot(int id_, bool taken_, int parked) Park_spot::Park_spot(Customer* parked_, int id_, bool taken_)
: parked_customer{parked}, : parked{nullptr},
id{id_}, id{id_},
taken{taken_} // TODO: think about how init parked? taken{taken_} // TODO: think about how init parked?
{} {}
@ -16,16 +16,16 @@ Park_spot::Park_spot(int id_, bool taken_, int parked)
// clock in en out, calls de juist(in/out) van de customer aan de hand van // clock in en out, calls de juist(in/out) van de customer aan de hand van
// internal state van taken // internal state van taken
void Park_spot::clock(Customer& c_customer) { void Park_spot::clock(Customer* c_customer) {
if (!taken) { if (!taken) {
parked_customer = c_customer.id; parked = c_customer;
taken = true; taken = true;
c_customer.clock_in(id); parked->clock_in(id);
update_db(); update_db();
} else { } else {
taken = false; taken = false;
c_customer.clock_out(id); parked->clock_out(id);
parked_customer = 0; parked = nullptr;
update_db(); update_db();
} }
} }
@ -37,7 +37,7 @@ void Park_spot::update_db() {
"UPDATE Park_spot SET taken = '', customer_id = '' where id = '';"; "UPDATE Park_spot SET taken = '', customer_id = '' where id = '';";
statement.insert(63, to_string(id)); statement.insert(63, to_string(id));
if (taken) { if (taken) {
statement.insert(49, to_string(parked_customer)); statement.insert(49, to_string(parked->id));
statement.insert(30, "1"); statement.insert(30, "1");
} else { } else {
statement.insert(49, "NULL"); statement.insert(49, "NULL");

View File

@ -25,10 +25,6 @@ vector<Park_time> query_parktimes_for_customer(int cid) {
return park_times; return park_times;
} }
//--------------------------------------------- customers
vector<Customer> query_customer_with_name(string name) { vector<Customer> query_customer_with_name(string name) {
/* /*
We use this instead of plain customers because: We use this instead of plain customers because:
@ -70,25 +66,33 @@ Customer query_customer_with_id(int id) {
Customer result{ Customer result{
id, name, password, Verhicle_type(verhicle), park_instances}; id, name, password, Verhicle_type(verhicle), park_instances};
// DEBUG // DEBUG
// cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n"; cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n";
return result; return result;
} }
} }
void query_all_parking_spots() {
// -------------- paroking spots SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > ?;");
query.bind(1, 0);
vector<Park_spot> query_all_parking_spots() {
vector<Park_spot> spots;
SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > 2;");
// query.bind(1, 2);
while (query.executeStep()) { while (query.executeStep()) {
int id = query.getColumn(0); int id = query.getColumn(0);
int taken = query.getColumn(1); int taken = query.getColumn(1);
int cid = query.getColumn(2); int cid = query.getColumn(2);
// park_customers.push_back(query_customer_with_id(cid)); park_customers.push_back(query_customer_with_id(cid));
spots.push_back({id, taken, cid}); parking_spots.push_back(
Park_spot{get_customer_ptr_for_parkspot(cid), id, bool(taken)});
} }
return spots;
} }
Customer* get_customer_ptr_for_parkspot(int id) {
if (!id) {
return nullptr;
}
for (int i = 0; i < park_customers.size(); i++) {
if (park_customers[i].id == id) {
return &park_customers[i];
}
}
return nullptr;
}

View File

@ -8,7 +8,6 @@ SQLite::Database start_db() {
while (sodium_init() < 0) { while (sodium_init() < 0) {
std::cout << "SODIUM NOT WORKING"; std::cout << "SODIUM NOT WORKING";
} }
db.exec( db.exec(
"create table if not exists Customer (id integer primary key, name " "create table if not exists Customer (id integer primary key, name "
"text, password text, verhicle int)"); "text, password text, verhicle int)");

View File

@ -9,8 +9,8 @@ string hash_password(string password) {
*/ */
const char* password_ = password.c_str(); const char* password_ = password.c_str();
char hashed_password_[crypto_pwhash_STRBYTES]; char hashed_password_[crypto_pwhash_STRBYTES];
int memory_limit = 3.2e+7; // 3.2e7 = 32e6 = 32 mb int memory_limit = 1.28e+8; // 1.28 e+8 = 128 e6 = 128 mb
int cpu_limit = 1; // this is n_threads int cpu_limit = 2; // this is n_threads
int result = crypto_pwhash_str(hashed_password_, int result = crypto_pwhash_str(hashed_password_,
password_, password_,

View File

@ -4,7 +4,6 @@
#include "Park_time.h" #include "Park_time.h"
#include "data.h" #include "data.h"
#include <vector> #include <vector>
using std::vector; using std::vector;
@ -23,6 +22,7 @@ park_time object. Voegt het toe aan een vector.
class Customer { class Customer {
public: public:
int id; int id;
string name; string name;
string password; string password;
@ -38,13 +38,17 @@ class Customer {
void delete_db(); void delete_db();
void gen_monthly(); // remove, make it a function in data void gen_monthly(); // remove, make it a function in data
Verhicle_type verhicle;
private: private:
Verhicle_type verhicle;
vector<Park_time> park_instances; vector<Park_time> park_instances;
void save_db(); void save_db();
int auto_increment_db(); int auto_increment_db();
}; };
static vector<Customer> park_customers; // save the customers that are parked in here
// parking_spot uses pointers, so it's better to save the parked customers here
// where we know they'll be destroyed at the end of this scope, instead of too early
// and end up with dangling pointers
#endif // CUSTOMER_H #endif // CUSTOMER_H

View File

@ -1,8 +1,5 @@
#ifndef PARK_SPOT_H
#define PARK_SPOT_H
#pragma once
#include "Customer.h" #include "Customer.h"
/* /*
db representation: db representation:
int id not null int id not null
@ -17,10 +14,10 @@ class Park_spot {
public: public:
int id; int id;
bool taken; bool taken;
int parked_customer; Customer* parked;
Park_spot(); Park_spot();
Park_spot(int id_, bool taken_, int parked); Park_spot(Customer* parked_, int id_, bool taken_);
void clock(Customer& c_customer); void clock(Customer* c_customer);
private: private:
void save_db(); void save_db();
@ -28,4 +25,5 @@ class Park_spot {
void delete_db(); void delete_db();
int auto_increment_db(); int auto_increment_db();
}; };
#endif // CUSTOMER_H
static vector<Park_spot> parking_spots; // to save the parking spots in memory

View File

@ -10,12 +10,9 @@ vector<Park_time> query_parktimes_for_customer(int cid);
vector<Customer> query_customer_with_name(string name); vector<Customer> query_customer_with_name(string name);
Customer query_customer_with_id(int id); Customer query_customer_with_id(int id);
Customer* get_customer_ptr_for_parkspot(int id);
vector<Park_spot> query_all_parking_spots(); // used for initializing the parking spots at start of the program void query_all_parking_spots(); // used for initializing the parking spots at start of the program
static vector<Park_spot> parking_spots = query_all_parking_spots(); // to save the parking spots in memory
static vector<Customer> park_customers;
// save the customers that are parked in here
#endif // CUSTOMER_H #endif // CUSTOMER_H

View File

@ -5,8 +5,8 @@
#include "encrypt.h" #include "encrypt.h"
namespace data { namespace data {
SQLite::Database start_db(); SQLite::Database
start_db();
static SQLite::Database db = start_db(); static SQLite::Database db = start_db();
} // namespace data } // namespace data

View File

@ -1,11 +1,8 @@
#include "headers/Query.h" #include "headers/Query.h"
#include <chrono> #include <array>
#include <thread> #include <thread>
using namespace std::chrono;
/* /*
Code strucure like this: Code strucure like this:
class declarations zijn in /headers/class_naam.h, en definitions van de member class declarations zijn in /headers/class_naam.h, en definitions van de member
@ -21,6 +18,9 @@ record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz.
De client clockt in en uit bij een spot. De client clockt in en uit bij een spot.
*/ */
void Wait(int sec) void Wait(int sec)
/* /*
a wait function where 1 sec represents 1 hour irl. a wait function where 1 sec represents 1 hour irl.
@ -29,14 +29,29 @@ a wait function where 1 sec represents 1 hour irl.
std::this_thread::sleep_for(seconds{sec}); std::this_thread::sleep_for(seconds{sec});
} }
int main() { Customer* get_customer_ptr_for_parkspot(int id);
// Customer sagar = query_customer_with_name("stefan udit")[0];
Customer sagar = query_customer_with_id(2); int main() {
cout << sagar.id << "," << sagar.name << "," << sagar.password; query_all_parking_spots();
// cout << parking_spots.size();
Customer p0 = query_customer_with_name("Shaquile")[0];
Customer p1 = query_customer_with_name("Sagar Ramsaransing")[0];
Customer p2 = query_customer_with_name("Joshua karto")[0];
Customer p3 = query_customer_with_name("Stefan udit")[0];
parking_spots[2].clock(&p1);
Wait(2);
parking_spots[2].clock(&p1);
Wait(1);
parking_spots[0].clock(&p2);
Wait(1);
parking_spots[1].clock(&p3);
Wait(1);
parking_spots[0].clock(&p2);
parking_spots[1].clock(&p3);
Wait(1);
parking_spots[1].clock(&p3);
// for (auto i : parking_spots){
// cout << i.id << "," << i.parked_customer;
// }
} }

BIN
test.db3

Binary file not shown.