Skip to content

Commit

Permalink
Adding stop event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Mar 20, 2016
1 parent 49163a3 commit faddc05
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions webserver/TcpEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "TcpEngine.h"
#include "utils/Logging.h"
using namespace Dumais::WebServer;


Expand All @@ -14,6 +15,16 @@ TcpEngine::~TcpEngine()
if (this->clientFactory != 0) delete this->clientFactory;
}

void TcpEngine::setStopEventHandler(std::function<void()> handler)
{
if (!this->tcpServer)
{
LOG("can't set event handler if init has not been called");
return;
}
this->tcpServer->setStopEventHandler(handler);
}

void TcpEngine::init(int port, std::string bindAddr, int maxConnections)
{
this->tcpServer = new TcpServer(this->clientFactory,port, bindAddr, maxConnections);
Expand Down
2 changes: 2 additions & 0 deletions webserver/TcpEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class TcpEngine: public TcpClientObserver
~TcpEngine();

void init(int port, std::string bindAddr, int maxConnections);
void setStopEventHandler(std::function<void()> handler);
bool startSecure(char* certificatePath, char* privateKeyPath);
bool start();
void stop();

private:
TcpServer* tcpServer;
IClientFactory *clientFactory;
std::function<void()> stopEventHandler;
};

}
Expand Down
12 changes: 12 additions & 0 deletions webserver/TcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ TcpServer::~TcpServer()
if (this->listenSocket != 0) delete this->listenSocket;
}

void TcpServer::setStopEventHandler(std::function<void()> handler)
{
stopEvent = handler;
}

bool TcpServer::setSecurity(char* certificatePath, char* privateKeyPath)
{
if (!SECURE_IMPLEMENTATION) return false;
Expand Down Expand Up @@ -78,6 +83,7 @@ bool TcpServer::start()

void TcpServer::stop()
{
LOG("setting stopping to true because of stop()");
stopping = true;
write(this->controlEventFd,(char*)&controlTxMessage[0], 8);

Expand Down Expand Up @@ -124,6 +130,9 @@ void TcpServer::run()
// Process all outgoing
this->processSend();
}

LOG("TCP server thread exit");

// Kill all clients
for (auto it = this->clientList.begin(); it != clientList.end(); it++)
{
Expand All @@ -135,6 +144,8 @@ void TcpServer::run()
close(this->controlEventFd);
close(efd);
delete[] events;

if (stopEvent) stopEvent();
}

void TcpServer::removeFdFromEpoll(int efd, int fd)
Expand All @@ -153,6 +164,7 @@ void TcpServer::addFdToEpoll(int efd, int fd)

void TcpServer::abort()
{
LOG("setting stopping to true because of abort");
this->stopping = true;
}

Expand Down
2 changes: 2 additions & 0 deletions webserver/TcpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TcpServer
~TcpServer();

bool start();
void setStopEventHandler(std::function<void()> handler);
bool setSecurity(char* certificatePath, char* privateKeyPath);
void run();
void stop();
Expand All @@ -29,6 +30,7 @@ class TcpServer
std::string bindAddress;
std::string certificatePath;
std::string privateKeyPath;
std::function<void()> stopEvent;


bool processAccept(int efd);
Expand Down

0 comments on commit faddc05

Please sign in to comment.