Merging changes
This commit is contained in:
1 parent
c4216d2272
commit
797816be7e
437 files changed
+54228
-28
No files matched your search
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using BCG.Utilities.General;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
/*
|
||||
* The threadsafe stream wraps around an input stream, and amalgamates all the values from that stream
|
||||
* locally. It only fires packets on the thread where the VSD's doWork function is called.
|
||||
*
|
||||
* */
|
||||
public class ThreadSafeStream : ViewStream, IWorker
|
||||
{
|
||||
|
||||
private ViewStream myParentStream;
|
||||
private ThreadSafeQueue<DataPacket> myQueuedUpdates;
|
||||
private ViewStreamDatabase myVSD;
|
||||
|
||||
public ThreadSafeStream(ViewStream parent) : this(ViewStreamDatabase.instance, parent) { }
|
||||
public ThreadSafeStream(ViewStreamDatabase vsd, ViewStream parentStream)
|
||||
{
|
||||
myParentStream = parentStream;
|
||||
myVSD = vsd;
|
||||
myQueuedUpdates = new ThreadSafeQueue<DataPacket>();
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber()
|
||||
{
|
||||
myParentStream.onChange += new ChangeDelegate(myParentStreamChangeHandler);
|
||||
myVSD.addWorker(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected override void myOnLastSubscriber()
|
||||
{
|
||||
myParentStream.onChange -= new ChangeDelegate(myParentStreamChangeHandler);
|
||||
myVSD.removeWorker(this);
|
||||
}
|
||||
|
||||
void myParentStreamChangeHandler(object newValue)
|
||||
{
|
||||
myQueuedUpdates.enqueue(newValue as DataPacket);
|
||||
}
|
||||
|
||||
public void doWork()
|
||||
{
|
||||
foreach (DataPacket dp in myQueuedUpdates.dequeueAll())
|
||||
{
|
||||
myFireEvent(dp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
using BCG.Utilities.General;
|
||||
using BCG.Utilities;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
// Marshal updates from one VSDB to another, the assumption being
|
||||
// that the two vsdb's are driven by different threads
|
||||
// constructor is called from the main thread
|
||||
public class VSDBMarshallerStream : ViewStream, IDisposable, IWorker
|
||||
{
|
||||
public class TargetDoWorkRelay : IWorker
|
||||
{
|
||||
public TargetDoWorkRelay(VSDBMarshallerStream owner)
|
||||
{
|
||||
_owner = owner;
|
||||
}
|
||||
public void doWork()
|
||||
{
|
||||
_owner.onTargetDoWork();
|
||||
}
|
||||
VSDBMarshallerStream _owner;
|
||||
}
|
||||
// source_stream is assumed to be driven by
|
||||
// source_db
|
||||
public VSDBMarshallerStream (
|
||||
ViewStreamDatabase source_db,
|
||||
ViewStream source_stream,
|
||||
ViewStreamDatabase target_db)
|
||||
{
|
||||
_source_thread = Thread.CurrentThread.ManagedThreadId;
|
||||
_source_db = source_db;
|
||||
_source_stream = source_stream;
|
||||
_target_db = target_db;
|
||||
_queued_actions = new ThreadSafeQueue<Request>();
|
||||
_queued_updates = new ThreadSafeQueue<Update>();
|
||||
_source_db.addWorker(this);
|
||||
}
|
||||
|
||||
// source doWork
|
||||
public void doWork()
|
||||
{
|
||||
// BlueEventLog2.instance().assert(_source_thread == Thread.CurrentThread.ManagedThreadId, "doWork can only be called from source thread");
|
||||
|
||||
var r = _queued_actions.dequeue();
|
||||
if (r != null)
|
||||
{
|
||||
if (r.is_subscription)
|
||||
{
|
||||
_current_source_subscription = r.subscription_id;
|
||||
_source_stream.onChange += new ChangeDelegate(_source_stream_onChange);
|
||||
}
|
||||
else
|
||||
{
|
||||
_source_stream.onChange -= new ChangeDelegate(_source_stream_onChange);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// BlueEventLog2.instance().assert(_source_thread == Thread.CurrentThread.ManagedThreadId, "Dispose can only be called from source thread");
|
||||
doWork();
|
||||
_source_db.removeWorker(this);
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber()
|
||||
{
|
||||
_target_thread = Thread.CurrentThread.ManagedThreadId;
|
||||
// BlueEventLog2.instance().assert(_source_thread != _target_thread, "Subscriptions should only occur in the target thread");
|
||||
|
||||
|
||||
_current_target_subscription = new Guid();
|
||||
_queued_actions.enqueue(new Request { is_subscription = true, subscription_id = _current_target_subscription});
|
||||
_do_work_relay = new TargetDoWorkRelay(this);
|
||||
_target_db.addWorker(_do_work_relay);
|
||||
}
|
||||
|
||||
protected override void myOnLastSubscriber()
|
||||
{
|
||||
// BlueEventLog2.instance().assert(Thread.CurrentThread.ManagedThreadId == _target_thread,
|
||||
// "Unsubscriptions should only occur in the target thread");
|
||||
_target_db.removeWorker(_do_work_relay);
|
||||
_queued_actions.enqueue(new Request { is_subscription = false});
|
||||
}
|
||||
|
||||
void _source_stream_onChange(object newValue)
|
||||
{
|
||||
// BlueEventLog2.instance().assert(_source_thread == Thread.CurrentThread.ManagedThreadId, "Updates should come from source thread");
|
||||
_queued_updates.enqueue(new Update { dp = (DataPacket)newValue, subscription = _current_target_subscription });
|
||||
}
|
||||
|
||||
// target doWork
|
||||
private void onTargetDoWork()
|
||||
{
|
||||
// BlueEventLog2.instance().assert(Thread.CurrentThread.ManagedThreadId == _target_thread,
|
||||
// "onTargetDoWork should only be called from target thread");
|
||||
var updates = _queued_updates.dequeueAll();
|
||||
foreach (var u in updates) {
|
||||
if (u.subscription == _current_target_subscription)
|
||||
myFireEvent(u.dp);
|
||||
}
|
||||
}
|
||||
|
||||
ViewStreamDatabase _source_db;
|
||||
ViewStreamDatabase _target_db;
|
||||
ViewStream _source_stream;
|
||||
class Request
|
||||
{
|
||||
public bool is_subscription;
|
||||
public Guid subscription_id;
|
||||
}
|
||||
private ThreadSafeQueue<Request> _queued_actions;
|
||||
|
||||
class Update
|
||||
{
|
||||
public DataPacket dp;
|
||||
public Guid subscription;
|
||||
}
|
||||
private ThreadSafeQueue<Update> _queued_updates;
|
||||
TargetDoWorkRelay _do_work_relay;
|
||||
Guid _current_source_subscription;
|
||||
Guid _current_target_subscription;
|
||||
int _source_thread;
|
||||
int _target_thread;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user