1 /* Copyright (C) 2019 MariaDB Corporation
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 
19 
20 #ifndef STORAGEMANGER_H_
21 #define STORAGEMANGER_H_
22 
23 #include "ClientRequestProcessor.h"
24 #include "messageFormat.h"
25 
26 #include <signal.h>
27 #include <boost/thread/mutex.hpp>
28 #include <sys/poll.h>
29 #include <tr1/unordered_map>
30 
31 
32 namespace storagemanager
33 {
34 
35 enum sessionCtrl {
36     ADDFD,
37     REMOVEFD,
38     SHUTDOWN
39 };
40 
41 #define MAX_SM_SOCKETS 200
42 
43 /**
44  * @brief StorageManager class initializes process and handles incoming requests.
45  */
46 class SessionManager
47 {
48 public:
49     static SessionManager *get();
50     ~SessionManager();
51 
52     /**
53      * start and manage socket connections
54      */
55     int start();
56 
57     void returnSocket(int socket);
58     void socketError(int socket);
59     void CRPTest(int socket,uint length);
60     void shutdownSM(int sig);
61 
62 private:
63     SessionManager();
64     //SMConfig&  config;
65     ClientRequestProcessor *crp;
66     struct pollfd fds[MAX_SM_SOCKETS];
67     int socketCtrl[2];
68     boost::mutex ctrlMutex;
69 
70     // These map a socket fd to its state between read iterations if a message header could not be found in the data
71     // available at the time.
72     struct SockState {
73         char remainingData[SM_HEADER_LEN];
74         uint remainingBytes;
75     };
76     std::tr1::unordered_map<int, SockState> sockState;
77 
78 };
79 
80 }
81 
82 #endif
83