/// <summary>
            /// synchroniously updates <see cref="CoinListing"/> repository with data from server.
            /// </summary>
            public void UpdateRepository()
            {
                try
                {
                    CoinListing[] coinListingArray = CMCRequestHandler.RequestCoinListings();

                    // update id-to-CoinListing, name-to-CoinListing, symbol-to-CoinListing dictionaries
                    for (int i = 0; i < coinListingArray.Length; i++)
                    {
                        CoinListing curCoinListing = coinListingArray[i];

                        // get CoinListing fields in lowercase
                        int    id              = curCoinListing.Id;
                        string lowercaseName   = curCoinListing.Name.ToLower();
                        string lowercaseSymbol = curCoinListing.Symbol.ToLower();

                        // update dictionaries
                        coinIdToCoinListing[id] = curCoinListing;
                        coinNameToCoinListing[lowercaseName]     = curCoinListing;
                        coinSymbolToCoinListing[lowercaseSymbol] = curCoinListing;
                    }
                }
                catch (DataRequestException dataRequestException) // request unsuccessful
                {
                    throw new RepositoryUpdateException(dataRequestException);
                }
            }
Esempio n. 2
0
            /// <summary>
            /// if <see cref="coinTickerUpdateThreadRunning"/> is true,
            /// updates <see cref="CoinTickerRequestMaxNumberOfCoins"/> least recently updated
            /// <see cref="CoinTicker"/>s in repository.
            /// </summary>
            /// <seealso cref="RequestHandler.RequestCoinTicker(int, int, eSortType)"/>
            /// <seealso cref="handleCoinTickerRepositoryUpdateException(DataRequestException)"/>
            private void coinTickerUpdateTask_Target()
            {
                while (repositoryUpdateThreadRunning)
                {
                    try
                    {
                        // fetch data of current coin section
                        CoinTicker[] currentCoinTickerSection = CMCRequestHandler.RequestCoinTicker(
                            leastRecentlyUpdatedCoinIndex,
                            CoinTickerRequestMaxNumberOfCoins);

                        // update appropriate section in coin data array with newly fetched data
                        int currentCoinDataSectionSize = Math.Min(
                            NumberOfCoinsInRepository - leastRecentlyUpdatedCoinIndex,
                            currentCoinTickerSection.Length);

                        // overwrite corresponding old CoinTickers in coinId-to-CoinTicker dictionary
                        foreach (CoinTicker coinTicker in currentCoinTickerSection)
                        {
                            coinIdToCoinTicker[coinTicker.Id] = coinTicker;
                        }

                        // calculate range of updated coin IDs
                        int   lowerBound         = leastRecentlyUpdatedCoinIndex;
                        int   upperBound         = leastRecentlyUpdatedCoinIndex + currentCoinDataSectionSize - 1;
                        Range updatedCoinIdRange = new Range(lowerBound, upperBound);

                        // raise repository update event
                        onRepositoryUpdated(updatedCoinIdRange);

                        // set start index of next coin section (0 if current update run is complete)
                        leastRecentlyUpdatedCoinIndex += currentCoinTickerSection.Length;

                        if (leastRecentlyUpdatedCoinIndex > NumberOfCoinsInRepository - 1)
                        {
                            leastRecentlyUpdatedCoinIndex = 0;
                        }

                        if (leastRecentlyUpdatedCoinIndex == 0) // current update run is complete
                        {
                            // if repository was not yet initialized, raise repository initialized event
                            if (!repositoryInitialized)
                            {
                                repositoryInitialized = true;
                                onRepositoryInitialized();
                            }
                            else // notify user that update run is complete
                            {
                                ConsoleIOManager.Instance.LogNotice(
                                    "Coin ticker repository updated.",
                                    ConsoleIOManager.eOutputReportType.System);
                            }

                            // wait until next update run
                            Task.Delay(COIN_TICKER_UPDATE_DELAY_TIME_UNTIL_NEXT_RUN).Wait();
                        }
                    }
                    // exception while requesting CoinTicker data from server
                    catch (DataRequestException dataRequestException)
                    {
                        handleCoinTickerRepositoryUpdateException(dataRequestException);

                        try
                        {
                            Task.Delay(COIN_TICKER_UPDATE_DELAY_TIME_AFTER_EXCEPTION).Wait();
                        }
                        catch (AggregateException) // thrown by Task.Delay(int).wait()
                        {
                        }
                    }
                    catch (AggregateException) // thrown by Task.Delay(int).wait()
                    {
                    }
                }
            }