2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FSys_CommunicationDispatcherClient.cpp
19 * @brief This is the implementation file for _CommunicationDispatcherClient class.
22 #include <FApp_AppInfo.h>
23 #include <FBaseSysLog.h>
25 #include <sys/types.h>
26 #include <FIo_AppServiceIpcMessages.h>
28 #include "FSys_CommunicationDispatcherClient.h"
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Io;
35 namespace Tizen { namespace System
37 static const wchar_t* _APPLICATION_ID_APP_SERVICE = L"aospd000043.osp-app-service";
38 bool ipcReady = false;
39 static const wchar_t* _COMMUNICATION_DISPATCHER_IPC_ID = L"osp.app.ipcserver.communicationdispatcher";
41 _CommunicationDispatcherClient* _CommunicationDispatcherClient::__pCommunicationDispatcherClient = null;
43 _CommunicationDispatcherClient::_CommunicationDispatcherClient()
46 __communicationDispatcherClientList.Construct(0, 0, __strHashCodeProvider, __strComparer);
50 if(_AppInfo::GetAppInfo()->GetApplicationId() != _APPLICATION_ID_APP_SERVICE)
52 __pIpcClient = new (std::nothrow) _IpcClient();
53 SysTryCatch(NID_SYS, __pIpcClient != null, , r, "It is failed to create IPC client");
55 r = __pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID, this);
60 SysTryCatch(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
64 SysLogException(NID_SYS, E_SYSTEM, "It is required by osp-app-service.");
72 _CommunicationDispatcherClient::~_CommunicationDispatcherClient()
77 __communicationDispatcherClientList.RemoveAll();
80 _CommunicationDispatcherClient*
81 _CommunicationDispatcherClient::GetInstance()
83 if(__pCommunicationDispatcherClient == null)
85 __pCommunicationDispatcherClient = new (std::nothrow) _CommunicationDispatcherClient();
87 return __pCommunicationDispatcherClient;
91 _CommunicationDispatcherClient::GetIpcClient()
100 _CommunicationDispatcherClient::RegisterCommunicationListener(String key, _ICommunicationDispatcherListener& listener)
102 result r = E_SUCCESS;
105 r = __communicationDispatcherClientList.ContainsKey(key, exist);
106 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
107 SysTryReturnResult(NID_SYS, exist == false, E_OBJ_ALREADY_EXIST, "Required key is already registered.");
108 r = __communicationDispatcherClientList.Add(key, &listener);
114 _CommunicationDispatcherClient::UnregisterCommunicationListener(String key)
116 result r = E_SUCCESS;
118 r = __communicationDispatcherClientList.ContainsKey(key, exist);
119 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check exist check.");
120 SysTryReturnResult(NID_SYS, exist == true, E_OBJ_NOT_FOUND, "Required key is not registered.");
121 r = __communicationDispatcherClientList.Remove(key);
126 _CommunicationDispatcherClient::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
128 SysLog(NID_SYS, "Enter.");
129 IPC_BEGIN_MESSAGE_MAP(_CommunicationDispatcherClient, message)
130 IPC_MESSAGE_HANDLER_EX(IoService_Data, &client, OnDataReceived)
131 IPC_END_MESSAGE_MAP_EX()
132 SysLog(NID_SYS, "Exit.");
136 _CommunicationDispatcherClient::OnDataReceived(const ArrayList& data)
138 result r = E_SUCCESS;
139 String* pServiceId = (String*) data.GetAt(0);
141 _ICommunicationDispatcherListener* listener = null;
143 SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service id.");
145 r = __communicationDispatcherClientList.GetValue(*pServiceId, listener);
147 SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS && listener != null, E_SYSTEM, "Service[%ls] is available, but listener does not exist. [%s]", pServiceId->GetPointer(), GetErrorMessage(r));
149 listener->OnDataReceived(data);
151 SysLog(NID_SYS, "Message is delivered to \"%ls\"[%x]", pServiceId->GetPointer(), listener);
152 ArrayList* temp = const_cast< ArrayList *> (&data);
153 temp->RemoveAll(true);