Tizen 2.1 base
[platform/framework/native/net.git] / src / http / FNetHttp_HttpManagedNetConnectionEventListenerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 /**
19  * @file                FNetHttp_HttpManagedNetConnectionEventListenerImpl.cpp
20  * @brief               This is the implementation file for the _HttpManagedNetConnectionEventListenerImpl Class.
21  */
22
23 #include <FNetManagedNetConnection.h>
24 #include <FNetHttpHttpTransaction.h>
25 #include <FBaseSysLog.h>
26 #include "FNet_NetConnectionInfoImpl.h"
27 #include "FNet_ManagedNetConnectionImpl.h"
28 #include "FNetHttp_HttpSessionImpl.h"
29 #include "FNetHttp_HttpTransactionImpl.h"
30 #include "FNetHttp_HttpCommon.h"
31 #include "FNetHttp_HttpManagedNetConnectionEventListenerImpl.h"
32 #include "FNetHttp_HttpTransactionEvent.h"
33 #include "FNetHttp_HttpTransactionEventArg.h"
34
35 using namespace Tizen::Net;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38
39 namespace Tizen { namespace Net { namespace Http
40 {
41
42 _HttpManagedNetConnectionEventListenerImpl::_HttpManagedNetConnectionEventListenerImpl(_HttpSessionImpl* pHttpSessionImpl)
43         : __isReceivedEvent(false)
44         , __pHttpSessionImpl(pHttpSessionImpl)
45 {
46 }
47
48 _HttpManagedNetConnectionEventListenerImpl::~_HttpManagedNetConnectionEventListenerImpl()
49 {
50 }
51
52 void
53 _HttpManagedNetConnectionEventListenerImpl::OnManagedNetConnectionStarted(ManagedNetConnection& managedNetConnection)
54 {
55         SysLog(NID_NET_HTTP, "The network state is started.");
56
57         ProcessPendingTransactions(managedNetConnection);
58
59         SysLog(NID_NET_HTTP, "Called OnManagedNetConnectionStarted()");
60 }
61
62 void
63 _HttpManagedNetConnectionEventListenerImpl::OnManagedNetConnectionStopped(ManagedNetConnection& managedNetConnection, NetConnectionStoppedReason reason)
64 {
65         result r = E_SUCCESS;
66         SysLog(NID_NET_HTTP, "The network state is stopped with reason(%d).", reason);
67
68         if (!__isReceivedEvent)
69         {
70                 AbortPendingTransactions(managedNetConnection);
71                 __pHttpSessionImpl->SetSessionState(false);
72         }
73         else
74         {
75                 r = __pHttpSessionImpl->Disconnect();
76                 if (IsFailed(r))
77                 {
78                         SysLog(NID_NET_HTTP, "Failed to disconnect the HttpSession.");
79                 }
80         }
81
82         SysLog(NID_NET_HTTP, "Called OnManagedNetConnectionStopped()");
83 }
84
85 void
86 _HttpManagedNetConnectionEventListenerImpl::OnManagedNetConnectionSuspended(ManagedNetConnection& managedNetConnection)
87 {
88         SysLog(NID_NET_HTTP, "The network state is suspended.");
89 }
90
91 void
92 _HttpManagedNetConnectionEventListenerImpl::OnManagedNetConnectionResumed(ManagedNetConnection& managedNetConnection)
93 {
94         SysLog(NID_NET_HTTP, "The network state is resumed.");
95 }
96
97 void
98 _HttpManagedNetConnectionEventListenerImpl::OnManagedNetConnectionBearerChanged(ManagedNetConnection& managedNetConnection)
99 {
100         SysLog(NID_NET_HTTP, "The network state is changed.");
101 }
102
103 bool
104 _HttpManagedNetConnectionEventListenerImpl::IsEventReceived(void) const
105 {
106         return __isReceivedEvent;
107 }
108
109 void
110 _HttpManagedNetConnectionEventListenerImpl::SetReceivedEvent(bool isReceivedEvent)
111 {
112         __isReceivedEvent = isReceivedEvent;
113 }
114
115 void
116 _HttpManagedNetConnectionEventListenerImpl::ProcessPendingTransactions(ManagedNetConnection& managedNetConnection)
117 {
118         SysAssertf(__pHttpSessionImpl != null, "The __pHttpSessionImpl must not be null.");
119
120         result r = E_SUCCESS;
121
122         if (__isReceivedEvent == false)
123         {
124                 _ManagedNetConnectionImpl* pManagedNetConnectionImpl = _ManagedNetConnectionImpl::GetInstance(managedNetConnection);
125                 SysAssertf(pManagedNetConnectionImpl != null, "pConnectionInfo must not be null.");
126
127                 const NetConnectionInfo* pConnectionInfo = pManagedNetConnectionImpl->GetNetConnectionInfo();
128                 SysAssertf(pConnectionInfo != null, "pConnectionInfo must not be null.");
129
130                 const _NetConnectionInfoImpl* pConnectionInfoImpl = _NetConnectionInfoImpl::GetInstance(*pConnectionInfo);
131                 SysAssertf(pConnectionInfoImpl != null, "pConnectionInfoImpl must not be null.");
132
133                 String proxyAddressOfManagedMode = pConnectionInfoImpl->GetProxyAddress();
134                 String deviceName = pConnectionInfoImpl->GetDeviceName();
135                 SysLog(NID_NET_HTTP, "[Preference Mode] Network is started. the device name is %ls, the system proxy address is %ls.", deviceName.GetPointer(), proxyAddressOfManagedMode.GetPointer());
136
137                 if (__pHttpSessionImpl->GetProxyAddress() != null)
138                 {
139                         proxyAddressOfManagedMode = *__pHttpSessionImpl->GetProxyAddress();
140                         SysLog(NID_NET_HTTP, "[Preference Mode] Use a specific proxy address[%ls] of application.", proxyAddressOfManagedMode.GetPointer());
141                 }
142                 else
143                 {
144                         String* pPorxyAddress = new (std::nothrow) String(proxyAddressOfManagedMode);
145                         SysTryReturnVoidResult(NID_NET_HTTP, pPorxyAddress != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
146                         __pHttpSessionImpl->SetProxyAddress(pPorxyAddress);
147                 }
148
149                 __pHttpSessionImpl->SetDeviceName(deviceName);
150
151                 HttpTransaction* pHttpTransaction = null;
152                 _HttpTransactionImpl* pHttpTransactionImpl = null;
153                 ArrayList* pTransactionList = __pHttpSessionImpl->GetActiveTransactions();
154                 SysAssertf(pTransactionList != null, "__pHttpSessionImpl must not be null.");
155
156                 int countOfActiveTransactions = pTransactionList->GetCount();
157
158                 for (int i = 0; i < countOfActiveTransactions; i++)
159                 {
160                         pHttpTransaction = static_cast< HttpTransaction* >(pTransactionList->GetAt(i));
161                         if (pHttpTransaction != null)
162                         {
163                                 pHttpTransactionImpl = _HttpTransactionImpl::GetInstance(*pHttpTransaction);
164                                 SysAssertf(pHttpTransactionImpl != null, "__pHttpSessionImpl must not be null.");
165
166                                 if (pHttpTransactionImpl->IsPendingTransaction())
167                                 {
168                                         r = pHttpTransactionImpl->SubmitPendingTransaction(deviceName, proxyAddressOfManagedMode);
169                                         if (IsFailed(r))
170                                         {
171                                                 SysLog(NID_NET_HTTP, "Failed to submit the pending HttpTransaction.");
172                                         }
173                                 }
174                         }
175                 }
176
177                 __isReceivedEvent = true;
178         }
179 }
180
181 void
182 _HttpManagedNetConnectionEventListenerImpl::AbortPendingTransactions(ManagedNetConnection& managedNetConnection)
183 {
184         SysAssertf(__pHttpSessionImpl != null, "__pHttpSessionImpl must not be null.");
185
186         result r = E_SUCCESS;
187
188         r = managedNetConnection.SetManagedNetConnectionEventListener(null);
189         if (IsFailed(r))
190         {
191                 SysLog(NID_NET_HTTP, "Failed to unset the ManagedNetConnectionEventListener.");
192         }
193
194         HttpTransaction* pHttpTransaction = null;
195         _HttpTransactionImpl* pHttpTransactionImpl = null;
196         ArrayList* pTransactionList = __pHttpSessionImpl->GetActiveTransactions();
197         SysAssertf(pTransactionList != null, "__pHttpSessionImpl must not be null.");
198
199         int countOfActiveTransactions = pTransactionList->GetCount();
200
201         for (int i = 0; i < countOfActiveTransactions; i++)
202         {
203                 pHttpTransaction = static_cast< HttpTransaction* >(pTransactionList->GetAt(i));
204                 if (pHttpTransaction != null)
205                 {
206                         pHttpTransactionImpl = _HttpTransactionImpl::GetInstance(*pHttpTransaction);
207                         SysAssertf(pHttpTransactionImpl != null, "__pHttpSessionImpl must not be null.");
208
209                         if (pHttpTransactionImpl->IsPendingTransaction())
210                         {
211                                 //Fire the event(_HTTP_TRANSACTION_EVENT_TYPE_ABORTED)  - E_NETWORK_UNAVAILABLE
212                                 pHttpTransactionImpl->GetHttpTransactionEvent()->FireTransactionAbortedEvent(E_NETWORK_UNAVAILABLE);
213                         }
214                 }
215         }
216         __isReceivedEvent = true;
217 }
218
219 } } } // Tizen::Net::Http