Add Privacy-Guard
[platform/core/security/privacy-guard.git] / server / inc / SocketService.h
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #ifndef _SOCKETSERVICE_H_
18 #define _SOCKETSERVICE_H_
19
20 #include <string>
21 #include <mutex>
22 #include <list>
23 #include <map>
24 #include <memory>
25 #include <pthread.h>
26 #include "SocketConnection.h"
27
28 typedef void(*socketServiceCallback)(SocketConnection* pConnector);
29
30 class SocketService
31 {
32         struct ConnectionInfo{
33                 ConnectionInfo(int fd, void* pData) : connFd(fd), pData(pData) {}
34                 int connFd;
35                 void* pData;
36         };
37         class ServiceCallback
38         {
39         public:
40                 ServiceCallback(socketServiceCallback callback)
41                         : serviceCallback(callback)
42                 {}
43                 socketServiceCallback serviceCallback;
44         };
45         
46 private:
47         static const int MAX_LISTEN;
48         static const int TIMEOUT_SEC;
49         static const int TIMEOUT_NSEC;
50         int m_listenFd;
51         int m_signalToClose;
52         pthread_t m_mainThread;
53
54         typedef std::shared_ptr<ServiceCallback> ServiceCallbackPtr;
55         //Map for callback methods, key is a method name and value is a callback to method
56         typedef std::map<std::string, ServiceCallbackPtr> ServiceMethodCallbackMap;
57         //Map for interface methods, key is an interface name and value is a map of available methods with callbacks
58         std::map <std::string, ServiceMethodCallbackMap > m_callbackMap;
59
60         std::list < int > m_clientSocketList;
61         std::mutex m_clientSocketListMutex;
62
63 private:
64         static void* serverThread(void* );
65         static void* connectionThread(void* pData);
66         int connectionService(int fd);
67         int mainloop(void);
68         void closeConnections(void);
69
70         void addClientSocket(int clientSocket);
71         void removeClientSocket(int clientSocket);
72         bool popClientSocket(int* pClientSocket);
73
74 public:
75         SocketService(void);
76         ~SocketService(void);
77         int initialize(void);
78         int registerServiceCallback(const std::string &interfaceName, const std::string &methodName, socketServiceCallback callbackMethod);
79         int start(void);
80         int stop(void);
81         int shutdown(void);
82 };
83
84 #endif //_SOCKETSERVICE_H_