Ejemplo n.º 1
0
        public static IObservable <PingReply> Send(IPAddress address, int timeout, byte[] buffer)
        {
            Contract.Requires(address != null);
            Contract.Requires(timeout >= 0);
            Contract.Requires(buffer != null);
            Contract.Ensures(Contract.Result <IObservable <PingReply> >() != null);

            return(Observable2.UsingHot(new Ping(), ping => ping.SendObservable(address, timeout, buffer)));
        }
Ejemplo n.º 2
0
        public void ctor_100_5()
        {
            var meter = new FrequencyMeter(200, TimeSpan.FromSeconds(5));

            Observable2.Interval(TimeSpan.FromMilliseconds(30))
            .Select(_ => meter.RecordLap())
            .Subscribe(Console.WriteLine);

            Thread.Sleep(3000);
        }
        public static IObservable <string> DownloadFile(
            Uri address,
            string fileName)
        {
            Contract.Requires(address != null);
            Contract.Requires(fileName != null);
            Contract.Ensures(Contract.Result <IObservable <string> >() != null);

            return(Observable2.UsingHot(new WebClient(), client => client.DownloadFileObservable(address, fileName)));
        }
Ejemplo n.º 4
0
        public static IObservable <PingReply> Send(string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(hostNameOrAddress));
            Contract.Requires(timeout >= 0);
            Contract.Requires(buffer != null);
            Contract.Requires(options != null);
            Contract.Ensures(Contract.Result <IObservable <PingReply> >() != null);

            return(Observable2.UsingHot(new Ping(), ping => ping.SendObservable(hostNameOrAddress, timeout, buffer, options)));
        }
        public static IObservable <Either <DownloadProgressChangedEventArgs, string> > DownloadFileWithProgress(
            Uri address,
            string fileName)
        {
            Contract.Requires(address != null);
            Contract.Requires(fileName != null);
            Contract.Ensures(Contract.Result <IObservable <Either <DownloadProgressChangedEventArgs, string> > >() != null);

            return(Observable2.UsingHot(new WebClient(), client => client.DownloadFileWithProgress(address, fileName)));
        }
        public void ctor()
        {
            var conditioner = new FrequencyConditioner(30);

            Observable2.Interval(TimeSpan.FromMilliseconds(33))
            .Take(150)
            .Where(_ => conditioner.CheckLap())
            .Do(i => Console.WriteLine("{0}: {1}", i, conditioner.Frequency))
            .Do(_ => Thread.Sleep(40))
            .Wait();
        }
        public static IObservable <byte[]> UploadValues(
            Uri address,
            string method,
            NameValueCollection values)
        {
            Contract.Requires(address != null);
            Contract.Requires(values != null);
            Contract.Ensures(Contract.Result <IObservable <byte[]> >() != null);

            return(Observable2.UsingHot(new WebClient(), client => client.UploadValuesObservable(address, method, values)));
        }
        public static IObservable <Either <UploadProgressChangedEventArgs, byte[]> > UploadDataWithProgress(
            Uri address,
            string method,
            byte[] data)
        {
            Contract.Requires(address != null);
            Contract.Requires(data != null);
            Contract.Ensures(Contract.Result <IObservable <Either <UploadProgressChangedEventArgs, byte[]> > >() != null);

            return(Observable2.UsingHot(new WebClient(), client => client.UploadDataWithProgress(address, method, data)));
        }
        public static IObservable <byte[]> UploadData(
            Uri address,
            string method,
            byte[] data)
        {
            Contract.Requires(address != null);
            Contract.Requires(data != null);
            Contract.Ensures(Contract.Result <IObservable <byte[]> >() != null);

            return(Observable2.UsingHot(new WebClient(), client => client.UploadDataObservable(address, method, data)));
        }
