2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FIo_ServerChannelImpl.cpp
19 * @brief This is the implementation file for the _ServerChannelImpl class.
23 #include <unique_ptr.h>
25 #include <FApp_AppInfo.h>
26 #include <FBaseSysLog.h>
27 #include <FIoChannelTypes.h>
28 #include <FIoServerChannel.h>
29 #include <FAppPkg_PackageManagerImpl.h>
30 #include "FIo_ServerChannelImpl.h"
31 #include "FIo_Channel.h"
32 #include "FIo_ChannelManager.h"
35 using namespace Tizen::App;
36 using namespace Tizen::App::Package;
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
40 namespace Tizen { namespace Io
43 _ServerChannelImpl::_ServerChannelImpl(void)
44 : __pServerChannel(null)
45 , __pRequestListener(null)
50 _ServerChannelImpl::~_ServerChannelImpl(void)
55 _ServerChannelImpl::Construct(ServerChannel* pServerChannel, const Tizen::Base::String& channelName)
57 String channelId(_AppInfo::GetApplicationId());
59 if (!channelName.IsEmpty())
61 channelId.Append(L'.');
62 channelId.Append(channelName);
66 _ChannelManager* pManager = _ChannelManager::GetInstance();
67 SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager");
69 _Channel* pChannel = pManager->GetChannel(channelId);
70 SysTryReturnResult(NID_IO, pChannel != null, E_SYSTEM, "Failed to create a channel: [%ls]", channelId.GetPointer());
72 pChannel->SetChannelRequestEventListener(*this);
74 __pServerChannel = pServerChannel;
75 __pChannel = pChannel;
81 _ServerChannelImpl::SetChannelRequestEventListener(IChannelRequestEventListener* pRequestListener)
83 __pRequestListener = (IChannelRequestEventListener*) pRequestListener;
87 _ServerChannelImpl::SendResponse(const String& clientChannelId, const IList* pArgs, RequestId reqId)
89 const int APP_ID_LEN = 10;
91 String channelId = clientChannelId;
94 if (clientChannelId.GetLength() <= APP_ID_LEN)
96 execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(clientChannelId);
97 channelId.Append(L'.');
98 channelId.Append(execName);
103 return __pChannel->SendNullResponse(channelId, reqId);
106 const ArrayList* pList = dynamic_cast<const ArrayList*>(pArgs);
109 unique_ptr<ArrayList> pArrayList(new (std::nothrow) ArrayList());
110 pArrayList->Construct(*pArgs);
112 return __pChannel->SendResponse(channelId, *pArrayList, reqId);
115 return __pChannel->SendResponse(channelId, *pList, reqId);
119 _ServerChannelImpl::OnChannelRequestReceivedN(RequestId reqId, const String& channelId, IList* pArgs)
121 SysLog(NID_IO, "Received from [%ls]", channelId.GetPointer());
123 if (__pRequestListener != null)
125 if (_AppInfo::IsOspCompat() == false)
127 __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, channelId, pArgs);
132 channelId.SubString(0, 10, appId);
134 __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, appId, pArgs);
139 pArgs->RemoveAll(true);
146 _ServerChannelImpl::GetInstance(ServerChannel* pChannel)
149 result r = E_SUCCESS;
150 static _ServerChannelImpl* pImpl = null;
153 unique_ptr<_ServerChannelImpl> p(new (std::nothrow) _ServerChannelImpl);
154 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
156 r = p->Construct(pChannel);
157 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
166 _ServerChannelImpl::GetServerChannel(const Tizen::Base::String& channelName)
168 _ChannelManager* pManager = _ChannelManager::GetInstance();
169 SysTryReturn(NID_IO, pManager != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a _ChannelManager.");
171 ServerChannel* pChannel = pManager->GetServerChannel(channelName);
172 if (pChannel == null)
174 // unique_ptr cannot be used because Dtor is private
175 pChannel = new (std::nothrow) ServerChannel;
176 SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
178 unique_ptr<_ServerChannelImpl> pImpl(new (std::nothrow) _ServerChannelImpl);
179 SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
181 result r = pImpl->Construct(pChannel, channelName);
182 SysTryCatch(NID_IO, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
184 pChannel->__pServerChannelImpl = pImpl.release();
186 pManager->AddServerChannel(channelName, pChannel);