IPC: Support older glib
[platform/core/security/vasum.git] / common / ipc / types.cpp
1 /*
2 *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 *  Contact: Jan Olszak <j.olszak@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 /**
20  * @file
21  * @author  Jan Olszak (j.olszak@samsung.com)
22  * @brief   Types definitions and helper functions
23  */
24
25 #include "config.hpp"
26
27 #include "ipc/types.hpp"
28 #include "logger/logger.hpp"
29
30 #include <atomic>
31
32 namespace vasum {
33 namespace ipc {
34
35 namespace {
36 std::atomic<MessageID> gLastMessageID(0);
37 } // namespace
38
39 MessageID getNextMessageID()
40 {
41     return ++gLastMessageID;
42 }
43
44 std::string toString(const Status status)
45 {
46     switch (status) {
47     case Status::OK: return "No error, everything is OK";
48     case Status::PARSING_ERROR: return "Exception during reading/parsing data from the socket";
49     case Status::SERIALIZATION_ERROR: return "Exception during writing/serializing data to the socket";
50     case Status::PEER_DISCONNECTED: return "No such peer. Might got disconnected.";
51     case Status::NAUGHTY_PEER: return "Peer performed a forbidden action.";
52     case Status::REMOVED_PEER: return "Removing peer";
53     case Status::CLOSING: return "Closing IPC";
54     case Status::UNDEFINED: return "Undefined state";
55     default: return "Unknown status";
56     }
57 }
58
59 void throwOnError(const Status status)
60 {
61     if (status == Status::OK) {
62         return;
63     }
64
65     std::string message = toString(status);
66     LOGE(message);
67
68     switch (status) {
69     case Status::PARSING_ERROR: throw IPCParsingException(message);
70     case Status::SERIALIZATION_ERROR: throw IPCSerializationException(message);
71     case Status::PEER_DISCONNECTED: throw IPCPeerDisconnectedException(message);
72     case Status::NAUGHTY_PEER: throw IPCNaughtyPeerException(message);
73     case Status::REMOVED_PEER: throw IPCException(message);
74     case Status::CLOSING: throw IPCException(message);
75     case Status::UNDEFINED: throw IPCException(message);
76     default: return throw IPCException(message);
77     }
78 }
79 } // namespace ipc
80 } // namespace vasum