/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Collections.Generic; using System.Linq; namespace QuantConnect.Brokerages { /// /// Helper class for /// public class BrokerageMultiWebSocketEntry { private readonly Dictionary _symbolWeights; private readonly List _symbols; private readonly object _locker = new(); /// /// Gets the web socket instance /// public IWebSocket WebSocket { get; } /// /// Gets the sum of symbol weights for this web socket /// public int TotalWeight { get; private set; } /// /// Gets the number of symbols subscribed /// public int SymbolCount { get { lock (_locker) { return _symbols.Count; } } } /// /// Returns whether the symbol is subscribed /// /// /// public bool Contains(Symbol symbol) { lock (_locker) { return _symbols.Contains(symbol); } } /// /// Returns the list of subscribed symbols /// /// public IReadOnlyCollection Symbols { get { lock (_locker) { return _symbols.ToList(); } } } /// /// Initializes a new instance of the class /// /// A dictionary of symbol weights /// The web socket instance public BrokerageMultiWebSocketEntry(Dictionary symbolWeights, IWebSocket webSocket) { _symbolWeights = symbolWeights; _symbols = new List(); WebSocket = webSocket; } /// /// Initializes a new instance of the class /// /// The web socket instance public BrokerageMultiWebSocketEntry(IWebSocket webSocket) : this(null, webSocket) { } /// /// Adds a symbol to the entry /// /// The symbol to add public void AddSymbol(Symbol symbol) { lock (_locker) { _symbols.Add(symbol); } if (_symbolWeights != null && _symbolWeights.TryGetValue(symbol, out var weight)) { TotalWeight += weight; } } /// /// Removes a symbol from the entry /// /// The symbol to remove public void RemoveSymbol(Symbol symbol) { lock (_locker) { _symbols.Remove(symbol); } if (_symbolWeights != null && _symbolWeights.TryGetValue(symbol, out var weight)) { TotalWeight -= weight; } } } }