diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-11 23:25:05 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-11 23:25:05 +0000 |
commit | 11cd112650c6c51b270eeae329d63a7cd732e605 (patch) | |
tree | 06d1122825e90616c5410157d83750800baf4540 /remoting/webapp/log_to_server.js | |
parent | 2c9f0def901b383bb003c4d10c198cabec1277f6 (diff) | |
download | chromium_src-11cd112650c6c51b270eeae329d63a7cd732e605.zip chromium_src-11cd112650c6c51b270eeae329d63a7cd732e605.tar.gz chromium_src-11cd112650c6c51b270eeae329d63a7cd732e605.tar.bz2 |
Rename webapp_it2me to remoting_webapp and move it from webapp/me2mom to webapp/
The remoting webapp is not it2me specific anymore. Renaming stuff and
moving it to avoid confusion, particularly for the newcomers.
Review URL: http://codereview.chromium.org/9148043
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117322 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/webapp/log_to_server.js')
-rw-r--r-- | remoting/webapp/log_to_server.js | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/remoting/webapp/log_to_server.js b/remoting/webapp/log_to_server.js new file mode 100644 index 0000000..92841cc --- /dev/null +++ b/remoting/webapp/log_to_server.js @@ -0,0 +1,237 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * @fileoverview + * Module for sending log entries to the server. + */ + +'use strict'; + +/** @suppress {duplicate} */ +var remoting = remoting || {}; + +/** + * @constructor + */ +remoting.LogToServer = function() { + /** @type Array.<string> */ + this.pendingEntries = []; + /** @type {remoting.StatsAccumulator} */ + this.statsAccumulator = new remoting.StatsAccumulator(); + /** @type string */ + this.sessionId = ''; + /** @type number */ + this.sessionIdGenerationTime = 0; + /** @type number */ + this.sessionStartTime = 0; +}; + +// Constants used for generating a session ID. +/** @private */ +remoting.LogToServer.SESSION_ID_ALPHABET_ = + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; +/** @private */ +remoting.LogToServer.SESSION_ID_LEN_ = 20; + +// The maximum age of a session ID, in milliseconds. +remoting.LogToServer.MAX_SESSION_ID_AGE = 24 * 60 * 60 * 1000; + +// The time over which to accumulate connection statistics before logging them +// to the server, in milliseconds. +remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME = 60 * 1000; + +/** + * Logs a client session state change. + * + * @param {remoting.ClientSession.State} state + * @param {remoting.ClientSession.ConnectionError} connectionError + */ +remoting.LogToServer.prototype.logClientSessionStateChange = + function(state, connectionError) { + this.maybeExpireSessionId(); + // Maybe set the session ID and start time. + if (remoting.LogToServer.isStartOfSession(state)) { + if (this.sessionId == '') { + this.setSessionId(); + } + if (this.sessionStartTime == 0) { + this.sessionStartTime = new Date().getTime(); + } + } + // Log the session state change. + var entry = remoting.ServerLogEntry.makeClientSessionStateChange( + state, connectionError); + entry.addHostFields(); + entry.addChromeVersionField(); + entry.addWebappVersionField(); + entry.addSessionIdField(this.sessionId); + // Maybe clear the session start time, and log the session duration. + if (remoting.LogToServer.isEndOfSession(state) && + (this.sessionStartTime != 0)) { + entry.addSessionDurationField( + (new Date().getTime() - this.sessionStartTime) / 1000.0); + this.sessionStartTime = 0; + } + this.log(entry); + // Don't accumulate connection statistics across state changes. + this.logAccumulatedStatistics(); + this.statsAccumulator.empty(); + // Maybe clear the session ID. + if (remoting.LogToServer.isEndOfSession(state)) { + this.clearSessionId(); + } +}; + +/** + * Whether a session state is one of the states that occurs at the start of + * a session. + * + * @private + * @param {remoting.ClientSession.State} state + * @return {boolean} + */ +remoting.LogToServer.isStartOfSession = function(state) { + return ((state == remoting.ClientSession.State.CONNECTING) || + (state == remoting.ClientSession.State.INITIALIZING) || + (state == remoting.ClientSession.State.CONNECTED)); +}; + +/** + * Whether a session state is one of the states that occurs at the end of + * a session. + * + * @private + * @param {remoting.ClientSession.State} state + * @return {boolean} + */ +remoting.LogToServer.isEndOfSession = function(state) { + return ((state == remoting.ClientSession.State.CLOSED) || + (state == remoting.ClientSession.State.CONNECTION_FAILED)); +}; + +/** + * Logs connection statistics. + * @param {Object.<string, number>} stats the connection statistics + */ +remoting.LogToServer.prototype.logStatistics = function(stats) { + this.maybeExpireSessionId(); + // Store the statistics. + this.statsAccumulator.add(stats); + // Send statistics to the server if they've been accumulating for at least + // 60 seconds. + if (this.statsAccumulator.getTimeSinceFirstValue() >= + remoting.LogToServer.CONNECTION_STATS_ACCUMULATE_TIME) { + this.logAccumulatedStatistics(); + } +}; + +/** + * Moves connection statistics from the accumulator to the log server. + * + * If all the statistics are zero, then the accumulator is still emptied, + * but the statistics are not sent to the log server. + * + * @private + */ +remoting.LogToServer.prototype.logAccumulatedStatistics = function() { + var entry = remoting.ServerLogEntry.makeStats(this.statsAccumulator); + if (entry) { + entry.addHostFields(); + entry.addChromeVersionField(); + entry.addWebappVersionField(); + entry.addSessionIdField(this.sessionId); + this.log(entry); + } + this.statsAccumulator.empty(); +}; + +/** + * Sends a log entry to the server. + * + * @private + * @param {remoting.ServerLogEntry} entry + */ +remoting.LogToServer.prototype.log = function(entry) { + // Send the stanza to the debug log. + remoting.debug.log('Enqueueing log entry:'); + entry.toDebugLog(1); + // Store a stanza for the entry. + this.pendingEntries.push(entry.toStanza()); + // Stop if there's no connection to the server. + if (!remoting.wcs) { + return; + } + // Send all pending entries to the server. + remoting.debug.log('Sending ' + this.pendingEntries.length + ' log ' + + ((this.pendingEntries.length == 1) ? 'entry' : 'entries') + + ' to the server.'); + var stanza = '<cli:iq to="remoting@bot.talk.google.com" type="set" ' + + 'xmlns:cli="jabber:client"><gr:log xmlns:gr="google:remoting">'; + while (this.pendingEntries.length > 0) { + stanza += /** @type string */ this.pendingEntries.shift(); + } + stanza += '</gr:log></cli:iq>'; + remoting.wcs.sendIq(stanza); +}; + +/** + * Sets the session ID to a random string. + * + * @private + */ +remoting.LogToServer.prototype.setSessionId = function() { + this.sessionId = remoting.LogToServer.generateSessionId(); + this.sessionIdGenerationTime = new Date().getTime(); +}; + +/** + * Clears the session ID. + * + * @private + */ +remoting.LogToServer.prototype.clearSessionId = function() { + this.sessionId = ''; + this.sessionIdGenerationTime = 0; +}; + +/** + * Sets a new session ID, if the current session ID has reached its maximum age. + * + * This method also logs the old and new session IDs to the server, in separate + * log entries. + * + * @private + */ +remoting.LogToServer.prototype.maybeExpireSessionId = function() { + if ((this.sessionId != '') && + (new Date().getTime() - this.sessionIdGenerationTime >= + remoting.LogToServer.MAX_SESSION_ID_AGE)) { + // Log the old session ID. + var entry = remoting.ServerLogEntry.makeSessionIdOld(this.sessionId); + this.log(entry); + // Generate a new session ID. + this.setSessionId(); + // Log the new session ID. + entry = remoting.ServerLogEntry.makeSessionIdNew(this.sessionId); + this.log(entry); + } +}; + +/** + * Generates a string that can be used as a session ID. + * + * @private + * @return {string} a session ID + */ +remoting.LogToServer.generateSessionId = function() { + var idArray = []; + for (var i = 0; i < remoting.LogToServer.SESSION_ID_LEN_; i++) { + var index = + Math.random() * remoting.LogToServer.SESSION_ID_ALPHABET_.length; + idArray.push( + remoting.LogToServer.SESSION_ID_ALPHABET_.slice(index, index + 1)); + } + return idArray.join(''); +}; |