Fix PackageParser for bug
[platform/framework/native/appfw.git] / src / system / FSys_RuntimeClient.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FSys_RuntimeClient.cpp
19  * @brief               This is the implementation file for _RuntimeClient class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <FBaseInteger.h>
25 #include <FBaseLongLong.h>
26 #include <FBaseSysLog.h>
27
28 #include <FApp_AppInfo.h>
29
30 #include <FIo_AppServiceIpcMessages.h>
31 #include "FSys_SystemServiceMessageClient.h"
32 #include "FSys_RuntimeClient.h"
33
34 using namespace std;
35
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;
41
42 namespace Tizen { namespace System
43 {
44
45 static const int _RUNTIME_GET_PARAM_TYPE = 1;
46 static const wchar_t* _RUNTIME_SERVICE_ID = L"osp.sys.ipcserver.runtimeinfo";
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";
50
51 _RuntimeClient* _RuntimeClient::__pRuntimeClient= null;
52
53 class _RuntimeAsyncEventArg : public IEventArg
54 {
55 public:
56         long long value;
57         result rCode;
58 };
59
60 class _RuntimeAsyncEvent : public Event
61 {
62 protected:
63         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
64         {
65                 IRuntimeInfoGetLonglongAsyncResultListener* pListener = dynamic_cast<IRuntimeInfoGetLonglongAsyncResultListener*> (&listener);
66                 const _RuntimeAsyncEventArg* pArg = dynamic_cast<const _RuntimeAsyncEventArg*>(&arg);
67
68                 if(pListener == null || pArg == null)
69                 {
70                         SysLogException(NID_SYS, E_SYSTEM, "It is failed to get listener or arguemnt");
71                         return;
72                 }
73                 pListener->OnResultReceivedForGetValueAsync(pArg->value, pArg->rCode);
74                 RemoveListener(listener);
75         }
76 };
77
78 _RuntimeClient*
79 _RuntimeClient::GetInstance(void)
80 {
81         if(__pRuntimeClient == null)
82         {
83                 __pRuntimeClient = new (std::nothrow) _RuntimeClient();
84         }
85         return __pRuntimeClient;
86 }
87
88 _RuntimeClient::_RuntimeClient()
89         : __msgCount(0)
90         , __pMessageClient(null)
91 {
92         result r = E_SUCCESS;
93         static String RUNTIME_SERVICE_ID(_RUNTIME_SERVICE_ID);
94
95         __pMessageClient = _SystemServiceMessageClient::CreateInstance(RUNTIME_SERVICE_ID);
96         SysTryCatch(NID_SYS, __pMessageClient != null, r = E_SYSTEM, r, "It is failed to create SystemServiceMessageClient.");
97
98         r = __pMessageClient->RegisterListener(RUNTIME_SERVICE_ID, *this);
99         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on MessageClient.");
100
101         __asyncEventList.Construct();
102 CATCH:
103         SetLastResult(r);
104 }
105
106 _RuntimeClient::~_RuntimeClient()
107 {
108         result r = E_SUCCESS;
109         String key(_RUNTIME_SERVICE_ID);
110         r = __pMessageClient->UnregisterListener(key);
111         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on MessageClient.");
112
113         __asyncEventList.RemoveAll(true);
114         delete __pMessageClient;
115 CATCH:
116         SetLastResult(r);
117 }
118
119 result
120 _RuntimeClient::GetDirectorySizeValueAsync(const String& path, IRuntimeInfoGetLonglongAsyncResultListener* listener)
121 {
122         result r = E_SUCCESS;
123
124         ArrayList requestMessages;
125         ArrayList responseMessages;
126
127         requestMessages.Construct();
128         responseMessages.Construct();
129
130         String serviceId(_RUNTIME_SERVICE_ID);
131         String commandId(_RUNTIME_GET_SIZE);
132         String messageId;
133         messageId.Append(__msgCount);
134
135         requestMessages.Add(serviceId);
136         requestMessages.Add(commandId);
137         requestMessages.Add(path);
138         requestMessages.Add(messageId);
139
140         unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
141         r = __pMessageClient->GetIpcClient()->SendRequest(*pMsg);
142         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send request by IPC [%s]", GetErrorMessage(r));
143
144         String* pResult = (String*)responseMessages.GetAt(2);
145         SysTryReturnResult(NID_SYS, pResult != null, E_SYSTEM, "There is no result code.");
146
147         if(*pResult == _RUNTIME_RESULT_SUCCESS)
148                 r = E_SUCCESS;
149         else
150                 r = E_SYSTEM;
151
152         SysLog(NID_SYS, "r is %s.", GetErrorMessage(r));
153
154         if(r == E_SUCCESS)
155         {
156                 _RuntimeAsyncEvent* pEvent = new (std::nothrow) _RuntimeAsyncEvent();
157                 pEvent->AddListener(*listener);
158
159                 __asyncEventList.Add(new Integer(__msgCount), pEvent);
160
161                 __msgCount++;
162         }
163         return r;
164 }
165
166 void
167 _RuntimeClient::OnDataReceived(const Tizen::Base::Collection::ArrayList& data)
168 {
169         SysLog(NID_SYS, "Receive result");
170         String* pServiceId = (String*)(data.GetAt(0));
171         String* pResponseId = (String*)(data.GetAt(1));
172         String* pMessageId = (String*)(data.GetAt(2));
173         String* pValue = (String*)(data.GetAt(3));
174         String* pResultCode = (String*)(data.GetAt(4));
175
176         SysTryReturnVoidResult(NID_SYS, pServiceId != null && pResponseId != null && pMessageId != null && pValue != null && pResultCode != null, E_SYSTEM, "There is no result data.");
177
178         int msg_id = 0;
179         Integer::Parse(*pMessageId, msg_id);
180
181         Integer msgKey(msg_id);
182
183         _RuntimeAsyncEvent* pEvent = dynamic_cast <_RuntimeAsyncEvent*> (__asyncEventList.GetValue(msgKey));
184         SysTryReturnVoidResult(NID_SYS, pEvent != null, E_SYSTEM, "There is no registered event.");
185
186         _RuntimeAsyncEventArg* pEventArg = new (std::nothrow) _RuntimeAsyncEventArg();
187
188         LongLong::Parse(*pValue, pEventArg->value);
189
190         if(*pResultCode == _RUNTIME_RESULT_SUCCESS)
191         {
192                 pEventArg->rCode = E_SUCCESS;
193         }
194         else
195         {
196                 pEventArg->rCode = E_SYSTEM;
197         }
198
199         pEvent->Fire(*pEventArg);
200 }
201
202 } } // Tizen::System
203