change version to 1.2.2.0
[platform/framework/native/net.git] / src / FNet_NetEndPointImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 /**
18  * @file                FNet_NetEndPointImpl.cpp
19  * @brief               This is the implementation for the _NetEndPointImpl class.
20  */
21
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"
29
30 using namespace std;
31 using namespace Tizen::Base;
32
33 namespace Tizen { namespace Net
34 {
35
36 static const String _IP_PORT_DELIMITER_STRING = L":";
37 static const String _INVALID_NET_END_POINT_STRING = L"";
38
39 _NetEndPointImpl::_NetEndPointImpl(void)
40         : __pIpAddress(null)
41         , __port(0)
42 {
43 }
44
45 _NetEndPointImpl::_NetEndPointImpl(const IpAddress& ipAddress, unsigned short port)
46         : __pIpAddress(null)
47         , __port(0)
48 {
49         ClearLastResult();
50
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));
53
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));
57
58         __port = port;
59 }
60
61 _NetEndPointImpl::_NetEndPointImpl(const _NetEndPointImpl& value)
62         : __pIpAddress(null)
63         , __port(0)
64 {
65         ClearLastResult();
66
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));
71
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));
75
76         __port = value.__port;
77 }
78
79 _NetEndPointImpl::~_NetEndPointImpl()
80 {
81         __port = 0;
82 }
83
84 NetAddressFamily
85 _NetEndPointImpl::GetNetAddressFamily(void) const
86 {
87         ClearLastResult();
88
89         SysTryReturn(NID_NET, __pIpAddress != null, NET_AF_NONE, E_INVALID_STATE,
90                         "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
91
92         return __pIpAddress->GetNetAddressFamily();
93 }
94
95 unsigned short
96 _NetEndPointImpl::GetPort(void) const
97 {
98         ClearLastResult();
99
100         SysTryReturn(NID_NET, __pIpAddress != null, 0, E_INVALID_STATE,
101                         "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
102
103         return __port;
104 }
105
106 IpAddress*
107 _NetEndPointImpl::GetAddress(void) const
108 {
109         ClearLastResult();
110
111         SysTryReturn(NID_NET, __pIpAddress != null, null, E_INVALID_STATE,
112                         "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
113
114         return __pIpAddress.get();
115 }
116
117 String
118 _NetEndPointImpl::GetNetEndPoint(void) const
119 {
120         ClearLastResult();
121
122         result r = E_SUCCESS;
123         String netEndPoint;
124         String ipAddress;
125         String port;
126
127         SysTryCatch(NID_NET, __pIpAddress != null, , E_INVALID_STATE,
128                         "[%s] The object is in invalid state.", GetErrorMessage(E_INVALID_STATE));
129
130 // 1. set port to String
131         port = Short::ToString(__port);
132
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));
137
138 // 3. Append IpAddress + port into one
139         netEndPoint.Clear();
140
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));
144
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));
148
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));
152
153         return netEndPoint;
154
155 CATCH:
156         return _INVALID_NET_END_POINT_STRING;
157 }
158
159 bool
160 _NetEndPointImpl::IsValid(void) const
161 {
162         if (__pIpAddress != null)
163         {
164                 return true;
165         }
166
167         return false;
168 }
169
170 result
171 _NetEndPointImpl::Update(const IpAddress& ipAddress, unsigned short port)
172 {
173         SysTryReturnResult(NID_NET, ipAddress.GetNetAddressFamily() != NET_AF_NONE, E_INVALID_ARG,
174                         "Invalid argument is used. IP address is invalid.");
175
176         std::unique_ptr<IpAddress> pIpAddress(ipAddress.CloneN());
177         SysTryReturnResult(NID_NET, pIpAddress != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
178
179         __pIpAddress.reset(pIpAddress.release());
180         __port = port;
181
182         return E_SUCCESS;
183 }
184
185 _NetEndPointImpl&
186 _NetEndPointImpl::operator =(const _NetEndPointImpl& rhs)
187 {
188         if (this == &rhs)
189         {
190                 return *this;
191         }
192
193         if (rhs.__pIpAddress != null)
194         {
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));
198
199                 __pIpAddress.reset(pIpAddress.release());
200         }
201         else
202         {
203                 __pIpAddress.reset(null);
204         }
205
206         __port = rhs.__port;
207
208         return *this;
209 }
210
211 bool
212 _NetEndPointImpl::Equals(const Object& obj) const
213 {
214         const _NetEndPointImpl* pObj = dynamic_cast <const _NetEndPointImpl*>(&obj);
215
216         if (pObj == null)
217         {
218                 return false;
219         }
220
221         if ((__pIpAddress != null) && (pObj->__pIpAddress != null))
222         {
223                 if (__pIpAddress->Equals(*pObj->__pIpAddress) == false)
224                 {
225                         return false;
226                 }
227         }
228         else if ((__pIpAddress != null) || (pObj->__pIpAddress != null))
229         {
230                 return false;
231         }
232
233         if (__port != pObj->__port)
234         {
235                 return false;
236         }
237
238         return true;
239 }
240
241 int
242 _NetEndPointImpl::GetHashCode(void) const
243 {
244         int hashCode = _HASH_CODE_INITIAL_VALUE;
245
246         if (__pIpAddress != null)
247         {
248                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __pIpAddress->GetHashCode();
249         }
250         else
251         {
252                 hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode;
253         }
254
255         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __port;
256
257         return hashCode;
258 }
259
260 _NetEndPointImpl*
261 _NetEndPointImpl::GetInstance(NetEndPoint& netEndPoint)
262 {
263         return netEndPoint.__pNetEndPointImpl;
264 }
265
266 const _NetEndPointImpl*
267 _NetEndPointImpl::GetInstance(const NetEndPoint& netEndPoint)
268 {
269         return netEndPoint.__pNetEndPointImpl;
270 }
271
272 } } // Tizen::Net