Merge "Change the way to conver Mbs to Wcs and vice versa" into tizen_2.1
[platform/framework/native/appfw.git] / src / system / FSys_CommunicationDispatcherClient.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSys_CommunicationDispatcherClient.cpp
20  * @brief               This is the implementation file for _CommunicationDispatcherClient class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <FIo_AppServiceIpcMessages.h>
27
28 #include "FSys_CommunicationDispatcherClient.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Io;
33
34 namespace Tizen { namespace System
35 {
36
37 bool ipcReady = false;
38 static const wchar_t* _COMMUNICATION_DISPATCHER_IPC_ID = L"osp.app.ipcserver.communicationdispatcher";
39
40 _CommunicationDispatcherClient* _CommunicationDispatcherClient::__pCommunicationDispatcherClient = null;
41
42 _CommunicationDispatcherClient::_CommunicationDispatcherClient()
43 : __pIpcClient(null)
44 {
45         __communicationDispatcherClientList.Construct(0, 0, __strHashCodeProvider, __strComparer);
46
47         result r = E_SUCCESS;
48
49         if(getuid() != 0)
50         {
51                 __pIpcClient = new (std::nothrow) _IpcClient();
52                 SysTryCatch(NID_SYS, __pIpcClient != null, , r, "It is failed to create IPC client");
53
54                 r = __pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID, this);
55                 if(r != E_SUCCESS)
56                         ipcReady = false;
57                 else
58                         ipcReady = true;
59         SysTryCatch(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
60         }
61
62 CATCH:
63         SetLastResult(r);
64
65 }
66
67 _CommunicationDispatcherClient::~_CommunicationDispatcherClient()
68 {
69         delete __pIpcClient;
70         __pIpcClient = null;
71
72         __communicationDispatcherClientList.RemoveAll();
73 }
74
75 _CommunicationDispatcherClient*
76 _CommunicationDispatcherClient::GetInstance()
77 {
78         if(__pCommunicationDispatcherClient == null)
79         {
80                 __pCommunicationDispatcherClient = new (std::nothrow) _CommunicationDispatcherClient();
81         }
82         return __pCommunicationDispatcherClient;
83 }
84
85 _IpcClient*
86 _CommunicationDispatcherClient::GetIpcClient()
87 {
88         if(ipcReady == false)
89                 return null;
90
91         return __pIpcClient;
92 }
93
94 result
95 _CommunicationDispatcherClient::RegisterCommunicationListener(String key, _ICommunicationDispatcherListener& listener)
96 {
97         result r = E_SUCCESS;
98         bool exist = false;
99
100         r = __communicationDispatcherClientList.ContainsKey(key, exist);
101         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
102         SysTryReturnResult(NID_SYS, exist == false, E_OBJ_ALREADY_EXIST, "Required key is already registered.");
103         r = __communicationDispatcherClientList.Add(key, &listener);
104
105         return r;
106 }
107
108 result
109 _CommunicationDispatcherClient::UnregisterCommunicationListener(String key)
110 {
111         result r = E_SUCCESS;
112         bool exist = false;
113         r = __communicationDispatcherClientList.ContainsKey(key, exist);
114         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
115         SysTryReturnResult(NID_SYS, exist == true, E_OBJ_NOT_FOUND, "Required key is not registered.");
116         r = __communicationDispatcherClientList.Remove(key);
117         return r;
118 }
119
120 void
121 _CommunicationDispatcherClient::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
122 {
123         SysLog(NID_SYS, "Enter.");
124         IPC_BEGIN_MESSAGE_MAP(_CommunicationDispatcherClient, message)
125         IPC_MESSAGE_HANDLER_EX(IoService_Data, &client, OnDataReceived)
126         IPC_END_MESSAGE_MAP_EX()
127         SysLog(NID_SYS, "Exit.");
128 }
129
130 void
131 _CommunicationDispatcherClient::OnDataReceived(const ArrayList& data)
132 {
133         result r = E_SUCCESS;
134         String* pServiceId = (String*) data.GetAt(0);
135
136          _ICommunicationDispatcherListener* listener = null;
137
138         SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service id.");
139
140         r = __communicationDispatcherClientList.GetValue(*pServiceId, listener);
141
142         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS && listener != null, E_SYSTEM, "Service[%ls] is available, but listener does not exist. [%s]", pServiceId->GetPointer(), GetErrorMessage(r));
143
144         listener->OnDataReceived(data);
145
146         SysLog(NID_SYS, "Message is delivered to \"%ls\"[%x]", pServiceId->GetPointer(), listener);
147         ArrayList* temp = const_cast< ArrayList *> (&data);
148         temp->RemoveAll(true);
149 }
150
151 }}