From a4c150cc061a2b7fd96271933ed6e01ed66ed702 Mon Sep 17 00:00:00 2001 From: malloc Date: Tue, 17 Apr 2018 17:01:27 -0500 Subject: [PATCH] squid broadcasting --- server/src/ctx/master.hpp | 9 ++++ server/src/ctx/master_client.cpp | 2 + server/src/ctx/master_intra.cpp | 2 + server/src/ctx/slave.cpp | 2 + server/src/ctx/slave.hpp | 14 ++++++ server/src/hosts/master.hpp | 4 +- server/src/hosts/master_intra.cpp | 7 +++ server/src/hosts/slave.hpp | 2 +- server/src/main.cpp | 6 --- server/src/sock/intrasock.hpp | 4 ++ server/src/sock/pool.hpp | 71 +++++++++++++++++-------------- server/src/sock/scapesock.hpp | 4 ++ 12 files changed, 87 insertions(+), 40 deletions(-) create mode 100644 server/src/ctx/master.hpp create mode 100644 server/src/ctx/master_client.cpp create mode 100644 server/src/ctx/master_intra.cpp create mode 100644 server/src/ctx/slave.cpp create mode 100644 server/src/ctx/slave.hpp diff --git a/server/src/ctx/master.hpp b/server/src/ctx/master.hpp new file mode 100644 index 0000000..91e4805 --- /dev/null +++ b/server/src/ctx/master.hpp @@ -0,0 +1,9 @@ +#ifndef SOSC_CTX_MASTER_H +#define SOSC_CTX_MASTER_H + +namespace sosc { +namespace ctx { + +}} + +#endif diff --git a/server/src/ctx/master_client.cpp b/server/src/ctx/master_client.cpp new file mode 100644 index 0000000..2092c57 --- /dev/null +++ b/server/src/ctx/master_client.cpp @@ -0,0 +1,2 @@ +#include "master.hpp" + diff --git a/server/src/ctx/master_intra.cpp b/server/src/ctx/master_intra.cpp new file mode 100644 index 0000000..2092c57 --- /dev/null +++ b/server/src/ctx/master_intra.cpp @@ -0,0 +1,2 @@ +#include "master.hpp" + diff --git a/server/src/ctx/slave.cpp b/server/src/ctx/slave.cpp new file mode 100644 index 0000000..f2c726b --- /dev/null +++ b/server/src/ctx/slave.cpp @@ -0,0 +1,2 @@ +#include "slave.hpp" + diff --git a/server/src/ctx/slave.hpp b/server/src/ctx/slave.hpp new file mode 100644 index 0000000..f16d5a4 --- /dev/null +++ b/server/src/ctx/slave.hpp @@ -0,0 +1,14 @@ +#ifndef SOSC_CTX_SLAVE_H +#define SOSC_CTX_SLAVE_H + +namespace sosc { +namespace ctx { +class SlaveContext { +public: + +private: + +}; +}} + +#endif diff --git a/server/src/hosts/master.hpp b/server/src/hosts/master.hpp index 30ac4fb..76dc079 100644 --- a/server/src/hosts/master.hpp +++ b/server/src/hosts/master.hpp @@ -17,7 +17,7 @@ private: class MasterClientPool : public Pool { protected: - bool ProcessClient(MasterClient* client) override; + bool ProcessClient(MasterClient client) override; }; /** MASTER -> SLAVE **/ @@ -31,7 +31,7 @@ private: class MasterIntraPool : public Pool { protected: - bool ProcessClient(MasterIntra* client) override; + bool ProcessClient(MasterIntra client) override; }; } diff --git a/server/src/hosts/master_intra.cpp b/server/src/hosts/master_intra.cpp index 2092c57..db2510e 100644 --- a/server/src/hosts/master_intra.cpp +++ b/server/src/hosts/master_intra.cpp @@ -1,2 +1,9 @@ #include "master.hpp" +sosc::MasterIntra::MasterIntra(IntraClient client) { + +} + +bool sosc::MasterIntraPool::ProcessClient(MasterIntra client) { + +} diff --git a/server/src/hosts/slave.hpp b/server/src/hosts/slave.hpp index 124347c..fa4ff1e 100644 --- a/server/src/hosts/slave.hpp +++ b/server/src/hosts/slave.hpp @@ -12,7 +12,7 @@ private: ScapeConnection sock; }; -class SlavePool : public Pool { +class SlavePool : public Pool { protected: bool ProcessClient(SlaveClient* client) override; }; diff --git a/server/src/main.cpp b/server/src/main.cpp index 2697b4f..7710068 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -19,12 +19,6 @@ #include "hosts/master.hpp" #include "hosts/slave.hpp" -/*class User; -class Test : sosc::Pool { -protected: - bool ProcessClient(User* client) override; -};*/ - bool master_intra(uint16_t port); bool master_client(uint16_t port); bool slave(uint16_t port); diff --git a/server/src/sock/intrasock.hpp b/server/src/sock/intrasock.hpp index 95ac224..1e736d6 100644 --- a/server/src/sock/intrasock.hpp +++ b/server/src/sock/intrasock.hpp @@ -17,6 +17,10 @@ public: return this->client_open; } + inline net::IpAddress GetIpAddress() const { + return this->client.GetIpAddress(); + } + inline void Close() { this->client_open = false; this->client.Close(); diff --git a/server/src/sock/pool.hpp b/server/src/sock/pool.hpp index 57d0e31..cddd90d 100644 --- a/server/src/sock/pool.hpp +++ b/server/src/sock/pool.hpp @@ -30,18 +30,19 @@ template class Pool { public: Pool(); + Pool(const Pool&) = delete; void Configure(const poolinfo_t& info); void Start(); - bool AddClient(T* client); - int ClientCount() const; + bool AddClient(T client); + int ClientCount(); inline bool IsOpen() const { return this->is_open; } void Stop(); protected: - virtual bool ProcessClient(T* client) = 0; + virtual bool ProcessClient(T client) = 0; private: bool IsStackFull(int stackCount) const; bool CanAddStack() const; @@ -49,10 +50,12 @@ private: class Stack { public: Stack(Pool* pool); + Stack(const Stack&) = delete; + void Start(); - void AddClient(T* client); - int ClientCount() const; + void AddClient(T client); + int ClientCount(); inline bool IsOpen() const { return this->is_open; } @@ -61,19 +64,19 @@ private: private: void StackThread(); - std::thread thread; - Pool *pool; + std::thread* thread; + Pool* pool; bool is_open; bool is_running; - std::list clients; + std::list clients; std::mutex clients_mtx; }; poolinfo_t info; bool is_open; - std::vector stacks; + std::vector stacks; friend class Stack; }; @@ -86,25 +89,25 @@ Pool::Pool() { template void Pool::Configure(const poolinfo_t& info) { this->info = info; - this->is_running = false; + this->is_open = false; } template void Pool::Start() { - if(this->is_running) + if(this->is_open) return; for(int i = 0; i < this->info.initial_count; ++i) { - this->stacks.push_back(Stack(this)); - this->stacks.back().Start(); + this->stacks.push_back(new Stack(this)); + this->stacks.back()->Start(); } - this->is_running = true; + this->is_open = true; } template bool Pool::IsStackFull(int stackCount) const { - poolinfo_t *info = &this->info; + const poolinfo_t *info = &this->info; return info->max_size != -1 && stackCount < info->initial_size @@ -120,8 +123,8 @@ bool Pool::CanAddStack() const { } template -bool Pool::AddClient(T* client) { - if(!this->is_running) +bool Pool::AddClient(T client) { + if(!this->is_open) return false; if(this->info.max_total != -1) @@ -132,17 +135,17 @@ bool Pool::AddClient(T* client) { Stack* lowestStack = nullptr; for(auto i = this->stacks.begin(); i != this->stacks.end(); ++i) { int thisCount; - if((thisCount = i->ClientCount()) > lowestCount) { + if((thisCount = (*i)->ClientCount()) > lowestCount) { lowestCount = thisCount; - lowestStack = &(*i); + lowestStack = *i; } } if(lowestStack != nullptr && !this->IsStackFull(lowestCount)) lowestStack->AddClient(client); else if(this->CanAddStack()) { - this->stacks.push_back(Stack(this)); - this->stacks.back().AddClient(client); + this->stacks.push_back(new Stack(this)); + this->stacks.back()->AddClient(client); } else return false; @@ -150,26 +153,28 @@ bool Pool::AddClient(T* client) { } template -int Pool::ClientCount() const { - if(!this->is_running) +int Pool::ClientCount() { + if(!this->is_open) return 0; int count = 0; for(auto i = this->stacks.begin(); i != this->stacks.end(); ++i) - count += i->ClientCount(); + count += (*i)->ClientCount(); return count; } template void Pool::Stop() { - if(!this->is_running) + if(!this->is_open) return; - for(auto i = this->stacks.begin(); i != this->stacks.end(); ++i) - i->Stop(); + for(auto i = this->stacks.begin(); i != this->stacks.end(); ++i) { + (*i)->Stop(); + delete *i; + } stacks->clear(); - this->is_running = false; + this->is_open = false; } template @@ -186,11 +191,13 @@ void Pool::Stack::Start() { this->is_open = true; this->is_running = true; - this->thread = std::thread(this->StackThread, this); + this->thread = new std::thread([&]() { + this->StackThread(); + }); } template -void Pool::Stack::AddClient(T* client) { +void Pool::Stack::AddClient(T client) { if(!this->is_open || !this->is_running) return; @@ -200,7 +207,7 @@ void Pool::Stack::AddClient(T* client) { } template -int Pool::Stack::ClientCount() const { +int Pool::Stack::ClientCount() { if(!this->is_open || !this->is_running) return 0; @@ -233,6 +240,8 @@ void Pool::Stack::Stop() { this->is_running = false; this->thread.join(); + + delete this->thread; this->is_open = false; } } diff --git a/server/src/sock/scapesock.hpp b/server/src/sock/scapesock.hpp index b8f65f5..f4ecee9 100644 --- a/server/src/sock/scapesock.hpp +++ b/server/src/sock/scapesock.hpp @@ -29,6 +29,10 @@ public: return this->handshaked; } + inline net::IpAddress GetIpAddress() const { + return this->client.GetIpAddress(); + } + inline void Close() { this->client_open = false; this->client.Close();