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"
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Io;
40 namespace Tizen { namespace Io
43 _ChannelService::_ChannelService(void)
44 : __pIChannelServiceStub(null)
49 _ChannelService::~_ChannelService(void)
55 _ChannelService::Construct(_IChannelServiceStub& stub)
57 static _StringHashProvider hashProvider;
58 static _StringComparer stringComparer;
60 __channels.Construct(0, 0, hashProvider, stringComparer);
62 __pIChannelServiceStub = &stub;
64 __pIChannelServiceStub->SetChannelService(*this);
70 _ChannelService::RegisterChannel(const String& channelId, const _IChannelServiceEventListener& listener)
76 _ChannelService::RegisterChannel(const String& channelId, int clientId)
79 _ChannelInfo* pChannelInfo = null;
81 SysLog(NID_IO, "Register a channel : %ls", channelId.GetPointer());
83 r = __channels.GetValue(channelId, pChannelInfo);
84 if (pChannelInfo != null)
86 SysTryReturnResult(NID_IO, pChannelInfo->clientId != clientId, E_SYSTEM, "Channel has already been registered.");
88 SysLog(NID_IO, "Remove garbage values : %ls", channelId.GetPointer());
89 __channels.Remove(channelId);
92 pChannelInfo = new (std::nothrow) _ChannelInfo;
93 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
95 pChannelInfo->channelId = channelId;
96 pChannelInfo->clientId = clientId;
97 pChannelInfo->pGIOChannel = null;
99 __channels.Add(channelId, pChannelInfo);
105 _ChannelService::UnregisterChannel(const String& channelId)
111 _ChannelService::UnregisterChannel(int clientId)
113 SysLog(NID_IO, "Unregister - client = %d", clientId);
115 result r = E_OBJ_NOT_FOUND;
118 _ChannelInfo* pValue = null;
119 IListT<String>* pKeys = __channels.GetKeysN();
120 SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
122 int count = __channels.GetCount();
124 for (int i = 0; i < count; i++)
126 pKeys->GetAt(i, key);
127 __channels.GetValue(key, pValue);
128 if (pValue != null && pValue->clientId == clientId)
130 SysLog(NID_IO, "Unregister - Channel = %ls", key.GetPointer());
131 __channels.Remove(key);
144 _ChannelService::SendRequest(const String& src,
146 const ArrayList& args,
149 SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
151 _ChannelInfo* pChannelInfo = null;
153 __channels.GetValue(dest, pChannelInfo);
154 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
155 "Destination channel is not found.");
157 result r = __pIChannelServiceStub->SendRequest(pChannelInfo->clientId, src, dest, args, requestId);
158 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the request data.");
164 _ChannelService::SendNullRequest(const String& src,
168 SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
170 _ChannelInfo* pChannelInfo = null;
172 __channels.GetValue(dest, pChannelInfo);
173 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
174 "Destination channel is not found.");
176 __pIChannelServiceStub->SendNullRequest(pChannelInfo->clientId, src, dest, requestId);
182 _ChannelService::SendResponse(const String& src,
184 const ArrayList& args,
187 SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
189 _ChannelInfo* pChannelInfo = null;
191 __channels.GetValue(dest, pChannelInfo);
192 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
193 "Destination channel not found.");
195 result r = __pIChannelServiceStub->SendResponse(pChannelInfo->clientId, src, dest, args, requestId);
196 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the response data");
202 _ChannelService::SendNullResponse(const String& src,
206 SysLog(NID_IO, "[%ls] ---> [%ls], Request = %d", src.GetPointer(), dest.GetPointer(), requestId);
208 _ChannelInfo* pChannelInfo = null;
210 __channels.GetValue(dest, pChannelInfo);
211 SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
212 "Destination channel not found.");
214 __pIChannelServiceStub->SendNullResponse(pChannelInfo->clientId, src, dest, requestId);
220 _ChannelService::IsChannelRegistered(const String& channelId)
224 __channels.ContainsKey(channelId, out);