Cleaning up ISubscriberCore to use ne PubSubClientMssage class

This commit is contained in:
Taha Afzal authored and John Colvin committed 2024-05-25 15:37:33 +00:00
1 parent 90bcf5a455
commit 54184d0875
13 files changed
+370 -488

No files matched your search

@@ -8,14 +8,12 @@ using System.Collections.Generic;
using System.Threading;
using mbts;
namespace BCG
{
namespace BCG {
using HighPerformanceTimer = mbts.spark.HighPerformanceTimer;
public class ViewStreamDatabase : IWorker, IDisposable
{
public class ViewStreamDatabase : IWorker, IDisposable {
internal delegate void TagFireDelegate(string tag);
public delegate void DoWorkDelegate ();
public delegate void DoWorkDelegate();
public event DoWorkDelegate onDoWorkStart,
onDoWorkEnd;
internal event TagFireDelegate onTagFireStart,
@@ -24,10 +22,10 @@ namespace BCG
//and how big they are.
//public delegate void OnTagUpdateDelegate(PubMetaData metaData);
//public event OnTagUpdateDelegate onTagUpdate;
private IDictionary myITableAccumulatorMap = new Hashtable ();
private IDictionary myITableAccumulatorMap = new Hashtable();
private static ViewStreamDatabase mySelf;
private ISubscriberCore mySubscriberCore;
private ArrayList myWorkers = new ArrayList ();
private ArrayList myWorkers = new ArrayList();
private bool myIsDisposed = false;
private bool myIsInDoWork = false;
private static object myTimeInterval;
@@ -39,22 +37,18 @@ namespace BCG
private static readonly object myInstanceLockObject = new object();
private readonly object myStateLock = new object();
public ViewStreamDatabase ()
: this (new SubscriberCore ())
{
public ViewStreamDatabase()
: this(new SubscriberCore()) {
}
public ViewStreamDatabase (ISubscriberCore subscriberCore)
{
public ViewStreamDatabase(ISubscriberCore subscriberCore) {
mySubscriberCore = subscriberCore;
// if (DataStreamLoadFileConfig.instance.profileStreams)
// new DataStreamPerfRecorder(this);
}
private static int myGetTimeInterval()
{
if (myTimeInterval == null)
{
private static int myGetTimeInterval() {
if (myTimeInterval == null) {
// TODO
string timeInterval = String.Empty; //ConfigurationRequester.request("viewStreamDatabaseTimeOut");
if (timeInterval == null || timeInterval == "")
@@ -62,137 +56,130 @@ namespace BCG
else
myTimeInterval = Int32.Parse(timeInterval);
}
return (int) myTimeInterval;
return (int)myTimeInterval;
}
public static int instance_hashcode = -1;
public static ViewStreamDatabase instance
{
get
{
if(mySelf == null || mySelf.myIsDisposed)
lock(myInstanceLockObject)
{
if (mySelf == null || mySelf.myIsDisposed)
{
mySelf = new ViewStreamDatabase();
instance_hashcode = mySelf.GetHashCode();
public static ViewStreamDatabase instance {
get {
if (mySelf == null || mySelf.myIsDisposed)
lock (myInstanceLockObject) {
if (mySelf == null || mySelf.myIsDisposed) {
mySelf = new ViewStreamDatabase();
instance_hashcode = mySelf.GetHashCode();
}
}
}
return mySelf;
}
set
{
lock(myInstanceLockObject)
{
set {
lock (myInstanceLockObject) {
if (mySelf != null)
mySelf.mySubscriberCore.Dispose ();
mySelf.mySubscriberCore.Dispose();
mySelf = value;
instance_hashcode = mySelf.GetHashCode();
}
}
}
public ICollection<string> getCachedTables()
{
public ICollection<string> getCachedTables() {
List<string> result = new List<string>();
foreach (string s in myITableAccumulatorMap.Keys)
if (((ITableAccumulator)myITableAccumulatorMap[s]).isActive())
result.Add(s);
foreach (string s in myITableAccumulatorMap.Keys)
if (((ITableAccumulator)myITableAccumulatorMap[s]).isActive())
result.Add(s);
return result;
}
public ISubscriberCore subscriber{get{return mySubscriberCore;}}
public ITableAccumulator getITableAccumulator (string tableName)
{
public ISubscriberCore subscriber { get { return mySubscriberCore; } }
public ITableAccumulator getITableAccumulator(string tableName) {
// If theres a ITableAccumulator for the tableName its returned (to share subscriptions), otherwise
// a new one is obtained from the Subscriber factory
myITableAccumulatorMap[tableName] = myITableAccumulatorMap[tableName] == null ?
AccumulatorFactory.getAccumulator(tableName, mySubscriberCore) : myITableAccumulatorMap[tableName];
return myITableAccumulatorMap [tableName] as ITableAccumulator;
myITableAccumulatorMap[tableName] = myITableAccumulatorMap[tableName] == null ?
AccumulatorFactory.getAccumulator(tableName, mySubscriberCore) : myITableAccumulatorMap[tableName];
return myITableAccumulatorMap[tableName] as ITableAccumulator;
}
public void doWork ()
{
public void doWork() {
verifyAccess();
bts.Log.assert(false);
// TODO
if (myIsSuspended)
return;
if (myIsInDoWork)
return;
else
{
myIsInDoWork = true; // to allow reentrancy
try
{
if (onDoWorkStart != null)
onDoWorkStart();
// if (myIsSuspended)
// return;
// if (myIsInDoWork)
// return;
// else
// {
// myIsInDoWork = true; // to allow reentrancy
// try
// {
// if (onDoWorkStart != null)
// onDoWorkStart();
ArrayList updateList = mySubscriberCore.getUpdates(myGetTimeInterval());
IDictionary updateMap = myGetUpdateMap(updateList);
foreach (DictionaryEntry de in updateMap)
{
Stream[] allUpdates = (de.Value as ArrayList).ToArray(typeof(Stream)) as Stream[];
string tag = de.Key.ToString();
if (myITableAccumulatorMap[tag] == null){}
// BlueEventLog.logEvent("Received a SubscriberCore's callback for a tag that was never subscribed", BlueEventLevel.Warning);
else
{
myTimer.start();
// //ArrayList updateList = mySubscriberCore.getUpdates(myGetTimeInterval());
// IDictionary updateMap = myGetUpdateMap(updateList);
if (onTagFireStart != null)
onTagFireStart(tag);
(myITableAccumulatorMap[tag] as ITableAccumulator).update(allUpdates);
if (onTagFireEnd != null)
onTagFireEnd(tag);
myTimer.stop();
if (myTimer.lastIntervalTime > myLogTimeThreshold)
{
// BlueEventLog.logEvent("callback on tag " + tag.ToUpper() + " took " +
// myTimer.lastIntervalTime + " msecs", BlueEventLevel.Warning);
}
}
}
// foreach (DictionaryEntry de in updateMap)
// {
// Stream[] allUpdates = (de.Value as ArrayList).ToArray(typeof(Stream)) as Stream[];
// string tag = de.Key.ToString();
// if (myITableAccumulatorMap[tag] == null){}
// // BlueEventLog.logEvent("Received a SubscriberCore's callback for a tag that was never subscribed", BlueEventLevel.Warning);
// else
// {
// myTimer.start();
// to allow for registering of workers on the worker callback
myIsCallingWorkers = true;
myPendingOps = new ArrayList();
foreach (IWorker w in myWorkers)
{
myTimer.start();
w.doWork();
myTimer.stop();
if (myTimer.lastIntervalTime > _dowork_threshold){}
// mbts.Logger.log (bts.log.Level.Warn, bts.log.Subsystem.gen, String.Format ("VSD.dowork on worker {0} took {1}", w.GetType().ToString (), myTimer.lastIntervalTime));
}
myIsCallingWorkers = false;
foreach (PendingAddRemoveWorker op in myPendingOps)
if (op.isAdd)
addWorker(op.w);
else
removeWorker(op.w);
}
finally
{
myPendingOps = null;
myIsInDoWork = false;
myIsCallingWorkers = false;
if (onDoWorkEnd != null)
onDoWorkEnd ();
}
}//end if we're not in do work
// if (onTagFireStart != null)
// onTagFireStart(tag);
// (myITableAccumulatorMap[tag] as ITableAccumulator).update(allUpdates);
// if (onTagFireEnd != null)
// onTagFireEnd(tag);
// myTimer.stop();
// if (myTimer.lastIntervalTime > myLogTimeThreshold)
// {
// // BlueEventLog.logEvent("callback on tag " + tag.ToUpper() + " took " +
// // myTimer.lastIntervalTime + " msecs", BlueEventLevel.Warning);
// }
// }
// }
// // to allow for registering of workers on the worker callback
// myIsCallingWorkers = true;
// myPendingOps = new ArrayList();
// foreach (IWorker w in myWorkers)
// {
// myTimer.start();
// w.doWork();
// myTimer.stop();
// if (myTimer.lastIntervalTime > _dowork_threshold){}
// // mbts.Logger.log (bts.log.Level.Warn, bts.log.Subsystem.gen, String.Format ("VSD.dowork on worker {0} took {1}", w.GetType().ToString (), myTimer.lastIntervalTime));
// }
// myIsCallingWorkers = false;
// foreach (PendingAddRemoveWorker op in myPendingOps)
// if (op.isAdd)
// addWorker(op.w);
// else
// removeWorker(op.w);
// }
// finally
// {
// myPendingOps = null;
// myIsInDoWork = false;
// myIsCallingWorkers = false;
// if (onDoWorkEnd != null)
// onDoWorkEnd ();
// }
// }//end if we're not in do work
}
private bool myIsSuspended = false;
@@ -201,38 +188,32 @@ namespace BCG
public void resume() { myIsSuspended = false; }
public bool checkAccess()
{
public bool checkAccess() {
if (this.myThreadOwner == null) this.myThreadOwner = Thread.CurrentThread;
return this.myThreadOwner == Thread.CurrentThread;
}
public void verifyAccess()
{
if (!checkAccess())
{
public void verifyAccess() {
if (!checkAccess()) {
string errorMessage = string.Format("Invalid cross thread access on ViewStreamDatabase. Owning thread: {0}. Accessing thread: {1}", this.myThreadOwner.ManagedThreadId, Thread.CurrentThread.ManagedThreadId);
//#if DEBUG
// BlueEventLog2.instance().assert(false, errorMessage);
//#else
// BlueEventLog2.instance().logEvent(errorMessage, BlueEventLog2Level.Fatal);
//#endif
//#if DEBUG
// BlueEventLog2.instance().assert(false, errorMessage);
//#else
// BlueEventLog2.instance().logEvent(errorMessage, BlueEventLog2Level.Fatal);
//#endif
}
}
private IDictionary myGetUpdateMap (ArrayList updates)
{
IDictionary retVal = new Hashtable ();
foreach (PubSubUpdate update in updates)
{
private IDictionary myGetUpdateMap(ArrayList updates) {
IDictionary retVal = new Hashtable();
foreach (PubSubUpdate update in updates) {
if (retVal[update.tag] == null)
retVal[update.tag] = new ArrayList ();
(retVal[update.tag] as ArrayList).Add (update.body);
retVal[update.tag] = new ArrayList();
(retVal[update.tag] as ArrayList).Add(update.body);
}
if (retVal.Count > 0)
{
if (retVal.Count > 0) {
string tagString = "";
foreach (string tag in retVal.Keys)
tagString += tag + " , ";
@@ -240,44 +221,38 @@ namespace BCG
return retVal;
}
public bool isConsistent{get {return true;}}
public bool isConsistent { get { return true; } }
public void addWorker(IWorker w)
{
public void addWorker(IWorker w) {
verifyAccess();
if (myIsCallingWorkers)
myPendingOps.Add(new PendingAddRemoveWorker(true,w));
myPendingOps.Add(new PendingAddRemoveWorker(true, w));
else
myWorkers.Add(w);
}
public void removeWorker(IWorker w)
{
public void removeWorker(IWorker w) {
verifyAccess();
if (myIsCallingWorkers)
myPendingOps.Add(new PendingAddRemoveWorker(false,w));
myPendingOps.Add(new PendingAddRemoveWorker(false, w));
else
myWorkers.Remove(w);
}
#region IDisposable Members
public void Dispose()
{
if (!myIsDisposed)
{
public void Dispose() {
if (!myIsDisposed) {
myIsDisposed = true;
mySubscriberCore.Dispose ();
mySubscriberCore.Dispose();
}
}
private ArrayList myPendingOps;
private bool myIsCallingWorkers = false;
class PendingAddRemoveWorker
{
public PendingAddRemoveWorker(bool isAdd, IWorker w)
{
class PendingAddRemoveWorker {
public PendingAddRemoveWorker(bool isAdd, IWorker w) {
this.isAdd = isAdd;
this.w = w;
}