forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtapplicationserverbase.cpp
186 lines (153 loc) · 4.85 KB
/
tapplicationserverbase.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Copyright (c) 2010-2019, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include <TWebApplication>
#include <TActionContext>
#include <TDispatcher>
#include <TActionController>
#include "tapplicationserverbase.h"
#include <QLibrary>
#include <QList>
#include <QDir>
#include <QDateTime>
#ifdef Q_OS_WIN
# include <winsock2.h>
# include <ws2tcpip.h>
#endif
#ifdef Q_OS_UNIX
# include <tfcore_unix.h>
#endif
/*!
\class TApplicationServerBase
\brief The TApplicationServerBase class provides functionality common to
an web application server.
*/
namespace {
QList<QLibrary*> libsLoaded;
QDateTime loadedTimestamp;
}
bool TApplicationServerBase::loadLibraries()
{
bool ret = true;
// Loads libraries
if (libsLoaded.isEmpty()) {
// Sets work directory
QString libPath = Tf::app()->libPath();
if (! QDir(libPath).exists()) {
tSystemError("lib directory not found");
return false;
}
// To resolve the symbols in the app libraries
QDir::setCurrent(libPath);
#if defined(Q_OS_WIN)
const QStringList libs = { "controller", "view" };
#else
const QStringList libs = { "libcontroller", "libview" };
#endif
for (auto &libname : libs) {
auto lib = new QLibrary(libPath + libname);
if (lib->load()) {
tSystemDebug("Library loaded: %s", qPrintable(lib->fileName()));
libsLoaded << lib;
} else {
tSystemWarn("%s", qPrintable(lib->errorString()));
ret = false;
unloadLibraries();
break;
}
}
QStringList controllers = TActionController::availableControllers();
tSystemDebug("Available controllers: %s", qPrintable(controllers.join(" ")));
if (ret) {
loadedTimestamp = latestLibraryTimestamp();
}
}
QDir::setCurrent(Tf::app()->webRootPath());
return ret;
}
void TApplicationServerBase::unloadLibraries()
{
for (auto lib : libsLoaded) {
lib->unload();
tSystemDebug("Library unloaded: %s", qPrintable(lib->fileName()));
}
libsLoaded.clear();
}
QDateTime TApplicationServerBase::latestLibraryTimestamp()
{
#if defined(Q_OS_WIN)
const QStringList libs = { "controller", "model", "view", "helper" };
#elif defined(Q_OS_LINUX)
const QStringList libs = { "libcontroller.so", "libmodel.so", "libview.so", "libhelper.so" };
#elif defined(Q_OS_DARWIN)
const QStringList libs = { "libcontroller.dylib", "libmodel.dylib", "libview.dylib", "libhelper.dylib" };
#else
const QStringList libs = { "libcontroller.so", "libmodel.so", "libview.so", "libhelper.so" };
#endif
QDateTime ret = QDateTime::fromTime_t(0);
QString libPath = Tf::app()->libPath();
for (auto &lib : libs) {
QFileInfo fi(libPath + lib);
if (fi.isFile() && fi.lastModified() > ret) {
ret = fi.lastModified();
}
}
return ret;
}
bool TApplicationServerBase::newerLibraryExists()
{
return (latestLibraryTimestamp() > loadedTimestamp);
}
QPair<QHostAddress, quint16> TApplicationServerBase::getPeerInfo(int socketDescriptor)
{
auto peerInfo = QPair<QHostAddress, quint16>(QHostAddress(), 0);
union {
sockaddr a;
sockaddr_in a4;
sockaddr_in6 a6;
} sa;
socklen_t sasize = sizeof(sa);
memset(&sa, 0, sizeof(sa));
if (socketDescriptor <= 0 || ::getpeername(socketDescriptor, &sa.a, &sasize) < 0) {
return peerInfo;
}
if (sa.a.sa_family == AF_INET6) {
// IPv6
Q_IPV6ADDR tmp;
memcpy(&tmp, &sa.a6.sin6_addr, sizeof(tmp));
peerInfo.first.setAddress(tmp);
peerInfo.second = ntohs(sa.a6.sin6_port);
} else {
// IPv4
peerInfo.first.setAddress(ntohl(sa.a4.sin_addr.s_addr));
peerInfo.second = ntohs(sa.a4.sin_port);
}
return peerInfo;
}
void TApplicationServerBase::invokeStaticInitialize()
{
// Calls staticInitialize()
TDispatcher<TActionController> dispatcher("applicationcontroller");
bool dispatched = dispatcher.invoke("staticInitialize", QStringList(), Qt::DirectConnection);
if (!dispatched) {
tSystemWarn("No such method: staticInitialize() of ApplicationController");
}
}
void TApplicationServerBase::invokeStaticRelease()
{
// Calls staticRelease()
TDispatcher<TActionController> dispatcher("applicationcontroller");
bool dispatched = dispatcher.invoke("staticRelease", QStringList(), Qt::DirectConnection);
if (!dispatched) {
tSystemDebug("No such method: staticRelease() of ApplicationController");
}
}
TApplicationServerBase::TApplicationServerBase()
{ }
TApplicationServerBase::~TApplicationServerBase()
{
nativeSocketCleanup();
}