Skip to content

Commit 5016ee4

Browse files
committed
Command line parameters '--config-path=' and '--server-name=' were added
1 parent 5824571 commit 5016ee4

20 files changed

+921
-179
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Common:
3030

3131
* [gnutls](https://www.gnutls.org/)
3232

33-
Linux: `dl`, `pthread`, `gnutls`
33+
Linux: `dl`, `pthread`, `rt`, `gnutls`
3434

3535
Windows: `ws2_32.lib`, `libgnutls.dll.a`
3636

@@ -65,6 +65,10 @@ Server start
6565
```
6666

6767
Configuration files must be located in the working (current) directory.
68+
Or input a parameter `--config-path=<path>` to set the directory with configuration files.
69+
70+
Use the parameter `--server-name=<name>` to define the name of web-server's instance.
71+
Instances can be used to run web-servers with different settings.
6872

6973
Server configuration
7074
--------------------

projects/qt-creator/httpserver.qbs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ Project {
1313
Properties {
1414
condition: qbs.targetOS.contains("linux")
1515
cpp.defines: outer.concat(["POSIX"])
16-
cpp.dynamicLibraries: outer.concat(["dl", "pthread"])
16+
cpp.dynamicLibraries: outer.concat(["dl", "pthread", "rt"])
1717
}
1818

1919
Properties {
2020
condition: qbs.targetOS.contains("windows")
21-
cpp.defines: outer.concat(["WIN32"])
21+
cpp.defines: outer.concat(["WIN32", "NOMINMAX"])
2222
}
2323

2424
files: [
@@ -35,6 +35,8 @@ Project {
3535
"../../src/Event.h",
3636
"../../src/FileIncoming.cpp",
3737
"../../src/FileIncoming.h",
38+
"../../src/GlobalMutex.cpp",
39+
"../../src/GlobalMutex.h",
3840
"../../src/Main.cpp",
3941
"../../src/Main.h",
4042
"../../src/Module.cpp",
@@ -50,6 +52,9 @@ Project {
5052
"../../src/ServerApplicationsTree.h",
5153
"../../src/ServerRequest.h",
5254
"../../src/ServerResponse.h",
55+
"../../src/ServerStructuresArguments.h",
56+
"../../src/SharedMemory.cpp",
57+
"../../src/SharedMemory.h",
5358
"../../src/SignalHandlers.cpp",
5459
"../../src/SignalHandlers.h",
5560
"../../src/Socket.cpp",

src/Event.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#include "Event.h"
23

34
namespace HttpServer

src/GlobalMutex.cpp

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
2+
#include "GlobalMutex.h"
3+
4+
#ifdef WIN32
5+
#include <Windows.h>
6+
#elif POSIX
7+
#include <fcntl.h>
8+
#endif
9+
10+
namespace HttpServer
11+
{
12+
GlobalMutex::GlobalMutex() : mtx_desc(nullptr)
13+
{
14+
15+
}
16+
17+
GlobalMutex::~GlobalMutex()
18+
{
19+
this->close();
20+
}
21+
22+
bool GlobalMutex::create(const std::string &mutexName)
23+
{
24+
this->close();
25+
26+
#ifdef WIN32
27+
this->mtx_desc = ::CreateMutex(nullptr, false, mutexName.c_str() );
28+
29+
if (nullptr == this->mtx_desc)
30+
{
31+
return false;
32+
}
33+
#elif POSIX
34+
::sem_t *sem = ::sem_open(mutexName.c_str(), O_CREAT, 0666, 1);
35+
36+
if (SEM_FAILED == sem)
37+
{
38+
return false;
39+
}
40+
41+
this->mtx_desc = sem;
42+
#else
43+
#error "Undefine platform"
44+
#endif
45+
46+
this->mtx_name = mutexName;
47+
48+
return true;
49+
}
50+
51+
bool GlobalMutex::destory(const std::string &mutexName)
52+
{
53+
#ifdef WIN32
54+
return true;
55+
#elif POSIX
56+
return 0 == ::sem_unlink(mutexName.c_str() );
57+
#else
58+
#error "Undefine platform"
59+
#endif
60+
}
61+
62+
bool GlobalMutex::destory()
63+
{
64+
#ifdef WIN32
65+
const bool ret = ::CloseHandle(this->mtx_desc);
66+
67+
this->close();
68+
69+
return ret;
70+
#elif POSIX
71+
const int ret = ::sem_unlink(this->mtx_name.c_str() );
72+
73+
this->close();
74+
75+
return 0 == ret;
76+
#else
77+
#error "Undefine platform"
78+
#endif
79+
}
80+
81+
bool GlobalMutex::open(const std::string &mutexName)
82+
{
83+
this->close();
84+
85+
#ifdef WIN32
86+
this->mtx_desc = ::OpenMutex(SYNCHRONIZE, false, mutexName.c_str() );
87+
88+
if (nullptr == this->mtx_desc)
89+
{
90+
return false;
91+
}
92+
#elif POSIX
93+
::sem_t *sem = ::sem_open(mutexName.c_str(), 0);
94+
95+
if (SEM_FAILED == sem)
96+
{
97+
return false;
98+
}
99+
100+
this->mtx_desc = sem;
101+
#else
102+
#error "Undefine platform"
103+
#endif
104+
105+
this->mtx_name = mutexName;
106+
107+
return true;
108+
}
109+
110+
bool GlobalMutex::close()
111+
{
112+
if (this->mtx_desc)
113+
{
114+
#ifdef WIN32
115+
::CloseHandle(this->mtx_desc);
116+
this->mtx_desc = nullptr;
117+
#elif POSIX
118+
::sem_close(this->mtx_desc);
119+
this->mtx_desc = nullptr;
120+
#else
121+
#error "Undefine platform"
122+
#endif
123+
124+
this->mtx_name.clear();
125+
126+
return true;
127+
}
128+
129+
return false;
130+
}
131+
132+
bool GlobalMutex::is_open() const
133+
{
134+
#ifdef WIN32
135+
return nullptr != this->mtx_desc;
136+
#elif POSIX
137+
return nullptr != this->mtx_desc;
138+
#else
139+
#error "Undefine platform"
140+
#endif
141+
}
142+
143+
bool GlobalMutex::lock() const
144+
{
145+
#ifdef WIN32
146+
return WAIT_OBJECT_0 == ::WaitForSingleObject(this->mtx_desc, INFINITE);
147+
#elif POSIX
148+
return 0 == ::sem_wait(this->mtx_desc);
149+
#else
150+
#error "Undefine platform"
151+
#endif
152+
}
153+
154+
bool GlobalMutex::try_lock() const
155+
{
156+
#ifdef WIN32
157+
return WAIT_OBJECT_0 == ::WaitForSingleObject(this->mtx_desc, 0);
158+
#elif POSIX
159+
return 0 == ::sem_trywait(this->mtx_desc);
160+
#else
161+
#error "Undefine platform"
162+
#endif
163+
}
164+
165+
bool GlobalMutex::unlock() const
166+
{
167+
#ifdef WIN32
168+
return ::ReleaseMutex(this->mtx_desc);
169+
#elif POSIX
170+
return 0 == ::sem_post(this->mtx_desc);
171+
#else
172+
#error "Undefine platform"
173+
#endif
174+
}
175+
};

src/GlobalMutex.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#ifdef WIN32
4+
#include <wtypes.h>
5+
#include <WinDef.h>
6+
#elif POSIX
7+
#include <semaphore.h>
8+
#endif
9+
10+
#include <string>
11+
12+
namespace HttpServer
13+
{
14+
class GlobalMutex
15+
{
16+
private:
17+
#ifdef WIN32
18+
::HANDLE mtx_desc;
19+
#elif POSIX
20+
::sem_t *mtx_desc;
21+
#else
22+
#error "Undefine platform"
23+
#endif
24+
25+
std::string mtx_name;
26+
27+
public:
28+
GlobalMutex();
29+
~GlobalMutex();
30+
31+
bool create(const std::string &mutexName);
32+
bool destory();
33+
34+
static bool destory(const std::string &mutexName);
35+
36+
bool open(const std::string &mutexName);
37+
bool close();
38+
39+
bool is_open() const;
40+
41+
bool lock() const;
42+
bool try_lock() const;
43+
bool unlock() const;
44+
};
45+
};

src/Main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ int main(const int argc, const char *argv[])
3434
}
3535
else
3636
{
37-
std::cout << "Unknown command, see --help" << std::endl;
37+
std::cout << "Unknown parameter, see --help" << std::endl;
3838
}
3939
}
4040
else if (1 == argc)
4141
{
42-
std::cout << "Try: " << argv[0] << " --help" << std::endl;
42+
std::cout << "Try to run with the parameter --help" << std::endl;
4343
}
4444
else
4545
{
46-
std::cout << "Arguments failure, see --help command" << std::endl;
46+
std::cout << "The number of arguments cannot be equal to zero. Enter the parameter --help" << std::endl;
4747
}
4848

4949
return exitcode;

src/Module.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
#include "Module.h"
33

44
#ifdef WIN32
5+
#include <Windows.h>
6+
57
#ifdef UNICODE
68
#include <codecvt>
79
#endif
810
#elif POSIX
11+
#include <dlfcn.h>
912
#include <iostream>
1013
#endif
1114

@@ -31,6 +34,11 @@ namespace HttpServer
3134
obj.lib_handle = nullptr;
3235
}
3336

37+
bool Module::is_open() const
38+
{
39+
return nullptr != this->lib_handle;
40+
}
41+
3442
bool Module::open(const std::string &libPath)
3543
{
3644
if (is_open() )
@@ -68,7 +76,7 @@ namespace HttpServer
6876
#else
6977
const std::string &lib_path = libPath;
7078
#endif
71-
79+
7280
lib_handle = ::LoadLibraryEx(lib_path.c_str(), 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
7381

7482
if (cookie)
@@ -156,6 +164,16 @@ namespace HttpServer
156164
return false;
157165
}
158166

167+
bool Module::operator ==(const Module &obj) const
168+
{
169+
return this->lib_handle == obj.lib_handle;
170+
}
171+
172+
bool Module::operator !=(const Module &obj) const
173+
{
174+
return this->lib_handle != obj.lib_handle;
175+
}
176+
159177
Module &Module::operator =(const Module &obj)
160178
{
161179
if (*this != obj)

src/Module.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#pragma once
22

33
#ifdef WIN32
4-
#include <windows.h>
5-
#elif POSIX
6-
#include <dlfcn.h>
7-
#else
8-
#error "Undefine platform"
4+
#include <wtypes.h>
5+
#include <WinDef.h>
96
#endif
107

118
#include <string>
@@ -31,26 +28,16 @@ namespace HttpServer
3128

3229
~Module() = default;
3330

34-
inline bool is_open() const
35-
{
36-
return nullptr != lib_handle;
37-
}
31+
bool is_open() const;
3832

3933
bool open(const std::string &libPath);
4034
void close();
4135

4236
bool find(const std::string &symbolName, void *(**addr)(void *) ) const;
4337
bool find(const char *symbolName, void *(**addr)(void *) ) const;
4438

45-
inline bool operator ==(const Module &obj) const
46-
{
47-
return lib_handle == obj.lib_handle;
48-
}
49-
50-
inline bool operator !=(const Module &obj) const
51-
{
52-
return lib_handle != obj.lib_handle;
53-
}
39+
bool operator ==(const Module &obj) const;
40+
bool operator !=(const Module &obj) const;
5441

5542
Module &operator =(const Module &obj);
5643
Module &operator =(Module &&obj);

0 commit comments

Comments
 (0)