Ejemplo n.º 10
0
        public void ctor_2()
        {
            var filter = new FrequencyFilter(25);

            Observable2.Interval(TimeSpan.FromMilliseconds(20))
            .Do(_ => filter.CheckLap())
            .Select(_ => filter.ArrangedFrequency)
            .Subscribe(Console.WriteLine);

            Thread.Sleep(3000);
        }
        public static IObservable <Either <UploadProgressChangedEventArgs, byte[]> > UploadValuesWithProgress(
            Uri address,
            string method,
            NameValueCollection values)
        {
            Contract.Requires(address != null);
            Contract.Requires(values != null);
            Contract.Ensures(Contract.Result <IObservable <Either <UploadProgressChangedEventArgs, byte[]> > >() != null);

            return(Observable2.UsingHot(new WebClient(), client => client.UploadValuesWithProgress(address, method, values)));
        }
        public static IObservable <string> DownloadString(
            Uri address)
        {
            Contract.Requires(address != null);
            Contract.Ensures(Contract.Result <IObservable <string> >() != null);

#if !SILVERLIGHT
            return(Observable2.UsingHot(new WebClient(), client => client.DownloadStringObservable(address)));
#else
            return(new WebClient().DownloadStringObservable(address));
#endif
        }
Ejemplo n.º 13
0
        public static IObservable <Stream> OpenRead(
            Uri address)
        {
            Contract.Requires(address != null);
            Contract.Ensures(Contract.Result <IObservable <Stream> >() != null);

#if !SILVERLIGHT
            return(Observable2.UsingHot(new WebClient(), client => client.OpenReadObservable(address)));
#else
            return(new WebClient().OpenReadObservable(address));
#endif
        }
Ejemplo n.º 14
0
        public static IObservable <Either <DownloadProgressChangedEventArgs, Stream> > OpenReadWithProgress(
            Uri address)
        {
            Contract.Requires(address != null);
            Contract.Ensures(Contract.Result <IObservable <Either <DownloadProgressChangedEventArgs, Stream> > >() != null);

#if !SILVERLIGHT
            return(Observable2.UsingHot(new WebClient(), client => client.OpenReadWithProgress(address)));
#else
            return(new WebClient().OpenReadWithProgress(address));
#endif
        }
