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 const String& appId = _AppInfo::GetAppId();
59 const String& appExecutableName = _AppInfo::GetAppExecutableName();
61 String channelId(appId);
62 channelId.Append(L'.');
63 channelId.Append(appExecutableName);
65 if (!channelName.IsEmpty())
67 channelId.Append(L'.');
68 channelId.Append(channelName);
72 _ChannelManager* pManager = _ChannelManager::GetInstance();
73 SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager");
75 _Channel* pChannel = pManager->GetChannel(channelId);
76 SysTryReturnResult(NID_IO, pChannel != null, E_SYSTEM, "Failed to create a channel: [%ls]", channelId.GetPointer());
78 pChannel->SetChannelRequestEventListener(*this);
80 __pServerChannel = pServerChannel;
81 __pChannel = pChannel;
87 _ServerChannelImpl::SetChannelRequestEventListener(IChannelRequestEventListener* pRequestListener)
89 __pRequestListener = (IChannelRequestEventListener*) pRequestListener;
93 _ServerChannelImpl::SendResponse(const String& clientChannelId, const IList* pArgs, RequestId reqId)
95 const int APP_ID_LEN = 10;
97 String channelId = clientChannelId;
100 if (clientChannelId.GetLength() <= APP_ID_LEN)
102 execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(clientChannelId);
103 channelId.Append(L'.');
104 channelId.Append(execName);
109 return __pChannel->SendNullResponse(channelId, reqId);
112 const ArrayList* pList = dynamic_cast<const ArrayList*>(pArgs);
115 unique_ptr<ArrayList> pArrayList(new (std::nothrow) ArrayList());
116 pArrayList->Construct(*pArgs);
118 return __pChannel->SendResponse(channelId, *pArrayList, reqId);
121 return __pChannel->SendResponse(channelId, *pList, reqId);
125 _ServerChannelImpl::OnChannelRequestReceivedN(RequestId reqId, const String& channelId, IList* pArgs)
127 SysLog(NID_IO, "Received from [%ls]", channelId.GetPointer());
129 if (__pRequestListener != null)
131 if (_AppInfo::IsOspCompat() == false)
133 __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, channelId, pArgs);
138 channelId.SubString(0, 10, appId);
140 __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, appId, pArgs);
145 pArgs->RemoveAll(true);
152 _ServerChannelImpl::GetInstance(ServerChannel* pChannel)
155 result r = E_SUCCESS;
156 static _ServerChannelImpl* pImpl = null;
159 unique_ptr<_ServerChannelImpl> p(new (std::nothrow) _ServerChannelImpl);
160 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
162 r = p->Construct(pChannel);
163 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
172 _ServerChannelImpl::GetServerChannel(const Tizen::Base::String& channelName)
174 _ChannelManager* pManager = _ChannelManager::GetInstance();
175 SysTryReturn(NID_IO, pManager != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a _ChannelManager.");
177 ServerChannel* pChannel = pManager->GetServerChannel(channelName);
178 if (pChannel == null)
180 // unique_ptr cannot be used because Dtor is private
181 pChannel = new (std::nothrow) ServerChannel;
182 SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
184 unique_ptr<_ServerChannelImpl> pImpl(new (std::nothrow) _ServerChannelImpl);
185 SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
187 result r = pImpl->Construct(pChannel, channelName);
188 SysTryCatch(NID_IO, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
190 pChannel->__pServerChannelImpl = pImpl.release();
192 pManager->AddServerChannel(channelName, pChannel);