Initial commit
[platform/core/security/key-manager.git] / src / manager / main / generic-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        generic-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       Implementation of GenericSocketService and GenericSocketManager.
24  */
25
26 #ifndef _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_
27 #define _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_
28
29 #include <vector>
30 #include <string>
31
32 #include <dpl/exception.h>
33
34 #include <generic-event.h>
35
36 extern "C" {
37 struct msghdr;
38 } // extern "C"
39
40 namespace CentralKeyManager {
41
42 typedef int InterfaceID;
43
44 struct ConnectionID {
45     int sock;                                 // This is decriptor used for connection
46     int counter;                              // Unique handler per socket
47     inline bool operator<(const ConnectionID &second) const {
48         return counter < second.counter;
49     }
50 };
51
52 typedef std::vector<unsigned char> RawBuffer;
53
54 struct GenericSocketManager;
55
56 struct GenericSocketService {
57     typedef std::string SmackLabel;
58     typedef std::string ServiceHandlerPath;
59     struct ServiceDescription {
60         ServiceDescription(const char *path,
61             const char *smackLabel,
62             InterfaceID interfaceID = 0,
63             bool useSendMsg = false)
64           : smackLabel(smackLabel)
65           , interfaceID(interfaceID)
66           , serviceHandlerPath(path)
67           , useSendMsg(useSendMsg)
68         {}
69
70         SmackLabel smackLabel;                 // Smack label for socket
71         InterfaceID interfaceID;               // All data from serviceHandlerPath will be marked with this interfaceHandler
72         ServiceHandlerPath serviceHandlerPath; // Path to file
73         bool useSendMsg;
74     };
75
76     typedef std::vector<ServiceDescription> ServiceDescriptionVector;
77
78     struct AcceptEvent : public GenericEvent {
79         ConnectionID connectionID;
80         InterfaceID interfaceID;
81     };
82
83     struct WriteEvent : public GenericEvent {
84         ConnectionID connectionID;
85         size_t size;
86         size_t left;
87     };
88
89     struct ReadEvent : public GenericEvent {
90         ConnectionID connectionID;
91         RawBuffer rawBuffer;
92     };
93
94     struct CloseEvent : public GenericEvent {
95         ConnectionID connectionID;
96     };
97
98     virtual void SetSocketManager(GenericSocketManager *manager) {
99         m_serviceManager = manager;
100     }
101
102     virtual ServiceDescriptionVector GetServiceDescription() = 0;
103     virtual void Event(const AcceptEvent &event) = 0;
104     virtual void Event(const WriteEvent &event) = 0;
105     virtual void Event(const ReadEvent &event) = 0;
106     virtual void Event(const CloseEvent &event) = 0;
107
108     GenericSocketService() : m_serviceManager(NULL) {}
109     virtual ~GenericSocketService(){}
110 protected:
111     GenericSocketManager *m_serviceManager;
112 };
113
114 struct GenericSocketManager {
115     virtual void MainLoop() = 0;
116     virtual void RegisterSocketService(GenericSocketService *ptr) = 0;
117     virtual void Close(ConnectionID connectionID) = 0;
118     virtual void Write(ConnectionID connectionID, const RawBuffer &rawBuffer) = 0;
119     virtual ~GenericSocketManager(){}
120 };
121
122 } // namespace CentralKeyManager
123
124 #endif // _CENT_KEY_MNG_GENERIC_SERVICE_MANAGER_