| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 const String _DART_SESSION_ID = "DARTSESSID"; | 7 const String _DART_SESSION_ID = "DARTSESSID"; |
| 8 | 8 |
| 9 // A _HttpSession is a node in a double-linked list, with _next and _prev being | 9 // A _HttpSession is a node in a double-linked list, with _next and _prev being |
| 10 // the previous and next pointers. | 10 // the previous and next pointers. |
| 11 class _HttpSession implements HttpSession { | 11 class _HttpSession implements HttpSession { |
| 12 _HttpSession(_HttpSessionManager this._sessionManager, String this.id) | 12 _HttpSession(_HttpSessionManager this._sessionManager, String this.id) |
| 13 : _lastSeen = new Date.now(); | 13 : _lastSeen = new DateTime.now(); |
| 14 | 14 |
| 15 void destroy() { | 15 void destroy() { |
| 16 _destroyed = true; | 16 _destroyed = true; |
| 17 _sessionManager._removeFromTimeoutQueue(this); | 17 _sessionManager._removeFromTimeoutQueue(this); |
| 18 _sessionManager._sessions.remove(id); | 18 _sessionManager._sessions.remove(id); |
| 19 } | 19 } |
| 20 | 20 |
| 21 // Mark the session as seen. This will reset the timeout and move the node to | 21 // Mark the session as seen. This will reset the timeout and move the node to |
| 22 // the end of the timeout queue. | 22 // the end of the timeout queue. |
| 23 void _markSeen() { | 23 void _markSeen() { |
| 24 _lastSeen = new Date.now(); | 24 _lastSeen = new DateTime.now(); |
| 25 _sessionManager._bumpToEnd(this); | 25 _sessionManager._bumpToEnd(this); |
| 26 } | 26 } |
| 27 | 27 |
| 28 dynamic data; | 28 dynamic data; |
| 29 | 29 |
| 30 Date get lastSeen => _lastSeen; | 30 DateTime get lastSeen => _lastSeen; |
| 31 | 31 |
| 32 final String id; | 32 final String id; |
| 33 | 33 |
| 34 void set onTimeout(void callback()) { | 34 void set onTimeout(void callback()) { |
| 35 _timeoutCallback = callback; | 35 _timeoutCallback = callback; |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Destroyed marked. Used by the http connection to see if a session is valid. | 38 // Destroyed marked. Used by the http connection to see if a session is valid. |
| 39 bool _destroyed = false; | 39 bool _destroyed = false; |
| 40 Date _lastSeen; | 40 DateTime _lastSeen; |
| 41 Function _timeoutCallback; | 41 Function _timeoutCallback; |
| 42 _HttpSessionManager _sessionManager; | 42 _HttpSessionManager _sessionManager; |
| 43 // Pointers in timeout queue. | 43 // Pointers in timeout queue. |
| 44 _HttpSession _prev; | 44 _HttpSession _prev; |
| 45 _HttpSession _next; | 45 _HttpSession _next; |
| 46 } | 46 } |
| 47 | 47 |
| 48 // Private class used to manage all the active sessions. The sessions are stored | 48 // Private class used to manage all the active sessions. The sessions are stored |
| 49 // in two ways: | 49 // in two ways: |
| 50 // | 50 // |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 var session = _head; | 131 var session = _head; |
| 132 session.destroy(); // Will remove the session from timeout queue and map. | 132 session.destroy(); // Will remove the session from timeout queue and map. |
| 133 if (session._timeoutCallback != null) { | 133 if (session._timeoutCallback != null) { |
| 134 session._timeoutCallback(); | 134 session._timeoutCallback(); |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 | 137 |
| 138 void _startTimer() { | 138 void _startTimer() { |
| 139 assert(_timer == null); | 139 assert(_timer == null); |
| 140 if (_head != null) { | 140 if (_head != null) { |
| 141 int seconds = new Date.now().difference(_head.lastSeen).inSeconds; | 141 int seconds = new DateTime.now().difference(_head.lastSeen).inSeconds; |
| 142 _timer = new Timer((_sessionTimeout - seconds) * 1000, _timerTimeout); | 142 _timer = new Timer((_sessionTimeout - seconds) * 1000, _timerTimeout); |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 void _stopTimer() { | 146 void _stopTimer() { |
| 147 if (_timer != null) { | 147 if (_timer != null) { |
| 148 _timer.cancel(); | 148 _timer.cancel(); |
| 149 _timer = null; | 149 _timer = null; |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 | 152 |
| 153 Map<String, _HttpSession> _sessions; | 153 Map<String, _HttpSession> _sessions; |
| 154 int _sessionTimeout = 20 * 60; // 20 mins. | 154 int _sessionTimeout = 20 * 60; // 20 mins. |
| 155 _HttpSession _head; | 155 _HttpSession _head; |
| 156 _HttpSession _tail; | 156 _HttpSession _tail; |
| 157 Timer _timer; | 157 Timer _timer; |
| 158 | 158 |
| 159 external static Uint8List _getRandomBytes(int count); | 159 external static Uint8List _getRandomBytes(int count); |
| 160 } | 160 } |
| 161 | 161 |
| OLD | NEW |