modify logs
[platform/core/security/privacy-manager.git] / common / inc / SocketConnection.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 _SOCKET_CONNECTION_H_
18 #define _SOCKET_CONNECTION_H_
19
20 #include <dlog.h>
21 #include <new>
22 #include <list>
23 #include <utility>
24 #include <SocketStream.h>
25 #include <PrivacyManagerTypes.h>
26 #include <iostream>
27 #include <Utils.h>
28 /*
29  * This class implements interface for generic read and write from given socket.
30  * It does not maintain socket descriptor, so any connecting and disconnecting should be
31  */
32
33 /*
34  * Throws SocketConnectionException when read/write will not succeed or if any bad allocation
35  * exception occurs during read.
36  */
37
38 class EXTERN_API SocketConnection
39 {
40
41 public:
42
43         explicit SocketConnection(int socket_fd) : m_socketStream(socket_fd){
44                 LOGI("Created");
45         }
46
47         template<typename T, typename ...Args>
48         int read(T* out, const Args&... args )
49         {
50                 int res = read(out);
51                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
52                 res = read(args...);
53                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
54
55                 return PRIV_MGR_ERROR_SUCCESS;
56         }
57
58         template<typename T>
59         int read(T* out)
60         {
61                 int length = 0;
62                 int res = m_socketStream.readStream(sizeof(length), &length);
63                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "readStream : %d", res);
64                 char* pBuf = new (std::nothrow) char[length + 1];
65                 TryReturn(pBuf != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_MGR_ERROR_OUT_OF_MEMORY);
66
67                 res = m_socketStream.readStream(length, pBuf);
68                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
69
70                 pBuf[length] = 0;
71
72                 out = T(pBuf);
73
74                 delete[] pBuf;
75
76                 return PRIV_MGR_ERROR_SUCCESS;
77         }
78         
79         int read(bool* pB)
80         {
81                 int length = 0;
82                 int res = m_socketStream.readStream(sizeof(length), &length);
83                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "readStream : %d", res);
84
85                 char* pBuf = new (std::nothrow) char[length + 1];
86                 TryReturn(pBuf != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_MGR_ERROR_OUT_OF_MEMORY);
87
88                 res = m_socketStream.readStream(length, pBuf);
89                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
90
91                 pBuf[length] = 0;
92
93                 *pB = * reinterpret_cast <bool* > (pBuf);
94
95                 delete[] pBuf;
96
97                 return PRIV_MGR_ERROR_SUCCESS;
98         }
99
100         int read(bool& b)
101         {
102                 return read(&b);
103         }
104         
105         int read(int& i)
106         {
107                 return read(&i);
108         }
109
110         int read(int* pI)
111         {
112                 int length = 0;
113                 int res = m_socketStream.readStream(sizeof(length), &length);
114                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "readStream : %d", res);
115
116                 char* pBuf = new (std::nothrow) char[length + 1];
117                 TryReturn(pBuf != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_MGR_ERROR_OUT_OF_MEMORY);
118
119                 res = m_socketStream.readStream(length, pBuf);
120                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
121
122                 pBuf[length] = 0;
123
124                 *pI = * reinterpret_cast <int* > (pBuf);
125
126                 delete[] pBuf;
127
128                 return PRIV_MGR_ERROR_SUCCESS;
129         }
130
131         int read(std::string* pStr)
132         {
133                 int length = 0;
134                 int res = m_socketStream.readStream(sizeof(length), &length);
135                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "readStream : %d", res);
136
137                 char* pBuf = new (std::nothrow) char[length + 1];
138                 TryReturn(pBuf != NULL, PRIV_MGR_ERROR_OUT_OF_MEMORY, , "new : %d", PRIV_MGR_ERROR_OUT_OF_MEMORY);
139
140                 m_socketStream.readStream(length, pBuf);
141                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, delete[] pBuf, "readStream : %d", res);
142
143                 pBuf[length] = 0;
144
145                 *pStr = std::string(pBuf);
146                 delete[] pBuf;
147
148                 return PRIV_MGR_ERROR_SUCCESS;
149         }
150         
151         int read(std::string& str)
152         {
153                 return read(&str);
154         }
155
156         template < typename T >
157         int  read (std::list<T>& list)
158         {
159                 int length;
160                 int res = read(length);
161                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
162
163                 for (int i = 0; i < length; ++i) 
164                 {
165                         T obj;
166                         res = read (obj);
167                         TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
168                         list.push_back(obj);
169                 }
170
171                 return PRIV_MGR_ERROR_SUCCESS;
172         }
173
174         template < typename T >
175         int read (std::list<T>* pList)
176         {
177                 return read(*pList);
178         }
179
180         template < typename K, typename P >
181         void read (std::pair<K, P>& pair)
182         {
183                 int res = read( pair.first);
184                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
185                 res = read( pair.second);
186                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "read : %d", res);
187
188                 return PRIV_MGR_ERROR_SUCCESS;
189         }
190
191         template < typename K, typename P >
192         int read (std::pair<K, P>* pPair)
193         {
194                 return read( *pPair);
195         }
196
197         template<typename T, typename ...Args>
198         int write(const T& in, const Args&... args)
199         {
200                 int res = write(in);
201                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
202                 res = write(args...);
203                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
204
205                 return PRIV_MGR_ERROR_SUCCESS;
206         }
207
208         int write(const std::string& in)
209         {
210                 int length = in.size();
211                 int res = m_socketStream.writeStream(sizeof(length), &length);
212                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
213                 res = m_socketStream.writeStream(length, in.c_str());
214                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
215
216                 return PRIV_MGR_ERROR_SUCCESS;
217         }
218         
219         int write(const unsigned int& in)
220         {
221                 int length = sizeof(in);
222                 int res = m_socketStream.writeStream(sizeof(length), &length);
223                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
224                 res = m_socketStream.writeStream(length, &in);
225                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
226
227                 return PRIV_MGR_ERROR_SUCCESS;
228         }
229
230         int write(const int& in)
231         {
232                 int length = sizeof(in);
233                 int res = m_socketStream.writeStream(sizeof(length), &length);
234                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
235                 res = m_socketStream.writeStream(length, &in);
236                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
237
238                 return PRIV_MGR_ERROR_SUCCESS;
239         }
240
241         int write(const bool& in)
242         {
243                 int length = sizeof(in);
244                 int res = m_socketStream.writeStream(sizeof(length), &length);
245                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
246                 res = m_socketStream.writeStream(length, &in);
247                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
248
249                 return PRIV_MGR_ERROR_SUCCESS;
250         }
251
252         int write(const char*& in)
253         {
254                 int length = strlen(in);
255                 int res = m_socketStream.writeStream(sizeof(length), &length);
256                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
257                 res = m_socketStream.writeStream(length, in);
258                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "writeStream : %d", res);
259
260                 return PRIV_MGR_ERROR_SUCCESS;
261         }
262
263         template<typename T, typename ...Args>
264         int write(const T* in, const Args&... args)
265         {
266                 int res = write(in);
267                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
268                 res = write(args...);
269                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
270
271                 return PRIV_MGR_ERROR_SUCCESS;
272         }
273
274         template<typename K, typename T>
275         int write(const std::pair<K, T> p)
276         {
277                 int res = write(p.first);
278                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
279                 res = write(p.second);
280                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
281
282                 return PRIV_MGR_ERROR_SUCCESS;
283         }
284
285         template<typename K, typename T>
286         int write(const std::pair<K, T&> p)
287         {
288                 int res = write(p.first);
289                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
290                 res = write(p.second);
291                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
292
293                 return PRIV_MGR_ERROR_SUCCESS;
294         }
295
296         template<typename K, typename T>
297         int write(const std::pair<K, T&>* pPair)
298         {
299                 return write(*pPair);
300         }
301
302         template<typename T>
303         int write(const std::list <T> list)
304         {
305                 int length = list.size();
306                 int res = write(length);
307                 TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
308                 for (typename std::list <T>::const_iterator iter = list.begin(); iter != list.end(); iter++) {
309                         res = write(*iter);
310                         TryReturn(res == PRIV_MGR_ERROR_SUCCESS, res, , "write : %d", res);
311                 }
312
313                 return PRIV_MGR_ERROR_SUCCESS;
314         }
315
316         template<typename T>
317         int write(const std::list <T>* pList)
318         {
319                 return write(*pList);
320         }
321
322
323 private:
324         SocketStream m_socketStream;
325 };
326
327 #endif // _SOCKET_CONNECTION_H_