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.
24 #include <unique_ptr.h>
26 #include <FBaseStringComparer.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include <FBaseRt_EventDispatcher.h>
30 #include "FIo_ChannelService.h"
31 #include "FIo_ChannelMessages.h"
32 #include "FIo_IChannelServiceStub.h"
33 #include "FIo_ChannelCAppStub.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Io;
42 namespace Tizen { namespace Io
45 _ChannelService::_ChannelService(void)
46 : __pIChannelServiceStub(null)
51 _ChannelService::~_ChannelService(void)
57 _ChannelService::Construct(_IChannelServiceStub& stub)
59 static _StringHashProvider hashProvider;
60 static _StringComparer stringComparer;
63 __channels.Construct(0, 0, hashProvider, stringComparer);
65 __pIChannelServiceStub = &stub;
67 __pIChannelServiceStub->SetChannelService(*this);
70 unique_ptr<_ChannelCAppStub> pCStub(new (std::nothrow) _ChannelCAppStub());
71 SysTryReturnResult(NID_IO, pCStub != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
73 r = pCStub->Construct();
74 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to create a CAppStub.");
76 pCStub->SetChannelService(*this);
84 _ChannelService::RegisterChannel(const String& channelId, const _IChannelServiceEventListener& listener)
90 _ChannelService::RegisterChannel(const String& channelId, int clientId, unsigned int type)
93 _ChannelInfo* pChannelInfo = null;
95 SysLog(NID_IO, "Register a channel : %ls", channelId.GetPointer());
97 r = __channels.GetValue(channelId, pChannelInfo);
98 if (pChannelInfo != null)
100 SysTryReturnResult(NID_IO, pChannelInfo->clientId != clientId, E_SYSTEM, "Channel has already been registered.");
102 SysLog(NID_IO, "Remove garbage values : %ls", channelId.GetPointer());
103 __channels.Remove(channelId);
106 pChannelInfo = new (std::nothrow) _ChannelInfo;
107 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
109 pChannelInfo->channelId = channelId;
110 pChannelInfo->clientId = clientId;
111 pChannelInfo->type = type;
112 pChannelInfo->pGIOChannel = null;
114 __channels.Add(channelId, pChannelInfo);
120 _ChannelService::RegisterChannel(const String& channelId, int clientId, void* pGIOChannel)
122 result r = E_SUCCESS;
123 _ChannelInfo* pChannelInfo = null;
125 SysLog(NID_IO, "Register a channel : [%ls]", channelId.GetPointer());
127 r = __channels.GetValue(channelId, pChannelInfo);
128 if (pChannelInfo != null)
130 SysTryReturnResult(NID_IO, pChannelInfo->clientId != clientId, E_SYSTEM, "Channel has already been registered.");
132 SysLog(NID_IO, "Remove garbage values : %ls", channelId.GetPointer());
133 __channels.Remove(channelId);
136 pChannelInfo = new (std::nothrow) _ChannelInfo;
137 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
139 pChannelInfo->channelId = channelId;
140 pChannelInfo->clientId = clientId;
141 pChannelInfo->type = CAPP_CHANNEL;
142 pChannelInfo->pGIOChannel = pGIOChannel;
144 __channels.Add(channelId, pChannelInfo);
150 _ChannelService::UnregisterChannel(const String& channelId)
156 _ChannelService::UnregisterChannel(int clientId)
158 SysLog(NID_IO, "Unregister - client = %d", clientId);
160 result r = E_OBJ_NOT_FOUND;
163 _ChannelInfo* pValue = null;
164 IListT<String>* pKeys = __channels.GetKeysN();
165 SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
167 int count = __channels.GetCount();
169 for (int i = 0; i < count; i++)
171 pKeys->GetAt(i, key);
172 __channels.GetValue(key, pValue);
173 if (pValue != null && pValue->clientId == clientId)
175 SysLog(NID_IO, "Unregister - Channel = %ls", key.GetPointer());
176 __channels.Remove(key);
189 _ChannelService::SendRequest(const String& src,
191 const ArrayList& args,
194 SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
196 _ChannelInfo* pChannelInfo = null;
198 __channels.GetValue(dest, pChannelInfo);
199 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
200 "Destination channel is not found.");
202 result r = __pIChannelServiceStub->SendRequest(pChannelInfo->clientId, src, dest, args, requestId);
203 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the request data.");
209 _ChannelService::SendNullRequest(const String& src,
213 SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
215 _ChannelInfo* pChannelInfo = null;
217 __channels.GetValue(dest, pChannelInfo);
218 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
219 "Destination channel is not found.");
221 __pIChannelServiceStub->SendNullRequest(pChannelInfo->clientId, src, dest, requestId);
227 _ChannelService::SendResponse(const String& src,
229 const ArrayList& args,
232 SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
234 _ChannelInfo* pChannelInfo = null;
236 __channels.GetValue(dest, pChannelInfo);
237 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
238 "Destination channel not found.");
241 if (pChannelInfo->type == CAPP_CHANNEL)
243 bool ret = _ChannelCAppStub::SendResponse(requestId, pChannelInfo->pGIOChannel, args);
244 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a CApp.");
246 // Channel for OspApp
249 result r = __pIChannelServiceStub->SendResponse(pChannelInfo->clientId, src, dest, args, requestId);
250 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the response data");
257 _ChannelService::SendNullResponse(const String& src,
261 SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
263 _ChannelInfo* pChannelInfo = null;
265 __channels.GetValue(dest, pChannelInfo);
266 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
267 "Destination channel not found.");
269 __pIChannelServiceStub->SendNullResponse(pChannelInfo->clientId, src, dest, requestId);
275 _ChannelService::IsChannelRegistered(const String& channelId)
279 __channels.ContainsKey(channelId, out);