Example #1
0
        /// <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 <Maybe <T> > t)
        {
            StreamSink <T> alarm        = new StreamSink <T>();
            Maybe <ITimer> currentTimer = Maybe.None;
            IListener      l            = t.Listen(
                m =>
            {
                currentTimer.MatchSome(timer => timer.Cancel());
                currentTimer = m.Match(
                    time => Maybe.Some(
                        this.implementation.SetTimer(
                            time,
                            () =>
                {
                    lock (this.eventQueue)
                    {
                        this.eventQueue.Enqueue(new Event(time, alarm));
                    }
                    // Open and close a transaction to trigger queued
                    // events to run.
                    Transaction.RunVoid(() => { });
                })),
                    () => Maybe.None);
            });

            return(alarm.AttachListener(l));
        }
Example #2
0
        public static Stream <Maybe <string> > Lookup(Stream <string> sWord)
        {
            StreamSink <Maybe <string> > sDefinition = Stream.CreateSink <Maybe <string> >();
            IListener listener = sWord.Listen(wrd =>
            {
                Task.Run(() =>
                {
                    //System.out.println("look up " + wrd);
                    Maybe <string> def = Maybe.None;
                    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.Some(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.AttachListener(listener));
        }