예제 #1
0
        private static SubscribersList getMatchedSubscribers(Subscribers subscribers, Categories categories, List <Magazine> magazines)
        {
            SubscribersList matchedSubscribers = new SubscribersList();

            matchedSubscribers.subscribers = new List <string>();
            foreach (Subscriber subscriber in subscribers.data)
            {
                //build a list of categories this user subscribes to
                List <string> subscriberCategories = new List <string>();
                foreach (int id in subscriber.magazineIds)
                {
                    //get the category of this magazine
                    string category = magazines.Where(x => x.id == id).First().category;

                    //if it's not already on our list, add it
                    if (!subscriberCategories.Contains(category))
                    {
                        subscriberCategories.Add(category);
                    }
                }

                //if we have the same amount of categories, subscriber has one for each
                if (subscriberCategories.Count() == categories.data.Count())
                {
                    matchedSubscribers.subscribers.Add(subscriber.id);
                }
            }
            return(matchedSubscribers);
        }
예제 #2
0
        static void Main(string[] args)
        {
            token = getToken();
            Console.WriteLine("Token: " + token);

            //get categories
            Console.Write("Get categories... ");
            Categories categories = getCategories();

            Console.WriteLine("done.");


            //get subscribers
            Console.Write("Get subscribers... ");
            Subscribers subscribers = getSubscribers();

            Console.WriteLine("done.");

            //get all magazines from each category
            Console.Write("Get magazines... ");
            List <Magazine> magazines = getAllMagazines(categories.data);

            Console.WriteLine("done.");

            //for subscribers who match all categories
            SubscribersList matchedSubscribers = getMatchedSubscribers(subscribers, categories, magazines);

            string jsonAnswer = JsonConvert.SerializeObject(matchedSubscribers);
            string response   = postAnswer(jsonAnswer);

            parseAnswer(response);

            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();
        }
        /// <summary>
        /// Publish event of type TEvent for handling.
        /// When there are no subscribers for this event NoSubscribers event published
        /// </summary>
        /// <typeparam name="TEvent"></typeparam>
        /// <param name="eventInstance">Event to publish</param>
        /// <param name="sender">sender instance</param>
        public virtual void Publish <TEvent>(TEvent eventInstance, ICanPublish <TEvent> sender)
        {
            if (sender == null)
            {
                // there is no any formal need to have sender in Publish method yet
                // Idea behid this requirement is to mark all classes which can publish events with interface so they can be discoverable
                // (With or without source code)
                throw new ArgumentException("sender could not be null");
            }

            object outValue = null;

            if (_subscribers.TryGetValue(typeof(TEvent), out outValue))
            {
                SubscribersList <TEvent> chain = (SubscribersList <TEvent>)outValue;
                if (chain != null)
                {
                    chain.Execute(eventInstance);
                }
            }
            else
            {
                if (typeof(TEvent) != typeof(NoSubscribers))
                {
                    Publish <NoSubscribers>(countNoSubscribers <TEvent>(eventInstance), this);
                }
            }
        }
        private void ViewSubscribers(object sender, TappedRoutedEventArgs e)
        {
            List <UserViewModel> subs   = Lists.SelectedList.SubscribedUsers.ToList();
            SubscribersList      dialog = new SubscribersList(subs);

            dialog.ShowAsync();
        }
        /// <summary>
        /// Subscribe handler to specific TEvent type
        /// </summary>
        /// <typeparam name="TEvent"></typeparam>
        /// <param name="handler">delegate to handle event</param>
        public virtual void Subscribe <TEvent>(Delegate <TEvent> handler)
        {
            object workflowAsObject = _subscribers.GetOrAdd(typeof(TEvent), (i) => { return(new SubscribersList <TEvent>()); });

            lock (workflowAsObject)
            {
                SubscribersList <TEvent> workflow = (SubscribersList <TEvent>)workflowAsObject;
                if (workflow._subscribers.Count >= MaxSubscriptionsPerEventType)
                {
                    throw new InvalidOperationException(
                              string.Format("More than {0} subscriptions per event is not allowed by default to prevent possible errors. Increase MaxSubscriptionsPerEvent or check your code for errors", MaxSubscriptionsPerEventType));
                }

                workflow.OnEvent += handler;
            }
        }
예제 #6
0
 public Broker()
 {
     Publishers  = new DevicesList();
     Subscribers = new SubscribersList();
 }