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. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 bool get isNew => _isNew; | 42 bool get isNew => _isNew; |
43 | 43 |
44 void set onTimeout(void callback()) { | 44 void set onTimeout(void callback()) { |
45 _timeoutCallback = callback; | 45 _timeoutCallback = callback; |
46 } | 46 } |
47 | 47 |
48 // Map implementation: | 48 // Map implementation: |
49 bool containsValue(value) => _data.containsValue(value); | 49 bool containsValue(value) => _data.containsValue(value); |
50 bool containsKey(key) => _data.containsKey(key); | 50 bool containsKey(key) => _data.containsKey(key); |
51 operator [](key) => _data[key]; | 51 operator [](key) => _data[key]; |
52 void operator []=(key, value) { _data[key] = value; } | 52 void operator []=(key, value) { |
| 53 _data[key] = value; |
| 54 } |
| 55 |
53 putIfAbsent(key, ifAbsent) => _data.putIfAbsent(key, ifAbsent); | 56 putIfAbsent(key, ifAbsent) => _data.putIfAbsent(key, ifAbsent); |
54 addAll(Map other) => _data.addAll(other); | 57 addAll(Map other) => _data.addAll(other); |
55 remove(key) => _data.remove(key); | 58 remove(key) => _data.remove(key); |
56 void clear() { _data.clear(); } | 59 void clear() { |
57 void forEach(void f(key, value)) { _data.forEach(f); } | 60 _data.clear(); |
| 61 } |
| 62 |
| 63 void forEach(void f(key, value)) { |
| 64 _data.forEach(f); |
| 65 } |
| 66 |
58 Iterable get keys => _data.keys; | 67 Iterable get keys => _data.keys; |
59 Iterable get values => _data.values; | 68 Iterable get values => _data.values; |
60 int get length => _data.length; | 69 int get length => _data.length; |
61 bool get isEmpty => _data.isEmpty; | 70 bool get isEmpty => _data.isEmpty; |
62 bool get isNotEmpty => _data.isNotEmpty; | 71 bool get isNotEmpty => _data.isNotEmpty; |
63 | 72 |
64 String toString() => 'HttpSession id:$id $_data'; | 73 String toString() => 'HttpSession id:$id $_data'; |
65 } | 74 } |
66 | 75 |
67 // Private class used to manage all the active sessions. The sessions are stored | 76 // Private class used to manage all the active sessions. The sessions are stored |
68 // in two ways: | 77 // in two ways: |
69 // | 78 // |
70 // * In a map, mapping from ID to HttpSession. | 79 // * In a map, mapping from ID to HttpSession. |
71 // * In a linked list, used as a timeout queue. | 80 // * In a linked list, used as a timeout queue. |
72 class _HttpSessionManager { | 81 class _HttpSessionManager { |
73 Map<String, _HttpSession> _sessions; | 82 Map<String, _HttpSession> _sessions; |
74 int _sessionTimeout = 20 * 60; // 20 mins. | 83 int _sessionTimeout = 20 * 60; // 20 mins. |
75 _HttpSession _head; | 84 _HttpSession _head; |
76 _HttpSession _tail; | 85 _HttpSession _tail; |
77 Timer _timer; | 86 Timer _timer; |
78 | 87 |
79 _HttpSessionManager() : _sessions = {}; | 88 _HttpSessionManager() : _sessions = {}; |
80 | 89 |
81 String createSessionId() { | 90 String createSessionId() { |
82 const int _KEY_LENGTH = 16; // 128 bits. | 91 const int _KEY_LENGTH = 16; // 128 bits. |
83 var data = _IOCrypto.getRandomBytes(_KEY_LENGTH); | 92 var data = _IOCrypto.getRandomBytes(_KEY_LENGTH); |
84 return _CryptoUtils.bytesToHex(data); | 93 return _CryptoUtils.bytesToHex(data); |
85 } | 94 } |
86 | 95 |
87 _HttpSession getSession(String id) => _sessions[id]; | 96 _HttpSession getSession(String id) => _sessions[id]; |
88 | 97 |
89 _HttpSession createSession() { | 98 _HttpSession createSession() { |
90 var id = createSessionId(); | 99 var id = createSessionId(); |
91 // TODO(ajohnsen): Consider adding a limit and throwing an exception. | 100 // TODO(ajohnsen): Consider adding a limit and throwing an exception. |
92 // Should be very unlikely however. | 101 // Should be very unlikely however. |
93 while (_sessions.containsKey(id)) { | 102 while (_sessions.containsKey(id)) { |
94 id = createSessionId(); | 103 id = createSessionId(); |
95 } | 104 } |
96 var session = _sessions[id] = new _HttpSession(this, id); | 105 var session = _sessions[id] = new _HttpSession(this, id); |
97 _addToTimeoutQueue(session); | 106 _addToTimeoutQueue(session); |
98 return session; | 107 return session; |
99 } | 108 } |
100 | 109 |
101 void set sessionTimeout(int timeout) { | 110 void set sessionTimeout(int timeout) { |
102 _sessionTimeout = timeout; | 111 _sessionTimeout = timeout; |
103 _stopTimer(); | 112 _stopTimer(); |
104 _startTimer(); | 113 _startTimer(); |
105 } | 114 } |
106 | 115 |
107 void close() { _stopTimer(); } | 116 void close() { |
| 117 _stopTimer(); |
| 118 } |
108 | 119 |
109 void _bumpToEnd(_HttpSession session) { | 120 void _bumpToEnd(_HttpSession session) { |
110 _removeFromTimeoutQueue(session); | 121 _removeFromTimeoutQueue(session); |
111 _addToTimeoutQueue(session); | 122 _addToTimeoutQueue(session); |
112 } | 123 } |
113 | 124 |
114 void _addToTimeoutQueue(_HttpSession session) { | 125 void _addToTimeoutQueue(_HttpSession session) { |
115 if (_head == null) { | 126 if (_head == null) { |
116 assert(_tail == null); | 127 assert(_tail == null); |
117 _tail = _head = session; | 128 _tail = _head = session; |
(...skipping 21 matching lines...) Expand all Loading... |
139 _stopTimer(); | 150 _stopTimer(); |
140 _startTimer(); | 151 _startTimer(); |
141 } | 152 } |
142 if (_tail == session) { | 153 if (_tail == session) { |
143 _tail = session._prev; | 154 _tail = session._prev; |
144 } | 155 } |
145 session._next = session._prev = null; | 156 session._next = session._prev = null; |
146 } | 157 } |
147 | 158 |
148 void _timerTimeout() { | 159 void _timerTimeout() { |
149 _stopTimer(); // Clear timer. | 160 _stopTimer(); // Clear timer. |
150 assert(_head != null); | 161 assert(_head != null); |
151 var session = _head; | 162 var session = _head; |
152 session.destroy(); // Will remove the session from timeout queue and map. | 163 session.destroy(); // Will remove the session from timeout queue and map. |
153 if (session._timeoutCallback != null) { | 164 if (session._timeoutCallback != null) { |
154 session._timeoutCallback(); | 165 session._timeoutCallback(); |
155 } | 166 } |
156 } | 167 } |
157 | 168 |
158 void _startTimer() { | 169 void _startTimer() { |
159 assert(_timer == null); | 170 assert(_timer == null); |
160 if (_head != null) { | 171 if (_head != null) { |
161 int seconds = new DateTime.now().difference(_head.lastSeen).inSeconds; | 172 int seconds = new DateTime.now().difference(_head.lastSeen).inSeconds; |
162 _timer = new Timer(new Duration(seconds: _sessionTimeout - seconds), | 173 _timer = new Timer( |
163 _timerTimeout); | 174 new Duration(seconds: _sessionTimeout - seconds), _timerTimeout); |
164 } | 175 } |
165 } | 176 } |
166 | 177 |
167 void _stopTimer() { | 178 void _stopTimer() { |
168 if (_timer != null) { | 179 if (_timer != null) { |
169 _timer.cancel(); | 180 _timer.cancel(); |
170 _timer = null; | 181 _timer = null; |
171 } | 182 } |
172 } | 183 } |
173 } | 184 } |
174 | |
OLD | NEW |