tizen 2.4 release
[framework/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 <vasum.h>
36
37 #include <dpl/exception.h>
38 #include <generic-socket-manager.h>
39
40 namespace CKM {
41
42 class SocketManager : public GenericSocketManager {
43 public:
44     class Exception {
45     public:
46         DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
47         DECLARE_EXCEPTION_TYPE(Base, InitFailed)
48         DECLARE_EXCEPTION_TYPE(Base, GetSystemdSocketFailed)
49     };
50     SocketManager();
51     virtual ~SocketManager();
52     virtual void MainLoop();
53     virtual void MainLoopStop();
54
55     virtual void RegisterSocketService(GenericSocketService *service);
56     virtual void Close(ConnectionID connectionID);
57     virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer);
58
59 protected:
60     void CreateDomainSocket(
61         GenericSocketService *service,
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     vsm_context_h m_vsmCtx;
123 };
124
125 } // namespace CKM
126
127 #endif // _CENT_KEY_MNG_SOCKET_MANAGER_