Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(584)

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/lib/io/http_session.dart

Issue 2698353003: unfork DDC's copy of most SDK libraries (Closed)
Patch Set: revert core_patch Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of dart.io;
6
7 const String _DART_SESSION_ID = "DARTSESSID";
8
9 // A _HttpSession is a node in a double-linked list, with _next and _prev being
10 // the previous and next pointers.
11 class _HttpSession implements HttpSession {
12 // Destroyed marked. Used by the http connection to see if a session is valid.
13 bool _destroyed = false;
14 bool _isNew = true;
15 DateTime _lastSeen;
16 Function _timeoutCallback;
17 _HttpSessionManager _sessionManager;
18 // Pointers in timeout queue.
19 _HttpSession _prev;
20 _HttpSession _next;
21 final String id;
22
23 final Map _data = new HashMap();
24
25 _HttpSession(this._sessionManager, this.id) : _lastSeen = new DateTime.now();
26
27 void destroy() {
28 _destroyed = true;
29 _sessionManager._removeFromTimeoutQueue(this);
30 _sessionManager._sessions.remove(id);
31 }
32
33 // Mark the session as seen. This will reset the timeout and move the node to
34 // the end of the timeout queue.
35 void _markSeen() {
36 _lastSeen = new DateTime.now();
37 _sessionManager._bumpToEnd(this);
38 }
39
40 DateTime get lastSeen => _lastSeen;
41
42 bool get isNew => _isNew;
43
44 void set onTimeout(void callback()) {
45 _timeoutCallback = callback;
46 }
47
48 // Map implementation:
49 bool containsValue(value) => _data.containsValue(value);
50 bool containsKey(key) => _data.containsKey(key);
51 operator [](key) => _data[key];
52 void operator []=(key, value) { _data[key] = value; }
53 putIfAbsent(key, ifAbsent) => _data.putIfAbsent(key, ifAbsent);
54 addAll(Map other) => _data.addAll(other);
55 remove(key) => _data.remove(key);
56 void clear() { _data.clear(); }
57 void forEach(void f(key, value)) { _data.forEach(f); }
58 Iterable get keys => _data.keys;
59 Iterable get values => _data.values;
60 int get length => _data.length;
61 bool get isEmpty => _data.isEmpty;
62 bool get isNotEmpty => _data.isNotEmpty;
63
64 String toString() => 'HttpSession id:$id $_data';
65 }
66
67 // Private class used to manage all the active sessions. The sessions are stored
68 // in two ways:
69 //
70 // * In a map, mapping from ID to HttpSession.
71 // * In a linked list, used as a timeout queue.
72 class _HttpSessionManager {
73 Map<String, _HttpSession> _sessions;
74 int _sessionTimeout = 20 * 60; // 20 mins.
75 _HttpSession _head;
76 _HttpSession _tail;
77 Timer _timer;
78
79 _HttpSessionManager() : _sessions = {};
80
81 String createSessionId() {
82 const int _KEY_LENGTH = 16; // 128 bits.
83 var data = _IOCrypto.getRandomBytes(_KEY_LENGTH);
84 return _CryptoUtils.bytesToHex(data);
85 }
86
87 _HttpSession getSession(String id) => _sessions[id];
88
89 _HttpSession createSession() {
90 var id = createSessionId();
91 // TODO(ajohnsen): Consider adding a limit and throwing an exception.
92 // Should be very unlikely however.
93 while (_sessions.containsKey(id)) {
94 id = createSessionId();
95 }
96 var session = _sessions[id] = new _HttpSession(this, id);
97 _addToTimeoutQueue(session);
98 return session;
99 }
100
101 void set sessionTimeout(int timeout) {
102 _sessionTimeout = timeout;
103 _stopTimer();
104 _startTimer();
105 }
106
107 void close() { _stopTimer(); }
108
109 void _bumpToEnd(_HttpSession session) {
110 _removeFromTimeoutQueue(session);
111 _addToTimeoutQueue(session);
112 }
113
114 void _addToTimeoutQueue(_HttpSession session) {
115 if (_head == null) {
116 assert(_tail == null);
117 _tail = _head = session;
118 _startTimer();
119 } else {
120 assert(_timer != null);
121 assert(_tail != null);
122 // Add to end.
123 _tail._next = session;
124 session._prev = _tail;
125 _tail = session;
126 }
127 }
128
129 void _removeFromTimeoutQueue(_HttpSession session) {
130 if (session._next != null) {
131 session._next._prev = session._prev;
132 }
133 if (session._prev != null) {
134 session._prev._next = session._next;
135 }
136 if (_head == session) {
137 // We removed the head element, start new timer.
138 _head = session._next;
139 _stopTimer();
140 _startTimer();
141 }
142 if (_tail == session) {
143 _tail = session._prev;
144 }
145 session._next = session._prev = null;
146 }
147
148 void _timerTimeout() {
149 _stopTimer(); // Clear timer.
150 assert(_head != null);
151 var session = _head;
152 session.destroy(); // Will remove the session from timeout queue and map.
153 if (session._timeoutCallback != null) {
154 session._timeoutCallback();
155 }
156 }
157
158 void _startTimer() {
159 assert(_timer == null);
160 if (_head != null) {
161 int seconds = new DateTime.now().difference(_head.lastSeen).inSeconds;
162 _timer = new Timer(new Duration(seconds: _sessionTimeout - seconds),
163 _timerTimeout);
164 }
165 }
166
167 void _stopTimer() {
168 if (_timer != null) {
169 _timer.cancel();
170 _timer = null;
171 }
172 }
173 }
174
OLDNEW
« no previous file with comments | « pkg/dev_compiler/tool/input_sdk/lib/io/http_parser.dart ('k') | pkg/dev_compiler/tool/input_sdk/lib/io/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698