private void HandleNewStockPrice(StockPriceMessage message) { if (_series.ContainsKey(message.StockSymbol)) { var seriesToUpdate = _series[message.StockSymbol]; var newDataPoint = new DataPoint(DateTimeAxis.ToDouble(message.Date), Axis.ToDouble(message.StockPrice)); if (seriesToUpdate.Points.Count > 10) { seriesToUpdate.Points.RemoveAt(0); } seriesToUpdate.Points.Add(newDataPoint); RefreshChart(); } }
public StockActor(string stockSymbol) { _stockSymbol = stockSymbol; _subscribers = new HashSet <IActorRef>(); _priceLookupChild = Context.ActorOf(Context.DI().Props <StockPriceLookupActor>()); Receive <SubscribeToNewStockPricesMessage>(message => _subscribers.Add(message.Subscriber)); Receive <UnSubscribeFromNewStockPricesMessage>(message => _subscribers.Remove(message.Subscriber)); Receive <RefreshStockPriceMessage>(message => _priceLookupChild.Tell(message)); Receive <UpdatedStockPriceMessage>( message => { _stockPrice = message.Price; var stockPriceMessage = new StockPriceMessage(_stockSymbol, _stockPrice, message.Date); foreach (var subscriber in _subscribers) { subscriber.Tell(stockPriceMessage); } } ); }
public static IAgent <StockAgentMessage> Create(string stockSymbol) { // Start price lookup child actor var gateWay = new RandomStockPriceServiceGateway(); var priceLookupChild = StockPriceLookupActor.Create(gateWay); // Start Price actor var self = Agent.Start <HashSet <IAgent <ChartSeriesMessage> >, StockAgentMessage>( new HashSet <IAgent <ChartSeriesMessage> >(), async(subscribers, message) => { switch (message) { case SubscribeToNewStockPricesMessage msg: subscribers.Add(msg.Subscriber); return(subscribers); case UnSubscribeFromNewStockPricesMessage msg: subscribers.Remove(msg.Subscriber); return(subscribers); case RefreshStockPriceMessage msg: var request = new RefreshStockPriceMessage(msg.StockSymbol); var updatedPriceMsg = await priceLookupChild.Ask(request); var stockPriceMessage = new StockPriceMessage( msg.StockSymbol, updatedPriceMsg.Price, updatedPriceMsg.Date); foreach (var subscriber in subscribers) { subscriber.Post(stockPriceMessage); } return(subscribers); default: throw new ArgumentException( message: "message is not a recognized", paramName: nameof(message)); } }); // Start timer that trigger refresh events (just to avoid dependency on Rx) Timer timer = new Timer(75); var refreshMsg = new RefreshStockPriceMessage(stockSymbol); timer.AutoReset = true; timer.Elapsed += (sender, e) => self.Post(refreshMsg); timer.Start(); // TODO: Ideally we have to stop and dispose timer return(self); }
/// <summary> /// Called when a new stock price is received. /// </summary> /// <param name="message"></param> private void HandleNewStockPrice(StockPriceMessage message) { if (!this._series.ContainsKey(message.StockSymbol)) { return; } var series = this._series[message.StockSymbol]; // Show at most 10 points if (series.Points.Count > 10) { series.Points.RemoveAt(0); } series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(message.Date), Axis.ToDouble(message.Price))); this.RefreshChart(); }
private void HandleNewStockPrice(StockPriceMessage message) { if (_series.ContainsKey(message.StockSymbol)) { var series = _series[message.StockSymbol]; var newDataPoint = new DataPoint(DateTimeAxis.ToDouble(message.Date), LinearAxis.ToDouble(message.Price)); if (series.Points.Count > 10) { series.Points.RemoveAt(0); } series.Points.Add(newDataPoint); RefreshChart(); } }
private void HandleNewStockPrice(StockPriceMessage message) { if (!_series.ContainsKey(message.StockSymbol)) { return; } var series = _series[message.StockSymbol]; var newDataPoint = new DataPoint(DateTimeAxis.ToDouble(message.Date), LinearAxis.ToDouble(message.StockPrice)); // Keep the last 10 data points on graph if (series.Points.Count > 10) { series.Points.RemoveAt(0); } series.Points.Add(newDataPoint); RefreshChart(); }
public StockActor(string stockSymbol) { _stockSymbol = stockSymbol; _priceLookupChild = Context.ActorOf(Context.DI().Props <StockPriceLookugActor>()); Receive <SubscribeToNewStockPriceMessage>(a => _subscribers.Add(a.Subscriber)); Receive <UnSubscribeToNewStockPriceMessage>(a => _subscribers.Remove(a.Subscriber)); Receive <RefreshStockPriceMessage>(a => _priceLookupChild.Tell(a)); Receive <UpdatedStockPriceMessage>(a => { _stockPrice = a.Price; var stockPriceMessage = new StockPriceMessage(_stockSymbol, _stockPrice, a.Date); foreach (var sub in _subscribers) { sub.Tell(stockPriceMessage); } }); }