~george-edison55/jethttp/jethttp-singlethread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//=================================================
//
//                JetHTTP - main.cpp
//           Copyright 2011 - Nathan Osman
//     This file is released under the GPL v3
//
//=================================================

// Qt includes
#include <QSettings>
#include <QtCore/QCoreApplication>

// Linux specific code
#ifdef Q_OS_LINUX
#include <sys/signal.h>
#include <sys/socket.h>
#include <QSocketNotifier>
#endif

// Windows specific code
#ifdef Q_OS_WIN
#include <windows.h>
#endif

// JetHTTP includes
#include <CJetHTTPServer.h>

// Signal handler
#ifdef Q_OS_LINUX

int pair[2];

void SignalHandler(int)
{
    char c = 0;
    write(pair[0], &c, sizeof(c));
}

#endif

int main(int argc, char *argv[])
{
    try
    {
        QCoreApplication a(argc, argv);

        // Set the application default values
        QCoreApplication::setApplicationName("JetHTTP");
        QCoreApplication::setOrganizationName("Nathan Osman");

        CJetHTTPServer server;

        // Install the signal handler
        #ifdef Q_OS_LINUX

        // Set up the signals
        if(socketpair(AF_UNIX, SOCK_STREAM, 0, pair))
            qFatal("Error creating socket pair.");

        QSocketNotifier notifier(pair[1], QSocketNotifier::Read);
        server.connect(&notifier, SIGNAL(activated(int)), SLOT(OnSignal(int)));

        // Now we install the signal handler
        struct sigaction info;
        memset(&info,0,sizeof(info));

        info.sa_handler = SignalHandler;
        sigemptyset(&info.sa_mask);
        info.sa_flags = SA_RESTART;

        if(sigaction(SIGINT, &info, NULL))
            qFatal("Error installing SIGINT signal handler.");

        if(sigaction(SIGTERM, &info, NULL))
            qFatal("Error installing SIGTERM signal handler.");

        #endif

        return a.exec();
    }
    catch(QString val)
    {
        qDebug(val.toUtf8().data());
    }

    return 0;
}