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_RuntimeClient.cpp
19 * @brief This is the implementation file for _RuntimeClient class.
22 #include <unique_ptr.h>
24 #include <FBaseInteger.h>
25 #include <FBaseLongLong.h>
26 #include <FBaseSysLog.h>
28 #include <FApp_AppInfo.h>
30 #include <FIo_AppServiceIpcMessages.h>
31 #include "FSys_CommunicationDispatcherClient.h"
32 #include "FSys_RuntimeClient.h"
36 using namespace Tizen::App;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Io;
42 namespace Tizen { namespace System
45 static const int _RUNTIME_GET_PARAM_TYPE = 1;
46 static const wchar_t* _RUNTIME_SERVICE_ID = L"osp.system.service";
47 static const wchar_t* _RUNTIME_GET_SIZE = L"osp.system.command.runtime.get.size";
48 static const wchar_t* _RUNTIME_RESULT_SUCCESS = L"osp.system.result.success";
49 static const wchar_t* _RUNTIME_RESULT_SYSTEM = L"osp.system.result.system";
51 _RuntimeClient* _RuntimeClient::__pRuntimeClient= null;
53 class _RuntimeAsyncEventArg : public IEventArg
60 class _RuntimeAsyncEvent : public Event
63 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
65 IRuntimeInfoGetLonglongAsyncResultListener* pListener = dynamic_cast<IRuntimeInfoGetLonglongAsyncResultListener*> (&listener);
66 const _RuntimeAsyncEventArg* pArg = dynamic_cast<const _RuntimeAsyncEventArg*>(&arg);
68 if(pListener == null || pArg == null)
70 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listener or arguemnt");
73 pListener->OnResultReceivedForGetValueAsync(pArg->value, pArg->rCode);
74 RemoveListener(listener);
79 _RuntimeClient::GetInstance(void)
81 if(__pRuntimeClient == null)
83 __pRuntimeClient = new (std::nothrow) _RuntimeClient();
85 return __pRuntimeClient;
88 _RuntimeClient::_RuntimeClient()
93 static String RUNTIME_SERVICE_ID(_RUNTIME_SERVICE_ID);
95 _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
96 SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
98 r = pCommunicationDispatcherClient->RegisterCommunicationListener(RUNTIME_SERVICE_ID, *this);
99 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
101 __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
103 __asyncEventList.Construct();
108 _RuntimeClient::~_RuntimeClient()
110 result r = E_SUCCESS;
111 String key(_RUNTIME_SERVICE_ID);
112 _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
113 SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
115 r = pCommunicationDispatcherClient->UnregisterCommunicationListener(key);
116 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
118 __asyncEventList.RemoveAll(true);
125 _RuntimeClient::GetDirectorySizeValueAsync(const String& path, IRuntimeInfoGetLonglongAsyncResultListener* listener)
127 result r = E_SUCCESS;
129 ArrayList requestMessages;
130 ArrayList responseMessages;
132 requestMessages.Construct();
133 responseMessages.Construct();
135 String serviceId(_RUNTIME_SERVICE_ID);
136 String commandId(_RUNTIME_GET_SIZE);
138 messageId.Append(__msgCount);
140 requestMessages.Add(serviceId);
141 requestMessages.Add(commandId);
142 requestMessages.Add(path);
143 requestMessages.Add(messageId);
145 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
146 r = __pIpcClient->SendRequest(*pMsg);
147 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
149 String* pResult = (String*)responseMessages.GetAt(2);
150 SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
151 SysLog(NID_SYS, "Result is %ls.", pResult->GetPointer());
153 if(*pResult == _RUNTIME_RESULT_SUCCESS)
158 SysLog(NID_SYS, "r is %s.", GetErrorMessage(r));
162 _RuntimeAsyncEvent* pEvent = new (std::nothrow) _RuntimeAsyncEvent();
163 pEvent->AddListener(*listener);
165 __asyncEventList.Add(new Integer(__msgCount), pEvent);
172 _RuntimeClient::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
174 SysLog(NID_SYS, "Receive result");
175 String* pServiceId = (String*)(data.GetAt(0));
176 String* pResponseId = (String*)(data.GetAt(1));
177 String* pMessageId = (String*)(data.GetAt(2));
178 String* pValue = (String*)(data.GetAt(3));
179 String* pResultCode = (String*)(data.GetAt(4));
181 SysTryReturnVoidResult(NID_SYS, pServiceId != null && pResponseId != null && pMessageId != null && pValue != null && pResultCode != null, E_SYSTEM, "There is no result data.");
184 Integer::Parse(*pMessageId, msg_id);
186 Integer msgKey(msg_id);
188 _RuntimeAsyncEvent* pEvent = dynamic_cast <_RuntimeAsyncEvent*> (__asyncEventList.GetValue(msgKey));
189 SysTryReturnVoidResult(NID_SYS, pEvent != null, E_SYSTEM, "There is no registered event.");
191 _RuntimeAsyncEventArg* pEventArg = new (std::nothrow) _RuntimeAsyncEventArg();
193 LongLong::Parse(*pValue, pEventArg->value);
195 if(*pResultCode == _RUNTIME_RESULT_SUCCESS)
197 pEventArg->rCode = E_SUCCESS;
201 pEventArg->rCode = E_SYSTEM;
204 pEvent->Fire(*pEventArg);