private void monitor()
        {
            string lastUid;
            try
            {
                lastUid = File.ReadAllText(Constants.DATA_DIR + @"\galnet");
            }
            catch
            {
                lastUid = null;
            }

            while (running)
            {
                List<News> newsItems = new List<News>();
                string firstUid = null;
                try
                {
                    foreach (FeedItem item in new FeedReader(new GalnetFeedItemNormalizer(), true).RetrieveFeed(SOURCE))
                    {
                        if (firstUid == null)
                        {
                            // Obtain the ID of the first item that we read
                            firstUid = item.Id;
                            if (lastUid == null)
                            {
                                // We don't have any ID yet; use this as the marker
                                break;
                            }
                        }

                        if (item.Id == lastUid)
                        {
                            // Reached the first item we have already seen - go no further
                            break;
                        }

                        News newsItem = new News(item.PublishDate.DateTime, item.Title, item.GetContent());
                        newsItems.Add(newsItem);
                    }
                }
                catch (WebException wex)
                {
                    Logging.Error("Exception attempting to obtain galnet feed: ", wex);
                }

                if (newsItems.Count > 0)
                {
                    EDDI.Instance.eventHandler(new GalnetNewsPublishedEvent(DateTime.Now, newsItems));
                }

                if (firstUid != lastUid)
                {
                    Logging.Debug("Updated latest UID to " + firstUid);
                    File.WriteAllText(Constants.DATA_DIR + @"\galnet", firstUid);
                    lastUid = firstUid;
                }
                Thread.Sleep(120000);
            }
        }
