2 // Open Service Platform
3 // Copyright (c) 2012-2013 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.
18 * @file FNet_DnsImpl.cpp
19 * @brief This is the implementation for the _DnsImpl class.
22 #include <FNetNetConnection.h>
24 #include <FNetIDnsEventListener.h>
25 #include <FNetManagedNetConnection.h>
26 #include <FNetNetConnectionManager.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include "FNet_NetTypes.h"
30 #include "FNet_ManagedNetConnectionImpl.h"
31 #include "FNet_NetConnectionManagerImpl.h"
32 #include "FNet_DnsImpl.h"
33 #include "FNet_DnsEvent.h"
34 #include "FNet_DnsManagedNetConnectionEventListener.h"
35 #include "FNet_DnsRequestHandler.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
42 namespace Tizen { namespace Net
45 _DnsImpl::_DnsImpl(void)
46 : __pNetConnection(null)
48 , __pDnsRequestHandlerList(null)
49 , __pManagedNetConnectionEventListener(null)
50 , __pManagedNetConnection(null)
54 _DnsImpl::~_DnsImpl(void)
56 if (__pManagedNetConnection != null)
58 __pManagedNetConnection->SetManagedNetConnectionEventListener(null);
63 _DnsImpl::Construct(const NetConnection& netConnection, IDnsEventListener& listener)
66 NetConnectionState netConnectionState = NET_CONNECTION_STATE_NONE;
68 SysAssertf(__pDnsEvent == null,
69 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
71 netConnectionState = netConnection.GetConnectionState();
72 SysTryReturnResult(NID_NET, (netConnectionState == NET_CONNECTION_STATE_STARTED ||
73 netConnectionState == NET_CONNECTION_STATE_RESUMED ||
74 netConnectionState == NET_CONNECTION_STATE_SUSPENDED),
75 E_INVALID_CONNECTION, "Network Connection is not established.");
77 unique_ptr<_DnsEvent> pDnsEvent(new (std::nothrow) _DnsEvent());
78 SysTryReturnResult(NID_NET, pDnsEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
80 r = pDnsEvent->Construct();
81 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
83 r = pDnsEvent->AddListener(listener, true);
84 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
86 unique_ptr<ArrayList, _CollectionDeleter> pDnsRequestHandlerList(new (std::nothrow) ArrayList());
87 SysTryReturnResult(NID_NET, pDnsRequestHandlerList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
89 r = pDnsRequestHandlerList->Construct();
90 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
92 __pNetConnection = const_cast <NetConnection*>(&netConnection);
93 __pDnsEvent = move(pDnsEvent);
94 __pDnsRequestHandlerList = move(pDnsRequestHandlerList);
100 _DnsImpl::Construct(IDnsEventListener& listener)
102 result r = E_SUCCESS;
104 SysAssertf(__pDnsEvent == null,
105 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
107 unique_ptr<_DnsEvent> pDnsEvent(new (std::nothrow) _DnsEvent());
108 SysTryReturnResult(NID_NET, pDnsEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
110 r = pDnsEvent->Construct();
111 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
113 r = pDnsEvent->AddListener(listener, true);
114 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
116 unique_ptr<ArrayList, _CollectionDeleter> pDnsRequestHandlerList(new (std::nothrow) ArrayList());
117 SysTryReturnResult(NID_NET, pDnsRequestHandlerList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
119 r = pDnsRequestHandlerList->Construct();
120 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
122 //check for default mode
123 if (!_NetConnectionManagerImpl::IsDefaultMode())
125 SysLog(NID_NET, "DNS uses ManagedNetConnection(Non-default mode)");
127 _NetConnectionManagerImpl* pConnectionManagerImpl = _NetConnectionManagerImpl::GetInstance();
128 SysTryReturnResult(NID_NET, pConnectionManagerImpl != null, E_SYSTEM,
129 "A system error has been occurred. Network framework is not ready.");
131 unique_ptr<_DnsManagedNetConnectionEventListener> pManagedNetConnectionEventListener(new (std::nothrow) _DnsManagedNetConnectionEventListener(pDnsRequestHandlerList.get()));
132 SysTryReturnResult(NID_NET, pManagedNetConnectionEventListener != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
134 unique_ptr<ManagedNetConnection> pManagedNetConnection(pConnectionManagerImpl->GetManagedNetConnectionN());
135 SysTryReturnResult(NID_NET, pManagedNetConnection != null, E_INVALID_CONNECTION, "ManagedNetConnection is not found.");
137 _ManagedNetConnectionImpl* pManagedNCImpl= _ManagedNetConnectionImpl::GetInstance(*pManagedNetConnection);
138 SysTryReturnResult(NID_NET, pManagedNCImpl != null, E_INVALID_CONNECTION, "_ManagedNetConnectionImpl is null.");
140 r = pManagedNetConnection->SetManagedNetConnectionEventListener(pManagedNetConnectionEventListener.get());
141 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_CONNECTION, "Failed to set ManagedNetConnectionEventListener.");
143 r = pManagedNCImpl->Start();
144 SysTryReturnResult(NID_NET, r == E_SUCCESS, E_INVALID_CONNECTION, "Failed to start ManagedNetConnection.");
146 NetConnectionState connState = pManagedNCImpl->GetConnectionState();
147 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
149 SysLog(NID_NET, "ManagedNetConnection is already connected.");
150 pManagedNetConnectionEventListener->__isConnected = true;
153 __pManagedNetConnectionEventListener = move(pManagedNetConnectionEventListener);
154 __pManagedNetConnection = move(pManagedNetConnection);
157 __pDnsEvent = move(pDnsEvent);
158 __pDnsRequestHandlerList = move(pDnsRequestHandlerList);
164 _DnsImpl::GetHostByName(const String& hostName)
166 result r = E_SUCCESS;
168 SysAssertf(__pDnsEvent != null, "Not yet constructed. Construct() should be called before use.");
170 SysTryReturnResult(NID_NET, !hostName.IsEmpty(), E_INVALID_ARG, "Invalid argument is used. hostName is an empty string.");
172 ByteBuffer* pByteBuff = null;
174 //Check if proper utf8 string, if fails, return
175 pByteBuff = StringUtil::StringToUtf8N(hostName);
176 SysTryReturnResult(NID_NET, GetLastResult() != E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "Propagating.");
177 SysTryReturnResult(NID_NET, pByteBuff != null, E_INVALID_ARG, "Invalid argument is used. hostName=%ls.", hostName.GetPointer());
180 unique_ptr<char[]> pHostName(_StringConverter::CopyToCharArrayN(hostName));
181 SysTryReturnResult(NID_NET, pHostName != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
183 // create a request to be handled asynchronously
184 unique_ptr<_DnsRequest> pDnsRequest(new (std::nothrow) _DnsRequest(NET_DNS_EVENT_HOSTNAME, pHostName.get(), *__pDnsEvent));
185 SysTryReturnResult(NID_NET, pDnsRequest != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
187 // Ownership is transferred to pDnsRequest
190 // create request handler and pass request to handle
191 unique_ptr<_DnsRequestHandler> pDnsRequestHandler(new (std::nothrow) _DnsRequestHandler(pDnsRequest.get()));
192 SysTryReturnResult(NID_NET, pDnsRequestHandler != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
194 // Ownership is transferred to pDnsRequestHandler
195 pDnsRequest.release();
197 r = pDnsRequestHandler->Construct();
198 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
200 r = __pDnsRequestHandlerList->Add(*pDnsRequestHandler);
201 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
203 // if current network is not Custom NetConnection, or ManagedNetConnection is not already started.
204 if (__pManagedNetConnection != null && !__pManagedNetConnectionEventListener->__isConnected)
206 SysLog(NID_NET, "Waiting for ManagedNetConnection started event");
210 r = pDnsRequestHandler->Start();
211 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
214 pDnsRequestHandler.release();
220 _DnsImpl::GetHostByAddress(const IpAddress& ipAddress)
222 result r = E_SUCCESS;
224 SysAssertf(__pDnsEvent != null, "Not yet constructed. Construct() should be called before use.");
226 SysTryReturnResult(NID_NET, !ipAddress.ToString().IsEmpty(), E_INVALID_ARG,
227 "Invalid argument is used. IpAddress is empty.");
229 SysTryReturnResult(NID_NET, ipAddress.GetNetAddressFamily() == NET_AF_IPV4, E_INVALID_ARG,
230 "Invalid argument is used. IPv4 only supported.");
232 unique_ptr<char[]> pIpAddr(_StringConverter::CopyToCharArrayN(ipAddress.ToString()));
233 SysTryReturnResult(NID_NET, pIpAddr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
235 unique_ptr<_DnsRequest> pDnsRequest(new (std::nothrow) _DnsRequest(NET_DNS_EVENT_ADDRESS, pIpAddr.get(), *__pDnsEvent));
236 SysTryReturnResult(NID_NET, pDnsRequest != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
238 // Ownership is transferred to pDnsRequest
241 // create request handler and pass request to handle
242 unique_ptr<_DnsRequestHandler> pDnsRequestHandler(new (std::nothrow) _DnsRequestHandler(pDnsRequest.get()));
243 SysTryReturnResult(NID_NET, pDnsRequestHandler != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
245 // Ownership is transferred to pDnsRequestHandler
246 pDnsRequest.release();
248 r = pDnsRequestHandler->Construct();
249 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
251 r = __pDnsRequestHandlerList->Add(*pDnsRequestHandler);
252 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
254 // if current network is not Custom NetConnection, or ManagedNetConnection is not already started.
255 if (__pManagedNetConnection != null && !__pManagedNetConnectionEventListener->__isConnected)
257 SysLog(NID_NET, "Waiting for ManagedNetConnection started event");
261 r = pDnsRequestHandler->Start();
262 SysTryReturnResult(NID_NET, r == E_SUCCESS, r, "Propagating.");
265 pDnsRequestHandler.release();
271 _DnsImpl::GetInstance(Dns& dns)
273 return dns.__pDnsImpl;
277 _DnsImpl::GetInstance(const Dns& dns)
279 return dns.__pDnsImpl;