Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / system / FSys_CommunicationDispatcherClient.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 /**
18  * @file                FSys_CommunicationDispatcherClient.cpp
19  * @brief               This is the implementation file for _CommunicationDispatcherClient class.
20  */
21
22 #include <FBaseSysLog.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <FIo_AppServiceIpcMessages.h>
26
27 #include "FSys_CommunicationDispatcherClient.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Io;
32
33 namespace Tizen { namespace System
34 {
35
36 bool ipcReady = false;
37 static const wchar_t* _COMMUNICATION_DISPATCHER_IPC_ID = L"osp.app.ipcserver.communicationdispatcher";
38
39 _CommunicationDispatcherClient* _CommunicationDispatcherClient::__pCommunicationDispatcherClient = null;
40
41 _CommunicationDispatcherClient::_CommunicationDispatcherClient()
42 : __pIpcClient(null)
43 {
44         __communicationDispatcherClientList.Construct(0, 0, __strHashCodeProvider, __strComparer);
45
46         result r = E_SUCCESS;
47
48         if(getuid() != 0)
49         {
50                 __pIpcClient = new (std::nothrow) _IpcClient();
51                 SysTryCatch(NID_SYS, __pIpcClient != null, , r, "It is failed to create IPC client");
52
53                 r = __pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID, this);
54                 if(r != E_SUCCESS)
55                         ipcReady = false;
56                 else
57                         ipcReady = true;
58         SysTryCatch(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
59         }
60
61 CATCH:
62         SetLastResult(r);
63
64 }
65
66 _CommunicationDispatcherClient::~_CommunicationDispatcherClient()
67 {
68         delete __pIpcClient;
69         __pIpcClient = null;
70
71         __communicationDispatcherClientList.RemoveAll();
72 }
73
74 _CommunicationDispatcherClient*
75 _CommunicationDispatcherClient::GetInstance()
76 {
77         if(__pCommunicationDispatcherClient == null)
78         {
79                 __pCommunicationDispatcherClient = new (std::nothrow) _CommunicationDispatcherClient();
80         }
81         return __pCommunicationDispatcherClient;
82 }
83
84 _IpcClient*
85 _CommunicationDispatcherClient::GetIpcClient()
86 {
87         if(ipcReady == false)
88                 return null;
89
90         return __pIpcClient;
91 }
92
93 result
94 _CommunicationDispatcherClient::RegisterCommunicationListener(String key, _ICommunicationDispatcherListener& listener)
95 {
96         result r = E_SUCCESS;
97         bool exist = false;
98
99         r = __communicationDispatcherClientList.ContainsKey(key, exist);
100         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
101         SysTryReturnResult(NID_SYS, exist == false, E_OBJ_ALREADY_EXIST, "Required key is already registered.");
102         r = __communicationDispatcherClientList.Add(key, &listener);
103
104         return r;
105 }
106
107 result
108 _CommunicationDispatcherClient::UnregisterCommunicationListener(String key)
109 {
110         result r = E_SUCCESS;
111         bool exist = false;
112         r = __communicationDispatcherClientList.ContainsKey(key, exist);
113         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
114         SysTryReturnResult(NID_SYS, exist == true, E_OBJ_NOT_FOUND, "Required key is not registered.");
115         r = __communicationDispatcherClientList.Remove(key);
116         return r;
117 }
118
119 void
120 _CommunicationDispatcherClient::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
121 {
122         SysLog(NID_SYS, "Enter.");
123         IPC_BEGIN_MESSAGE_MAP(_CommunicationDispatcherClient, message)
124         IPC_MESSAGE_HANDLER_EX(IoService_Data, &client, OnDataReceived)
125         IPC_END_MESSAGE_MAP_EX()
126         SysLog(NID_SYS, "Exit.");
127 }
128
129 void
130 _CommunicationDispatcherClient::OnDataReceived(const ArrayList& data)
131 {
132         result r = E_SUCCESS;
133         String* pServiceId = (String*) data.GetAt(0);
134
135          _ICommunicationDispatcherListener* listener = null;
136
137         SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service id.");
138
139         r = __communicationDispatcherClientList.GetValue(*pServiceId, listener);
140
141         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS && listener != null, E_SYSTEM, "Service[%ls] is available, but listener does not exist. [%s]", pServiceId->GetPointer(), GetErrorMessage(r));
142
143         listener->OnDataReceived(data);
144
145         SysLog(NID_SYS, "Message is delivered to \"%ls\"[%x]", pServiceId->GetPointer(), listener);
146         ArrayList* temp = const_cast< ArrayList *> (&data);
147         temp->RemoveAll(true);
148 }
149
150 }}