/// <summary> /// A timer that fires at the specified time. /// </summary> /// <param name="t">The time to fire at.</param> /// <returns>A stream which fires at the specified time.</returns> public Stream <T> At(Cell <IMaybe <T> > t) { StreamSink <T> alarm = new StreamSink <T>(); CurrentTimer current = new CurrentTimer(); IListener l = t.Listen(m => { current.Timer.Match(timer => timer.Cancel(), () => { }); current.Timer = m.Match( timer => Maybe.Just(this.implementation.SetTimer(timer, () => { lock (this.eventQueue) { this.eventQueue.Enqueue(new Event(timer, alarm)); } // Open and close a transaction to trigger queued // events to run. Transaction.RunVoid(() => { }); })), Maybe.Nothing <ITimer>); }); return(alarm.AddCleanup(l)); }
public static Stream <IMaybe <string> > Lookup(Stream <string> sWord) { StreamSink <IMaybe <string> > sDefinition = new StreamSink <IMaybe <string> >(); IListener listener = sWord.ListenWeak(wrd => { Task.Run(() => { //System.out.println("look up " + wrd); IMaybe <string> def = Maybe.Nothing <string>(); try { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Connect("dict.org", 2628); NetworkStream ns = new NetworkStream(s); StreamReader r = new StreamReader(ns); StreamWriter w = new StreamWriter(ns); try { r.ReadLine(); w.WriteLine("DEFINE ! " + wrd); w.Flush(); string result = r.ReadLine(); if (result != null && result.StartsWith("150")) { result = r.ReadLine(); } if (result != null && result.StartsWith("151")) { StringBuilder b = new StringBuilder(); while (true) { string l = r.ReadLine(); if (l == ".") { break; } b.AppendLine(l); } def = Maybe.Just(b.ToString()); } else { MessageBox.Show("ERROR: " + result); } } finally { try { s.Close(); s.Dispose(); } catch { // ignored } } } catch (Exception e) { MessageBox.Show("ERROR: " + e); } finally { sDefinition.Send(def); } }); }); return(sDefinition.AddCleanup(listener)); }