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_SystemServiceMessageClient.cpp
19 * @brief This is the implementation file for _SystemServiceMessageClient class.
22 #include <sys/types.h>
24 #include <FApp_AppInfo.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseRtThread.h>
27 #include <FIo_AppServiceIpcMessages.h>
28 #include "FSys_SystemServiceMessageClient.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 _SystemServiceMessageClient::_SystemServiceMessageClient()
39 , __pSystemServiceMessageListener(null)
43 _SystemServiceMessageClient::_SystemServiceMessageClient(const String id)
45 , __pSystemServiceMessageListener(null)
48 std::unique_ptr< _IpcClient > pIpcClient(new (std::nothrow) _IpcClient());
49 SysTryReturn(NID_SYS, pIpcClient != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It is failed to create IPC client");
51 r = pIpcClient->Construct(id, this);
52 SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
54 __pIpcClient = std::move(pIpcClient);
57 _SystemServiceMessageClient::~_SystemServiceMessageClient()
61 _SystemServiceMessageClient*
62 _SystemServiceMessageClient::CreateInstance(const Tizen::Base::String id)
64 _SystemServiceMessageClient* pSystemServiceMessageClient = new (std::nothrow) _SystemServiceMessageClient(id);
65 SysTryReturn(NID_SYS, pSystemServiceMessageClient, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It is not enough memory.");
66 return pSystemServiceMessageClient;
70 _SystemServiceMessageClient::GetIpcClient()
72 return __pIpcClient.get();
76 _SystemServiceMessageClient::RegisterListener(const String key, _ICommunicationDispatcherListener& listener)
78 SysTryReturnResult(NID_SYS, !__pSystemServiceMessageListener, E_OBJ_ALREADY_EXIST,
79 "SystemService listener was set already.[%ls]", key.GetPointer());
80 __pSystemServiceMessageListener = &listener;
85 _SystemServiceMessageClient::UnregisterListener(const String key)
87 SysTryReturnResult(NID_SYS, __pSystemServiceMessageListener, E_OBJ_NOT_FOUND,
88 "SystemService listener is not set.[%ls]",key.GetPointer());
89 __pSystemServiceMessageListener = null;
94 _SystemServiceMessageClient::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
96 SysLog(NID_SYS, "Enter.");
97 IPC_BEGIN_MESSAGE_MAP(_SystemServiceMessageClient, message)
98 IPC_MESSAGE_HANDLER_EX(IoService_Data, &client, OnDataReceived)
99 IPC_END_MESSAGE_MAP_EX()
100 SysLog(NID_SYS, "Exit.");
104 _SystemServiceMessageClient::OnDataReceived(const ArrayList& data)
106 result r = E_SUCCESS;
107 String* pServiceId = (String*) data.GetAt(0);
109 SysTryReturnVoidResult(NID_SYS, pServiceId != null, E_SYSTEM, "There is no service id.");
110 SysLog(NID_SYS, "Service id = %ls", pServiceId->GetPointer());
112 SysTryReturnVoidResult(NID_SYS, __pSystemServiceMessageListener, E_SYSTEM,
113 "Service[%ls] is available, but listener does not exist. [%s]", pServiceId->GetPointer(), GetErrorMessage(r));
115 __pSystemServiceMessageListener->OnDataReceived(data);
117 SysLog(NID_SYS, "Message is delivered to \"%ls\"[%x]", pServiceId->GetPointer(), __pSystemServiceMessageListener);
118 ArrayList* temp = const_cast< ArrayList *> (&data);
119 temp->RemoveAll(true);
123 _SystemServiceMessageClient::OnIpcServerDisconnected(_IpcClient& client)
125 SysLogException(NID_SYS, E_SYSTEM, "The IpcServer was disconnected. [%ls].", client.GetName().GetPointer());
126 result r = E_SUCCESS;
127 std::unique_ptr< _IpcClient > pIpcClient(new (std::nothrow) _IpcClient());
128 SysTryReturn(NID_SYS, pIpcClient != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It is failed to create IPC client");
133 r = pIpcClient->Construct(client.GetName(), this);
135 Tizen::Base::Runtime::Thread::Sleep(1000);
136 }while (r != E_SUCCESS && retry < 10);
138 SysTryReturn(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
140 __pIpcClient.reset(pIpcClient.release());