OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Destroyed marked. Used by the http connection to see if a session is valid. | 12 // Destroyed marked. Used by the http connection to see if a session is valid. |
13 bool _destroyed = false; | 13 bool _destroyed = false; |
14 bool _isNew = true; | 14 bool _isNew = true; |
15 DateTime _lastSeen; | 15 DateTime _lastSeen; |
16 Function _timeoutCallback; | 16 Function _timeoutCallback; |
17 _HttpSessionManager _sessionManager; | 17 _HttpSessionManager _sessionManager; |
18 // Pointers in timeout queue. | 18 // Pointers in timeout queue. |
19 _HttpSession _prev; | 19 _HttpSession _prev; |
20 _HttpSession _next; | 20 _HttpSession _next; |
| 21 final String id; |
21 | 22 |
22 final Map _data = new HashMap(); | 23 final Map _data = new HashMap(); |
23 | 24 |
24 _HttpSession(_HttpSessionManager this._sessionManager, String this.id) | 25 _HttpSession(this._sessionManager, this.id) : _lastSeen = new DateTime.now(); |
25 : _lastSeen = new DateTime.now(); | |
26 | 26 |
27 void destroy() { | 27 void destroy() { |
28 _destroyed = true; | 28 _destroyed = true; |
29 _sessionManager._removeFromTimeoutQueue(this); | 29 _sessionManager._removeFromTimeoutQueue(this); |
30 _sessionManager._sessions.remove(id); | 30 _sessionManager._sessions.remove(id); |
31 } | 31 } |
32 | 32 |
33 // Mark the session as seen. This will reset the timeout and move the node to | 33 // Mark the session as seen. This will reset the timeout and move the node to |
34 // the end of the timeout queue. | 34 // the end of the timeout queue. |
35 void _markSeen() { | 35 void _markSeen() { |
36 _lastSeen = new DateTime.now(); | 36 _lastSeen = new DateTime.now(); |
37 _sessionManager._bumpToEnd(this); | 37 _sessionManager._bumpToEnd(this); |
38 } | 38 } |
39 | 39 |
40 DateTime get lastSeen => _lastSeen; | 40 DateTime get lastSeen => _lastSeen; |
41 | 41 |
42 bool get isNew => _isNew; | 42 bool get isNew => _isNew; |
43 | 43 |
44 final String id; | |
45 | |
46 void set onTimeout(void callback()) { | 44 void set onTimeout(void callback()) { |
47 _timeoutCallback = callback; | 45 _timeoutCallback = callback; |
48 } | 46 } |
49 | 47 |
50 // Map implementation: | 48 // Map implementation: |
51 bool containsValue(value) => _data.containsValue(value); | 49 bool containsValue(value) => _data.containsValue(value); |
52 bool containsKey(key) => _data.containsKey(key); | 50 bool containsKey(key) => _data.containsKey(key); |
53 operator [](key) => _data[key]; | 51 operator [](key) => _data[key]; |
54 void operator []=(key, value) { _data[key] = value; } | 52 void operator []=(key, value) { _data[key] = value; } |
55 putIfAbsent(key, ifAbsent) => _data.putIfAbsent(key, ifAbsent); | 53 putIfAbsent(key, ifAbsent) => _data.putIfAbsent(key, ifAbsent); |
56 addAll(Map other) => _data.addAll(other); | 54 addAll(Map other) => _data.addAll(other); |
57 remove(key) => _data.remove(key); | 55 remove(key) => _data.remove(key); |
58 void clear() => _data.clear(); | 56 void clear() => _data.clear(); |
59 void forEach(void f(key, value)) => _data.forEach(f); | 57 void forEach(void f(key, value)) => _data.forEach(f); |
60 Iterable get keys => _data.keys; | 58 Iterable get keys => _data.keys; |
61 Iterable get values => _data.values; | 59 Iterable get values => _data.values; |
62 int get length => _data.length; | 60 int get length => _data.length; |
63 bool get isEmpty => _data.isEmpty; | 61 bool get isEmpty => _data.isEmpty; |
64 bool get isNotEmpty => _data.isNotEmpty; | 62 bool get isNotEmpty => _data.isNotEmpty; |
65 } | 63 } |
66 | 64 |
67 // Private class used to manage all the active sessions. The sessions are stored | 65 // Private class used to manage all the active sessions. The sessions are stored |
68 // in two ways: | 66 // in two ways: |
69 // | 67 // |
70 // * In a map, mapping from ID to HttpSession. | 68 // * In a map, mapping from ID to HttpSession. |
71 // * In a linked list, used as a timeout queue. | 69 // * In a linked list, used as a timeout queue. |
72 class _HttpSessionManager { | 70 class _HttpSessionManager { |
| 71 Map<String, _HttpSession> _sessions; |
| 72 int _sessionTimeout = 20 * 60; // 20 mins. |
| 73 _HttpSession _head; |
| 74 _HttpSession _tail; |
| 75 Timer _timer; |
| 76 |
73 _HttpSessionManager() : _sessions = {}; | 77 _HttpSessionManager() : _sessions = {}; |
74 | 78 |
75 String createSessionId() { | 79 String createSessionId() { |
76 const int _KEY_LENGTH = 16; // 128 bits. | 80 const int _KEY_LENGTH = 16; // 128 bits. |
77 var data = _IOCrypto.getRandomBytes(_KEY_LENGTH); | 81 var data = _IOCrypto.getRandomBytes(_KEY_LENGTH); |
78 return _CryptoUtils.bytesToHex(data); | 82 return _CryptoUtils.bytesToHex(data); |
79 } | 83 } |
80 | 84 |
81 _HttpSession getSession(String id) { | 85 _HttpSession getSession(String id) => _sessions[id]; |
82 return _sessions[id]; | |
83 } | |
84 | 86 |
85 _HttpSession createSession() { | 87 _HttpSession createSession() { |
86 var id = createSessionId(); | 88 var id = createSessionId(); |
87 // TODO(ajohnsen): Consider adding a limit and throwing an exception. | 89 // TODO(ajohnsen): Consider adding a limit and throwing an exception. |
88 // Should be very unlikely however. | 90 // Should be very unlikely however. |
89 while (_sessions.containsKey(id)) { | 91 while (_sessions.containsKey(id)) { |
90 id = createSessionId(); | 92 id = createSessionId(); |
91 } | 93 } |
92 var session = _sessions[id] = new _HttpSession(this, id); | 94 var session = _sessions[id] = new _HttpSession(this, id); |
93 _addToTimeoutQueue(session); | 95 _addToTimeoutQueue(session); |
94 return session; | 96 return session; |
95 } | 97 } |
96 | 98 |
97 void set sessionTimeout(int timeout) { | 99 void set sessionTimeout(int timeout) { |
98 _sessionTimeout = timeout; | 100 _sessionTimeout = timeout; |
99 _stopTimer(); | 101 _stopTimer(); |
100 _startTimer(); | 102 _startTimer(); |
101 } | 103 } |
102 | 104 |
103 void close() { | 105 void close() => _stopTimer(); |
104 _stopTimer(); | |
105 } | |
106 | 106 |
107 void _bumpToEnd(_HttpSession session) { | 107 void _bumpToEnd(_HttpSession session) { |
108 _removeFromTimeoutQueue(session); | 108 _removeFromTimeoutQueue(session); |
109 _addToTimeoutQueue(session); | 109 _addToTimeoutQueue(session); |
110 } | 110 } |
111 | 111 |
112 void _addToTimeoutQueue(_HttpSession session) { | 112 void _addToTimeoutQueue(_HttpSession session) { |
113 if (_head == null) { | 113 if (_head == null) { |
114 assert(_tail == null); | 114 assert(_tail == null); |
115 _tail = _head = session; | 115 _tail = _head = session; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 _timerTimeout); | 161 _timerTimeout); |
162 } | 162 } |
163 } | 163 } |
164 | 164 |
165 void _stopTimer() { | 165 void _stopTimer() { |
166 if (_timer != null) { | 166 if (_timer != null) { |
167 _timer.cancel(); | 167 _timer.cancel(); |
168 _timer = null; | 168 _timer = null; |
169 } | 169 } |
170 } | 170 } |
171 | |
172 Map<String, _HttpSession> _sessions; | |
173 int _sessionTimeout = 20 * 60; // 20 mins. | |
174 _HttpSession _head; | |
175 _HttpSession _tail; | |
176 Timer _timer; | |
177 } | 171 } |
178 | 172 |
OLD | NEW |