Ejemplo n.º 15
0
        public void ctor_1()
        {
            var filter = new FrequencyFilter(25);
            var meter  = new FrequencyMeter();

            Observable2.Interval(TimeSpan.FromMilliseconds(20))
            .Where(_ => filter.CheckLap())
            .Do(_ => meter.RecordLap())
            .Subscribe(Console.WriteLine);

            Thread.Sleep(3000);
            Assert.IsTrue(meter.Frequency > 24);
            Assert.IsTrue(meter.Frequency <= 25);
        }
        private static IObservable <PingReply> CreatePingObservable(Ping ping, Action <object> start)
        {
            Contract.Requires(ping != null);
            Contract.Requires(start != null);
            Contract.Ensures(Contract.Result <IObservable <PingReply> >() != null);

            return(Observable2.FromEventBasedAsyncPattern <PingCompletedEventHandler, PingCompletedEventArgs>(
                       h => h.Invoke,
                       h => ping.PingCompleted += h,
                       h => ping.PingCompleted -= h,
                       start,
                       ping.SendAsyncCancel)
                   .Select(e => e.EventArgs.Reply));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Sends the specified e-mail message to an SMTP server for delivery.
        /// </summary>
        /// <param name="client">The object that sends the e-mail message.</param>
        /// <param name="message">A <see cref="MailMessage"/> that contains the message to send.</param>
        /// <returns>A singleton observable that contains the cached result of sending the specified <paramref name="message"/>.</returns>
        public static IObservable <EventPattern <AsyncCompletedEventArgs> > SendObservable(
            this SmtpClient client,
            MailMessage message)
        {
            Contract.Requires(client != null);
            Contract.Requires(message != null);
            Contract.Ensures(Contract.Result <IObservable <EventPattern <AsyncCompletedEventArgs> > >() != null);

            return(Observable2.FromEventBasedAsyncPattern <SendCompletedEventHandler>(
                       d => d.Invoke,
                       eh => client.SendCompleted += eh,
                       eh => client.SendCompleted -= eh,
                       token => client.SendAsync(message, token),
                       client.SendAsyncCancel));
        }
Ejemplo n.º 18
0
        public static IObservable <string> UploadString(
            Uri address,
            string method,
            string data)
        {
            Contract.Requires(address != null);
            Contract.Requires(method != null);
            Contract.Ensures(Contract.Result <IObservable <string> >() != null);

#if !SILVERLIGHT
            return(Observable2.UsingHot(new WebClient(), client => client.UploadStringObservable(address, method, data)));
#else
            return(new WebClient().UploadStringObservable(address, method, data));
#endif
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Downloads the specified resource as a <see cref="Byte"/> array.
        /// </summary>
        /// <param name="client">The object that downloads the resource.</param>
        /// <param name="address">A <see cref="Uri"/> containing the URI to download.</param>
        /// <returns>An observable that caches the result of the download and replays it to observers.</returns>
        public static IObservable <byte[]> DownloadDataObservable(
            this WebClient client,
            Uri address)
        {
            Contract.Requires(client != null);
            Contract.Requires(address != null);
            Contract.Ensures(Contract.Result <IObservable <byte[]> >() != null);

            return(Observable2.FromEventBasedAsyncPattern <DownloadDataCompletedEventHandler, DownloadDataCompletedEventArgs>(
                       handler => handler.Invoke,
                       handler => client.DownloadDataCompleted += handler,
                       handler => client.DownloadDataCompleted -= handler,
                       token => client.DownloadDataAsync(address, token),
                       client.CancelAsync)
                   .Select(e => e.EventArgs.Result));
        }
        /// <summary>
        /// Downloads the specified resource as a file.
        /// </summary>
        /// <param name="client">The object that downloads the resource.</param>
        /// <param name="address">A <see cref="Uri"/> containing the URI to download.</param>
        /// <param name="fileName">The file to create or overwrite with the resource.</param>
        /// <returns>An observable that caches the result of the download and replays it to observers.</returns>
        public static IObservable <string> DownloadFileObservable(
            this WebClient client,
            Uri address,
            string fileName)
        {
            Contract.Requires(client != null);
            Contract.Requires(address != null);
            Contract.Requires(fileName != null);
            Contract.Ensures(Contract.Result <IObservable <string> >() != null);

            return(Observable2.FromEventBasedAsyncPattern <AsyncCompletedEventHandler, AsyncCompletedEventArgs>(
                       handler => handler.Invoke,
                       handler => client.DownloadFileCompleted += handler,
                       handler => client.DownloadFileCompleted -= handler,
                       token => client.DownloadFileAsync(address, fileName, token),
                       client.CancelAsync)
                   .Select(e => fileName));
        }
Ejemplo n.º 21
0
        protected override void Main()
        {
            //// Note that Rxx for Silverlight does not support DependencyProperty change notifications,
            //// due to limitations in Silverlight.

            var foo = new Foo();
            var bar = new Bar();

            IObservable <string> seeFooChange = Observable2.FromPropertyChangedPattern(foo, o => o.Value);
            IObservable <string> seeBarChange = Observable2.FromPropertyChangedPattern(bar, o => o.Value);

            using (seeFooChange.Subscribe(ConsoleOutput))
                using (seeBarChange.Subscribe(ConsoleOutput))
                {
                    foo.Value = UserInput(Text.PromptFormat, Instructions.EnterAValueForFoo);
                    bar.Value = UserInput(Text.PromptFormat, Instructions.EnterAValueForBar);
                }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Opens a writeable stream to the specified resource.
        /// </summary>
        /// <param name="client">The object that uploads to the resource.</param>
        /// <param name="address">The URI of the resource to receive the stream.</param>
        /// <param name="method">The HTTP method used to send data to the resource.  If <see langword="null"/>, the default is POST for HTTP and STOR for FTP.</param>
        /// <returns>An observable containing the writeable stream that sends data to the resource.</returns>
        public static IObservable <Stream> OpenWriteObservable(
            this WebClient client,
            Uri address,
            string method)
        {
            Contract.Requires(client != null);
            Contract.Requires(address != null);
            Contract.Requires(method != null);
            Contract.Ensures(Contract.Result <IObservable <Stream> >() != null);

            return(Observable2.FromEventBasedAsyncPattern <OpenWriteCompletedEventHandler, OpenWriteCompletedEventArgs>(
                       handler => handler.Invoke,
                       handler => client.OpenWriteCompleted += handler,
                       handler => client.OpenWriteCompleted -= handler,
                       token => client.OpenWriteAsync(address, method, token),
                       client.CancelAsync)
                   .Select(e => e.EventArgs.Result));
        }
        private void TrySubscribe(object obj)
        {
            var observable = Observable2.Coerce <object>(obj);

            if (observable == null)
            {
                SetScalarValue(obj);
            }
            else
            {
                source = observable;

                currentValue = null;
                hasValue     = false;

                subscribing = Dispatcher.CurrentDispatcher.BeginInvoke((Action)Subscribe, DispatcherPriority.DataBind);
            }
        }
Ejemplo n.º 24
0
        protected override void Main()
        {
            var foo = new Foo();
            var bar = new Bar();
            var baz = new Baz();

            IObservable <string> seeFooChange = Observable2.FromPropertyChangedPattern(() => foo.Value);
            IObservable <string> seeBarChange = Observable2.FromPropertyChangedPattern(() => bar.Value);
            IObservable <string> seeBazChange = Observable2.FromPropertyChangedPattern(() => baz.Value);

            using (seeFooChange.Subscribe(ConsoleOutput))
                using (seeBarChange.Subscribe(ConsoleOutput))
                    using (seeBazChange.Subscribe(ConsoleOutput))
                    {
                        foo.Value = UserInput(Text.PromptFormat, Instructions.EnterAValueForFoo);
                        bar.Value = UserInput(Text.PromptFormat, Instructions.EnterAValueForBar);
                        baz.Value = UserInput(Text.PromptFormat, Instructions.EnterAValueForBaz);
                    }
        }
        /// <summary>
        /// Uploads data to the specified resource.
        /// </summary>
        /// <param name="client">The object that uploads to the resource.</param>
        /// <param name="address">The URI of the resource to receive the collection.</param>
        /// <param name="method">The HTTP method used to send data to the resource.  If <see langword="null"/>, the default is POST for HTTP and STOR for FTP.</param>
        /// <param name="values">The collection of data as name/value pairs to send to the resource.</param>
        /// <returns>An observable that caches the response from the server and replays it to observers.</returns>
        public static IObservable <byte[]> UploadValuesObservable(
            this WebClient client,
            Uri address,
            string method,
            NameValueCollection values)
        {
            Contract.Requires(client != null);
            Contract.Requires(address != null);
            Contract.Requires(values != null);
            Contract.Ensures(Contract.Result <IObservable <byte[]> >() != null);

            return(Observable2.FromEventBasedAsyncPattern <UploadValuesCompletedEventHandler, UploadValuesCompletedEventArgs>(
                       handler => handler.Invoke,
                       handler => client.UploadValuesCompleted += handler,
                       handler => client.UploadValuesCompleted -= handler,
                       token => client.UploadValuesAsync(address, method, values, token),
                       client.CancelAsync)
                   .Select(e => e.EventArgs.Result));
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Uploads a string to the specified resource.
        /// </summary>
        /// <param name="client">The object that uploads to the resource.</param>
        /// <param name="address">The URI of the resource to receive the string.</param>
        /// <param name="method">The HTTP method used to send data to the resource.  If <see langword="null"/>, the default is POST for HTTP and STOR for FTP.</param>
        /// <param name="data">The string to send to the resource.</param>
        /// <returns>An observable that caches the response from the server and replays it to observers.</returns>
        public static IObservable <string> UploadStringObservable(
            this WebClient client,
            Uri address,
            string method,
            string data)
        {
            Contract.Requires(client != null);
            Contract.Requires(address != null);
            Contract.Requires(method != null);
            Contract.Ensures(Contract.Result <IObservable <string> >() != null);

            return(Observable2.FromEventBasedAsyncPattern <UploadStringCompletedEventHandler, UploadStringCompletedEventArgs>(
                       handler => handler.Invoke,
                       handler => client.UploadStringCompleted += handler,
                       handler => client.UploadStringCompleted -= handler,
                       token => client.UploadStringAsync(address, method, data, token),
                       client.CancelAsync)
                   .Select(e => e.EventArgs.Result));
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Sends an e-mail message to an SMTP server for delivery.
        /// </summary>
        /// <param name="client">The object that sends the e-mail message.</param>
        /// <param name="from">Contains the address information of the message sender.</param>
        /// <param name="recipients">Contains the addresses that the message is sent to.</param>
        /// <param name="subject">Contains the subject line for the message.</param>
        /// <param name="body">Contains the message body.</param>
        /// <returns>A singleton observable that contains the cached result of sending the message.</returns>
        public static IObservable <EventPattern <AsyncCompletedEventArgs> > SendObservable(
            this SmtpClient client,
            string from,
            string recipients,
            string subject,
            string body)
        {
            Contract.Requires(client != null);
            Contract.Requires(!string.IsNullOrEmpty(from));
            Contract.Requires(!string.IsNullOrEmpty(recipients));
            Contract.Ensures(Contract.Result <IObservable <EventPattern <AsyncCompletedEventArgs> > >() != null);

            return(Observable2.FromEventBasedAsyncPattern <SendCompletedEventHandler>(
                       d => d.Invoke,
                       eh => client.SendCompleted += eh,
                       eh => client.SendCompleted -= eh,
                       token => client.SendAsync(from, recipients, subject, body, token),
                       client.SendAsyncCancel));
        }
        /// <summary>
        /// Adds a command binding to the specified <paramref name="element"/> and returns an observable sequence with two notification channels,
        /// with the right channel receiving notifications when the <paramref name="command"/> is executed and the left channel
        /// receiving notifications when the <paramref name="command"/> is queried as to whether it can be executed.
        /// </summary>
        /// <param name="command">The <see cref="ICommand"/> from which notifications are received.</param>
        /// <param name="element">The <see cref="UIElement"/> that queries or executes the specified <paramref name="command"/>.</param>
        /// <returns>An observable sequence with two notification channels, with the right channel receiving notifications when the <paramref name="command"/> is executed and
        /// the left channel receiving notifications when the <paramref name="command"/> is queried as to whether it can be executed.</returns>
        public static IObservable <Either <EventPattern <CanExecuteRoutedEventArgs>, EventPattern <ExecutedRoutedEventArgs> > > AsObservable(
            this ICommand command,
            UIElement element)
        {
            Contract.Requires(command != null);
            Contract.Requires(element != null);
            Contract.Ensures(Contract.Result <IObservable <Either <EventPattern <CanExecuteRoutedEventArgs>, EventPattern <ExecutedRoutedEventArgs> > > >() != null);

            return(Observable2.CreateEither <EventPattern <CanExecuteRoutedEventArgs>, EventPattern <ExecutedRoutedEventArgs> >(
                       observer =>
            {
                var binding = new CommandBinding(
                    command,
                    (sender, e) => observer.OnNextRight(new EventPattern <ExecutedRoutedEventArgs>(sender, e)),
                    (sender, e) => observer.OnNextLeft(new EventPattern <CanExecuteRoutedEventArgs>(sender, e)));

                element.CommandBindings.Add(binding);

                return () => element.CommandBindings.Remove(binding);
            }));
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Downloads the specified resource as a <see cref="Byte"/> array and includes a channel for progress notifications.
        /// </summary>
        /// <param name="client">The object that downloads the resource.</param>
        /// <param name="address">A <see cref="Uri"/> containing the URI to download.</param>
        /// <returns>An observable that contains progress notifications in the left channel and the data in the right channel.</returns>
        public static IObservable <Either <DownloadProgressChangedEventArgs, byte[]> > DownloadDataWithProgress(
            this WebClient client,
            Uri address)
        {
            Contract.Requires(client != null);
            Contract.Requires(address != null);
            Contract.Ensures(Contract.Result <IObservable <Either <DownloadProgressChangedEventArgs, byte[]> > >() != null);

            return(Observable2.FromEventBasedAsyncPattern <DownloadDataCompletedEventHandler, DownloadDataCompletedEventArgs, DownloadProgressChangedEventHandler, DownloadProgressChangedEventArgs>(
                       handler => handler.Invoke,
                       handler => client.DownloadDataCompleted += handler,
                       handler => client.DownloadDataCompleted -= handler,
                       handler => handler.Invoke,
                       handler => client.DownloadProgressChanged += handler,
                       handler => client.DownloadProgressChanged -= handler,
                       token => client.DownloadDataAsync(address, token),
                       client.CancelAsync)
                   .Select(
                       left => left.EventArgs,
                       right => right.EventArgs.Result));
        }
        private void Bind(object obj, Type targetType)
        {
            Contract.Requires(targetType != null);

            if (!disposed && !object.Equals(obj, boundSource))
            {
                boundSource = obj;

                CancelSubscribe();

                observer = Observable2.CoerceObserver <object>(obj);

                if (targetType == typeof(IEnumerable))
                {
                    TrySubscribeEnumerable(obj);
                }
                else
                {
                    TrySubscribe(obj);
                }
            }
        }