...
Code Block |
---|
public class ComponentA { /// <summary> /// True if request for data has been send out /// </summary> private bool _hasSendRequests = false; /// <summary> /// List of items that needs to be exchanged in each time steup /// </summary> private List<IExchangeItem> ActiveItems; /// <summary> /// List of items in ActiveItems that have not yet been updated /// </summary> private List<IExchangeItem> PendingActiveItems; /// <summary> /// List of items that other linkable components have requested data for, /// i.e., which is in the other linkable components PendingActiveItems list /// </summary> private List<IExchangeItem> IncomingRequests; /// <summary> /// Update the component, returns true if updated, false if not updated /// </summary> /// <param name="forceUpdate">Force the component to update always</param> public bool Update(bool forceUpdate) { // Check if we have already send out requests for active items for this timestep. if (!_hasSendRequests) { // We have not send requests, send a request for data for each active item foreach (IExchangeItem item in ActiveItems) { // Send request, see if data is already available // If data is not available, the other component registers this item in its list of IncomingRequests bool dataIsReady = SendRequestForData(item); if (dataIsReady) { // Data is ready, transfer data TransferDataFor(item); } else { // Data is not ready, put it in pending list PendingActiveItems.Add(item); } } _hasSendRequests = true; } // If this is a forced update, theget the best possible data for each pending active item if (forceUpdate) { foreach (IExchangeItem item in PendingActiveItems) { GetBestPossibleDataFor(item); } PendingActiveItems.Clear(); } // If we have pending active items, we can not update the component if (PendingActiveItems.Count > 0) return (false); // No pending active items, we can update component to next timestep. DoUpdateComponent(); // Check each incoming request, whether data is now available foreach (IExchangeItem item in IncomingRequests) { if (DataIsAvaiableFor(item)) { // Data is now available for this item, send data to component. // The other component must remove item from its PendingActiveItems TransferDataFor(item); IncomingRequests.Remove(item); } } return (true); } } |
...