public void SetListener(PortfolioListener newListener) { if (newListener == null) { //we don't accept a null parameter. to delete the actual listener //the RemoveListener method must be used return; } //Set the listener this.listener = newListener; _log.Debug("Listener set on " + this.id); //copy the actual listener to a constant that will be used //by the asynchronous notification PortfolioListener localListener = newListener; //Clone the actual status of the portfolio, for use //by the asynchronous notification IDictionary currentStatus = (Hashtable)quantities.Clone(); //Create a task to asynchronously //notify the listener of the actual status executor.Add(delegate() { // call the onActualStatus on the listener; // in case the listener has just been detached, // the listener should detect the case localListener.OnActualStatus(currentStatus); }); }
private void Empty() { _log.Debug("Cleaning status " + this.id); //remove all the quantities so that the portfolio will result empty quantities.Clear(); if (this.listener != null) { //copy the actual listener to a constant that will be used //by the asynchronous notification PortfolioListener localListener = this.listener; executor.Add(delegate() { // call the request on the listener; // in case the listener has just been detached, // the listener should detect the case localListener.Empty(); }); } }
public void RemoveListener() { //remove the listener this.listener = null; }
private void ChangeQty(string stock, int qty) { //Get the old quantity for the stock Object oldQtyObject = quantities[stock]; int newQty; int oldQty; if (oldQtyObject == null) { //If oldQtyObject is null it means that we don't have that stock on our portfolio if (qty <= 0) { //We can't sell something we don't have, warn and return. _log.Warn(this.id + "|No stock to sell: " + stock); return; } //Set oldQty to 0 to let the listener know that we previously didn't have such stock oldQty = 0; //The new quantity is equal to the bought value newQty = qty; } else { oldQty = Convert.ToInt32(quantities[stock]); Debug.Assert(oldQty > 0); //The new quantity will be the value of the old quantity plus the qty value. //If qty is a negative number than we are selling, in the other case we're buying newQty = oldQty + qty; // overflow check; just in case if (qty > 0 && newQty <= qty) { newQty = oldQty; _log.Warn(this.id + "|Quantity overflow; order ignored: " + stock); return; } } if (newQty < 0) { //We sold more than we had _log.Warn(this.id + "|Not enough stock to sell: " + stock); //We interpret this as "sell everything" newQty = 0; } if (newQty == 0) { //If we sold everything we remove the stock from the internal structure quantities.Remove(stock); } else { //Save the actual quantity in internal structure quantities[stock] = newQty; } if (this.listener != null) { //copy the actual listener to a constant that will be used //by the asynchronous notification PortfolioListener localListener = this.listener; //copy the values to constant to be used //by the asynchronous notification int newVal = newQty; int oldVal = oldQty; string stockId = stock; //If we have a listener, create a task to asynchronously //notify the listener of the actual status executor.Add(delegate() { // call the update on the listener; // in case the listener has just been detached, // the listener should detect the case localListener.Update(stockId, newVal, oldVal); }); } }