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_NetEndPointImpl.cpp
19 * @brief This is the implementation for the _NetEndPointImpl class.
22 #include <FBaseShort.h>
23 #include <FNetNetEndPoint.h>
24 #include <FNetIp4Address.h>
25 #include <FBaseSysLog.h>
26 #include "FNet_NetTypes.h"
27 #include "FNet_Ip4AddressImpl.h"
28 #include "FNet_NetEndPointImpl.h"
31 using namespace Tizen::Base;
33 namespace Tizen { namespace Net
36 static const String _IP_PORT_DELIMITER_STRING = L":";
37 static const String _INVALID_NET_END_POINT_STRING = L"";
39 _NetEndPointImpl::_NetEndPointImpl(void)
45 _NetEndPointImpl::_NetEndPointImpl(const IpAddress& ipAddress, unsigned short port)
51 SysTryReturnVoidResult(NID_NET, ipAddress.GetNetAddressFamily() != NET_AF_NONE,
52 E_INVALID_ARG, "[%s] Invalid argument is used. IP address is invalid.", GetErrorMessage(E_INVALID_ARG));
54 __pIpAddress.reset(ipAddress.CloneN());
55 SysTryReturnVoidResult(NID_NET, __pIpAddress != null, E_OUT_OF_MEMORY,
56 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
61 _NetEndPointImpl::_NetEndPointImpl(const _NetEndPointImpl& value)
67 SysTryReturnVoidResult(NID_NET, value.__pIpAddress != null, E_INVALID_ARG,
68 "[%s] Invalid argument is used. The instance is invalid.", GetErrorMessage(E_INVALID_ARG));
69 SysTryReturnVoidResult(NID_NET, value.__pIpAddress->GetNetAddressFamily() != NET_AF_NONE, E_INVALID_ARG,
70 "[%s] Invalid argument is used. The instance is invalid.", GetErrorMessage(E_INVALID_ARG));
72 __pIpAddress.reset(value.__pIpAddress->CloneN());
73 SysTryReturnVoidResult(NID_NET, __pIpAddress != null, E_OUT_OF_MEMORY,
74 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
76 __port = value.__port;
79 _NetEndPointImpl::~_NetEndPointImpl()
85 _NetEndPointImpl::GetNetAddressFamily(void) const
89 SysTryReturn(NID_NET, __pIpAddress != null, NET_AF_NONE, E_INVALID_STATE,
90 "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
92 return __pIpAddress->GetNetAddressFamily();
96 _NetEndPointImpl::GetPort(void) const
100 SysTryReturn(NID_NET, __pIpAddress != null, 0, E_INVALID_STATE,
101 "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
107 _NetEndPointImpl::GetAddress(void) const
111 SysTryReturn(NID_NET, __pIpAddress != null, null, E_INVALID_STATE,
112 "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
114 return __pIpAddress.get();
118 _NetEndPointImpl::GetNetEndPoint(void) const
122 result r = E_SUCCESS;
127 SysTryCatch(NID_NET, __pIpAddress != null, , E_INVALID_STATE,
128 "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
130 // 1. set port to String
131 port = Short::ToString(__port);
133 // 2. set IpAddress to String
134 ipAddress = __pIpAddress->ToString();
135 SysTryCatch(NID_NET, !ipAddress.IsEmpty(), , E_INVALID_STATE,
136 "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
138 // 3. Append IpAddress + port into one
141 r = netEndPoint.Append(ipAddress);
142 SysTryCatch(NID_NET, r == E_SUCCESS, , E_OUT_OF_MEMORY,
143 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
145 r = netEndPoint.Append(_IP_PORT_DELIMITER_STRING);
146 SysTryCatch(NID_NET, r == E_SUCCESS, , E_OUT_OF_MEMORY,
147 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
149 r = netEndPoint.Append(port);
150 SysTryCatch(NID_NET, r == E_SUCCESS, , E_OUT_OF_MEMORY,
151 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
156 return _INVALID_NET_END_POINT_STRING;
160 _NetEndPointImpl::IsValid(void) const
162 if (__pIpAddress != null)
171 _NetEndPointImpl::Update(const IpAddress& ipAddress, unsigned short port)
173 SysTryReturnResult(NID_NET, ipAddress.GetNetAddressFamily() != NET_AF_NONE, E_INVALID_ARG,
174 "Invalid argument is used. IP address is invalid.");
176 std::unique_ptr<IpAddress> pIpAddress(ipAddress.CloneN());
177 SysTryReturnResult(NID_NET, pIpAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
179 __pIpAddress.reset(pIpAddress.release());
186 _NetEndPointImpl::operator =(const _NetEndPointImpl& rhs)
193 if (rhs.__pIpAddress != null)
195 std::unique_ptr<IpAddress> pIpAddress(rhs.__pIpAddress->CloneN());
196 SysTryReturn(NID_NET, pIpAddress != null, *this, E_OUT_OF_MEMORY,
197 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
199 __pIpAddress.reset(pIpAddress.release());
203 __pIpAddress.reset(null);
212 _NetEndPointImpl::Equals(const Object& obj) const
214 const _NetEndPointImpl* pObj = dynamic_cast <const _NetEndPointImpl*>(&obj);
221 if ((__pIpAddress != null) && (pObj->__pIpAddress != null))
223 if (__pIpAddress->Equals(*pObj->__pIpAddress) == false)
228 else if ((__pIpAddress != null) || (pObj->__pIpAddress != null))
233 if (__port != pObj->__port)
242 _NetEndPointImpl::GetHashCode(void) const
244 int hashCode = _HASH_CODE_INITIAL_VALUE;
246 if (__pIpAddress != null)
248 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pIpAddress->GetHashCode();
252 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
255 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __port;
261 _NetEndPointImpl::GetInstance(NetEndPoint& netEndPoint)
263 return netEndPoint.__pNetEndPointImpl;
266 const _NetEndPointImpl*
267 _NetEndPointImpl::GetInstance(const NetEndPoint& netEndPoint)
269 return netEndPoint.__pNetEndPointImpl;