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_ServerChannelImpl.cpp
20 * @brief This is the implementation file for the _ServerChannelImpl class.
24 #include <unique_ptr.h>
26 #include <FApp_AppInfo.h>
27 #include <FBaseSysLog.h>
28 #include <FIoChannelTypes.h>
29 #include <FIoServerChannel.h>
30 #include <FAppPkg_PackageManagerImpl.h>
31 #include "FIo_ServerChannelImpl.h"
32 #include "FIo_Channel.h"
33 #include "FIo_ChannelManager.h"
36 using namespace Tizen::App;
37 using namespace Tizen::App::Package;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
41 namespace Tizen { namespace Io
44 _ServerChannelImpl::_ServerChannelImpl(void)
45 : __pServerChannel(null)
46 , __pRequestListener(null)
51 _ServerChannelImpl::~_ServerChannelImpl(void)
56 _ServerChannelImpl::Construct(ServerChannel* pServerChannel, const Tizen::Base::String& channelName)
58 String channelId(_AppInfo::GetApplicationId());
60 if (!channelName.IsEmpty())
62 channelId.Append(L'.');
63 channelId.Append(channelName);
67 _ChannelManager* pManager = _ChannelManager::GetInstance();
68 SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager");
70 _Channel* pChannel = pManager->GetChannel(channelId);
71 SysTryReturnResult(NID_IO, pChannel != null, E_SYSTEM, "Failed to create a channel: [%ls]", channelId.GetPointer());
73 pChannel->SetChannelRequestEventListener(*this);
75 __pServerChannel = pServerChannel;
76 __pChannel = pChannel;
82 _ServerChannelImpl::SetChannelRequestEventListener(IChannelRequestEventListener* pRequestListener)
84 __pRequestListener = (IChannelRequestEventListener*) pRequestListener;
88 _ServerChannelImpl::SendResponse(const String& clientChannelId, const IList* pArgs, RequestId reqId)
90 const int APP_ID_LEN = 10;
92 String channelId = clientChannelId;
95 if (clientChannelId.GetLength() <= APP_ID_LEN)
97 execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(clientChannelId);
98 channelId.Append(L'.');
99 channelId.Append(execName);
104 return __pChannel->SendNullResponse(channelId, reqId);
107 const ArrayList* pList = dynamic_cast<const ArrayList*>(pArgs);
110 unique_ptr<ArrayList> pArrayList(new (std::nothrow) ArrayList());
111 pArrayList->Construct(*pArgs);
113 return __pChannel->SendResponse(channelId, *pArrayList, reqId);
116 return __pChannel->SendResponse(channelId, *pList, reqId);
120 _ServerChannelImpl::OnChannelRequestReceivedN(RequestId reqId, const String& channelId, IList* pArgs)
122 SysLog(NID_IO, "Received from [%ls]", channelId.GetPointer());
124 if (__pRequestListener != null)
126 if (_AppInfo::IsOspCompat() == false)
128 __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, channelId, pArgs);
133 channelId.SubString(0, 10, appId);
135 __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, appId, pArgs);
140 pArgs->RemoveAll(true);
147 _ServerChannelImpl::GetInstance(ServerChannel* pChannel)
150 result r = E_SUCCESS;
151 static _ServerChannelImpl* pImpl = null;
154 unique_ptr<_ServerChannelImpl> p(new (std::nothrow) _ServerChannelImpl);
155 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
157 r = p->Construct(pChannel);
158 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
167 _ServerChannelImpl::GetServerChannel(const Tizen::Base::String& channelName)
169 _ChannelManager* pManager = _ChannelManager::GetInstance();
170 SysTryReturn(NID_IO, pManager != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a _ChannelManager.");
172 ServerChannel* pChannel = pManager->GetServerChannel(channelName);
173 if (pChannel == null)
175 // unique_ptr cannot be used because Dtor is private
176 pChannel = new (std::nothrow) ServerChannel;
177 SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
179 unique_ptr<_ServerChannelImpl> pImpl(new (std::nothrow) _ServerChannelImpl);
180 SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
182 result r = pImpl->Construct(pChannel, channelName);
183 SysTryCatch(NID_IO, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
185 pChannel->__pServerChannelImpl = pImpl.release();
187 pManager->AddServerChannel(channelName, pChannel);