2023-01-24 15:29:31 +01:00
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
*
|
2023-01-24 15:29:31 +01:00
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
* that the following conditions are met:
|
2013-06-24 13:41:48 +02:00
|
|
|
*
|
2023-01-24 15:29:31 +01:00
|
|
|
* * 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.
|
2013-06-24 13:41:48 +02:00
|
|
|
*
|
2023-01-24 15:29:31 +01:00
|
|
|
* 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.
|
2013-06-24 13:41:48 +02:00
|
|
|
*/
|
|
|
|
|
2013-07-03 12:22:08 +02:00
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Net;
|
2014-07-03 11:00:05 +02:00
|
|
|
using System.Runtime.Serialization;
|
2017-05-24 10:23:47 +02:00
|
|
|
using XenAPI;
|
2017-11-17 02:04:45 +01:00
|
|
|
using XenCenterLib;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2017-05-24 10:23:47 +02:00
|
|
|
namespace XenAdmin.Core
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2014-07-03 11:00:05 +02:00
|
|
|
[Serializable]
|
2017-05-24 10:23:47 +02:00
|
|
|
public class EventFromBlockedException : Exception
|
2014-07-03 11:00:05 +02:00
|
|
|
{
|
2017-05-24 10:23:47 +02:00
|
|
|
public EventFromBlockedException() { }
|
2014-07-03 11:00:05 +02:00
|
|
|
|
2017-05-24 10:23:47 +02:00
|
|
|
public EventFromBlockedException(string message) : base(message) { }
|
2014-07-03 11:00:05 +02:00
|
|
|
|
2017-05-24 10:23:47 +02:00
|
|
|
public EventFromBlockedException(string message, Exception exception) : base(message, exception) { }
|
2014-07-03 11:00:05 +02:00
|
|
|
|
2017-05-24 10:23:47 +02:00
|
|
|
protected EventFromBlockedException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
2014-07-03 11:00:05 +02:00
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
public static class XenObjectDownloader
|
|
|
|
{
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
private const double EVENT_FROM_TIMEOUT = 30; // 30 seconds
|
|
|
|
|
2017-05-24 10:23:47 +02:00
|
|
|
/// <summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
/// Gets all objects from the server. Used in order to fill the cache.
|
|
|
|
/// This function implements the new event system, available from in API version 1.9.
|
|
|
|
/// In the new event system, (GetAllRecords + GetEvents) sequence will replace (RegisterForEvents + DownloadObjects + GetNextEvents).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="session">The session over which to download the objects. Must not be null.</param>
|
|
|
|
/// <param name="changes">The queue that the ObjectChanges will be put into. Must not be null.</param>
|
|
|
|
/// <param name="cancelled">Used by GetEvents().</param>
|
|
|
|
/// <param name="token">Used by GetEvents().</param>
|
2017-05-24 00:12:06 +02:00
|
|
|
public static void GetAllObjects(Session session, LockFreeQueue<ObjectChange> changes, HTTP.FuncBool cancelled, ref string token)
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
|
|
|
// download objects that are not covered by event.from(), e.g. Roles
|
2017-05-24 00:12:06 +02:00
|
|
|
|
|
|
|
var roleRecords = Role.get_all_records(session);
|
|
|
|
foreach (KeyValuePair<XenRef<Role>, Role> entry in roleRecords)
|
|
|
|
changes.Enqueue(new ObjectChange(typeof(Role), entry.Key.opaque_ref, entry.Value));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
// get all objects with event.from()
|
|
|
|
token = "";
|
2017-05-24 00:12:06 +02:00
|
|
|
GetEvents(session, changes, cancelled, ref token);
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Blocks until events are sent on the session, or timeout is reached, then processes any received events and adds them
|
|
|
|
/// to eventQueue. This function implements the new event system, available in API version 1.9.
|
|
|
|
/// In the new event system, (GetAllRecords + GetEvents) sequence will replace (RegisterForEvents + DownloadObjects + GetNextEvents).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="session"></param>
|
|
|
|
/// <param name="eventQueue"></param>
|
|
|
|
/// <param name="cancelled"></param>
|
|
|
|
/// <param name="token">A token used by event.from().
|
|
|
|
/// It should be the empty string when event.from is first called, which is the replacement of get_all_records.
|
|
|
|
/// </param>
|
2017-05-24 00:12:06 +02:00
|
|
|
public static void GetEvents(Session session, LockFreeQueue<ObjectChange> eventQueue, HTTP.FuncBool cancelled, ref string token)
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2017-09-13 18:14:07 +02:00
|
|
|
Event[] events = {};
|
2013-06-24 13:41:48 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var classes = new [] { "*" }; // classes that we are interested in receiving events from
|
|
|
|
var eventResult = Event.from(session, classes, token, EVENT_FROM_TIMEOUT);
|
2017-09-13 18:14:07 +02:00
|
|
|
|
2023-03-10 14:28:23 +01:00
|
|
|
var batch = (EventBatch)eventResult;
|
|
|
|
events = batch.events;
|
|
|
|
token = batch.token;
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
catch (WebException e)
|
|
|
|
{
|
2017-05-24 10:23:47 +02:00
|
|
|
// Catch timeout, and turn it into an EventFromBlockedException so we can recognise it later (CA-33145)
|
2013-06-24 13:41:48 +02:00
|
|
|
if (e.Status == WebExceptionStatus.Timeout)
|
2017-05-24 10:23:47 +02:00
|
|
|
throw new EventFromBlockedException();
|
2023-03-10 14:28:23 +01:00
|
|
|
|
|
|
|
throw;
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cancelled())
|
|
|
|
return;
|
|
|
|
|
2023-03-10 14:28:23 +01:00
|
|
|
foreach (Event evt in events)
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2023-03-10 14:28:23 +01:00
|
|
|
var objectChange = ProcessEvent(evt.class_, evt.operation, evt.opaqueRef, evt.snapshot, false);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2023-03-10 14:28:23 +01:00
|
|
|
if (objectChange != null)
|
|
|
|
eventQueue.Enqueue(objectChange);
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns null if we get an event we're not interested in, or an unparseable event (e.g. for an object type we don't know about).
|
|
|
|
/// </summary>
|
2017-09-13 18:14:07 +02:00
|
|
|
private static ObjectChange ProcessEvent(string class_, string operation, string opaqueRef, object snapshot, bool marshall)
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2017-09-13 18:14:07 +02:00
|
|
|
switch (class_.ToLowerInvariant())
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
|
|
|
case "session":
|
|
|
|
case "event":
|
|
|
|
case "user":
|
|
|
|
case "secret":
|
|
|
|
// We don't track events on these objects
|
|
|
|
return null;
|
|
|
|
|
|
|
|
default:
|
2017-09-13 18:14:07 +02:00
|
|
|
Type typ = Marshalling.GetXenAPIType(class_);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
if (typ == null)
|
|
|
|
{
|
2017-09-13 18:14:07 +02:00
|
|
|
log.DebugFormat("Unknown {0} event for class {1}.", operation, class_);
|
2013-06-24 13:41:48 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:14:07 +02:00
|
|
|
switch (operation)
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
|
|
|
case "add":
|
|
|
|
case "mod":
|
2017-09-13 18:14:07 +02:00
|
|
|
var marshalled = marshall ? Marshalling.convertStruct(typ, (Hashtable)snapshot) : snapshot;
|
|
|
|
return new ObjectChange(typ, opaqueRef, marshalled);
|
2013-06-24 13:41:48 +02:00
|
|
|
case "del":
|
2017-09-13 18:14:07 +02:00
|
|
|
return new ObjectChange(typ, opaqueRef, null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
default:
|
2017-09-13 18:14:07 +02:00
|
|
|
log.DebugFormat("Unknown event operation {0} for opaque ref {1}", operation, opaqueRef);
|
2013-06-24 13:41:48 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|