/// <summary> /// Prints the received popcorn. /// </summary> private Task PrintPopcorn(HttpListenerContext context) { HttpListenerRequest request = context.Request; if (request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase)) { StreamReader reader = new StreamReader(request.InputStream); PoppedPopcorn popcorn = JsonConvert.DeserializeObject <PoppedPopcorn>(reader.ReadToEnd()); Console.WriteLine(popcorn.ToString()); } return(Task.CompletedTask); }
/// <summary> /// Pops a popcorn. /// </summary> private void PopPopcorn(object sender, ElapsedEventArgs e) { lock (_lock) { _popcornPop.Stop(); // One kernel has popped into the bowl (client) _kernelsInMachine -= 1; // Assign a random cook time for the next kernel; _popcornPop.Interval = _nextPop.Next(1, CookTime); // Send a kernel to the client. PoppedPopcorn popcorn = new PoppedPopcorn((uint)_nextPop.Next(1, 10)); var data = new StringContent(JsonConvert.SerializeObject(popcorn), Encoding.UTF8); // Don't bother waiting (It's the client's fault is there's a mess on the bench) _popcornBowl.PostAsync(_bowlName, data); } }