/* * Server.h * * Created on: Apr 24, 2015 * Author: rheijden */ #ifndef INCLUDE_SERVER_TCPCONNECTIONDIRECTOR_H_ #define INCLUDE_SERVER_TCPCONNECTIONDIRECTOR_H_ #include #include #include #include #include #include #define DESIRED_WORKERS(ncon) static_cast(MAX(10, (ncon / 5))) class DeviceFacade; class TCPConnection; class ProtocolFacade; struct Message; class TCPConnectionDirector { public: const int kWorkerMailboxTimeoutMs = 50; const size_t kWorkerSelectTimeoutMs = 10; const float kMinimumWorkers = 10; const float kMaximumWorkers = 100; const float kConnectionsPerWorker = 5; const float kWorkerMovingAverageFactor = 30; const float kWorkerFastScalingThreshold = 2; /** * constructor of the connection director * @param c pointer to config * @param d pointer to device facade */ TCPConnectionDirector(const std::shared_ptr &c, const std::shared_ptr &d); ~TCPConnectionDirector(); /** * create a new connection * @param fd socket * @param info * @return true if .. false if .. */ bool newConnection(int fd, const struct sockaddr_in &info); private: struct Worker { std::shared_ptr workerThread; std::atomic_bool running; }; std::atomic_bool __running; uint64_t __connectionTimeoutMs; std::mutex __workerMutex; std::shared_ptr __cfg; std::vector> __workerList; std::shared_ptr> __mailbox; std::shared_ptr __protoFacade; std::shared_ptr __deviceFacade; std::atomic_size_t __nConnections; std::shared_ptr workerScaler; float __workerCountAverage; /** * thread function which adjusts the number of workers to * the desired amount (based on number of connections) */ void __scaleWorkers(void); /** * Calculates how many workers are desired */ size_t __workerCount(void); /** * stop inactive worker */ void __terminateWorkers(ssize_t); /** * create new worker */ void __spawnWorkers(ssize_t); void __encodeAndWrite(const std::shared_ptr &conn, const std::shared_ptr &msg); void __worker(std::shared_ptr worker); void __workerRead(const std::shared_ptr &conn); void __workerMessage(const std::shared_ptr &conn, const std::shared_ptr &msg); }; #endif // INCLUDE_SERVER_TCPCONNECTIONDIRECTOR_H_