978dbee145cbb47a79f82bc9d1099f24464c8512
[platform/core/security/key-manager.git] / src / manager / main / socket-manager.h
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@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  * @author      Zofia Abramowska (z.abramowska@samsung.com)
22  * @version     1.0
23  * @brief       SocketManager implementation.
24  */
25
26 #ifndef _CENT_KEY_MNG_SOCKET_MANAGER_
27 #define _CENT_KEY_MNG_SOCKET_MANAGER_
28
29 #include <vector>
30 #include <queue>
31 #include <string>
32 #include <mutex>
33 #include <thread>
34
35 #include <dpl/exception.h>
36
37 #include <generic-socket-manager.h>
38
39 namespace CKM {
40
41 class SocketManager : public GenericSocketManager {
42 public:
43     class Exception {
44     public:
45         DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
46         DECLARE_EXCEPTION_TYPE(Base, InitFailed)
47     };
48     SocketManager();
49     virtual ~SocketManager();
50     virtual void MainLoop();
51     virtual void MainLoopStop();
52
53     virtual void RegisterSocketService(GenericSocketService *service);
54     virtual void Close(ConnectionID connectionID);
55     virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer);
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         InterfaceID interfaceID;
80         GenericSocketService *service;
81         time_t timeout;
82         RawBuffer rawBuffer;
83         int counter;
84
85         SocketDescription()
86           : isListen(false)
87           , isOpen(false)
88           , isTimeout(false)
89           , interfaceID(-1)
90           , service(NULL)
91         {}
92     };
93
94     SocketDescription& CreateDefaultReadSocketDescription(int sock, bool timeout);
95
96     typedef std::vector<SocketDescription> SocketDescriptionVector;
97
98     struct WriteBuffer {
99         ConnectionID connectionID;
100         RawBuffer rawBuffer;
101     };
102
103     struct Timeout {
104         time_t time;
105         int sock;
106         bool operator<(const Timeout &second) const {
107             return time > second.time; // mininum first!
108         }
109     };
110
111     SocketDescriptionVector m_socketDescriptionVector;
112     fd_set m_readSet;
113     fd_set m_writeSet;
114     int m_maxDesc;
115     bool m_working;
116     std::mutex m_eventQueueMutex;
117     std::queue<WriteBuffer> m_writeBufferQueue;
118     std::queue<ConnectionID> m_closeQueue;
119     int m_notifyMe[2];
120     int m_counter;
121     std::priority_queue<Timeout> m_timeoutQueue;
122 };
123
124 } // namespace CKM
125
126 #endif // _CENT_KEY_MNG_SOCKET_MANAGER_