All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Server.h
1 /*
2  Copyright (c) 2014 by Elvis Angelaccio
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  THE SOFTWARE.
21 */
22 
23 #ifndef SERVER_H
24 #define SERVER_H
25 
26 #include <csignal>
27 
28 #include "SystemHandler.h"
29 #include "sync.h"
30 #include "../net/ServerHandler.h"
31 #include "../net/server_protocol.h"
32 
33 
34 namespace server
35 {
36  typedef enum {DEFAULT, RUNNING, STOPPED} ServerState;
38  extern sig_atomic_t currentServerState;
39  const std::string DEFAULT_DATA_DIR = "database";
40  const std::string DEFAULT_CONF_DIR = "conf";
41  const std::string VERSION = "1.0.0";
42 
43  struct NetworkThreadArgs;
44 
50  class Server
51  {
52 
53  public:
54 
55  Server();
56  ~Server();
57 
63  bool checkArguments(int argc, char **argv);
64 
66  void start();
67 
68  private:
69 
70  static const net::Port DEFAULT_PORT = 4444;
71  static const int POSIX_DSS_THREAD_TIMER = 30;
72  static const int WIN32_DSS_THREAD_TIMER = 30000;
74  std::string dataDirectory;
75  std::string confDirectory;
76  net::Port port;
77  int connectionCount;
79  Mutex mutex;
80  RWLock lock;
82  /* System Handlers */
83  net::ServerHandler *serverHandler;
84  SystemHandler systemHandler;
86  /* System core functions */
87 
89  void setSignalHandler();
90 
92  void createDssThread();
93 
98  void createNetworkThread(int connectedSocket);
99 
100 
102  void dssThread();
103 
109  void networkThread(int connectedSocket, net::SocketState& threadState);
110 
111 
118  void doInsertCommand(int connectedSocket, net::SocketState& threadState, const net::InsertRequest& insertRequest);
119 
120 
121  bool fillSdFields(const net::InsertRequest& insertRequest, entity::DataSet& dataSet);
122 
123 
124  bool receiveFiles(int connectedSocket, net::SocketState& threadState, const net::InsertRequest& insertRequest, entity::DataSet& dataSet);
125 
126  bool processFiles(const net::InsertRequest& insertRequest, const entity::DataSet& dataSet);
127 
128 
129  void doRemoveCommand(int connectedSocket, net::SocketState& threadState, const net::DataSetRequest& dataSetRequest);
130 
131  void doGetCommand(int connectedSocket, net::SocketState& threadState, const net::DataSetRequest& dataSetRequest);
132 
133  void doSearchCommand(int connectedSocket, net::SocketState& threadState, const net::SearchRequest& searchRequest);
134 
135 
141  void doSpeclistCommand(int connectedSocket, net::SocketState& threadState);
142 
144  void shutdown();
145 
147  void printUsage(const char *program);
148 
150  void printInitialMessage();
151 
153  static Server *getContext();
154 
155  /* Platform-specific declarations for the threads starting functions and for the signal handling functions. */
156 #if (defined(_WIN32) || defined(__WIN32__) || defined(WIN32))
157 
158  static unsigned int __stdcall win32DssThread(void *args); // __stdcall rqeuired by _beginthreadex()
159  static unsigned int __stdcall win32NetworkThread(void *args);
160  static BOOL WINAPI consoleHandler(DWORD signal);
161 
162  std::map<unsigned int, HANDLE> networkThreadHandles;
163  std::map<unsigned int, NetworkThreadArgs*> networkThreadArgs;
164  HANDLE dssThreadHandle;
166  Event dssThreadEvent;
168 #else
169  static void *posixDssThread(void *args);
170  static void *posixNetworkThread(void *args);
171  static void signalHandler(int signal);
172 
173  std::map<pthread_t, NetworkThreadArgs*> networkThreads;
174  pthread_t dssThreadId;
175 
176  CondVarBundle dssThreadCond;
178 #endif
179 
180  };
181 
184  {
185  Server *server;
186  };
187 
190  {
191  int connectedSocket;
194  };
195 
196 }
197 
198 
199 
200 
201 #endif
bool checkArguments(int argc, char **argv)
Definition: Server.cpp:68
net::SocketState threadState
Definition: Server.h:192
Definition: Server.h:183
An INSERT request message.
Definition: server_protocol.h:67
A SEARCH request message.
Definition: server_protocol.h:114
SocketState
Definition: net.h:44
A Mutex object.
Definition: sync.h:40
void start()
Definition: Server.cpp:156
A Data Set (DS) is an instance of a DSS and is defined by a SetDescriptor and a set of DataItems...
Definition: DataSet.h:36
Definition: SystemHandler.h:38
Definition: Server.h:189
Definition: sync.h:118
Server * server
Definition: Server.h:193
System main class, implementing the server side.
Definition: Server.h:50
A Read-Write lock object.
Definition: sync.h:69
Server side of the SimDB socket interface. This class provides the server side functions to simplify ...
Definition: ServerHandler.h:38
A GET or REMOVE request message.
Definition: server_protocol.h:94
Definition: Server.h:34