|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <memory> |
|
19 #include <iostream> |
|
20 #include <sstream> |
|
21 |
|
22 #include "logger.h" |
|
23 |
|
24 #include "serverconnectionfactorybase.h" |
|
25 #include "runtimeexception.h" |
|
26 #include "javacommonutils.h" |
|
27 #include "pusherrorcodes.h" |
|
28 #include "pushexception.h" |
|
29 #include "pendingconnectionlistener.h" |
|
30 |
|
31 using namespace java::push; |
|
32 using namespace java::runtime; |
|
33 using namespace java::util; |
|
34 |
|
35 /** |
|
36 * |
|
37 */ |
|
38 OS_EXPORT ServerConnectionFactoryBase::ServerConnectionFactoryBase() |
|
39 { |
|
40 } |
|
41 |
|
42 /** |
|
43 * |
|
44 */ |
|
45 OS_EXPORT ServerConnectionFactoryBase::~ServerConnectionFactoryBase() |
|
46 { |
|
47 JELOG2(EJavaPush); |
|
48 try |
|
49 { |
|
50 for (SrvConnContainerIter_t iter = mServerConnections.begin(); |
|
51 iter != mServerConnections.end(); ++iter) |
|
52 { |
|
53 delete(iter->second.getConn()); |
|
54 } |
|
55 mServerConnections.clear(); |
|
56 } |
|
57 catch (...) |
|
58 { |
|
59 ELOG(EJavaPush,"ERROR!!! ~ServerConnectionFactoryBase(): Unexpected exception was caught"); |
|
60 } |
|
61 } |
|
62 |
|
63 /** |
|
64 * |
|
65 */ |
|
66 OS_EXPORT ServerConnection& |
|
67 ServerConnectionFactoryBase::createPushServerConn(const std::wstring& aUri, |
|
68 const std::wstring& aFilter, |
|
69 ConnectionListener* aListener, |
|
70 PendingConnectionListener* aPendingConnListener) |
|
71 { |
|
72 JELOG2(EJavaPush); |
|
73 ScopedLock lockObj(mMutex); |
|
74 |
|
75 SrvConnContainerIter_t iter = mServerConnections.find(aUri); |
|
76 if (iter != mServerConnections.end()) |
|
77 { |
|
78 if ((SrvConnContainerData::PUSH == iter->second.connType()) |
|
79 || (SrvConnContainerData::PUSH_LISTEN_BY_MIDLET == iter->second.connType())) |
|
80 { |
|
81 std::string errTxt("Object already exists"); |
|
82 ELOG1(EJavaPush, "ERROR: %s",errTxt.c_str()); |
|
83 ELOG1WSTR(EJavaPush,"Error in createPushServerConn() URI: %s",aUri); |
|
84 throw PushException(PUSH_CONNECTION_ALREADY_EXISTS,errTxt,__FILE__,__FUNCTION__,__LINE__); |
|
85 } |
|
86 else |
|
87 { |
|
88 //Connection type is NORMAL so we have to change it to PUSH_LISTEN_BY_MIDLET. |
|
89 iter->second.setConnType(SrvConnContainerData::PUSH_LISTEN_BY_MIDLET); |
|
90 iter->second.setPendingConnListener(aPendingConnListener); |
|
91 iter->second.getConn()->setFilter(aFilter); |
|
92 return *(iter->second.getConn()); |
|
93 } |
|
94 } |
|
95 std::auto_ptr<ServerConnection> newSrvConn(createSrvConnObj(aUri,aFilter)); |
|
96 newSrvConn->open(aListener); |
|
97 SrvConnContainerData newDataObj(newSrvConn.release(),aPendingConnListener, |
|
98 false,SrvConnContainerData::PUSH); |
|
99 mServerConnections.insert(std::pair<std::wstring,SrvConnContainerData>(aUri,newDataObj)); |
|
100 |
|
101 return *(newDataObj.getConn()); |
|
102 } |
|
103 |
|
104 /** |
|
105 * |
|
106 */ |
|
107 OS_EXPORT ServerConnection* ServerConnectionFactoryBase::create(const std::wstring& aUri) |
|
108 { |
|
109 JELOG2(EJavaPush); |
|
110 ScopedLock lockObj(mMutex); |
|
111 |
|
112 SrvConnContainerIter_t iter = mServerConnections.find(aUri); |
|
113 if (iter == mServerConnections.end()) |
|
114 { |
|
115 std::auto_ptr<ServerConnection> newSrvConn(createSrvConnObj(aUri,L"")); |
|
116 SrvConnContainerData newDataObj(newSrvConn.release(),0,false,SrvConnContainerData::NORMAL); |
|
117 mServerConnections.insert(std::pair<std::wstring,SrvConnContainerData>(aUri,newDataObj)); |
|
118 return newDataObj.getConn(); |
|
119 } |
|
120 |
|
121 if (!isMultipleSrvConnAllowed()) |
|
122 { |
|
123 if (SrvConnContainerData::PUSH_LISTEN_BY_MIDLET == iter->second.connType() |
|
124 || SrvConnContainerData::NORMAL == iter->second.connType()) |
|
125 { |
|
126 std::string errTxt("Type of the connection is: "); |
|
127 errTxt.append(java::util::JavaCommonUtils::intToString((int)iter->second.connType())); |
|
128 throw PushException(PUSH_CONNECTION_ALREADY_EXISTS,errTxt,__FILE__,__FUNCTION__,__LINE__); |
|
129 } |
|
130 } |
|
131 |
|
132 if (SrvConnContainerData::PUSH == iter->second.connType()) |
|
133 { |
|
134 iter->second.setConnType(SrvConnContainerData::PUSH_LISTEN_BY_MIDLET); |
|
135 } |
|
136 |
|
137 return (*iter).second.getConn(); |
|
138 } |
|
139 |
|
140 /** |
|
141 * |
|
142 */ |
|
143 OS_EXPORT void |
|
144 ServerConnectionFactoryBase::getPushConnections(bool aAvailable,std::list<std::wstring>& aUriList) |
|
145 { |
|
146 JELOG2(EJavaPush); |
|
147 ScopedLock lockObj(mMutex); |
|
148 |
|
149 for (SrvConnContainerIter_t iter = mServerConnections.begin(); iter != mServerConnections.end(); ++iter) |
|
150 { |
|
151 if ((SrvConnContainerData::PUSH == iter->second.connType()) |
|
152 || (SrvConnContainerData::PUSH_LISTEN_BY_MIDLET == iter->second.connType())) |
|
153 { |
|
154 if (true == aAvailable) |
|
155 { |
|
156 if (true == iter->second.isActive()) |
|
157 { |
|
158 aUriList.push_back((iter->first)); |
|
159 } |
|
160 } |
|
161 else |
|
162 { |
|
163 WLOG1WSTR(EJavaPush,"ServerConnectionFactoryBase::getPushConnections() URI: %s",(iter->first)); |
|
164 aUriList.push_back((iter->first)); |
|
165 } |
|
166 }//end if( (SrvConnContainerData::PUSH == iter->second.connType())...) |
|
167 }//end for |
|
168 } |
|
169 |
|
170 /** |
|
171 * |
|
172 */ |
|
173 OS_EXPORT void ServerConnectionFactoryBase::closeAllConnections() |
|
174 { |
|
175 JELOG2(EJavaPush); |
|
176 ScopedLock lockObj(mMutex); |
|
177 |
|
178 for (SrvConnContainerIter_t iter = mServerConnections.begin(); iter != mServerConnections.end();) |
|
179 { |
|
180 if (false == deletePushConnection(iter)) |
|
181 iter++; |
|
182 }//end for |
|
183 } |
|
184 |
|
185 /** |
|
186 * |
|
187 */ |
|
188 OS_EXPORT void ServerConnectionFactoryBase::deletePushConnection(const std::wstring& aUri) |
|
189 { |
|
190 JELOG2(EJavaPush); |
|
191 ScopedLock lockObj(mMutex); |
|
192 |
|
193 SrvConnContainerIter_t iter = mServerConnections.find(aUri); |
|
194 if (iter == mServerConnections.end()) |
|
195 return; |
|
196 deletePushConnection(iter); |
|
197 } |
|
198 |
|
199 /** |
|
200 * |
|
201 */ |
|
202 bool ServerConnectionFactoryBase::deletePushConnection(SrvConnContainerIter_t& aIter) |
|
203 { |
|
204 JELOG2(EJavaPush); |
|
205 |
|
206 if (SrvConnContainerData::PUSH == aIter->second.connType()) |
|
207 { |
|
208 aIter->second.getConn()->close(); |
|
209 delete aIter->second.getConn(); |
|
210 mServerConnections.erase(aIter++); |
|
211 return true; |
|
212 } |
|
213 else if (SrvConnContainerData::PUSH_LISTEN_BY_MIDLET == aIter->second.connType()) |
|
214 { |
|
215 //MIDlet listens connection so we cannot delete it here. |
|
216 aIter->second.setConnType(SrvConnContainerData::NORMAL); |
|
217 } |
|
218 //This method does nothing for non-push server connection. Non-push |
|
219 //connections must be release by releaseConnection operation. |
|
220 return false; |
|
221 } |
|
222 |
|
223 /** |
|
224 * |
|
225 */ |
|
226 OS_EXPORT ServerConnection* ServerConnectionFactoryBase::getPushConnection(const std::wstring& aUri) |
|
227 { |
|
228 JELOG2(EJavaPush); |
|
229 ScopedLock lockObj(mMutex); |
|
230 |
|
231 SrvConnContainerIter_t iter = mServerConnections.find(aUri); |
|
232 if (iter == mServerConnections.end()) |
|
233 return 0; |
|
234 if (SrvConnContainerData::NORMAL == iter->second.connType()) |
|
235 return 0; |
|
236 return iter->second.getConn(); |
|
237 } |
|
238 |
|
239 /** |
|
240 * |
|
241 */ |
|
242 OS_EXPORT void ServerConnectionFactoryBase::releaseConnection(const std::wstring& aUri) |
|
243 { |
|
244 JELOG2(EJavaPush); |
|
245 ScopedLock lockObj(mMutex); |
|
246 |
|
247 SrvConnContainerIter_t iter = mServerConnections.find(aUri); |
|
248 if (iter == mServerConnections.end()) |
|
249 return; |
|
250 |
|
251 if (SrvConnContainerData::NORMAL == iter->second.connType()) |
|
252 { |
|
253 iter->second.getConn()->close(); |
|
254 delete iter->second.getConn(); |
|
255 mServerConnections.erase(iter); |
|
256 return; |
|
257 } |
|
258 else if (SrvConnContainerData::PUSH_LISTEN_BY_MIDLET == iter->second.connType()) |
|
259 { |
|
260 iter->second.getConn()->close(); |
|
261 iter->second.setConnType(SrvConnContainerData::PUSH); |
|
262 return; |
|
263 } |
|
264 //This operation does nothing for connection which type is PUSH. |
|
265 } |
|
266 |
|
267 /** |
|
268 * |
|
269 */ |
|
270 OS_EXPORT void ServerConnectionFactoryBase::setPendingMsgFlag |
|
271 (const std::wstring& aUri,bool aPendingMsg) |
|
272 { |
|
273 JELOG2(EJavaPush); |
|
274 ScopedLock lockObj(mMutex); |
|
275 |
|
276 //LOG1WSTR(EJavaPush,EInfo,"setPendingMsgFlag() URI: %s",aUri); |
|
277 //LOG1(EJavaPush,EInfo,"setPendingMsgFlag() aPendingMsg: %d",(int)aPendingMsg); |
|
278 |
|
279 SrvConnContainerIter_t iter = mServerConnections.find(aUri); |
|
280 if (iter != mServerConnections.end()) |
|
281 { |
|
282 iter->second.setActive(aPendingMsg); |
|
283 PendingConnectionListener* tmpListener = iter->second.getPendingConnListener(); |
|
284 if (0 != tmpListener) |
|
285 tmpListener->setPendingStatus(aUri,aPendingMsg); |
|
286 } |
|
287 } |
|
288 |