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 SysTryReturnResult(NID_IO, pChannelInfo == null, E_SYSTEM, "Channel has already been registered.");
100 pChannelInfo = new (std::nothrow) _ChannelInfo;
101 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
103 pChannelInfo->channelId = channelId;
104 pChannelInfo->clientId = clientId;
105 pChannelInfo->type = type;
106 pChannelInfo->pGIOChannel = null;
108 __channels.Add(channelId, pChannelInfo);
114 _ChannelService::RegisterChannel(const String& channelId, int clientId, void* pGIOChannel)
116 result r = E_SUCCESS;
117 _ChannelInfo* pChannelInfo = null;
119 SysLog(NID_IO, "Register a channel : [%ls]", channelId.GetPointer());
121 r = __channels.GetValue(channelId, pChannelInfo);
122 SysTryReturnResult(NID_IO, pChannelInfo == null, E_SYSTEM, "Channel has already been registered.");
124 pChannelInfo = new (std::nothrow) _ChannelInfo;
125 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
127 pChannelInfo->channelId = channelId;
128 pChannelInfo->clientId = clientId;
129 pChannelInfo->type = CAPP_CHANNEL;
130 pChannelInfo->pGIOChannel = pGIOChannel;
132 __channels.Add(channelId, pChannelInfo);
138 _ChannelService::UnregisterChannel(const String& channelId)
144 _ChannelService::UnregisterChannel(int clientId)
146 SysLog(NID_IO, "Unregister - clientId = %d", clientId);
148 result r = E_OBJ_NOT_FOUND;
151 _ChannelInfo* pValue = null;
152 IListT<String>* pKeys = __channels.GetKeysN();
153 SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
155 int count = __channels.GetCount();
157 for (int i = 0; i < count; i++)
159 pKeys->GetAt(i, key);
160 __channels.GetValue(key, pValue);
161 if (pValue != null && pValue->clientId == clientId)
163 SysLog(NID_IO, "Unregister - ChannelId = %ls", key.GetPointer());
164 __channels.Remove(key);
177 _ChannelService::SendRequest(const String& src,
179 const ArrayList& args,
182 SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
184 _ChannelInfo* pChannelInfo = null;
186 __channels.GetValue(dest, pChannelInfo);
187 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
188 "Destination channel is not found.");
190 result r = __pIChannelServiceStub->SendRequest(pChannelInfo->clientId, src, dest, args, requestId);
191 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the request data.");
197 _ChannelService::SendNullRequest(const String& src,
201 SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
203 _ChannelInfo* pChannelInfo = null;
205 __channels.GetValue(dest, pChannelInfo);
206 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
207 "Destination channel is not found.");
209 __pIChannelServiceStub->SendNullRequest(pChannelInfo->clientId, src, dest, requestId);
215 _ChannelService::SendResponse(const String& src,
217 const ArrayList& args,
220 SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
222 _ChannelInfo* pChannelInfo = null;
224 __channels.GetValue(dest, pChannelInfo);
225 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
226 "Destination channel not found.");
229 if (pChannelInfo->type == CAPP_CHANNEL)
231 bool ret = _ChannelCAppStub::SendResponse(requestId, pChannelInfo->pGIOChannel, args);
232 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a CApp.");
234 // Channel for OspApp
237 result r = __pIChannelServiceStub->SendResponse(pChannelInfo->clientId, src, dest, args, requestId);
238 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the response data");
245 _ChannelService::SendNullResponse(const String& src,
249 SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
251 _ChannelInfo* pChannelInfo = null;
253 __channels.GetValue(dest, pChannelInfo);
254 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
255 "Destination channel not found.");
257 __pIChannelServiceStub->SendNullResponse(pChannelInfo->clientId, src, dest, requestId);
263 _ChannelService::IsChannelRegistered(const String& channelId)
267 __channels.ContainsKey(channelId, out);