diff --git a/.vscode/settings.json b/.vscode/settings.json index 11e5997..d2d6f45 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -45,6 +45,7 @@ "streambuf": "cpp", "cinttypes": "cpp", "typeinfo": "cpp", - "thread": "cpp" + "thread": "cpp", + "chrono": "cpp" } } \ No newline at end of file diff --git a/lib/tcp_socket.hpp b/lib/tcp_socket.hpp index d1819f5..9f26fe5 100644 --- a/lib/tcp_socket.hpp +++ b/lib/tcp_socket.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -102,4 +103,29 @@ class client { string pull (size_t byte_limit = 1024); }; + +class queue { // FIFO + public: + mutex io; + uint limit; + string address; + ushort port; + uint timeout; + server *srv = NULL; + SSL_CTX* securefds = NULL; + + deque clients; + thread controller; + + queue (const uint _limit, const string _address, const ushort _port, const uint _timeout = 100, SSL_CTX* _securefds = NULL); + queue (const uint _limit, server *_srv, const uint _timeout = 100, SSL_CTX* _securefds = NULL); + + ~queue(); + + void enqueue(); + client dequeue(); + +}; + + #endif \ No newline at end of file diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index 4fef6dc..bc48d2f 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -258,9 +258,6 @@ client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) { } #endif - - - if (securefds) { ssl = SSL_new(securefds); if (!ssl) { @@ -356,3 +353,168 @@ string client::pull (size_t byte_limit) { +queue::queue (const uint _limit, const string _address, const ushort _port, const uint _timeout, SSL_CTX* _securefds) { + + if (_limit > 1) { + limit = _limit; + } + else { + throw string("[ERROR] Limit out of range "); + } + + address = _address; + port = _port; + timeout = _timeout; + securefds = _securefds; + + + // for (uint i=0; i 0.6*limit) { + // // enqueue(); + // if (clients.size() <= 1) { + // if (address.empty() && srv != NULL) { + // clients.push_back(new client(srv, timeout, securefds)); + // } + // else { + // clients.push_back(new client(address, port, timeout, securefds)); + // } + // } + // // } + // // usleep(missing*100000); + // // io.unlock(); + // // usleep(1000); + // sleep(1); + + // } + // }); + +} + + + +queue::queue (const uint _limit, server *_srv, const uint _timeout, SSL_CTX* _securefds) { + if (_limit > 1) { + limit = _limit; + } + else { + throw string("[ERROR] Limit out of range "); + } + + srv = _srv; + timeout = _timeout; + securefds = _securefds; + + controller = thread([this](){ + cout << "Upao u tred "<< endl; + while(true) { + io.lock(); + // uint missing = limit-clients.size(); + // cout << "nedostaje " << missing << endl; + // if (missing > 0.6*limit) { + // enqueue(); + if (clients.size()< limit) { + if (address.empty() && srv != NULL) { + clients.push_back(new client(srv, timeout, securefds)); + } + else { + clients.push_back(new client(address, port, timeout, securefds)); + } + } + // } + // usleep(missing*100000); + io.unlock(); + // usleep(1000); + } + }); + +} + +void queue::enqueue () { + vector worker; + lock_guard master(io); + + for (uint i=0; i master(io); + client *cli; + while (true) { + if (clients.size() >= 1) { + cli = clients.front(); + clients.pop_front(); + cout << "uzimam jednog iz stacka uzeo sam " << endl; + return *cli; + } + else if (clients.size() == 1) { + controller = thread([&](){ + cout << "Upao u tred "<< endl; + while(true) { + // io.lock(); + // uint missing = limit-clients.size(); + // cout << "nedostaje " << missing << endl; + // if (missing > 0.6*limit) { + // enqueue(); + if (clients.size() <= 1) { + if (address.empty() && srv != NULL) { + clients.push_back(new client(srv, timeout, securefds)); + } + else { + clients.push_back(new client(address, port, timeout, securefds)); + } + } + // } + // usleep(missing*100000); + // io.unlock(); + // usleep(1000); + sleep(1); + + } + }); + } + + usleep(1000); + } +} + +queue::~queue () { + for (uint i=0; i~client(); + } + clients.clear(); +} \ No newline at end of file diff --git a/test/client.cpp b/test/client.cpp index f98cfbb..18194d3 100644 --- a/test/client.cpp +++ b/test/client.cpp @@ -1,44 +1,93 @@ #include #include +#include #include "../lib/tcp_socket.hpp" using namespace std; +using namespace chrono; int main() { try { + cout << "inicijalizacija reda" << endl; + // queue sockets(2, "127.0.0.1", 5000, 500); - uint n = 10000; - vector thr; - for (uint i=0; i(stop - start); + // cout << "Time taken by function: " << duration.count() << " microseconds" << endl; + // string sends = "Socket I " + to_string(a++); + // myserver.push(sends); + // cout << myserver.pull() << endl; + + // } catch (const string err) { + // cout << err << endl; + // } + // // usleep(100000); + // } + // }); - for (uint i=0; i(stop - start); + // cout << "Time taken by function: " << duration.count() << " microseconds" << endl; + // string sends = "Socket II " + to_string(a++); + // myserver.push(sends); + // cout << myserver.pull() << endl; - // client myserver("127.0.0.1", 5000, 5000, crypto.fds); - client myserver("localhost", 8000, 5000, crypto.fds); - // client myserver("localhost", 5000); - // cout << "init client " << endl; + // } catch (const string err) { + // cout << err << endl; + // } + // // usleep(100000); + // } + // }); + // t1.join(); + // t2.join(); - // string sends = "Hello world!"; - // cout << myserver.push(sends) << " " << sends.length() << endl; - // cout << "wait client " << endl; + // int a = 0; + // auto start = high_resolution_clock::now(); + // client myserver ("127.0.0.1", 5000, 500); + // // auto stop = high_resolution_clock::now(); + // // auto duration = duration_cast(stop - start); + // // cout << "Time taken by function: " << duration.count() << " microseconds" << endl; + // string sends = "Socket II " + to_string(a++); + // myserver.push(sends); + // cout << myserver.pull() << endl; + // auto stop = high_resolution_clock::now(); + // auto duration = duration_cast(stop - start); + // cout << "Time taken by function: " << duration.count() << " microseconds" << endl; + + + vector clis; + for (uint i =0 ; i<5; i++) { + clis.push_back(new client("127.0.0.1", 5000, 500)); + } - // cout << myserver.pull(); + cout << "Dodalo je đubre "<< endl; + sleep(5); + string sends = "Socket I "; + clis[1]->push(sends); + cout << clis[1]->pull() << endl; + sends = "Socket III "; + clis[3]->push(sends); + cout << clis[3]->pull() << endl; } catch (const string err) { diff --git a/test/client.o b/test/client.o index d18a994..cec033e 100755 Binary files a/test/client.o and b/test/client.o differ diff --git a/test/compile-server.sh b/test/compile-server.sh index 9a9e315..8defd91 100644 --- a/test/compile-server.sh +++ b/test/compile-server.sh @@ -1 +1 @@ -g++ server.cpp ../src/* -o server.o -lssl -lcrypto \ No newline at end of file +g++ server.cpp ../src/* -o server.o -lssl -lcrypto -lpthread \ No newline at end of file diff --git a/test/server.cpp b/test/server.cpp index 630bd94..979bc89 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -10,10 +10,10 @@ int main() { // secure crypto ("../example/cert.pem", "../example/privkey.pem"); // cout << "init server " << endl; // server myserver(5000, 100, crypto.fds); - cout << "init cert " << endl; - secure crypto ("../example/cert.pem", "../example/privkey.pem"); - cout << "init server " << endl; - server myserver(8000, 100, crypto.fds); + // cout << "init cert " << endl; + // secure crypto ("../example/cert.pem", "../example/privkey.pem"); + // cout << "init server " << endl; + // server myserver(8000, 100, crypto.fds); // cout << "init client " << endl; @@ -29,12 +29,12 @@ int main() { // // usleep(600*1000); // sleep(5); // myserver.cli->push(fromclient); - string fromclient = myserver.cli->pull(); + // string fromclient = myserver.cli->pull(); // string fromclient = myclient.pull(); - cout << "tell client " << fromclient << endl; + // cout << "tell client " << fromclient << endl; // usleep(600*1000); //sleep(5); - myserver.cli->push(fromclient); + // myserver.cli->push(fromclient); // myclient.push(fromclient); // myclient.~comming(); @@ -54,12 +54,12 @@ int main() { myserver.async(8, [](client &cli, mutex &io) { cout << "Klijent " << cli.ipv4 << endl; string fromclient = cli.pull(); - io.lock(); + // io.lock(); cout << "S klijenta " << fromclient << endl; - io.unlock(); + // io.unlock(); // fromclient += teststr; cli.push(fromclient); - }, 200); + }, 0); // string teststr = " Idemooo"; diff --git a/test/server.o b/test/server.o index 65bc4f7..dbca48a 100755 Binary files a/test/server.o and b/test/server.o differ