Compare commits

..

3 Commits

Author SHA1 Message Date
9ae95aef1c almost done 2019-07-01 21:51:23 -03:00
601f6c92bc so far, still working 2019-07-01 21:18:52 -03:00
cf1cfdfd79 so far, working 2019-07-01 20:42:41 -03:00
12 changed files with 61 additions and 76 deletions

View File

@ -11,16 +11,18 @@ 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
) )
@ -34,7 +36,7 @@ if (UNIX)
sqlite3 sqlite3
pthread pthread
dl dl
libsodium sodium
) )
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{nullptr}, id{auto_increment_db() + 1}, taken{false} { : parked_customer{0}, id{auto_increment_db() + 1}, taken{false} {
save_db(); save_db();
} }
Park_spot::Park_spot(Customer* parked_, int id_, bool taken_) Park_spot::Park_spot(int id_, bool taken_, int parked)
: parked{nullptr}, : parked_customer{parked},
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(Customer* parked_, int id_, bool taken_)
// 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 = c_customer; parked_customer = c_customer.id;
taken = true; taken = true;
parked->clock_in(id); c_customer.clock_in(id);
update_db(); update_db();
} else { } else {
taken = false; taken = false;
parked->clock_out(id); c_customer.clock_out(id);
parked = nullptr; parked_customer = 0;
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->id)); statement.insert(49, to_string(parked_customer));
statement.insert(30, "1"); statement.insert(30, "1");
} else { } else {
statement.insert(49, "NULL"); statement.insert(49, "NULL");

View File

@ -25,6 +25,10 @@ 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:
@ -66,33 +70,25 @@ 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() {
SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > ?;"); // -------------- paroking spots
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));
parking_spots.push_back( spots.push_back({id, taken, cid});
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,6 +8,7 @@ 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 = 1.28e+8; // 1.28 e+8 = 128 e6 = 128 mb int memory_limit = 3.2e+7; // 3.2e7 = 32e6 = 32 mb
int cpu_limit = 2; // this is n_threads int cpu_limit = 1; // this is n_threads
int result = crypto_pwhash_str(hashed_password_, int result = crypto_pwhash_str(hashed_password_,
password_, password_,

View File

@ -4,6 +4,7 @@
#include "Park_time.h" #include "Park_time.h"
#include "data.h" #include "data.h"
#include <vector> #include <vector>
using std::vector; using std::vector;
@ -22,7 +23,6 @@ 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,17 +38,13 @@ 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,5 +1,8 @@
#include "Customer.h" #ifndef PARK_SPOT_H
#define PARK_SPOT_H
#pragma once
#include "Customer.h"
/* /*
db representation: db representation:
int id not null int id not null
@ -14,10 +17,10 @@ class Park_spot {
public: public:
int id; int id;
bool taken; bool taken;
Customer* parked; int parked_customer;
Park_spot(); Park_spot();
Park_spot(Customer* parked_, int id_, bool taken_); Park_spot(int id_, bool taken_, int parked);
void clock(Customer* c_customer); void clock(Customer& c_customer);
private: private:
void save_db(); void save_db();
@ -25,5 +28,4 @@ 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,9 +10,12 @@ 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);
void query_all_parking_spots(); // used for initializing the parking spots at start of the program vector<Park_spot> 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 SQLite::Database start_db();
start_db();
static SQLite::Database db = start_db(); static SQLite::Database db = start_db();
} // namespace data } // namespace data

View File

@ -1,8 +1,11 @@
#include "headers/Query.h" #include "headers/Query.h"
#include <array> #include <chrono>
#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
@ -18,9 +21,6 @@ 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,29 +29,14 @@ a wait function where 1 sec represents 1 hour irl.
std::this_thread::sleep_for(seconds{sec}); std::this_thread::sleep_for(seconds{sec});
} }
Customer* get_customer_ptr_for_parkspot(int id);
int main() { int main() {
query_all_parking_spots(); // Customer sagar = query_customer_with_name("stefan udit")[0];
Customer sagar = query_customer_with_id(2);
Customer p0 = query_customer_with_name("Shaquile")[0]; cout << sagar.id << "," << sagar.name << "," << sagar.password;
Customer p1 = query_customer_with_name("Sagar Ramsaransing")[0]; // cout << parking_spots.size();
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.