private void OnQueryNotification(Notification notification)
 {
     lock (SynchLock)
     {
         Parallel.ForEach(RequestHandlers.Keys, request =>
         {
             if (request.Queries.Count(q => q.Name == notification.QueryName) != 0)
             {
                 try
                 {
                     RequestObserver handler = RequestHandlers[request];
                     if (handler != null && handler.Observer != null)
                     {
                         lock (handler)
                         {
                             handler.Observer.OnNext(new NotificationResponse(request.Id, handler.Notifications));
                             handler.Notifications.Clear();
                         }
                     }
                 }
                 catch (Exception ex)
                 {
                     if (_log.IsErrorEnabled)
                     {
                         _log.ErrorFormat("Notification failed for request '{0}': {1}", request.Id, ex.Message);
                     }
                 }
             }
         });
     }
 }
Beispiel #2
0
        /// <summary>
        /// Gets multiple oEmbeds
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="urls">The urls.</param>
        /// <param name="providerFilter">The provider filter.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static IEnumerable <Result> GetOEmbeds(this Client client, IEnumerable <Uri> urls, Func <Provider, bool> providerFilter, RequestOptions options)
        {
            if (urls == null)
            {
                throw new ArgumentNullException("urls");
            }

            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            var results    = new BlockingCollection <Result>();
            var redirector = new RequestObserver(client, options);

            redirector.Output.Subscribe(results.Add, results.CompleteAdding);

            IObservable <UrlRequest> requests = urls
                                                .ToObservable(NewThreadScheduler.Default)
                                                .Select(u => u.MakeUrlRequests(client))
                                                .WhereProvider(providerFilter);

            requests.Subscribe(redirector);

            return(results.GetConsumingEnumerable());
        }
Beispiel #3
0
 public WebServer(string host, int port, RequestObserver observer = null)
 {
     Host = host;
     Port = port;
     Observer = observer;
     ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     Handlers = new List<Handler>();
 }
Beispiel #4
0
        /// <summary>
        /// Gets multiple oEmbeds
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="urls">The urls.</param>
        /// <param name="providerFilter">The provider filter.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static IObservable <Result> GetOEmbeds(this Client client, IObservable <Uri> urls, Func <Provider, bool> providerFilter, RequestOptions options)
        {
            if (urls == null)
            {
                throw new ArgumentNullException("urls");
            }

            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            IObservable <UrlRequest> requests = urls
                                                .Select(u => u.MakeUrlRequests(client))
                                                .WhereProvider(providerFilter);

            var redirector = new RequestObserver(client, options);

            requests.Subscribe(redirector);

            return(redirector.Output);
        }
        public IDisposable RegisterNotification(NotificationRequest request, IObserver <NotificationResponse> observer)
        {
            try
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Registering notification request: {0}", request.Id);
                    foreach (var query in request.Queries)
                    {
                        _log.DebugFormat("Notification query: {0}, {1}", query.Name, query.Sql);
                    }
                }


                lock (SynchLock)
                {
                    if (RequestHandlers.Count == 0)
                    {
                        OnFirstSubscription();
                    }
                    var requestObserver = new RequestObserver(observer);
                    RequestHandlers.AddOrUpdate(request, requestObserver, (k, v) => requestObserver);

                    Parallel.ForEach(request.Queries, query =>
                    {
                        if (!_queryHandlers.ContainsKey(query.Name))
                        {
                            try
                            {
                                lock (_queryHandlers)
                                {
                                    T_Handler handler = new T_Handler();
                                    handler.Initialise(this, query);
                                    _queryHandlers.Add(query.Name, handler);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (_log.IsErrorEnabled)
                                {
                                    _log.ErrorFormat("Failed to register notification query: {0} for request: {1} - {2}", query.Name, request.Id, ex.Message);
                                }
                            }
                        }
                        var queryHandler = _queryHandlers[query.Name];
                        var subscription = queryHandler.Subscribe(Observer.Create <Notification>(OnQueryNotification));
                        request.Subscriptions.Add(subscription);
                    });
                }

                return(new Unsubscriber(this, request));
            }
            catch (Exception ex)
            {
                if (_log.IsErrorEnabled)
                {
                    _log.ErrorFormat("Failed to register notification request: {0} - {1}", request.Id, ex.Message);
                }
                throw ex;
            }
        }
        /// <summary>
        /// Gets multiple oEmbeds
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="urls">The urls.</param>
        /// <param name="providerFilter">The provider filter.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static IObservable<Result> GetOEmbeds(this Client client, IObservable<Uri> urls, Func<Provider, bool> providerFilter, RequestOptions options)
        {
            if (urls == null)
                throw new ArgumentNullException("urls");

            if (options == null)
                throw new ArgumentNullException("options");

            var requests = urls
                .ObserveOn(NewThreadScheduler.Default)
                .Select(u => u.MakeUrlRequests(client))
                .WhereProvider(providerFilter);

            var redirector = new RequestObserver(client, options);

            requests.Subscribe(redirector);

            return redirector.Output;
        }
        /// <summary>
        /// Gets multiple oEmbeds
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="urls">The urls.</param>
        /// <param name="providerFilter">The provider filter.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static IEnumerable<Result> GetOEmbeds(this Client client, IEnumerable<Uri> urls, Func<Provider, bool> providerFilter, RequestOptions options)
        {
            if (urls == null)
                throw new ArgumentNullException("urls");

            if (options == null)
                throw new ArgumentNullException("options");

            var results = new BlockingCollection<Result>();
            var redirector = new RequestObserver(client, options);

            redirector.Output.Subscribe(results.Add, results.CompleteAdding);

            var requests = urls
                .ToObservable(NewThreadScheduler.Default)
                .Select(u => u.MakeUrlRequests(client))
                .WhereProvider(providerFilter);

            requests.Subscribe(redirector);

            return results.GetConsumingEnumerable();
        }