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 FIo_ChannelService.cpp
20 * @brief This is the implementation file for the _ChannelService class.
28 #include <unique_ptr.h>
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"
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43 using namespace Tizen::Base::Runtime;
44 using namespace Tizen::Io;
47 namespace Tizen { namespace Io
50 _ChannelService::_ChannelService(void)
51 : __pIChannelServiceStub(null)
56 _ChannelService::~_ChannelService(void)
62 _ChannelService::Construct(_IChannelServiceStub& stub)
64 static _StringHashProvider hashProvider;
65 static _StringComparer stringComparer;
68 __channels.Construct(0, 0, hashProvider, stringComparer);
70 __pIChannelServiceStub = &stub;
72 __pIChannelServiceStub->SetChannelService(*this);
75 unique_ptr<_ChannelCAppStub> pCStub(new (std::nothrow) _ChannelCAppStub());
76 SysTryReturnResult(NID_IO, pCStub != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
78 r = pCStub->Construct();
79 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to create a CAppStub.");
81 pCStub->SetChannelService(*this);
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.");
87 r = pWebStub->Construct();
88 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to create a WebAppStub.");
90 pWebStub->SetChannelService(*this);
99 _ChannelService::RegisterChannel(const String& channelId, const _IChannelServiceEventListener& listener)
105 _ChannelService::RegisterChannel(const String& channelId, int clientId, unsigned int type)
107 result r = E_SUCCESS;
108 _ChannelInfo* pChannelInfo = null;
110 SysLog(NID_IO, "Register a channel : %ls.", channelId.GetPointer());
112 r = __channels.GetValue(channelId, pChannelInfo);
113 SysTryReturnResult(NID_IO, pChannelInfo == null, E_SYSTEM, "Channel has already been registered.");
115 pChannelInfo = new (std::nothrow) _ChannelInfo;
116 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
118 pChannelInfo->channelId = channelId;
119 pChannelInfo->clientId = clientId;
120 pChannelInfo->type = type;
121 pChannelInfo->pGIOChannel = null;
123 __channels.Add(channelId, pChannelInfo);
129 _ChannelService::RegisterChannel(const String& channelId, int clientId, void* pGIOChannel)
131 result r = E_SUCCESS;
132 _ChannelInfo* pChannelInfo = null;
134 SysLog(NID_IO, "Register a channel : [%ls]", channelId.GetPointer());
136 r = __channels.GetValue(channelId, pChannelInfo);
137 SysTryReturnResult(NID_IO, pChannelInfo == null, E_SYSTEM, "Channel has already been registered.");
139 pChannelInfo = new (std::nothrow) _ChannelInfo;
140 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
142 pChannelInfo->channelId = channelId;
143 pChannelInfo->clientId = clientId;
144 pChannelInfo->type = CAPP_CHANNEL;
145 pChannelInfo->pGIOChannel = pGIOChannel;
147 __channels.Add(channelId, pChannelInfo);
153 _ChannelService::UnregisterChannel(const String& channelId)
159 _ChannelService::UnregisterChannel(int clientId)
161 SysLog(NID_IO, "Unregister - clientId = %d", clientId);
163 result r = E_OBJ_NOT_FOUND;
166 _ChannelInfo* pValue = null;
167 IListT<String>* pKeys = __channels.GetKeysN();
168 SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
170 int count = __channels.GetCount();
172 for (int i = 0; i < count; i++)
174 pKeys->GetAt(i, key);
175 __channels.GetValue(key, pValue);
176 if (pValue != null && pValue->clientId == clientId)
178 SysLog(NID_IO, "Unregister - ChannelId = %ls", key.GetPointer());
179 __channels.Remove(key);
192 _ChannelService::SendRequest(const String& src,
194 const ArrayList& args,
197 SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
199 _ChannelInfo* pChannelInfo = null;
201 __channels.GetValue(dest, pChannelInfo);
202 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
203 "Destination channel is not found.");
205 // Channel for WebApp
206 if (pChannelInfo->type == WEBAPP_CHANNEL)
208 bool ret = _ChannelWebAppStub::SendResponse(pChannelInfo->clientId, args);
209 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a WebApp.");
211 // Channel for OspApp
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.");
222 _ChannelService::SendNullRequest(const String& src,
226 SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
228 _ChannelInfo* pChannelInfo = null;
230 __channels.GetValue(dest, pChannelInfo);
231 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
232 "Destination channel is not found.");
234 // Channel for WebApp
235 if (pChannelInfo->type == WEBAPP_CHANNEL)
237 SysLog(NID_IO, "WebApp Channel, the data are not JSON types");
240 // Channel for OspApp
243 __pIChannelServiceStub->SendNullRequest(pChannelInfo->clientId, src, dest, requestId);
250 _ChannelService::SendResponse(const String& src,
252 const ArrayList& args,
255 SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
257 _ChannelInfo* pChannelInfo = null;
259 __channels.GetValue(dest, pChannelInfo);
260 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
261 "Destination channel not found.");
263 // Channel for WebApp
264 if (pChannelInfo->type == WEBAPP_CHANNEL)
266 bool ret = _ChannelWebAppStub::SendResponse(pChannelInfo->clientId, args);
267 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a WebApp.");
270 else if (pChannelInfo->type == CAPP_CHANNEL)
272 bool ret = _ChannelCAppStub::SendResponse(requestId, pChannelInfo->pGIOChannel, args);
273 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a CApp.");
275 // Channel for OspApp
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");
286 _ChannelService::SendNullResponse(const String& src,
290 SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
292 _ChannelInfo* pChannelInfo = null;
294 __channels.GetValue(dest, pChannelInfo);
295 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
296 "Destination channel not found.");
298 // Channel for WebApp
299 if (pChannelInfo->type == WEBAPP_CHANNEL)
301 SysLog(NID_IO, "WebApp Channel, the data are not JSON types");
304 // Channel for OspApp
307 __pIChannelServiceStub->SendNullResponse(pChannelInfo->clientId, src, dest, requestId);
314 _ChannelService::IsChannelRegistered(const String& channelId)
318 __channels.ContainsKey(channelId, out);