Skip to content

Commit

Permalink
fixed un-registering of callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
braunms committed Sep 12, 2023
1 parent 11912ee commit 45ee7d0
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Visualizations/BaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override bool Initialize()
_timer.Start();

// Content Manager
bool initialized = _contentmanager.Initialize(_datamanager.RequestDataCallback, _datamanager.RegisterUpdateCallback);
bool initialized = _contentmanager.Initialize(_datamanager.RequestDataCallback, _datamanager.RegisterUpdateCallback, _datamanager.UnregisterUpdateCallback);
var required_services = _contentmanager.DependingServices();

// Data Manager
Expand Down Expand Up @@ -91,7 +91,7 @@ public bool ApplyConfigurations(string configuration)

public ContentCallbacks_Type GetContentCallbacks()
{
return new ContentCallbacks_Type(_contentmanager.AvailableContents, _contentmanager.CreateContent, _contentmanager.DeleteContent);
return new ContentCallbacks_Type(_contentmanager.AvailableContentsCallback, _contentmanager.CreateContentCallback, _contentmanager.DeleteContentCallback);
}

public DataManager.InputData_Delegate GetInputDataCallback()
Expand Down
29 changes: 23 additions & 6 deletions Visualizations/Data/DataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public class DataManager : AbstractService
/// Callback to register callback for getting notified on any data update
/// </summary>
public delegate void RegisterUpdateCallback_Delegate(UpdateCallback_Delegate update_callback);
public delegate void UnregisterCallback_Delegate(UpdateCallback_Delegate update_callback);

/// <summary>
/// Callback called on updated data
/// </summary>
public delegate void UpdateCallback_Delegate(bool new_data);


/* ------------------------------------------------------------------*/
// public functions

