Basic daemon support

This commit is contained in:
Naim A 2016-01-28 19:08:43 -08:00
parent 494e8314f1
commit 37c965019b
6 changed files with 18 additions and 16 deletions

View file

@ -18,7 +18,7 @@
# #
objects = main.o udpTracker.o database.o driver_sqlite.o \ objects = main.o udpTracker.o database.o driver_sqlite.o \
tools.o httpserver.o webapp.o logging.o tools.o httpserver.o webapp.o logging.o tracker.o
target = udpt target = udpt
%.o: src/%.c %.o: src/%.c
@ -33,7 +33,7 @@ all: $(target)
$(target): $(objects) $(target): $(objects)
@echo Linking... @echo Linking...
$(CXX) -O3 -o $(target) $(objects) $(LDFLAGS) -lboost_program_options -lsqlite3 -lpthread $(CXX) -O3 -o $(target) $(objects) $(LDFLAGS) -lboost_program_options -lsqlite3 -lpthread -lboost_thread -lboost_system
@echo Done. @echo Done.
clean: clean:
@echo Cleaning Up... @echo Cleaning Up...

View file

@ -15,7 +15,7 @@ We use [SQLite3](http://www.sqlite.org/) which is public-domain, and [Boost](htt
We didn't really work on creating any installer, at the moment you can just run udpt from anywhere on your filesystem. We didn't really work on creating any installer, at the moment you can just run udpt from anywhere on your filesystem.
Building udpt is pretty straightforward, just download the project or clone the repo: Building udpt is pretty straightforward, just download the project or clone the repo:
UDPT requires the SQLite3 develpment package to be installed. UDPT requires the SQLite3, boost_program_options and boost_thread develpment packages to be installed.
<pre> <pre>
$ git clone https://github.com/naim94a/udpt.git $ git clone https://github.com/naim94a/udpt.git

View file

@ -33,17 +33,16 @@
#include "tracker.hpp" #include "tracker.hpp"
UDPT::Logger *logger = NULL; UDPT::Logger *logger = NULL;
UDPT::Tracker *instance = NULL;
static void _signal_handler(int sig)
static void _signal_handler (int sig)
{ {
switch (sig) switch (sig)
{ {
case SIGTERM: case SIGTERM:
instance->stop(); UDPT::Tracker::getInstance().stop();
break; break;
case SIGHUP: case SIGHUP:
// TODO: Reload config.
break; break;
} }
} }
@ -73,7 +72,6 @@ static void daemonize(const boost::program_options::variables_map& conf)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Tracker& tracker = UDPT::Tracker::getInstance(); Tracker& tracker = UDPT::Tracker::getInstance();
instance = &tracker;
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
@ -186,8 +184,6 @@ int main(int argc, char *argv[])
tracker.start(var_map); tracker.start(var_map);
tracker.wait(); tracker.wait();
std::cerr << "Bye." << std::endl;
#ifdef WIN32 #ifdef WIN32
::WSACleanup(); ::WSACleanup();
#endif #endif

View file

@ -34,7 +34,7 @@ namespace UDPT
if (conf["apiserver.enable"].as<bool>()) if (conf["apiserver.enable"].as<bool>())
{ {
m_apiSrv = std::shared_ptr<UDPT::Server::HTTPServer>(new UDPT::Server::HTTPServer(conf)); m_apiSrv = std::shared_ptr<UDPT::Server::HTTPServer>(new UDPT::Server::HTTPServer(conf));
m_webApp = std::shared_ptr<UDPT::Server::WebApp>(new UDPT::Server::WebApp(m_apiSrv, m_udpTracker->conn, conf)); m_webApp = std::shared_ptr<UDPT::Server::WebApp>(new UDPT::Server::WebApp(m_apiSrv, m_udpTracker->m_conn.get(), conf));
m_webApp->deploy(); m_webApp->deploy();
} }

View file

@ -62,7 +62,7 @@ namespace UDPT
UDPTracker::~UDPTracker() UDPTracker::~UDPTracker()
{ {
// left empty. stop();
} }
void UDPTracker::start() void UDPTracker::start()
@ -141,17 +141,18 @@ namespace UDPT
for (std::vector<boost::thread>::iterator it = m_threads.begin(); it != m_threads.end(); ++it) for (std::vector<boost::thread>::iterator it = m_threads.begin(); it != m_threads.end(); ++it)
{ {
std::cout << "Interrupted thread " << it->get_id() << std::endl;
it->interrupt(); it->interrupt();
} }
wait();
}
void UDPTracker::wait()
{
for (std::vector<boost::thread>::iterator it = m_threads.begin(); it != m_threads.end(); ++it) for (std::vector<boost::thread>::iterator it = m_threads.begin(); it != m_threads.end(); ++it)
{ {
std::cout << "waiting for " << it->get_id() << std::endl;
it->join(); it->join();
} }
std::cout << "All threads terminated." << std::endl;
} }
int UDPTracker::sendError(UDPTracker* usi, SOCKADDR_IN* remote, uint32_t transactionID, const std::string &msg) int UDPTracker::sendError(UDPTracker* usi, SOCKADDR_IN* remote, uint32_t transactionID, const std::string &msg)

View file

@ -131,6 +131,11 @@ namespace UDPT
*/ */
void stop(); void stop();
/**
* Joins worker threads
*/
void wait();
/** /**
* Destroys resources that were created by constructor * Destroys resources that were created by constructor
* @param usi Instance to destroy. * @param usi Instance to destroy.