namespace StockSharp.Diagram;
using Ecng.Configuration;
///
/// The synchronization object for the composite elements debugger.
///
public class DebuggerSyncObject : ViewModelBase
{
private readonly Func _isBreak;
private readonly Action _breakAction;
private readonly Action _errorAction;
private readonly SyncObject _inputSync = new();
private readonly DiagramDebugger _debugger;
private readonly CompositionDiagramElement _rootElement;
private DiagramElement _currentElement;
private bool _waitOnNext;
private DiagramSocket _currentSocket;
private Exception _currentError;
private class DebuggerSyncObjectGuiWrapper(DebuggerSyncObject obj) : DispatcherNotifiableObject(ConfigManager.GetService(), obj)
{
}
///
/// Gui wrapper for property binding.
///
public INotifyPropertyChanged GuiWrapper {get;}
///
/// The current element.
///
public DiagramElement CurrentElement
{
get => _currentElement;
private set => SetField(ref _currentElement, value);
}
///
/// The current socket.
///
public DiagramSocket CurrentSocket
{
get => _currentSocket;
private set => SetField(ref _currentSocket, value);
}
///
/// The current error.
///
public Exception CurrentError
{
get => _currentError;
private set => SetField(ref _currentError, value);
}
///
/// Initializes a new instance of the .
///
///
/// The root diagram element.
/// The handler that returns a stop flag for the socket.
/// The action with the element at stop.
/// The action with the element at error.
public DebuggerSyncObject(DiagramDebugger debugger, CompositionDiagramElement rootElement, Func isBreak, Action breakAction, Action errorAction)
{
_debugger = debugger ?? throw new ArgumentNullException(nameof(debugger));
_rootElement = rootElement ?? throw new ArgumentNullException(nameof(rootElement));
_isBreak = isBreak ?? throw new ArgumentNullException(nameof(isBreak));
_breakAction = breakAction ?? throw new ArgumentNullException(nameof(breakAction));
_errorAction = errorAction ?? throw new ArgumentNullException(nameof(errorAction));
GuiWrapper = new DebuggerSyncObjectGuiWrapper(this);
}
///
protected override void DisposeManaged()
{
GuiWrapper.DoDispose();
base.DisposeManaged();
}
///
/// , if the debugger is stopped at the entry of the diagram element. Otherwise, .
///
public bool IsWaitingOnInput { get; private set; }
///
/// , if the debugger is stopped at the exit of the diagram element. Otherwise, .
///
public bool IsWaitingOnOutput { get; private set; }
///
/// Try wait on socket.
///
///
/// Is wait on input.
/// Operation result.
public bool TryWait(DiagramSocket socket, bool isOnInput)
{
if (_debugger.IsDisabled)
return false;
if (!_isBreak(socket) && !_waitOnNext)
{
if (socket.Parent == CurrentElement)
CurrentElement = null;
return false;
}
try
{
socket.IsBreakActive = true;
CurrentSocket = socket;
CurrentElement = socket.Parent;
_breakAction?.Invoke(this);
SetIsWaiting(isOnInput, true);
_waitOnNext = false;
_inputSync.Wait();
}
finally
{
socket.IsBreakActive = false;
CurrentSocket = null;
CurrentElement = null;
}
SetIsWaiting(isOnInput, false);
return true;
}
///
/// Try wait on error.
///
///
/// Error.
/// Operation result.
public bool TryWaitOnError(DiagramElement element, Exception error)
{
if (element is null)
throw new ArgumentNullException(nameof(element));
if (error is null)
throw new ArgumentNullException(nameof(error));
if (_debugger.IsDisabled)
return false;
if (element == _rootElement)
return false;
CurrentError = error;
CurrentElement = element;
_errorAction?.Invoke(this);
SetIsWaiting(true, true);
_waitOnNext = false;
_inputSync.Wait();
CurrentError = null;
CurrentElement = null;
SetIsWaiting(true, false);
return true;
}
private void SetIsWaiting(bool isOnInput, bool value)
{
if (isOnInput)
IsWaitingOnInput = value;
else
IsWaitingOnOutput = value;
}
///
/// To set the flag for waiting at the entry of the next diagram element.
///
public void SetWaitOnNext()
{
_waitOnNext = true;
}
///
/// Continue.
///
public void Continue()
{
if (IsWaitingOnInput || IsWaitingOnOutput)
_inputSync.Pulse();
}
}