Expand Down Expand Up @@ -122,14 +122,14 @@ public override bool Terminate()
/// <param name="input_data">Reference to the new input data.</param>
public void GetInputDataCallback(ref GenericDataStructure input_data)
{
if (input_data == null)
if (!_initialized)
{
Log.Default.Msg(Log.Level.Warn, "No input data available");
Log.Default.Msg(Log.Level.Error, "Initialization required prior to execution");
return;
}
if (!_initialized)
if (input_data == null)
{
Log.Default.Msg(Log.Level.Error, "Initialization required prior to execution");
Log.Default.Msg(Log.Level.Warn, "No input data available");
return;
}
_timer.Start();
Expand Down Expand Up @@ -182,12 +182,29 @@ public void RegisterUpdateCallback(UpdateCallback_Delegate update_callback)
{
if (_updated_callbacks.Contains(update_callback))
{
Log.Default.Msg(Log.Level.Debug, "callback for updated data already registered");
Log.Default.Msg(Log.Level.Debug, "Callback for updated data already registered");
return;
}
_updated_callbacks.Add(update_callback);
}

/// <summary>
/// Notify registered callers on updated input data. Called by visualizations.
/// </summary>
public void UnregisterUpdateCallback(UpdateCallback_Delegate update_callback)
{
/// XXX Required since DataManager might be deleted before VisualizationsManager
if (_updated_callbacks != null)
{
if (_updated_callbacks.Contains(update_callback))
{
_updated_callbacks.Remove(update_callback);
return;
}
Log.Default.Msg(Log.Level.Debug, "Callback for updated data already removed");
}
}

/// <summary>
/// Return the data for the requested type.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions Visualizations/Visualizations/ColumnsVisualization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Visualizations.Styles;
using System.Windows.Controls;
using Core.Utilities;
using System;



Expand All @@ -32,6 +33,14 @@ public class ColumnsVisualization : AbstractSciChartVisualization<SciChartSurfac
/* ------------------------------------------------------------------*/
// public functions

/// <summary>
/// DEBUG
/// </summary>
~ColumnsVisualization()
{
Console.WriteLine("DEBUG - DTOR: ColumnsVisualization");
}

public override bool ReCreate()
{
if (!base.ReCreate())
Expand Down
9 changes: 9 additions & 0 deletions Visualizations/Visualizations/LinesVisualization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Visualizations.Data;
using Core.GUI;
using Core.Utilities;
using System;



Expand All @@ -30,6 +31,14 @@ public class LinesVisualization : AbstractSciChartVisualization<SciChartSurface,
/* ------------------------------------------------------------------*/
// public functions

/// <summary>
/// DEBUG
/// </summary>
~LinesVisualization()
{
Console.WriteLine("DEBUG - DTOR: LinesVisualization");
}

public override bool ReCreate()
{
if (!base.ReCreate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public class DataConfigurator : AbstractGenericVisualization<System.Windows.Cont
/* ------------------------------------------------------------------*/
// public functions

/// <summary>
/// DEBUG
/// </summary>
~DataConfigurator()
{
Console.WriteLine("DEBUG - DTOR: DataConfigurator");
}

public override bool ReCreate()
{
if (!_initialized)
Expand Down
30 changes: 8 additions & 22 deletions Visualizations/Visualizations/Miscellaneous/LogConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,6 @@ namespace Miscellaneous
{
public class LogConsole : AbstractGenericVisualization<System.Windows.Controls.TextBlock, DataInterfaceGeneric<GenericDataStructure>>
{

/* ------------------------------------------------------------------
// Available methods/properties:
*
* - Data.SetDataStyle(...)
* - Content...
* - AddOption(MenuItem option)
* - SetScrollViewBackground(Brush background)
* - ScrollToBottom()
*
*
/* ------------------------------------------------------------------
// Methods/properties to implement:
*
* - public override string Name { get { return "..."; } }
* - public override bool Create()
* - [optional] public override bool Initialize()
* - [optional] public override bool Terminate()
*
*
------------------------------------------------------------------*/

/* ------------------------------------------------------------------*/
// properties

Expand All @@ -59,6 +37,14 @@ public class LogConsole : AbstractGenericVisualization<System.Windows.Controls.T
/* ------------------------------------------------------------------*/
// public functions

/// <summary>
/// DEBUG
/// </summary>
~LogConsole()
{
Console.WriteLine("DEBUG - DTOR: LogConsole");
}

public override bool Initialize()
{
var init = base.Initialize();
Expand Down
14 changes: 11 additions & 3 deletions Visualizations/Visualizations/ParallelCoordinatesVisualization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Windows.Controls;
using System.Windows.Media;
using Core.Utilities;
using System;



Expand All @@ -32,6 +33,14 @@ public class ParallelCoordinatesVisualization : AbstractSciChartVisualization<Sc
/* ------------------------------------------------------------------*/
// public functions

/// <summary>
/// DEBUG
/// </summary>
~ParallelCoordinatesVisualization()
{
Console.WriteLine("DEBUG - DTOR: ParallelCoordinatesVisualization");
}

public override bool ReCreate()
{
if (!base.ReCreate())
Expand Down Expand Up @@ -142,16 +151,15 @@ public override bool ReCreate()
XyDirection = SciChart.Charting.XyDirection.XYDirection
},
reorder_modifier
) ;
);


_timer.Stop();
_timer.Stop();
_created = true;
return _created;
}



/* ------------------------------------------------------------------*/
// private functions

Expand Down
9 changes: 9 additions & 0 deletions Visualizations/Visualizations/ScatterVisualization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows;
using System.Windows.Controls;
using Core.Utilities;
using System;



Expand All @@ -30,6 +31,14 @@ public class ScatterVisualization : AbstractSciChartVisualization<SciChartSurfac
/* ------------------------------------------------------------------*/
// public functions

/// <summary>
/// DEBUG
/// </summary>
~ScatterVisualization()
{
Console.WriteLine("DEBUG - DTOR: ScatterVisualization");
}

public override bool ReCreate()
{
if (!base.ReCreate())
Expand Down
18 changes: 12 additions & 6 deletions Visualizations/VisualizationsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,23 @@ public class VisualizationsManager : AbstractService
/* ------------------------------------------------------------------*/
// public functions

public bool Initialize(DataManager.RequestCallback_Delegate request_callback, DataManager.RegisterUpdateCallback_Delegate update_callback)
public bool Initialize(DataManager.RequestCallback_Delegate request_callback, DataManager.RegisterUpdateCallback_Delegate update_callback, DataManager.UnregisterCallback_Delegate unregister_callback)
{
if (_initialized)
{
Terminate();
}
if (request_callback == null)
if ((request_callback == null) || (update_callback == null) || (unregister_callback == null))
{
Log.Default.Msg(Log.Level.Error, "Missing request data callback");
Log.Default.Msg(Log.Level.Error, "Missing callback(s)");
return false;
}
_timer.Start();

_contents = new Dictionary<Type, Dictionary<string, AbstractVisualization>>();
_request_callback = request_callback;
_update_callback = update_callback;
_unregister_callback = unregister_callback;


// Register new visualizations here:
Expand Down Expand Up @@ -190,7 +191,7 @@ public List<Type> DependingServices()
/// Provide necessary information of available window content (called by window leaf).
/// </summary>
/// <returns>List of available content meta data.</returns>
public AvailableContentsList_Type AvailableContents()
public AvailableContentsList_Type AvailableContentsCallback()
{
var content_ids = new AvailableContentsList_Type();

Expand Down Expand Up @@ -225,7 +226,7 @@ public AvailableContentsList_Type AvailableContents()
/// <param name="content_id">The string ID of the content if present.</param>
/// <param name="content_type">Using string for content type to allow cross project compatibility.</param>
/// <returns>Tuple of content ID and the WPF Control element holding the actual content.</returns>
public AttachContentMetaData_Type CreateContent(string content_id, string content_type)
public AttachContentMetaData_Type CreateContentCallback(string content_id, string content_type)
{
if (!_initialized)
{
Expand Down Expand Up @@ -275,14 +276,16 @@ public AttachContentMetaData_Type CreateContent(string content_id, string conten
/// </summary>
/// <param name="content_id">The id of the content to be deleted.</param>
/// <return>True on success, false otherwise.</return>
public bool DeleteContent(string content_id)
public bool DeleteContentCallback(string content_id)
{
// Loop over registered types
foreach (var content_types in _contents)
{
if (content_types.Value.ContainsKey(content_id))
{
_unregister_callback(content_types.Value[content_id].UpdateCallback);
content_types.Value[content_id].Detach();
content_types.Value[content_id].Terminate();
return content_types.Value.Remove(content_id);
}
}
Expand Down Expand Up @@ -418,6 +421,8 @@ private bool clear_contents()
{
foreach (var content_data in content_types.Value)
{
_unregister_callback(content_data.Value.UpdateCallback);
content_data.Value.Detach();
terminated &= content_data.Value.Terminate();
}
content_types.Value.Clear();
Expand All @@ -434,5 +439,6 @@ private bool clear_contents()

private DataManager.RequestCallback_Delegate _request_callback = null;
private DataManager.RegisterUpdateCallback_Delegate _update_callback = null;
private DataManager.UnregisterCallback_Delegate _unregister_callback = null;
}
}

0 comments on commit 45ee7d0

Please sign in to comment.