OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * Module for sending log entries to the server. | 7 * Module for sending log entries to the server. |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
14 | 14 |
15 /** | 15 /** |
16 * @param {remoting.SignalStrategy} signalStrategy Signal strategy. | 16 * @param {remoting.SignalStrategy} signalStrategy Signal strategy. |
| 17 * @param {remoting.ClientSession.Mode} mode The mode of this connection. |
17 * @constructor | 18 * @constructor |
18 */ | 19 */ |
19 remoting.LogToServer = function(signalStrategy) { | 20 remoting.LogToServer = function(signalStrategy, mode) { |
20 /** @private */ | 21 /** @private */ |
21 this.statsAccumulator_ = new remoting.StatsAccumulator(); | 22 this.statsAccumulator_ = new remoting.StatsAccumulator(); |
22 /** @private */ | 23 /** @private */ |
23 this.sessionId_ = ''; | 24 this.sessionId_ = ''; |
24 /** @private */ | 25 /** @private */ |
25 this.sessionIdGenerationTime_ = 0; | 26 this.sessionIdGenerationTime_ = 0; |
26 /** @private */ | 27 /** @private */ |
27 this.sessionStartTime_ = 0; | 28 this.sessionStartTime_ = 0; |
28 /** @private */ | 29 /** @private */ |
29 this.signalStrategy_ = signalStrategy; | 30 this.signalStrategy_ = signalStrategy; |
| 31 /** @private */ |
| 32 this.mode_ = mode; |
| 33 /** @type {string} @private */ |
| 34 this.connectionType_ = ''; |
30 }; | 35 }; |
31 | 36 |
32 // Constants used for generating a session ID. | 37 // Constants used for generating a session ID. |
33 /** @private */ | 38 /** @private */ |
34 remoting.LogToServer.SESSION_ID_ALPHABET_ = | 39 remoting.LogToServer.SESSION_ID_ALPHABET_ = |
35 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; | 40 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; |
36 /** @private */ | 41 /** @private */ |
37 remoting.LogToServer.SESSION_ID_LEN_ = 20; | 42 remoting.LogToServer.SESSION_ID_LEN_ = 20; |
38 | 43 |
39 // The maximum age of a session ID, in milliseconds. | 44 // The maximum age of a session ID, in milliseconds. |
40 remoting.LogToServer.MAX_SESSION_ID_AGE = 24 * 60 * 60 * 1000; | 45 remoting.LogToServer.MAX_SESSION_ID_AGE = 24 * 60 * 60 * 1000; |
41 | 46 |
42 // The time over which to accumulate connection statistics before logging them | 47 // The time over which to accumulate connection statistics before logging them |
43 // to the server, in milliseconds. | 48 // to the server, in milliseconds. |
44 remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME = 60 * 1000; | 49 remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME = 60 * 1000; |
45 | 50 |
46 /** | 51 /** |
47 * Logs a client session state change. | 52 * Logs a client session state change. |
48 * | 53 * |
49 * @param {remoting.ClientSession.State} state | 54 * @param {remoting.ClientSession.State} state |
50 * @param {remoting.Error} connectionError | 55 * @param {remoting.Error} connectionError |
51 * @param {remoting.ClientSession.Mode} mode | |
52 */ | 56 */ |
53 remoting.LogToServer.prototype.logClientSessionStateChange = | 57 remoting.LogToServer.prototype.logClientSessionStateChange = |
54 function(state, connectionError, mode) { | 58 function(state, connectionError) { |
55 this.maybeExpireSessionId(mode); | 59 this.maybeExpireSessionId(); |
56 // Maybe set the session ID and start time. | 60 // Maybe set the session ID and start time. |
57 if (remoting.LogToServer.isStartOfSession(state)) { | 61 if (remoting.LogToServer.isStartOfSession(state)) { |
58 if (this.sessionId_ == '') { | 62 if (this.sessionId_ == '') { |
59 this.setSessionId(); | 63 this.setSessionId(); |
60 } | 64 } |
61 if (this.sessionStartTime_ == 0) { | 65 if (this.sessionStartTime_ == 0) { |
62 this.sessionStartTime_ = new Date().getTime(); | 66 this.sessionStartTime_ = new Date().getTime(); |
63 } | 67 } |
64 } | 68 } |
65 // Log the session state change. | 69 // Log the session state change. |
66 var entry = remoting.ServerLogEntry.makeClientSessionStateChange( | 70 var entry = remoting.ServerLogEntry.makeClientSessionStateChange( |
67 state, connectionError, mode); | 71 state, connectionError, this.mode_); |
68 entry.addHostFields(); | 72 entry.addHostFields(); |
69 entry.addChromeVersionField(); | 73 entry.addChromeVersionField(); |
70 entry.addWebappVersionField(); | 74 entry.addWebappVersionField(); |
71 entry.addSessionIdField(this.sessionId_); | 75 entry.addSessionIdField(this.sessionId_); |
72 // Maybe clear the session start time, and log the session duration. | 76 // Maybe clear the session start time, and log the session duration. |
73 if (remoting.LogToServer.shouldAddDuration(state) && | 77 if (remoting.LogToServer.shouldAddDuration(state) && |
74 (this.sessionStartTime_ != 0)) { | 78 (this.sessionStartTime_ != 0)) { |
75 entry.addSessionDurationField( | 79 entry.addSessionDurationField( |
76 (new Date().getTime() - this.sessionStartTime_) / 1000.0); | 80 (new Date().getTime() - this.sessionStartTime_) / 1000.0); |
77 if (remoting.LogToServer.isEndOfSession(state)) { | 81 if (remoting.LogToServer.isEndOfSession(state)) { |
78 this.sessionStartTime_ = 0; | 82 this.sessionStartTime_ = 0; |
79 } | 83 } |
80 } | 84 } |
81 this.log(entry); | 85 this.log(entry); |
82 // Don't accumulate connection statistics across state changes. | 86 // Don't accumulate connection statistics across state changes. |
83 this.logAccumulatedStatistics(mode); | 87 this.logAccumulatedStatistics(); |
84 this.statsAccumulator_.empty(); | 88 this.statsAccumulator_.empty(); |
85 // Maybe clear the session ID. | 89 // Maybe clear the session ID. |
86 if (remoting.LogToServer.isEndOfSession(state)) { | 90 if (remoting.LogToServer.isEndOfSession(state)) { |
87 this.clearSessionId(); | 91 this.clearSessionId(); |
88 } | 92 } |
89 }; | 93 }; |
90 | 94 |
91 /** | 95 /** |
| 96 * Set the connection type (direct, stun relay). |
| 97 * |
| 98 * @param {string} connectionType |
| 99 */ |
| 100 remoting.LogToServer.prototype.setConnectionType = function(connectionType) { |
| 101 this.connectionType_ = connectionType; |
| 102 }; |
| 103 |
| 104 /** |
92 * Whether a session state is one of the states that occurs at the start of | 105 * Whether a session state is one of the states that occurs at the start of |
93 * a session. | 106 * a session. |
94 * | 107 * |
95 * @private | 108 * @private |
96 * @param {remoting.ClientSession.State} state | 109 * @param {remoting.ClientSession.State} state |
97 * @return {boolean} | 110 * @return {boolean} |
98 */ | 111 */ |
99 remoting.LogToServer.isStartOfSession = function(state) { | 112 remoting.LogToServer.isStartOfSession = function(state) { |
100 return ((state == remoting.ClientSession.State.CONNECTING) || | 113 return ((state == remoting.ClientSession.State.CONNECTING) || |
101 (state == remoting.ClientSession.State.INITIALIZING) || | 114 (state == remoting.ClientSession.State.INITIALIZING) || |
(...skipping 25 matching lines...) Expand all Loading... |
127 remoting.LogToServer.shouldAddDuration = function(state) { | 140 remoting.LogToServer.shouldAddDuration = function(state) { |
128 // Duration is added to log entries at the end of the session, as well as at | 141 // Duration is added to log entries at the end of the session, as well as at |
129 // some intermediate states where it is relevant (e.g. to determine how long | 142 // some intermediate states where it is relevant (e.g. to determine how long |
130 // it took for a session to become CONNECTED). | 143 // it took for a session to become CONNECTED). |
131 return (remoting.LogToServer.isEndOfSession(state) || | 144 return (remoting.LogToServer.isEndOfSession(state) || |
132 (state == remoting.ClientSession.State.CONNECTED)); | 145 (state == remoting.ClientSession.State.CONNECTED)); |
133 }; | 146 }; |
134 | 147 |
135 /** | 148 /** |
136 * Logs connection statistics. | 149 * Logs connection statistics. |
137 * @param {Object.<string, number>} stats the connection statistics | 150 * @param {Object.<string, number>} stats The connection statistics |
138 * @param {remoting.ClientSession.Mode} mode | |
139 */ | 151 */ |
140 remoting.LogToServer.prototype.logStatistics = function(stats, mode) { | 152 remoting.LogToServer.prototype.logStatistics = function(stats) { |
141 this.maybeExpireSessionId(mode); | 153 this.maybeExpireSessionId(); |
142 // Store the statistics. | 154 // Store the statistics. |
143 this.statsAccumulator_.add(stats); | 155 this.statsAccumulator_.add(stats); |
144 // Send statistics to the server if they've been accumulating for at least | 156 // Send statistics to the server if they've been accumulating for at least |
145 // 60 seconds. | 157 // 60 seconds. |
146 if (this.statsAccumulator_.getTimeSinceFirstValue() >= | 158 if (this.statsAccumulator_.getTimeSinceFirstValue() >= |
147 remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME) { | 159 remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME) { |
148 this.logAccumulatedStatistics(mode); | 160 this.logAccumulatedStatistics(); |
149 } | 161 } |
150 }; | 162 }; |
151 | 163 |
152 /** | 164 /** |
153 * Moves connection statistics from the accumulator to the log server. | 165 * Moves connection statistics from the accumulator to the log server. |
154 * | 166 * |
155 * If all the statistics are zero, then the accumulator is still emptied, | 167 * If all the statistics are zero, then the accumulator is still emptied, |
156 * but the statistics are not sent to the log server. | 168 * but the statistics are not sent to the log server. |
157 * | 169 * |
158 * @private | 170 * @private |
159 * @param {remoting.ClientSession.Mode} mode | |
160 */ | 171 */ |
161 remoting.LogToServer.prototype.logAccumulatedStatistics = function(mode) { | 172 remoting.LogToServer.prototype.logAccumulatedStatistics = function() { |
162 var entry = remoting.ServerLogEntry.makeStats(this.statsAccumulator_, mode); | 173 var entry = remoting.ServerLogEntry.makeStats(this.statsAccumulator_, |
| 174 this.connectionType_, |
| 175 this.mode_); |
163 if (entry) { | 176 if (entry) { |
164 entry.addHostFields(); | 177 entry.addHostFields(); |
165 entry.addChromeVersionField(); | 178 entry.addChromeVersionField(); |
166 entry.addWebappVersionField(); | 179 entry.addWebappVersionField(); |
167 entry.addSessionIdField(this.sessionId_); | 180 entry.addSessionIdField(this.sessionId_); |
168 this.log(entry); | 181 this.log(entry); |
169 } | 182 } |
170 this.statsAccumulator_.empty(); | 183 this.statsAccumulator_.empty(); |
171 }; | 184 }; |
172 | 185 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 this.sessionIdGenerationTime_ = 0; | 223 this.sessionIdGenerationTime_ = 0; |
211 }; | 224 }; |
212 | 225 |
213 /** | 226 /** |
214 * Sets a new session ID, if the current session ID has reached its maximum age. | 227 * Sets a new session ID, if the current session ID has reached its maximum age. |
215 * | 228 * |
216 * This method also logs the old and new session IDs to the server, in separate | 229 * This method also logs the old and new session IDs to the server, in separate |
217 * log entries. | 230 * log entries. |
218 * | 231 * |
219 * @private | 232 * @private |
220 * @param {remoting.ClientSession.Mode} mode | |
221 */ | 233 */ |
222 remoting.LogToServer.prototype.maybeExpireSessionId = function(mode) { | 234 remoting.LogToServer.prototype.maybeExpireSessionId = function() { |
223 if ((this.sessionId_ != '') && | 235 if ((this.sessionId_ != '') && |
224 (new Date().getTime() - this.sessionIdGenerationTime_ >= | 236 (new Date().getTime() - this.sessionIdGenerationTime_ >= |
225 remoting.LogToServer.MAX_SESSION_ID_AGE)) { | 237 remoting.LogToServer.MAX_SESSION_ID_AGE)) { |
226 // Log the old session ID. | 238 // Log the old session ID. |
227 var entry = remoting.ServerLogEntry.makeSessionIdOld(this.sessionId_, mode); | 239 var entry = remoting.ServerLogEntry.makeSessionIdOld(this.sessionId_, |
| 240 this.mode_); |
228 this.log(entry); | 241 this.log(entry); |
229 // Generate a new session ID. | 242 // Generate a new session ID. |
230 this.setSessionId(); | 243 this.setSessionId(); |
231 // Log the new session ID. | 244 // Log the new session ID. |
232 entry = remoting.ServerLogEntry.makeSessionIdNew(this.sessionId_, mode); | 245 entry = remoting.ServerLogEntry.makeSessionIdNew(this.sessionId_, |
| 246 this.mode_); |
233 this.log(entry); | 247 this.log(entry); |
234 } | 248 } |
235 }; | 249 }; |
236 | 250 |
237 /** | 251 /** |
238 * Generates a string that can be used as a session ID. | 252 * Generates a string that can be used as a session ID. |
239 * | 253 * |
240 * @private | 254 * @private |
241 * @return {string} a session ID | 255 * @return {string} a session ID |
242 */ | 256 */ |
243 remoting.LogToServer.generateSessionId = function() { | 257 remoting.LogToServer.generateSessionId = function() { |
244 var idArray = []; | 258 var idArray = []; |
245 for (var i = 0; i < remoting.LogToServer.SESSION_ID_LEN_; i++) { | 259 for (var i = 0; i < remoting.LogToServer.SESSION_ID_LEN_; i++) { |
246 var index = | 260 var index = |
247 Math.random() * remoting.LogToServer.SESSION_ID_ALPHABET_.length; | 261 Math.random() * remoting.LogToServer.SESSION_ID_ALPHABET_.length; |
248 idArray.push( | 262 idArray.push( |
249 remoting.LogToServer.SESSION_ID_ALPHABET_.slice(index, index + 1)); | 263 remoting.LogToServer.SESSION_ID_ALPHABET_.slice(index, index + 1)); |
250 } | 264 } |
251 return idArray.join(''); | 265 return idArray.join(''); |
252 }; | 266 }; |
OLD | NEW |