Ejemplo n.º 2
0
        private void monitor()
        {
            const int inGameOnlyStartDelayMilliSecs = 5 * 60 * 1000;  // 5 mins
            const int passiveIntervalMilliSecs      = 15 * 60 * 1000; // 15 mins
            const int activeIntervalMilliSecs       = 5 * 60 * 1000;  // 5 mins

            bool firstRun = true;

            while (running)
            {
                if (configuration.galnetAlwaysOn)
                {
                    monitorGalnet();
                    Thread.Sleep(passiveIntervalMilliSecs);
                }
                else
                {
                    // We'll update the Galnet Monitor only if a journal event has taken place within the specified number of minutes
                    if ((DateTime.UtcNow - journalTimeStamp).TotalMilliseconds < passiveIntervalMilliSecs)
                    {
                        if (firstRun)
                        {
                            // Wait at least 5 minutes after starting before polling for new articles
                            firstRun = false;
                            Thread.Sleep(inGameOnlyStartDelayMilliSecs);
                        }
                        monitorGalnet();
                        Thread.Sleep(activeIntervalMilliSecs);
                    }
                    else
                    {
                        Logging.Debug("No in-game activity detected, skipping galnet feed update");
                        Thread.Sleep(passiveIntervalMilliSecs);
                    }
                }

                void monitorGalnet()
                {
                    List <News> newsItems = new List <News>();
                    string      firstUid  = null;

                    locales.TryGetValue(configuration.language, out locale);
                    string url = GetGalnetResource("sourceURL");

                    altURL = false;

                    Logging.Debug("Fetching Galnet articles from " + url);
                    FeedReader             feedReader = new FeedReader(new GalnetFeedItemNormalizer(), true);
                    IEnumerable <FeedItem> items      = null;

                    try
                    {
                        items = feedReader.RetrieveFeed(url);
                    }
                    catch (WebException wex)
                    {
                        // FDev has in the past made available an alternate Galnet feed. We'll try the alternate feed.
                        // If the alternate feed fails, the page may not currently be available without an FDev login.
                        Logging.Warn("Exception contacting primary Galnet feed: ", wex);
                        url    = GetGalnetResource("alternateURL");
                        altURL = true;
                        Logging.Warn("Trying alternate Galnet feed (may not work)" + url);
                        try
                        {
                            items = feedReader.RetrieveFeed(url);
                        }
                        catch (Exception ex)
                        {
                            Logging.Warn("Galnet feed exception (alternate url unsuccessful): ", ex);
                        }
                    }
                    catch (System.Xml.XmlException xex)
                    {
                        Logging.Error("Exception attempting to obtain galnet feed: ", xex);
                    }

                    if (items != null)
                    {
                        try
                        {
                            foreach (GalnetFeedItemNormalizer.ExtendedFeedItem item in items)
                            {
                                try
                                {
                                    if (firstUid == null)
                                    {
                                        // Obtain the ID of the first item that we read as a marker
                                        firstUid = item.Id;
                                    }

                                    if (item.Id == configuration.lastuuid)
                                    {
                                        // Reached the first item we have already seen - go no further
                                        break;
                                    }

                                    if (item.Title is null || item.GetContent() is null)
                                    {
                                        // Skip items which do not contain useful content.
                                        continue;
                                    }

                                    News newsItem = new News(item.Id, assignCategory(item.Title, item.GetContent()), item.Title, item.GetContent(), item.PublishDate.DateTime, false);
                                    newsItems.Add(newsItem);
                                    GalnetSqLiteRepository.Instance.SaveNews(newsItem);
                                }
                                catch (Exception ex)
                                {
                                    Dictionary <string, object> data = new Dictionary <string, object>()
                                    {
                                        { "item", item },
                                        { "exception", ex }
                                    };
                                    Logging.Error("Exception handling Galnet news item.", data);
                                }
                            }

                            if (firstUid != null && firstUid != configuration.lastuuid)
                            {
                                Logging.Debug("Updated latest UID to " + firstUid);
                                configuration.lastuuid = firstUid;
                                configuration.ToFile();
                            }

                            if (newsItems.Count > 0)
                            {
                                // Spin out event in to a different thread to stop blocking
                                Thread thread = new Thread(() =>
                                {
                                    try
                                    {
                                        EDDI.Instance.enqueueEvent(new GalnetNewsPublishedEvent(DateTime.UtcNow, newsItems));
                                    }
                                    catch (ThreadAbortException)
                                    {
                                        Logging.Debug("Thread aborted");
                                    }
                                })
                                {
                                    IsBackground = true
                                };
                                thread.Start();
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.Error("Exception attempting to handle galnet feed: ", ex);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void monitor()
        {
            const int inGameOnlyStartDelayMilliSecs = 5 * 60 * 1000; // 5 mins
            const int alwaysOnIntervalMilliSecs     = 2 * 60 * 1000; // 2 mins
            const int inGameOnlyIntervalMilliSecs   = 30 * 1000;     // 30 secs

            if (!configuration.galnetAlwaysOn)
            {
                // Wait at least 5 minutes after starting before polling for new articles, but only if galnetAlwaysOn is false
                Thread.Sleep(inGameOnlyStartDelayMilliSecs);
            }

            while (running)
            {
                if (configuration.galnetAlwaysOn)
                {
                    monitorGalnet();
                    Thread.Sleep(alwaysOnIntervalMilliSecs);
                }
                else
                {
                    // We'll update the Galnet Monitor only if a journal event has taken place within the specified number of minutes
                    if ((DateTime.UtcNow - EDDI.Instance.JournalTimeStamp).TotalMinutes < 10)
                    {
                        monitorGalnet();
                    }
                    else
                    {
                        Logging.Debug("No in-game activity detected, skipping galnet feed update");
                    }
                    Thread.Sleep(inGameOnlyIntervalMilliSecs);
                }

                void monitorGalnet()
                {
                    List <News> newsItems = new List <News>();
                    string      firstUid  = null;

                    try
                    {
                        locales.TryGetValue(configuration.language, out locale);
                        string url = GetGalnetResource("sourceURL");

                        Logging.Debug("Fetching Galnet articles from " + url);
                        IEnumerable <FeedItem> items = new FeedReader(new GalnetFeedItemNormalizer(), true).RetrieveFeed(url);
                        if (items != null)
                        {
                            foreach (GalnetFeedItemNormalizer.ExtendedFeedItem item in items)
                            {
                                if (firstUid == null)
                                {
                                    // Obtain the ID of the first item that we read as a marker
                                    firstUid = item.Id;
                                }

                                if (item.Id == configuration.lastuuid)
                                {
                                    // Reached the first item we have already seen - go no further
                                    break;
                                }

                                News newsItem = new News(item.Id, assignCategory(item.Title, item.GetContent()), item.Title, item.GetContent(), item.PublishDate.DateTime, false);
                                newsItems.Add(newsItem);
                                GalnetSqLiteRepository.Instance.SaveNews(newsItem);
                            }
                        }
                    }
                    catch (WebException wex)
                    {
                        Logging.Debug("Exception attempting to obtain galnet feed: ", wex);
                    }

                    if (firstUid != configuration.lastuuid)
                    {
                        Logging.Debug("Updated latest UID to " + firstUid);
                        configuration.lastuuid = firstUid;
                        configuration.ToFile();
                    }

                    if (newsItems.Count > 0)
                    {
                        // Spin out event in to a different thread to stop blocking
                        Thread thread = new Thread(() =>
                        {
                            try
                            {
                                EDDI.Instance.enqueueEvent(new GalnetNewsPublishedEvent(DateTime.UtcNow, newsItems));
                            }
                            catch (ThreadAbortException)
                            {
                                Logging.Debug("Thread aborted");
                            }
                        })
                        {
                            IsBackground = true
                        };
                        thread.Start();
                    }
                }
            }
        }
        private void monitor()
        {
            string lastUid;

            try
            {
                lastUid = File.ReadAllText(Constants.DATA_DIR + @"\galnet");
            }
            catch
            {
                lastUid = null;
            }

            while (running)
            {
                List <News> newsItems = new List <News>();
                string      firstUid  = null;
                try
                {
                    foreach (FeedItem item in new FeedReader(new GalnetFeedItemNormalizer(), true).RetrieveFeed(SOURCE))
                    {
                        if (firstUid == null)
                        {
                            // Obtain the ID of the first item that we read
                            firstUid = item.Id;
                            if (lastUid == null)
                            {
                                // We don't have any ID yet; use this as the marker
                                break;
                            }
                        }

                        if (item.Id == lastUid)
                        {
                            // Reached the first item we have already seen - go no further
                            break;
                        }

                        News newsItem = new News(item.PublishDate.DateTime, item.Title, item.GetContent());
                        newsItems.Add(newsItem);
                    }
                }
                catch (WebException wex)
                {
                    Logging.Error("Exception attempting to obtain galnet feed: " + wex.ToString());
                }

                if (newsItems.Count > 0)
                {
                    EDDI.Instance.eventHandler(new GalnetNewsPublishedEvent(DateTime.Now, newsItems));
                }

                if (firstUid != lastUid)
                {
                    Logging.Debug("Updated latest UID to " + firstUid);
                    File.WriteAllText(Constants.DATA_DIR + @"\galnet", firstUid);
                    lastUid = firstUid;
                }
                Thread.Sleep(120000);
            }
        }