public void Update(TickerMessage message) { if (message == null) { return; } if (ContainsKey(message.Symbol)) { this[message.Symbol].Update(message); } }
/// <summary> /// This method receives a single TickerMessage within some time limit. If no message /// comes in within the time limit, then it returns a null. Used by Monitoring method. /// </summary> /// <param name="timeout"></param> /// <returns></returns> private TickerMessage Receive(int timeout = 0) { TickerMessage message = null; byte[] receivedBytes = ReceiveBytes(timeout); if (receivedBytes != null && receivedBytes.Length > 0) { message = TickerMessage.Decode(receivedBytes); } return(message); }
public static TickerMessage Decode(byte[] bytes) { TickerMessage message = null; if (bytes != null && bytes.Length == 42) { message = new TickerMessage(); BinaryReader binaryReader = new BinaryReader(new MemoryStream(bytes)); // Read in Symbol as string, padded spaces to 6 characters, converted to a byte array using ASCII encoding byte[] tmp = binaryReader.ReadBytes(6); message.PaddedSymbol = Encoding.ASCII.GetString(tmp, 0, 6); // Read in Timestamp as a long integer of "ticks" in Network Standard Byte Order message.MessageTimestampInBytes = binaryReader.ReadBytes(8); // Read in OpeningPrice, as an integer in Network Standard Byte Order. The integer represents pennies (not dollars) message.OpeningPrice = IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); // Read in PreviousClosingPrice, as an integer in Network Standard Byte Order. The integer represents pennies (not dollars) message.PreviousClosingPrice = IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); // Read in CurrentPrice, as an integer in Network Standard Byte Order. The integer represents pennies (not dollars) message.CurrentPrice = IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); // Read in BidPrice, as an integer in Network Standard Byte order. The integer represents pennies (not dollars) message.BidPrice = IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); // Read in AskPrice, as an integer in Network Standard Byte order. The integer represents pennies (not dollars) message.AskPrice = IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); // Read in CurrentVolume, as an integer in Network Standard Byte Order message.CurrentVolume = IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); // Read in AverageVolume, as an integer in Network Standard Byte Order message.AverageVolume = IPAddress.NetworkToHostOrder(binaryReader.ReadInt32()); } return(message); }
public void Update(TickerMessage message) { // TODO: Update the state of the stock object // For the moment, we'll have it simply write the new stock info to the console. This code needs to be remove. // The observers will be responsible to presenting stock information to the user //Console.WriteLine($"{message.Symbol}\tprice={message.CurrentPrice}\t\tvolume={message.CurrentVolume}"); // Update the current Stock this.openingPrice = message.OpeningPrice; this.previousClosingPrice = message.PreviousClosingPrice; this.currentPrice = message.CurrentPrice; this.bidPrice = message.BidPrice; this.askPrice = message.AskPrice; this.currentVolume = message.CurrentVolume; this.averageVolume = message.AverageVolume; Console.WriteLine("================================="); Console.WriteLine("Name: " + this.CompanyName); Console.WriteLine("Symbol: " + this.Symbol); Console.WriteLine("Current Price: " + this.currentPrice); Console.WriteLine("================================="); }