AsperaFaspProxy
using Aspera.Transfer;
using Relativity.Transfer.Aspera.Resources;
using Relativity.Transfer.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Relativity.Transfer.Aspera
{
internal class AsperaFaspProxy : IAsperaFaspProxy, IDisposable
{
private static readonly object SyncRoot = new object();
private readonly ITransferLog log;
private readonly AsperaRuntime runtime;
private readonly AsperaClientConfiguration configuration;
private readonly IFileSystemService fileSystemService;
private FaspManager faspManager;
private bool disposed;
public AsperaFaspProxy(ITransferLog log, AsperaClientConfiguration configuration, IFileSystemService fileSystemService)
{
if (log == null)
throw new ArgumentNullException("log");
if (configuration == null)
throw new ArgumentNullException("configuration");
if (fileSystemService == null)
throw new ArgumentNullException("fileSystemService");
disposed = false;
faspManager = null;
this.log = log;
this.configuration = configuration;
this.fileSystemService = fileSystemService;
runtime = new AsperaRuntime(this.log);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void AddJobListener(Guid sessionId, FileTransferListener listener)
{
lock (SyncRoot) {
CheckInternalState();
faspManager.addJobListener(sessionId.ToString(), listener);
}
}
public void AddPersistentPath(Guid sessionId, TransferPath path)
{
if (path == (TransferPath)null)
throw new ArgumentNullException("path");
if (string.IsNullOrEmpty(path.SourcePath))
throw new ArgumentException(CoreStrings.SourcePathArgumentExceptionMessage, "path");
if (string.IsNullOrEmpty(path.TargetPath))
throw new ArgumentException(CoreStrings.TargetPathArgumentExceptionMessage, "path");
lock (SyncRoot) {
CheckInternalState();
string text = string.IsNullOrEmpty(path.TargetFileName) ? fileSystemService.GetFileName(path.SourcePath) : path.TargetFileName;
string destPath = (path.Direction != TransferDirection.Upload) ? (fileSystemService.IsUncPath(path.TargetPath) ? fileSystemService.CombineUnc(path.TargetPath, text) : fileSystemService.Combine(path.TargetPath, text)) : PathHelper.CombineUnix(path.TargetPath, text);
lock (SyncRoot) {
faspManager.addSource(sessionId.ToString(), path.SourcePath, destPath);
}
}
}
public void CancelTransfer(Guid sessionId, bool persistentSession)
{
lock (SyncRoot) {
CheckInternalState();
if (persistentSession)
faspManager.cancelTransfer(sessionId.ToString());
else
faspManager.terminateTransfer(sessionId.ToString());
}
}
public void DebugEnabled(bool value)
{
lock (SyncRoot) {
CheckInternalState();
faspManager.enableFaspDebug(value);
}
}
public IEnumerable<Guid> GetSessionIds()
{
lock (SyncRoot) {
CheckInternalState();
return from string x in faspManager.getSessionIDList()
select new Guid(x);
}
}
public SessionStats GetSessionStatistics(Guid sessionId)
{
lock (SyncRoot) {
CheckInternalState();
return faspManager.getSessionStats(sessionId.ToString());
}
}
public void SetupRuntime()
{
if (faspManager == null) {
lock (SyncRoot) {
if (faspManager == null) {
runtime.Install(configuration);
faspManager = FaspManager.getInstance();
faspManager.AutoRemoveCompletedTransferReferences = false;
AppDomain.CurrentDomain.UnhandledException += OnAsperaUnhandledException;
}
}
}
}
public void LockPersistentSession(Guid sessionId)
{
lock (SyncRoot) {
try {
CheckInternalState();
faspManager.lockPersistentSession(sessionId.ToString());
} catch (FaspManagerException exception) {
throw new TransferException(AsperaStrings.AsperaClientFaspManagerExceptionMessage, AsperaErrorHelper.IsFatalFaspException(exception));
}
}
}
public void RemoveJobListener(Guid sessionId, FileTransferListener listener)
{
lock (SyncRoot) {
CheckInternalState();
faspManager.removeJobListener(sessionId.ToString(), listener);
}
}
public void RemoveSession(Guid sessionId)
{
lock (SyncRoot) {
CheckInternalState();
faspManager.removeTransferReference(sessionId.ToString());
}
}
public void SetRate(Guid sessionId, int targetRateKbps, int minRateKbps, Policy policy)
{
lock (SyncRoot) {
CheckInternalState();
faspManager.setRate(sessionId.ToString(), targetRateKbps, minRateKbps, policy);
}
}
public Guid StartTransfer(JobOrder order, Guid jobId, int transferRetryCount, FileTransferListener listener)
{
lock (SyncRoot) {
CheckInternalState();
return new Guid(faspManager.startTransfer(order, jobId, transferRetryCount, listener));
}
}
private void CheckInternalState()
{
if (disposed)
throw new ObjectDisposedException(AsperaStrings.FaspProxyDisposedExceptionMessage);
if (faspManager != null)
return;
throw new TransferException(AsperaStrings.FaspProxyNotInitializedExceptionMessage);
}
private void OnAsperaUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
FaspManagerException ex = e.ExceptionObject as FaspManagerException;
if (ex != null)
log.LogError(ex, "The Aspera FASP manager threw an unhandled exception.", Array.Empty<object>());
else {
Exception ex2 = e.ExceptionObject as Exception;
if (ex2 != null)
log.LogError(ex2, "An unhandled exception was thrown.", Array.Empty<object>());
else
log.LogError("An unknown unhandled exception was thrown.", Array.Empty<object>());
}
}
private void Dispose(bool disposing)
{
if (!disposed) {
if (disposing) {
lock (SyncRoot) {
faspManager = null;
}
AppDomain.CurrentDomain.UnhandledException -= OnAsperaUnhandledException;
}
disposed = true;
}
}
}
}