2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FSys_RuntimeClient.cpp
20 * @brief This is the implementation file for _RuntimeClient class.
23 #include <unique_ptr.h>
25 #include <FBaseInteger.h>
26 #include <FBaseLongLong.h>
27 #include <FBaseSysLog.h>
29 #include <FApp_AppInfo.h>
31 #include <FIo_AppServiceIpcMessages.h>
32 #include "FSys_CommunicationDispatcherClient.h"
33 #include "FSys_RuntimeClient.h"
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Runtime;
41 using namespace Tizen::Io;
43 namespace Tizen { namespace System
46 static const int _RUNTIME_GET_PARAM_TYPE = 1;
47 static const wchar_t* _RUNTIME_SERVICE_ID = L"osp.system.service";
48 static const wchar_t* _RUNTIME_GET_SIZE = L"osp.system.command.runtime.get.size";
49 static const wchar_t* _RUNTIME_RESULT_SUCCESS = L"osp.system.result.success";
50 static const wchar_t* _RUNTIME_RESULT_SYSTEM = L"osp.system.result.system";
52 _RuntimeClient* _RuntimeClient::__pRuntimeClient= null;
54 class _RuntimeAsyncEventArg : public IEventArg
61 class _RuntimeAsyncEvent : public Event
64 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
66 IRuntimeInfoGetLonglongAsyncResultListener* pListener = dynamic_cast<IRuntimeInfoGetLonglongAsyncResultListener*> (&listener);
67 const _RuntimeAsyncEventArg* pArg = dynamic_cast<const _RuntimeAsyncEventArg*>(&arg);
69 if(pListener == null || pArg == null)
71 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listener or arguemnt");
74 pListener->OnResultReceivedForGetValueAsync(pArg->value, pArg->rCode);
75 RemoveListener(listener);
80 _RuntimeClient::GetInstance(void)
82 if(__pRuntimeClient == null)
84 __pRuntimeClient = new (std::nothrow) _RuntimeClient();
86 return __pRuntimeClient;
89 _RuntimeClient::_RuntimeClient()
94 static String RUNTIME_SERVICE_ID(_RUNTIME_SERVICE_ID);
96 _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
97 SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
99 r = pCommunicationDispatcherClient->RegisterCommunicationListener(RUNTIME_SERVICE_ID, *this);
100 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
102 __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
104 __asyncEventList.Construct();
109 _RuntimeClient::~_RuntimeClient()
111 result r = E_SUCCESS;
112 String key(_RUNTIME_SERVICE_ID);
113 _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
114 SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
116 r = pCommunicationDispatcherClient->UnregisterCommunicationListener(key);
117 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
119 __asyncEventList.RemoveAll(true);
126 _RuntimeClient::GetDirectorySizeValueAsync(const String& path, IRuntimeInfoGetLonglongAsyncResultListener* listener)
128 result r = E_SUCCESS;
130 ArrayList requestMessages;
131 ArrayList responseMessages;
133 requestMessages.Construct();
134 responseMessages.Construct();
136 String serviceId(_RUNTIME_SERVICE_ID);
137 String commandId(_RUNTIME_GET_SIZE);
139 messageId.Append(__msgCount);
141 requestMessages.Add(serviceId);
142 requestMessages.Add(commandId);
143 requestMessages.Add(path);
144 requestMessages.Add(messageId);
146 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
147 r = __pIpcClient->SendRequest(*pMsg);
148 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
150 String* pResult = (String*)responseMessages.GetAt(2);
151 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
152 SysLog(NID_SYS, "Result is %ls.", pResult->GetPointer());
154 if(*pResult == _RUNTIME_RESULT_SUCCESS)
159 SysLog(NID_SYS, "r is %s.", GetErrorMessage(r));
163 _RuntimeAsyncEvent* pEvent = new (std::nothrow) _RuntimeAsyncEvent();
164 pEvent->AddListener(*listener);
166 __asyncEventList.Add(new Integer(__msgCount), pEvent);
173 _RuntimeClient::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
175 SysLog(NID_SYS, "Receive result");
176 String* pServiceId = (String*)(data.GetAt(0));
177 String* pResponseId = (String*)(data.GetAt(1));
178 String* pMessageId = (String*)(data.GetAt(2));
179 String* pValue = (String*)(data.GetAt(3));
180 String* pResultCode = (String*)(data.GetAt(4));
182 SysTryReturnVoidResult(NID_SYS, pServiceId != null && pResponseId != null && pMessageId != null && pValue != null && pResultCode != null, E_SYSTEM, "There is no result data.");
185 Integer::Parse(*pMessageId, msg_id);
187 Integer msgKey(msg_id);
189 _RuntimeAsyncEvent* pEvent = dynamic_cast <_RuntimeAsyncEvent*> (__asyncEventList.GetValue(msgKey));
190 SysTryReturnVoidResult(NID_SYS, pEvent != null, E_SYSTEM, "There is no registered event.");
192 _RuntimeAsyncEventArg* pEventArg = new (std::nothrow) _RuntimeAsyncEventArg();
194 LongLong::Parse(*pValue, pEventArg->value);
196 if(*pResultCode == _RUNTIME_RESULT_SUCCESS)
198 pEventArg->rCode = E_SUCCESS;
202 pEventArg->rCode = E_SYSTEM;
205 pEvent->Fire(*pEventArg);