RSA sync with private
[platform/core/messaging/msg-service.git] / mapi / msg_control.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 #include <errno.h>
18
19 #include "MsgHandle.h"
20 #include "MsgDebug.h"
21 #include "MsgException.h"
22
23 #include "msg_private.h"
24 #include "msg.h"
25
26 /*==================================================================================================
27                                      FUNCTION IMPLEMENTATION
28 ==================================================================================================*/
29 EXPORT_API int msg_open_msg_handle(msg_handle_t *handle)
30 {
31         if (handle == NULL)
32         {
33                 MSG_FATAL("Input Paramter is NULL");
34                 return -EINVAL;
35         }
36         MsgHandle* pHandle = new MsgHandle();
37
38         // Create MsgHandle
39         *handle = (msg_handle_t)pHandle;
40
41         if (*handle == NULL)
42                 return -EINVAL;
43
44         try
45         {
46                 // Connect to Socket
47                 pHandle->openHandle();
48         }
49         catch (MsgException& e)
50         {
51                 MSG_FATAL("%s", e.what());
52
53                 if (e.errorCode() == MsgException::SERVER_READY_ERROR)
54                         return MSG_ERR_SERVER_NOT_READY;
55                 else
56                         return MSG_ERR_COMMUNICATION_ERROR;
57         }
58
59         return MSG_SUCCESS;
60 }
61
62
63 EXPORT_API int msg_close_msg_handle(msg_handle_t *handle)
64 {
65         if (handle == NULL || *handle == NULL)
66         {
67                 MSG_FATAL("Input Paramter is NULL");
68                 return -EINVAL;
69         }
70
71         MsgHandle* pHandle = (MsgHandle*)(*handle);
72
73         try
74         {
75                 // Disconnect to Socket
76                 pHandle->closeHandle(pHandle);
77         }
78         catch (MsgException& e)
79         {
80                 MSG_FATAL("%s", e.what());
81                 return MSG_ERR_COMMUNICATION_ERROR;
82         }
83
84         // Destroy MsgHandle
85         delete (MsgHandle*)(*handle);
86         (*handle) = NULL;
87
88         return MSG_SUCCESS;
89 }
90