/* Copyright (c) Citrix Systems, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, * with or without modification, are permitted provided * that the following conditions are met: * * * Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; using XenAdmin.Core; using XenAPI; using System.Collections; using XenAdmin.ServerDBs.FakeAPI; namespace XenAdmin.ServerDBs { internal class HighLoadEventGenerator { private readonly DbProxy _proxy; private DateTime lastHighLoadEvent; private bool _highLoadMode; public HighLoadEventGenerator(DbProxy proxy) { Util.ThrowIfParameterNull(proxy, "proxy"); _proxy = proxy; } /// /// Gets or sets a value indicating whether high-load mode should be turned on. /// /// true if [high load mode]; otherwise, false. private bool HighLoadMode { get { return _highLoadMode; } set { _highLoadMode = value; } } public List GetHighLoadEvents() { return GetHighLoadEvents(new string[] {"*"}, "mod"); } public List GetHighLoadEvents(string[] classes, string eventOperation) { // this fires an event every 2 seconds (for this connection). This is roughly equivalent to the rate at which events occur // during a VM boot up. ReadOnlyCollection typeNames = classes.Length == 0 || (classes.Length == 1 && classes[0] == "*") ? SimpleProxyMethodParser.AllTypes : new ReadOnlyCollection(classes); List output = new List(); if (HighLoadMode && DateTime.Now - lastHighLoadEvent > TimeSpan.FromSeconds(2)) { lastHighLoadEvent = DateTime.Now; foreach (string typeName in typeNames) { Response resp; if (TryGetAllRecords(typeName, out resp)) { foreach (string opaqueRef in ((Hashtable)resp.Value).Keys) { output.Add(DbProxy.MakeProxyEvent(typeName, opaqueRef, eventOperation, TypeCache.GetProxyType(typeName), _proxy.get_record(typeName, opaqueRef, false))); } } } } return output; } public List GetAddEvents(string[] classes) { var oldHighLoadMode = HighLoadMode; HighLoadMode = true; var output = GetHighLoadEvents(classes, "add"); HighLoadMode = oldHighLoadMode; return output; } private bool TryGetAllRecords(string clazz, out Response response) { response = new Response(); if (_proxy.db.Tables.Keys.Contains(clazz.ToLower())) { fakeUnknown fu = new fakeUnknown(clazz, _proxy); response = fu.get_all_records(); return true; } return false; } } }