using bts; using Bts.DataStream; using mbts.config; using System; using System.Collections; using System.Diagnostics; namespace BCG { /// /// Base class for all the sreams /// public class DataStream { internal static event EventHandler onFireStart; protected bool myTestMode = Configuration.get().IsTestMode; protected bool myFirstSubImplemented = true, myLastSubImplemented = true; protected IDictionary myDelMap = new Hashtable (); private Guid myID = Guid.Empty; public DateTime last_fired_time = DateTime.MinValue; public int n_updates = 0; public DataStream () : this (null) {} public DataStream (DataStream realSource) { myRealSource = realSource; myOnChangeDelegate = null; myPendingAdds = new Stack(); } public int numberOfSubscribers { get { return myOnChangeDelegate != null? myOnChangeDelegate.GetInvocationList().Length : 0; } } public delegate void ChangeDelegate(object newValue); public event ChangeDelegate onChange { add { ChangeDelegate currValue = value; if (myTestMode) { ChangeDelegateWrapper w = new ChangeDelegateWrapper (value); currValue = new ChangeDelegate (w.wrapperFunction); myDelMap.Add (value, currValue); } if (myRealSource != null) myRealSource.onChange += currValue; else { myOnChangeDelegate = (ChangeDelegate) Delegate.Combine (myOnChangeDelegate,currValue); if (myOnChangeDelegate.GetInvocationList().Length == 1) // first subscriber { myPendingAdds.Push(new ArrayList()); myOnFirstSubscriber(); ArrayList calledDelegates = (ArrayList) myPendingAdds.Pop(); object v = myGetValueForInitialSubscription(); //BlueTrace.WriteViewStreamSubscribe(this.ID, v != null); if (v != null && calledDelegates.IndexOf(currValue) == -1) currValue (v); } else { object v = myGetValueForInitialSubscription(); // BlueTrace.WriteViewStreamSubscribe(this.ID, v != null); if (v != null) currValue (v); } } } remove { ChangeDelegate currValue = value; // BlueTrace.WriteViewStreamUnsubscribe(this.ID); if (myTestMode) { currValue = myDelMap[value] as ChangeDelegate; myDelMap.Remove (value); } if (myRealSource != null) myRealSource.onChange -= currValue; else { myOnChangeDelegate = (ChangeDelegate) Delegate.Remove (myOnChangeDelegate,currValue); // this is need to avoid a callback in a reentrant add. if (myPendingAdds.Count > 0) ((ArrayList) (myPendingAdds.Peek())).Add(currValue); if (myOnChangeDelegate == null) // last subscriber { myOnLastSubscriber(); myLastValue = null; } } } } protected virtual object myGetValueForInitialSubscription() {return myLastValue;} protected void mySetRealSource (DataStream src) {myRealSource = src;} protected virtual void myOnFirstSubscriber() {myFirstSubImplemented = false;} protected virtual void myOnLastSubscriber () {myLastSubImplemented = false;} protected virtual void myFireEvent(object o) { last_fired_time = DateTime.Now; n_updates++; // BlueEventLog.logAssert (myRealSource == null); myLastValue = o; if (myOnChangeDelegate != null) { // this is need to avoid a callback in a reentrant add. if (myPendingAdds.Count > 0) foreach (Delegate d in myOnChangeDelegate.GetInvocationList()) ((ArrayList) (myPendingAdds.Peek())).Add(d); if (onFireStart != null) onFireStart(this, EventArgs.Empty); myOnChangeDelegate (o); } } public Guid ID { get { if (this.myID == Guid.Empty) // && BlueTraceHelper.IsEnabled(BlueTraceLevel.Verbose, BlueTraceKeywords.Streams)) { // BlueTraceHelper.FastInitGuid(ref this.myID); // BlueTrace.WriteViewStreamCreate(this.GetType().ToString(), this.myID); } return this.myID; } } // this was made internal for testing purposes only. // don't mess with it. internal ChangeDelegate myOnChangeDelegate; protected object myLastValue; protected DataStream myRealSource; private Stack myPendingAdds; } internal class ChangeDelegateWrapper { readonly Stopwatch timer = new(); private DataStream.ChangeDelegate myDel; internal ChangeDelegateWrapper (DataStream.ChangeDelegate del) {myDel = del;} internal void wrapperFunction (object o) { timer.Restart(); myDel (o); timer.Stop(); Log.Gen.diag($"Type: {myDel.Method.DeclaringType.FullName}, Method = {myDel.Method.Name}"); } } }