Separate the metadata
[platform/framework/native/channel-service.git] / src / FIo_ChannelService.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        FIo_ChannelService.cpp
20  * @brief       This is the implementation file for the _ChannelService class.
21  *
22  */
23
24 #include <vector>
25 #include <new>
26 #include <glib.h>
27
28 #include <unique_ptr.h>
29
30 #include <FBaseStringComparer.h>
31 #include <FBaseSysLog.h>
32 #include <FBase_StringConverter.h>
33 #include <FBaseRt_EventDispatcher.h>
34 #include "FIo_ChannelService.h"
35 #include "FIo_ChannelMessages.h"
36 #include "FIo_IChannelServiceStub.h"
37 #include "FIo_ChannelCAppStub.h"
38 #include "FIo_ChannelWebAppStub.h"
39
40 using namespace std;
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Base::Runtime;
44 using namespace Tizen::Io;
45
46
47 namespace Tizen { namespace Io
48 {
49
50 _ChannelService::_ChannelService(void)
51         : __pIChannelServiceStub(null)
52 {
53
54 }
55
56 _ChannelService::~_ChannelService(void)
57 {
58
59 }
60
61 result
62 _ChannelService::Construct(_IChannelServiceStub& stub)
63 {
64         static _StringHashProvider hashProvider;
65         static _StringComparer stringComparer;
66         result r = E_SUCCESS;
67
68         __channels.Construct(0, 0, hashProvider, stringComparer);
69
70         __pIChannelServiceStub = &stub;
71
72         __pIChannelServiceStub->SetChannelService(*this);
73
74         // Create a CAppStub
75         unique_ptr<_ChannelCAppStub> pCStub(new (std::nothrow) _ChannelCAppStub());
76         SysTryReturnResult(NID_IO, pCStub != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
77
78         r = pCStub->Construct();
79         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to create a CAppStub.");
80
81         pCStub->SetChannelService(*this);
82
83         // Create a WebAppStub
84         unique_ptr<_ChannelWebAppStub> pWebStub(new (std::nothrow) _ChannelWebAppStub());
85         SysTryReturnResult(NID_IO, pWebStub != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
86
87         r = pWebStub->Construct();
88         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to create a WebAppStub.");
89
90         pWebStub->SetChannelService(*this);
91
92         pCStub.release();
93         pWebStub.release();
94
95         return E_SUCCESS;
96 }
97
98 result
99 _ChannelService::RegisterChannel(const String& channelId, const _IChannelServiceEventListener& listener)
100 {
101         return E_SUCCESS;
102 }
103
104 result
105 _ChannelService::RegisterChannel(const String& channelId, int clientId, unsigned int type)
106 {
107         result r = E_SUCCESS;
108         _ChannelInfo* pChannelInfo = null;
109
110         SysLog(NID_IO, "Register a channel : %ls.", channelId.GetPointer());
111
112         r = __channels.GetValue(channelId, pChannelInfo);
113         SysTryReturnResult(NID_IO, pChannelInfo == null, E_SYSTEM,  "Channel has already been registered.");
114
115         pChannelInfo = new (std::nothrow) _ChannelInfo;
116         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
117
118         pChannelInfo->channelId = channelId;
119         pChannelInfo->clientId = clientId;
120         pChannelInfo->type = type;
121         pChannelInfo->pGIOChannel = null;
122
123         __channels.Add(channelId, pChannelInfo);
124
125         return E_SUCCESS;
126 }
127
128 result
129 _ChannelService::RegisterChannel(const String& channelId, int clientId, void* pGIOChannel)
130 {
131         result r = E_SUCCESS;
132         _ChannelInfo* pChannelInfo = null;
133
134         SysLog(NID_IO, "Register a channel : [%ls]", channelId.GetPointer());
135
136         r = __channels.GetValue(channelId, pChannelInfo);
137         SysTryReturnResult(NID_IO, pChannelInfo == null, E_SYSTEM, "Channel has already been registered.");
138
139         pChannelInfo = new (std::nothrow) _ChannelInfo;
140         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
141
142         pChannelInfo->channelId = channelId;
143         pChannelInfo->clientId = clientId;
144         pChannelInfo->type = CAPP_CHANNEL;
145         pChannelInfo->pGIOChannel = pGIOChannel;
146
147         __channels.Add(channelId, pChannelInfo);
148
149         return E_SUCCESS;
150 }
151
152 result
153 _ChannelService::UnregisterChannel(const String& channelId)
154 {
155         return E_SUCCESS;
156 }
157
158 result
159 _ChannelService::UnregisterChannel(int clientId)
160 {
161         SysLog(NID_IO, "Unregister - clientId =  %d", clientId);
162
163         result r = E_OBJ_NOT_FOUND;
164
165         String key;
166         _ChannelInfo* pValue = null;
167         IListT<String>* pKeys = __channels.GetKeysN();
168         SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
169
170         int count = __channels.GetCount();
171
172         for (int i = 0; i < count; i++)
173         {
174                 pKeys->GetAt(i, key);
175                 __channels.GetValue(key, pValue);
176                 if (pValue != null && pValue->clientId == clientId)
177                 {
178                         SysLog(NID_IO, "Unregister - ChannelId = %ls", key.GetPointer());
179                         __channels.Remove(key);
180                         delete pValue;
181
182                         r = E_SUCCESS;
183                 }
184         }
185
186         delete pKeys;
187
188         return r;
189 }
190
191 result
192 _ChannelService::SendRequest(const String& src,
193                                                          const String& dest,
194                                                          const ArrayList& args,
195                                                          int requestId)
196 {
197         SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
198
199         _ChannelInfo* pChannelInfo = null;
200
201         __channels.GetValue(dest, pChannelInfo);
202         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
203                                 "Destination channel is not found.");
204
205         // Channel for WebApp
206         if (pChannelInfo->type == WEBAPP_CHANNEL)
207         {
208                 bool ret = _ChannelWebAppStub::SendResponse(pChannelInfo->clientId, args);
209                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a WebApp.");
210         }
211         // Channel for OspApp
212         else
213         {
214                 result r = __pIChannelServiceStub->SendRequest(pChannelInfo->clientId, src, dest, args, requestId);
215                 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the request data.");
216         }
217
218         return E_SUCCESS;
219 }
220
221 result
222 _ChannelService::SendNullRequest(const String& src,
223                                                          const String& dest,
224                                                          int requestId)
225 {
226         SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
227
228         _ChannelInfo* pChannelInfo = null;
229
230         __channels.GetValue(dest, pChannelInfo);
231         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
232                                 "Destination channel is not found.");
233
234         // Channel for WebApp
235         if (pChannelInfo->type == WEBAPP_CHANNEL)
236         {
237                 SysLog(NID_IO, "WebApp Channel, the data are not JSON types");
238                 return E_SYSTEM;
239         }
240         // Channel for OspApp
241         else
242         {
243                 __pIChannelServiceStub->SendNullRequest(pChannelInfo->clientId, src, dest, requestId);
244         }
245
246         return E_SUCCESS;
247 }
248
249 result
250 _ChannelService::SendResponse(const String& src,
251                                                           const String& dest,
252                                                           const ArrayList& args,
253                                                           int requestId)
254 {
255         SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
256
257         _ChannelInfo* pChannelInfo = null;
258
259         __channels.GetValue(dest, pChannelInfo);
260         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
261                                 "Destination channel not found.");
262
263         // Channel for WebApp
264         if (pChannelInfo->type == WEBAPP_CHANNEL)
265         {
266                 bool ret = _ChannelWebAppStub::SendResponse(pChannelInfo->clientId, args);
267                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a WebApp.");
268         }
269         // Channel for CApp
270         else if (pChannelInfo->type == CAPP_CHANNEL)
271         {
272                 bool ret = _ChannelCAppStub::SendResponse(requestId, pChannelInfo->pGIOChannel, args);
273                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a CApp.");
274         }
275         // Channel for OspApp
276         else
277         {
278                 result r = __pIChannelServiceStub->SendResponse(pChannelInfo->clientId, src, dest, args, requestId);
279                 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the response data");
280         }
281
282         return E_SUCCESS;
283 }
284
285 result
286 _ChannelService::SendNullResponse(const String& src,
287                                                           const String& dest,
288                                                           int requestId)
289 {
290         SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
291
292         _ChannelInfo* pChannelInfo = null;
293
294         __channels.GetValue(dest, pChannelInfo);
295         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
296                                 "Destination channel not found.");
297
298         // Channel for WebApp
299         if (pChannelInfo->type == WEBAPP_CHANNEL)
300         {
301                 SysLog(NID_IO, "WebApp Channel, the data are not JSON types");
302                 return E_SYSTEM;
303         }
304         // Channel for OspApp
305         else
306         {
307                 __pIChannelServiceStub->SendNullResponse(pChannelInfo->clientId, src, dest, requestId);
308         }
309
310         return E_SUCCESS;
311 }
312
313 bool
314 _ChannelService::IsChannelRegistered(const String& channelId)
315 {
316         bool out = false;
317
318         __channels.ContainsKey(channelId, out);
319
320         return out;
321 }
322
323 }}