Refactoring of directory structure and CMake files
[platform/core/security/security-manager.git] / src / server / main / include / socket-manager.h
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18 /*
19  * @file        socket-manager.h
20  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
21  * @version     1.0
22  * @brief       SocketManager implementation.
23  */
24
25 #ifndef _SECURITY_MANAGER_SOCKET_MANAGER_
26 #define _SECURITY_MANAGER_SOCKET_MANAGER_
27
28 #include <vector>
29 #include <queue>
30 #include <string>
31 #include <mutex>
32 #include <thread>
33
34 #include <dpl/exception.h>
35
36 #include <generic-socket-manager.h>
37
38 namespace SecurityManager {
39
40 class SocketManager : public GenericSocketManager {
41 public:
42     class Exception {
43     public:
44         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
45         DECLARE_EXCEPTION_TYPE(Base, InitFailed)
46     };
47     SocketManager();
48     virtual ~SocketManager();
49     virtual void MainLoop();
50     virtual void MainLoopStop();
51
52     virtual void RegisterSocketService(GenericSocketService *service);
53     virtual void Close(ConnectionID connectionID);
54     virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer);
55     virtual void Write(ConnectionID connectionID, const SendMsgData &sendMsgData);
56
57 protected:
58     void CreateDomainSocket(
59         GenericSocketService *service,
60         const GenericSocketService::ServiceDescription &desc);
61     int CreateDomainSocketHelp(
62         const GenericSocketService::ServiceDescription &desc);
63     int GetSocketFromSystemD(
64         const GenericSocketService::ServiceDescription &desc);
65
66     void ReadyForRead(int sock);
67     void ReadyForWrite(int sock);
68     void ReadyForWriteBuffer(int sock);
69     void ReadyForSendMsg(int sock);
70     void ReadyForAccept(int sock);
71     void ProcessQueue(void);
72     void NotifyMe(void);
73     void CloseSocket(int sock);
74
75     struct SocketDescription {
76         bool isListen;
77         bool isOpen;
78         bool isTimeout;
79         bool useSendMsg;
80         InterfaceID interfaceID;
81         GenericSocketService *service;
82         time_t timeout;
83         RawBuffer rawBuffer;
84         std::queue<SendMsgData> sendMsgDataQueue;
85         int counter;
86
87         SocketDescription()
88           : isListen(false)
89           , isOpen(false)
90           , isTimeout(false)
91           , useSendMsg(false)
92           , interfaceID(-1)
93           , service(NULL)
94         {}
95     };
96
97     SocketDescription& CreateDefaultReadSocketDescription(int sock, bool timeout);
98
99     typedef std::vector<SocketDescription> SocketDescriptionVector;
100
101     struct WriteBuffer {
102         ConnectionID connectionID;
103         RawBuffer rawBuffer;
104     };
105
106     struct WriteData {
107         ConnectionID connectionID;
108         SendMsgData sendMsgData;
109     };
110
111     struct Timeout {
112         time_t time;
113         int sock;
114         bool operator<(const Timeout &second) const {
115             return time > second.time; // mininum first!
116         }
117     };
118
119     SocketDescriptionVector m_socketDescriptionVector;
120     fd_set m_readSet;
121     fd_set m_writeSet;
122     int m_maxDesc;
123     bool m_working;
124     std::mutex m_eventQueueMutex;
125     std::queue<WriteBuffer> m_writeBufferQueue;
126     std::queue<WriteData> m_writeDataQueue;
127     std::queue<ConnectionID> m_closeQueue;
128     int m_notifyMe[2];
129     int m_counter;
130     std::priority_queue<Timeout> m_timeoutQueue;
131 };
132
133 } // namespace SecurityManager
134
135 #endif // _SECURITY_MANAGER_SOCKET_MANAGER_