Ejemplo n.º 1
0
        public static async Task <bool> RemoveServerAsync(string forumUrl)
        {
            forumUrl = resolveFullPath(forumUrl);

            if (!subs.Contains(forumUrl))
            {
                return(false);
            }

            string        value  = cfg.AppSettings.Settings[CFG_PREFIX].Value;
            List <string> forums = new List <string>(value.Split(CFG_SEPARATOR, StringSplitOptions.RemoveEmptyEntries));

            // Remove from local list
            if (forums.Contains(forumUrl))
            {
                forums.Remove(forumUrl);
            }

            // Remove from subs list
            subs = new ConcurrentBag <string>(subs.Except(new string[] { forumUrl }));

            WowCircleForum forumObj = null;

            if (snapshot.ContainsKey(forumUrl))
            {
                snapshot.Remove(forumUrl, out forumObj);
            }

            await RefreshCfgAsync(forums);

            return(true);
        }
Ejemplo n.º 2
0
        private void OnParticipantMessageReceived(Participant participant, string message)
        {
            var data = new { Name = participant.Name, Body = message };

            foreach (var otherParticipant in participants.Except(new[] { participant }))
            {
                otherParticipant.SendMessage(data);
            }
        }
Ejemplo n.º 3
0
        private void Events_AskForStockSubscriptionEvent(string tradingSymbol, StockSubscribeMode mode, bool isSubscribe)
        {
            switch (mode)
            {
            case StockSubscribeMode.LTP:
                if (isSubscribe)
                {
                    if (!_configuredStocksForLTP.Any(s => s.Equals(tradingSymbol)))
                    {
                        _configuredStocksForLTP.Add(tradingSymbol);
                    }
                }
                else
                {
                    var stock = _configuredStocksForLTP.FirstOrDefault(s => s.Equals(tradingSymbol));
                    if (stock != null)
                    {
                        _configuredStocksForLTP = new ConcurrentBag <string>(_configuredStocksForLTP.Except(new[] { tradingSymbol }));
                        if (_cachedStockLtp.ContainsKey(tradingSymbol))
                        {
                            _cachedStockLtp.Remove(tradingSymbol);
                        }
                    }
                }
                break;
            }

            StartStopLTP(_configuredStocksForLTP.Any());
        }
Ejemplo n.º 4
0
        public Glass TakeGlassFromBarTop()
        {
            Glass glass = barTop.ElementAt(0);

            barTop = new ConcurrentBag <Glass>(barTop.Except(new[] { glass }));
            return(glass);
        }
Ejemplo n.º 5
0
        public void RemoveConnectionFromMatch(string connectionId, SimplerMatchHub hub)
        {
            foreach (var match in Matches.ToList())
            {
                if (match.ConnectionIds.Contains(connectionId))
                {
                    var thePlayer = match.Players.FirstOrDefault(p => p.ConnectionId == connectionId);

                    if (thePlayer != null)
                    {
                        System.Console.WriteLine($"Client Disconnected: {hub.Context.ConnectionId}");
                        hub.GetOtherMatchConnections(match).SendAsync("ShipQuitTheGame", thePlayer.Id);
                        match.Players.Remove(thePlayer);
                    }

                    match.ConnectionIds = new ConcurrentBag <string>(match.ConnectionIds.Except(new[] { connectionId }));
                }

                if (!match.ConnectionIds.Any())
                {
                    System.Console.WriteLine("Will remove match");
                    Matches = new ConcurrentBag <SimplerMatchState>(Matches.Except(new[] { match }));
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Overrides the controlled instance
        /// </summary>
        /// <typeparam name="T">Old controlled instance</typeparam>
        /// <typeparam name="T2">Overriding instance</typeparam>
        protected void OverrideControlledInstance <T, T2>()   where T : AbstractedSubController
            where T2 : new()
        {
            var oldController = _abstractedSubControllers.FirstOrDefault(x => x is T);

            if (typeof(T2) == typeof(T) || oldController == null)
            {
                // nothing to override
                return;
            }

            var instance = new T2();

            var controller = instance as AbstractedSubController;

            if (controller == null)
            {
                return;
            }

            controller.Character = Character;

            oldController.OnOverwritten();

            _abstractedSubControllers =
                new ConcurrentBag <AbstractedSubController>(_abstractedSubControllers.Except(new[] { oldController }))
            {
                controller
            };

            controller.OnAdded();
        }
Ejemplo n.º 7
0
        private void TrimExcess()
        {
            List <WeakReference <T> > toRemove = new List <WeakReference <T> >();

            foreach (int i in hashTable.Keys)
            {
                ConcurrentBag <WeakReference <T> > bucket = hashTable[i];
                foreach (WeakReference <T> item in bucket)
                {
                    if (!item.TryGetTarget(out T output))
                    {
                        toRemove.Add(item);
                    }
                }
                // Remove the identified items
                if (toRemove.Count == bucket.Count)
                {
                    hashTable.TryRemove(i, out bucket);
                }
                else
                {
                    hashTable.TryUpdate(i, new ConcurrentBag <WeakReference <T> >(bucket.Except(toRemove)), bucket);
                }
                Interlocked.Add(ref ExcessCountdown, toRemove.Count());

                toRemove.Clear();
            }
        }
Ejemplo n.º 8
0
        public List <Glass> RemoveGlasses()
        {
            int          glassesToRemove = glassesOnTable.Count;
            List <Glass> glassesToReturn = new List <Glass>();

            glassesToReturn.AddRange(glassesOnTable.Take <Glass>(glassesToRemove).ToList());
            glassesOnTable = new ConcurrentBag <Glass>(glassesOnTable.Except(glassesToReturn));
            return(glassesToReturn);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Handles a connection closure.
        /// </summary>
        /// <param name="closeReason">The reason for the <see cref="Connection"/> being closed.</param>
        /// <param name="connection">The <see cref="Connection"/> that closed.</param>
        private void connectionClosed(CloseReason closeReason, Connection connection)
        {
            if (connection.GetType().Equals(typeof(TcpConnection)))
            {
                List <UdpConnection> udpConnections = new List <UdpConnection>();
                TcpConnection        tcpConnection  = (TcpConnection)connection;
                while (!connections.TryRemove(tcpConnection, out udpConnections))
                {
                    Thread.Sleep(new Random().Next(0, 8)); //If we could not remove the tcpConnection, try it again.
                }
                udpConnections.ForEach(u => u.ExternalClose(closeReason));

                // cleanup the event handler for the TCP connection.
                connection.ConnectionEstablished -= udpConnectionReceived;
            }
            else if (connection.GetType().Equals(typeof(UdpConnection)))
            {
                TcpConnection tcpConnection = this[(UdpConnection)connection];
                //UDP connection already removed because the TCP connection is already dead.
                if (tcpConnection == null)
                {
                    return;
                }
                connections[tcpConnection].Remove((UdpConnection)connection);
            }
#if NET46
            else if (connection.GetType().Equals(typeof(BluetoothConnection)))
            {
                //Remove the bluetooth connection from the bag.
                bluetoothConnections = new ConcurrentBag <BluetoothConnection>(bluetoothConnections.Except(new[] { (BluetoothConnection)connection }));
            }
#endif

            if (connectionLost != null &&
                connectionLost.GetInvocationList().Length > 0 &&
                connection.GetType().Equals(typeof(TcpConnection)))
            {
                connectionLost(connection, ConnectionType.TCP, closeReason);
            }
            else if (connectionLost != null &&
                     connectionLost.GetInvocationList().Length > 0 &&
                     connection.GetType().Equals(typeof(UdpConnection)))
            {
                connectionLost(connection, ConnectionType.UDP, closeReason);
            }
#if NET46
            else if (connectionLost != null &&
                     connection.GetType().Equals(typeof(BluetoothConnection)))
            {
                connectionLost(connection, ConnectionType.Bluetooth, closeReason);
            }
#endif

            // remove the connection lost event handler to enable GC.
            connection.NetworkConnectionClosed -= connectionClosed;
        }
        public void ThenAllMessagesAreAccountedFor()
        {
            WaitUtility.WaitUntil("Not all messages received by the server in the given time.",
                                  () => Assert.AreEqual(0, expectedToServer.Except(messagesToServer).Count()),
                                  TimeSpan.FromMinutes(1));
            WaitUtility.WaitUntil("Not all messages received by the clients in the given time.",
                                  () => Assert.AreEqual(0, expectedToClients.Except(messagesToClients).Count()),
                                  TimeSpan.FromMinutes(1));

            Assert.AreEqual(0, messagesToServer.Except(expectedToServer).Count(), "Additional, unexpected messages received by the server.");
            Assert.AreEqual(0, messagesToClients.Except(expectedToClients).Count(), "Additional, unexpected messages received by the clients.");
        }
        internal void HandleResponse(string serialized, T service, DisterService <T> disterService, string id)
        {
            var o   = disterService.Serializer.Deserialize(serialized, responseType);
            var arr = waitingChunks.ToArray();

            if (arr.Any(x => x.Key == id))
            {
                var currentChunk = arr.First(x => x.Key == id);
                waitingChunks = new ConcurrentBag <KeyValuePair <string, object> >(waitingChunks.Except(new[] { currentChunk }));
                responseHandler(o, service);
            }
        }
Ejemplo n.º 12
0
 void OnPositionUpdate()
 {
     try
     {
         var positions = Client.GetPositions(ProductCode).GetResult();
         positions.Except(_positions).ForEach(e => PositionChanged?.Invoke(e, true));    // Notify opened positions
         _positions.Except(positions).ForEach(e => PositionChanged?.Invoke(e, false));   // Notify closed positions
         Interlocked.Exchange(ref _positions, new ConcurrentBag <BfPosition>(positions));
     }
     catch (Exception ex)
     {
         DebugEx.Trace(ex.Message);
     }
 }
Ejemplo n.º 13
0
        public bool RemoveMap(IMap map)
        {
            if (map == null || !_maps.Contains(map))
            {
                return(false);
            }

            _maps = new ConcurrentBag <IMap>(_maps.Except(new[] { map }));

            if (MapDeleted != null)
            {
                MapDeleted(map);
            }

            return(true);
        }
        private void RemovePeer(PeerConnector peerConnector, IpAddress ipAdr)
        {
            if (peerConnector == null)
            {
                throw new ArgumentNullException(nameof(peerConnector));
            }

            if (ipAdr == null)
            {
                throw new ArgumentNullException(nameof(ipAdr));
            }

            _peers = new ConcurrentBag <PeerConnector>(_peers.Except(new[] { peerConnector }));
            _peersRepository.RemovePeer(ipAdr);
            peerConnector.Dispose();
        }
Ejemplo n.º 15
0
        /// <summary>
        /// TCPs the or UDP connection closed.
        /// </summary>
        /// <param name="closeReason">The close reason.</param>
        /// <param name="connection">The connection.</param>
        private void connectionClosed(CloseReason closeReason, Connection connection)
        {
            if (connection.GetType().Equals(typeof(TcpConnection)))
            {
                List <UdpConnection> udpConnections = new List <UdpConnection>();
                TcpConnection        tcpConnection  = (TcpConnection)connection;
                while (!connections.TryRemove(tcpConnection, out udpConnections))
                {
                    Thread.Sleep(new Random().Next(0, 8)); //If we could not remove the tcpConnection, try it again.
                }
                udpConnections.ForEach(u => u.ExternalClose(closeReason));
            }
            else if (connection.GetType().Equals(typeof(UdpConnection)))
            {
                TcpConnection tcpConnection = this[(UdpConnection)connection];
                if (tcpConnection == null)
                {
                    return;                        //UDP connection already removed
                }
                //because the TCP connection is already dead.
                connections[tcpConnection].Remove((UdpConnection)connection);
            }
            else if (connection.GetType().Equals(typeof(BluetoothConnection)))
            {
                //Remove the bluetooth connection from the bag.
                bluetoothConnections = new ConcurrentBag <BluetoothConnection>(bluetoothConnections.Except(new[] { (BluetoothConnection)connection }));
            }

            if (connectionLost != null &&
                connectionLost.GetInvocationList().Length > 0 &&
                connection.GetType().Equals(typeof(TcpConnection)))
            {
                connectionLost(connection, ConnectionType.TCP, closeReason);
            }
            else if (connectionLost != null &&
                     connectionLost.GetInvocationList().Length > 0 &&
                     connection.GetType().Equals(typeof(UdpConnection)))
            {
                connectionLost(connection, ConnectionType.UDP, closeReason);
            }
            else if (connectionLost != null &&
                     connection.GetType().Equals(typeof(BluetoothConnection)))
            {
                connectionLost(connection, ConnectionType.Bluetooth, closeReason);
            }
        }
Ejemplo n.º 16
0
        public Task UpdateUser(UserViewModel viewModel)
        {
            var user = _users.FirstOrDefault(u => u.Email.Equals(viewModel.Email));

            if (user == null)
            {
                _users.Add(viewModel);
            }
            else
            {
                _users = new ConcurrentBag <UserViewModel>(_users.Except(new[] { viewModel }))
                {
                    viewModel
                }
            };

            return(Task.CompletedTask);
        }
    }
Ejemplo n.º 17
0
        public override async Task OnConnectedAsync()
        {
            var connectionID = Context.ConnectionId;
            await base.OnConnectedAsync();

            string assignedAnimal;

            lock (_connectionIdsAndAssignedAnimal)
            {
                assignedAnimal = _animals.Except(_connectionIdsAndAssignedAnimal.Select(connections => connections.Value)).OrderBy(x => Guid.NewGuid()).First();
                _connectionIdsAndAssignedAnimal.Add(connectionID, assignedAnimal);
            }

            await Clients.Caller.SendAsync("animalAssigned", assignedAnimal);

            await Clients.Others.SendAsync("newAnimalInTheFlock", assignedAnimal);

            await Clients.Caller.SendAsync("updatedAnimalList", _connectionIdsAndAssignedAnimal.Select(c => c.Value));
        }
Ejemplo n.º 18
0
 private ConcurrentBag <T> Remove <T>(ConcurrentBag <T> chatters, T disconnectedChatUser)
 {
     return(chatters = new ConcurrentBag <T>(chatters?.Except(new[] { disconnectedChatUser })));
 }
Ejemplo n.º 19
0
 private void Remove <T>(ConcurrentBag <T> players, T playerWithoutGame)
 {
     players = new ConcurrentBag <T>(players?.Except(new[] { playerWithoutGame }));
 }
Ejemplo n.º 20
0
 public static ConcurrentBag <T> Without <T>(this ConcurrentBag <T> bag, T item)
 {
     return(new ConcurrentBag <T>(bag.Except(new[] { item })));
 }
Ejemplo n.º 21
0
 private ConcurrentBag <T> Remove <T>(ConcurrentBag <T> players, T playerWithoutGame)
 {
     return(new ConcurrentBag <T>(players?.Except(new[] { playerWithoutGame })));
 }
Ejemplo n.º 22
0
        public override ActionTaskResult Process(ActionContext context)
        {
            int bundleSize             = GetBundleSize();
            int maxDegreeOfParallelism = GetMaxDegreeOfParallelism();

            string[] channels = context.Parameters.GetChannels();
            bool     localize = context.Parameters.GetLocalize();

            int    marketingProductContentId = int.Parse(_settingsService.GetSetting(SettingsTitles.MARKETING_PRODUCT_CONTENT_ID));
            string productsFieldName         = _settingsService.GetSetting(SettingsTitles.MARKETING_PRODUCT_PRODUCTS_FIELD_NAME);

            Dictionary <int, int[]> articleIdsToCheckRelationsByContentId;

            int[] productIds;

            if (context.ContentItemIds == null || context.ContentItemIds.Length == 0)
            {
                productIds = DoWithLogging(
                    () => Helpers.GetAllProductIds(int.Parse(context.Parameters["site_id"]), context.ContentId, _provider.GetCustomer()),
                    "Getting all products from content {contentId}", context.ContentId
                    );

                articleIdsToCheckRelationsByContentId = new Dictionary <int, int[]>
                {
                    { context.ContentId, productIds }
                };
            }
            else
            {
                productIds = DoWithLogging(
                    () => Helpers.ExtractRegionalProductIdsFromMarketing(context.ContentItemIds, _articleService, marketingProductContentId, productsFieldName),
                    "Getting regional product ids from marketing products content {contentId} using  field {fieldName} and ids {ids}",
                    marketingProductContentId, productsFieldName, context.ContentItemIds
                    );

                articleIdsToCheckRelationsByContentId = Helpers.GetContentIds(productIds, _provider.GetCustomer());
            }

            if (productIds.Length == 0)
            {
                return(ActionTaskResult.Error(new ActionTaskResultMessage()
                {
                    ResourceClass = nameof(SendProductActionStrings),
                    ResourceName = nameof(SendProductActionStrings.NotFound),
                    Extra = string.Join(", ", context.ContentItemIds)
                }, context.ContentItemIds));
            }

            foreach (var articleIdsWithContentId in articleIdsToCheckRelationsByContentId)
            {
                var checkResult =
                    DoWithLogging(
                        () => _articleService.CheckRelationSecurity(articleIdsWithContentId.Key, articleIdsWithContentId.Value, false),
                        "Checking relation security in content {contentId} for articles {ids}",
                        articleIdsWithContentId.Key, articleIdsWithContentId.Value
                        );

                string idsstr = string.Join(", ", checkResult.Where(n => !n.Value));

                if (!string.IsNullOrEmpty(idsstr))
                {
                    return(ActionTaskResult.Error(new ActionTaskResultMessage()
                    {
                        ResourceClass = nameof(SendProductActionStrings),
                        ResourceName = nameof(SendProductActionStrings.NoRelationAccess),
                        Extra = idsstr
                    }, context.ContentItemIds));
                }
            }

            const string skipPublishingKey = "skipPublishing";

            bool skipPublishing = context.Parameters.ContainsKey(skipPublishingKey) && bool.Parse(context.Parameters[skipPublishingKey]);

            const string skipLiveKey = "skipLive";

            bool skipLive = context.Parameters.ContainsKey(skipLiveKey) && bool.Parse(context.Parameters[skipLiveKey]);

            const string ignoredStatusKey = "IgnoredStatus";

            string ignoredStatus = (context.Parameters.ContainsKey(ignoredStatusKey)) ? context.Parameters[ignoredStatusKey] : null;


            float currentPercent = 0;

            object percentLocker = new object();



            var parts = productIds.Section(bundleSize).ToArray();

            var filteredInStage            = new ConcurrentBag <int>();
            var filteredInLive             = new ConcurrentBag <int>();
            var failed                     = new ConcurrentDictionary <int, object>();
            var missing                    = new ConcurrentBag <int>();
            var excluded                   = new ConcurrentBag <int>();
            var frozen                     = new ConcurrentBag <int>();
            var invisibleOrArchivedIds     = new ConcurrentBag <int>();
            var errors                     = new ConcurrentBag <Exception>();
            var validationErrors           = new ConcurrentDictionary <int, ActionTaskResult>();
            var validationErrorsSerialized = new ConcurrentDictionary <int, string>();

            Parallel.ForEach(parts, new ParallelOptions {
                MaxDegreeOfParallelism = maxDegreeOfParallelism
            },
                             () =>
            {
                HttpContextUserProvider.ForcedUserId = context.UserId;
                return(new Local
                {
                    ProductService = ObjectFactoryBase.Resolve <IProductService>(),
                    QpNotificationService = ObjectFactoryBase.Resolve <IQPNotificationService>(),
                    XmlProductService = ObjectFactoryBase.Resolve <IXmlProductService>()
                });
            },
                             (idsToProcess, ps, tl) =>
            {
                try
                {
                    if (TaskContext.IsCancellationRequested)
                    {
                        TaskContext.IsCancelled = true;

                        return(tl);
                    }

                    var localInvisibleOrArchivedIds = new HashSet <int>();


                    Article[] prodsStage = DoWithLogging(
                        () => tl.ProductService.GetProductsByIds(idsToProcess.ToArray()),
                        "Getting products {ids}", idsToProcess.ToArray()
                        );
                    IEnumerable <string> ignoredStatuses = ignoredStatus?.Split(',') ??
                                                           Enumerable.Empty <string>().ToArray();
                    var excludedStage = prodsStage.Where(n => ignoredStatuses.Contains(n.Status)).ToArray();

                    foreach (var item in excludedStage)
                    {
                        excluded.Add(item.Id);
                    }

                    prodsStage = prodsStage.Except(excludedStage).ToArray();

                    var frozenIds = new int[0];

                    if (!skipLive)
                    {
                        var idsToCheck = prodsStage.Select(p => p.Id).ToArray();
                        frozenIds      = DoWithLogging(
                            () => _freezeService.GetFrozenProductIds(idsToCheck),
                            "Getting freezing state for products {ids}", idsToCheck
                            );
                    }

                    prodsStage = prodsStage.Where(p => !frozenIds.Contains(p.Id)).ToArray();

                    foreach (int id in frozenIds)
                    {
                        frozen.Add(id);
                    }

                    if (TaskContext.IsCancellationRequested)
                    {
                        TaskContext.IsCancelled = true;

                        return(tl);
                    }

                    //Валидация продуктов
                    foreach (int id in prodsStage.Where(w => !w.Archived && w.Visible).Select(s => s.Id))
                    {
                        var xamlValidationErrors = DoWithLogging(
                            () => _articleService.XamlValidationById(id, true),
                            "Validating XAML for product {id}", id
                            );
                        var validationResult = ActionTaskResult.FromRulesException(xamlValidationErrors, id);

                        if (!validationResult.IsSuccess)
                        {
                            validationErrors.TryAdd(id, validationResult);
                            validationErrorsSerialized.TryAdd(id, validationResult.ToString());
                        }
                    }
                    prodsStage = prodsStage.Where(w => !validationErrors.Keys.Contains(w.Id)).ToArray();

                    var prodsLive = new Article[] { };

                    if (!skipLive)
                    {
                        if (!skipPublishing)
                        {
                            prodsLive = prodsStage;
                        }
                        else
                        {
                            prodsLive = DoWithLogging(
                                () => tl.ProductService.GetProductsByIds(idsToProcess.ToArray(), true),
                                "Getting separate live products {ids}", idsToProcess.ToArray()
                                );
                        }
                    }


                    if (TaskContext.IsCancellationRequested)
                    {
                        TaskContext.IsCancelled = true;

                        return(tl);
                    }

                    lock (percentLocker)
                    {
                        currentPercent += (float)50 / parts.Length;
                    }

                    TaskContext.SetProgress((byte)currentPercent);

                    // архивные или невидимые продукты следует удалить с витрин
                    foreach (var product in prodsLive.Where(product => product.Archived || !product.Visible))
                    {
                        localInvisibleOrArchivedIds.Add(product.Id);
                    }

                    if (!skipLive)
                    {
                        foreach (var item in idsToProcess.Except(prodsLive.Select(y => y.Id)))
                        {
                            missing.Add(item);
                        }
                    }

                    //неопубликованные или расщепленные публикуем сразу
                    int[] prodsToPublishIds = null;

                    if (!skipPublishing)
                    {
                        prodsToPublishIds = prodsLive
                                            .Where(x =>
                                                   !x.Archived &&
                                                   x.Visible &&
                                                   (
                                                       !x.IsPublished ||
                                                       PublishAction.GetAllArticlesToCheck(x).Any(p => !p.IsPublished) ||
                                                       _freezeService.GetFreezeState(x.Id) == FreezeState.Unfrosen
                                                   )
                                                   )
                                            .Select(x => x.Id)
                                            .ToArray();

                        if (TaskContext.IsCancellationRequested)
                        {
                            TaskContext.IsCancelled = true;

                            return(tl);
                        }

                        // удалим требующие публикации или удаления продукты
                        prodsLive = prodsLive
                                    .Where(p => !prodsToPublishIds.Contains(p.Id) && !localInvisibleOrArchivedIds.Contains(p.Id))
                                    .ToArray();
                    }


                    if (TaskContext.IsCancellationRequested)
                    {
                        TaskContext.IsCancelled = true;

                        return(tl);
                    }

                    lock (percentLocker)
                    {
                        currentPercent += (float)30 / parts.Length;
                    }

                    TaskContext.SetProgress((byte)currentPercent);

                    foreach (var id in localInvisibleOrArchivedIds)
                    {
                        invisibleOrArchivedIds.Add(id);
                    }

                    int sectionSize = Math.Min(bundleSize, 5);

                    var tasks =
                        ArticleFilter.LiveFilter.Filter(prodsLive)
                        .Section(sectionSize)
                        .Select(z => tl.QpNotificationService
                                .SendProductsAsync(z.ToArray(), false, context.UserName, context.UserId,
                                                   localize, false, channels)
                                .ContinueWith(y => UpdateFilteredIds(filteredInLive,
                                                                     y.IsFaulted ? null : y.Result, z, y.Exception, errors, failed)))
                        .Concat(ArticleFilter.DefaultFilter.Filter(prodsStage)
                                .Section(sectionSize)
                                .Select(z => tl.QpNotificationService.SendProductsAsync(z.ToArray(), true,
                                                                                        context.UserName, context.UserId, localize, false, channels)
                                        .ContinueWith(y => UpdateFilteredIds(filteredInStage,
                                                                             y.IsFaulted ? null : y.Result, z, y.Exception, errors, failed))))
                        .ToArray();



                    if (tasks.Length > 0)
                    {
                        float percentsPerTask = (float)10 / parts.Length / tasks.Length;

                        tasks = tasks
                                .Select(x => x.ContinueWith(y =>
                        {
                            lock (percentLocker)
                            {
                                currentPercent += percentsPerTask;
                            }
                            TaskContext.SetProgress((byte)currentPercent);
                        }))
                                .ToArray();

                        DoWithLogging(() => Task.WaitAll(tasks),
                                      "Sending notifications for live ({liveIds}) and stage ({stageIds}) products",
                                      ArticleFilter.LiveFilter.Filter(prodsLive).Select(n => n.Id).ToArray(),
                                      ArticleFilter.DefaultFilter.Filter(prodsStage).Select(n => n.Id).ToArray()
                                      );
                    }
                    else
                    {
                        lock (percentLocker)
                        {
                            currentPercent += (float)10 / parts.Length;
                        }
                        TaskContext.SetProgress((byte)currentPercent);
                    }

                    if (TaskContext.IsCancellationRequested)
                    {
                        TaskContext.IsCancelled = true;

                        return(tl);
                    }

                    // эти продукты имеют неопубликованные или расщепленные статьи
                    if (!skipPublishing && prodsToPublishIds.Length > 0)
                    {
                        var publishAction = ObjectFactoryBase.Resolve <PublishAction>();

                        var publishActionContext = new ActionContext
                        {
                            ContentItemIds = prodsToPublishIds,
                            Parameters     = new Dictionary <string, string>()
                            {
                                { ignoredStatusKey, ignoredStatus }
                            },
                            UserId   = context.UserId,
                            UserName = context.UserName
                        };

                        try
                        {
                            DoWithLogging(
                                () => publishAction.Process(publishActionContext),
                                "Calling PublishAction for products {ids}",
                                prodsToPublishIds
                                );
                        }
                        catch (ActionException ex)
                        {
                            var ids = ex.InnerExceptions.OfType <ProductException>().Select(x => x.ProductId);
                            Logger.Error()
                            .Exception(ex)
                            .Message("Exception has been thrown while publishing products {ids}", prodsToPublishIds)
                            .Write();

                            foreach (var pID in ids)
                            {
                                failed.TryAdd(pID, null);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Error()
                            .Exception(ex)
                            .Message("Exception has been thrown while publishing products {ids}", prodsToPublishIds)
                            .Write();
                        }
                    }

                    lock (percentLocker)
                    {
                        currentPercent += (float)10 / parts.Length;
                    }

                    TaskContext.SetProgress((byte)currentPercent);

                    return(tl);
                }
                catch (Exception ex)
                {
                    Logger.Error().Message("SendProductAction exception").Exception(ex).Write();

                    foreach (var item in idsToProcess)
                    {
                        failed.TryAdd(item, null);
                    }

                    errors.Add(ex);
                }

                HttpContextUserProvider.ForcedUserId = 0;
                return(tl);
            }, tt => { });

            if (TaskContext.IsCancellationRequested)
            {
                TaskContext.IsCancelled = true;

                return(ActionTaskResult.Error(new ActionTaskResultMessage()
                {
                    ResourceClass = nameof(SendProductActionStrings),
                    ResourceName = nameof(SendProductActionStrings.Cancelled)
                }, context.ContentItemIds));
            }

            var productsToRemove      = new int[0];
            var productsToRemoveCheck = missing
                                        .Concat(invisibleOrArchivedIds)
                                        .Except(excluded)
                                        .Except(frozen)
                                        .Except(validationErrors.Keys)
                                        .ToArray();

            if (productsToRemoveCheck.Length > 0)
            {
                // проверяем, какие из проблемных продуктов присутствуют на витрине
                var cmService = ObjectFactoryBase.Resolve <IList <IConsumerMonitoringService> >();

                productsToRemove = DoWithLogging(
                    () => cmService.SelectMany(s => s.FindExistingProducts(productsToRemoveCheck)).Distinct().ToArray(),
                    "Checking whether products {ids} are missing on fronts", productsToRemoveCheck
                    );

                if (productsToRemove.Length > 0)
                {
                    // эти продукты отсутствуют в DPC или не видны, но остались на витрине
                    // их надо удалить с витрин
                    var service        = ObjectFactoryBase.Resolve <IQPNotificationService>();
                    var productService = ObjectFactoryBase.Resolve <IProductService>();
                    DoWithLogging(
                        () => Task.WhenAll(productsToRemove.Section(20).Select(
                                               s => service.DeleteProductsAsync(
                                                   productService.GetSimpleProductsByIds(s.ToArray()),
                                                   context.UserName,
                                                   context.UserId,
                                                   false)
                                               )
                                           ).Wait(),
                        "Removing missing products from fronts {ids}", productsToRemove
                        );
                }
            }

            DoWithLogging(
                () => _validationService.UpdateValidationInfo(productIds, validationErrorsSerialized),
                "Updating validation info for products {ids}", productIds
                );

            int[] notFound = missing.Except(productsToRemove).Except(excluded).Except(frozen).Except(validationErrors.Keys).ToArray();

            var notSucceeded = failed.Keys.Concat(notFound).Concat(excluded).Concat(frozen)
                               .Concat(validationErrors.Keys).ToArray();

            var result = new ActionTaskResult()
            {
                FailedIds = notSucceeded
            };

            var msg = new ActionTaskResultMessage()
            {
                ResourceClass = nameof(SendProductActionStrings)
            };

            if (notSucceeded.Any())
            {
                msg.ResourceName = nameof(SendProductActionStrings.PartiallySucceededResult);
                msg.Extra        = string.Join(", ", notSucceeded);
                msg.Parameters   = new object[] { productIds.Length - notSucceeded.Length, productIds.Length };
            }

            else
            {
                msg.ResourceName = nameof(SendProductActionStrings.SucceededResult);
                msg.Parameters   = new object[] { productIds.Length };
            }

            result.Messages.Add(msg);

            if (errors.Any())
            {
                result.Messages.Add(new ActionTaskResultMessage()
                {
                    ResourceClass = nameof(SendProductActionStrings),
                    ResourceName  = nameof(SendProductActionStrings.Errors),
                    Extra         = string.Join(", ", errors.Select(x => x.Message).Distinct())
                });
            }

            AddMessages(result, nameof(SendProductActionStrings.ExcludedByStatus), excluded.ToArray());
            AddMessages(result, nameof(SendProductActionStrings.ExcludedWithFreezing), frozen.ToArray());
            AddMessages(result, nameof(SendProductActionStrings.NotFoundInDpc), notFound.ToArray());
            AddMessages(result, nameof(SendProductActionStrings.RemovedFromFronts), productsToRemove.ToArray());
            AddMessages(result, nameof(SendProductActionStrings.NotPassedByStageFiltration), filteredInStage.ToArray());
            AddMessages(result, nameof(SendProductActionStrings.NotPassedByLiveFiltration), filteredInLive.ToArray());

            if (validationErrors.Any())
            {
                result.Messages.AddRange(validationErrors.SelectMany(v => v.Value.Messages));
            }

            TaskContext.Result = result;

            return(result);
        }
        public override void Initialize(AnalysisContext analysisContext)
        {
            analysisContext.EnableConcurrentExecution();
            analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze);

            analysisContext.RegisterCompilationStartAction(startContext =>
            {
                var instantiatedTypes = new ConcurrentBag<INamedTypeSymbol>();
                var internalTypes = new ConcurrentBag<INamedTypeSymbol>();

                Compilation compilation = startContext.Compilation;

                // If the assembly being built by this compilation exposes its internals to
                // any other assembly, don't report any "uninstantiated internal class" errors.
                // If we were to report an error for an internal type that is not instantiated
                // by this assembly, and then it turned out that the friend assembly did
                // instantiate the type, that would be a false positive. We've decided it's
                // better to have false negatives (which would happen if the type were *not*
                // instantiated by any friend assembly, but we didn't report the issue) than
                // to have false positives.
                INamedTypeSymbol internalsVisibleToAttributeSymbol = compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.InternalsVisibleToAttribute");
                if (AssemblyExposesInternals(compilation, internalsVisibleToAttributeSymbol))
                {
                    return;
                }

                INamedTypeSymbol systemAttributeSymbol = compilation.GetTypeByMetadataName("System.Attribute");
                INamedTypeSymbol iConfigurationSectionHandlerSymbol = compilation.GetTypeByMetadataName("System.Configuration.IConfigurationSectionHandler");
                INamedTypeSymbol configurationSectionSymbol = compilation.GetTypeByMetadataName("System.Configuration.ConfigurationSection");
                INamedTypeSymbol safeHandleSymbol = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.SafeHandle");
                INamedTypeSymbol traceListenerSymbol = compilation.GetTypeByMetadataName("System.Diagnostics.TraceListener");
                INamedTypeSymbol mef1ExportAttributeSymbol = compilation.GetTypeByMetadataName("System.ComponentModel.Composition.ExportAttribute");
                INamedTypeSymbol mef2ExportAttributeSymbol = compilation.GetTypeByMetadataName("System.Composition.ExportAttribute");

                startContext.RegisterOperationAction(context =>
                {
                    IObjectCreationExpression expr = (IObjectCreationExpression)context.Operation;
                    var namedType = expr.Type as INamedTypeSymbol;
                    if (namedType != null)
                    {
                        instantiatedTypes.Add(namedType);
                    }
                }, OperationKind.ObjectCreationExpression);

                startContext.RegisterSymbolAction(context =>
                {
                    INamedTypeSymbol type = (INamedTypeSymbol)context.Symbol;
                    if (type.GetResultantVisibility() != SymbolVisibility.Public &&
                        !IsOkToBeUnused(type, compilation,
                            systemAttributeSymbol,
                            iConfigurationSectionHandlerSymbol,
                            configurationSectionSymbol,
                            safeHandleSymbol,
                            traceListenerSymbol,
                            mef1ExportAttributeSymbol,
                            mef2ExportAttributeSymbol))
                    {
                        internalTypes.Add(type);
                    }
                }, SymbolKind.NamedType);

                startContext.RegisterCompilationEndAction(context =>
                {
                    IEnumerable<INamedTypeSymbol> uninstantiatedInternalTypes = internalTypes
                        .Except(instantiatedTypes)
                        .Where(type => !HasInstantiatedNestedType(type, instantiatedTypes));

                    foreach (INamedTypeSymbol type in uninstantiatedInternalTypes)
                    {
                        context.ReportDiagnostic(type.CreateDiagnostic(Rule, type.FormatMemberName()));
                    }
                });
            });
        }
Ejemplo n.º 24
0
        internal static Result Send(int[] ids, int bundleSize)
        {
            var parts               = ids.Section(bundleSize);
            var failed              = new ConcurrentBag <int>();
            var missing             = new ConcurrentBag <int>();
            var invisibleOrArchived = new ConcurrentBag <int>();
            var errors              = new ConcurrentBag <Exception>();
            var productsToPublish   = new ConcurrentBag <Article>();

            var userProvider = ObjectFactoryBase.Resolve <IUserProvider>();

            int userId = userProvider.GetUserId();

            string userName = userProvider.GetUserName();

            HttpContextUserProvider.ForcedUserId = userId;

            Parallel.ForEach(parts, new ParallelOptions {
                MaxDegreeOfParallelism = 12
            },
                             () =>
            {
                HttpContextUserProvider.ForcedUserId = userId;
                return(new TLocal
                {
                    ProductService = ObjectFactoryBase.Resolve <IProductService>(),
                    QPNotificationService = ObjectFactoryBase.Resolve <IQPNotificationService>(),
                });
            },
                             (x, ps, tl) =>
            {
                try
                {
                    int n         = 0;
                    Article[] ps1 = null;
                    Article[] ps2 = null;


                    while (true)
                    {
                        // три попытки обработать продукт
                        try
                        {
                            var idsToPublish = new HashSet <int>();
                            var idsToRemove  = new HashSet <int>();
                            var prods        = tl.ProductService.GetProductsByIds(288, x.ToArray(), false);

                            // проверим, что продукты не надо публиковать или архивировать
                            foreach (var product in prods)
                            {
                                if (product.Archived || product.Visible == false)
                                {
                                    // архивные или невидимые продукты следует удалить с витрин
                                    idsToRemove.Add(product.Id);
                                    continue;
                                }

                                if (!product.IsPublished || PublishAction.GetAllArticlesToCheck(product).Where(p => !p.IsPublished).Any())
                                {
                                    // эти продукты имеют неопубликованные или расщепленные статьи
                                    productsToPublish.Add(product);
                                    idsToPublish.Add(product.Id);
                                }
                            }

                            foreach (var item in x.Except(prods.Select(y => y.Id)).Except(idsToPublish))
                            {
                                missing.Add(item);
                            }

                            // удалим требующие публикации или удаления продукты
                            prods = prods.Where(p => !idsToPublish.Contains(p.Id) || idsToRemove.Contains(p.Id)).ToArray();


                            // так как мы не отправляем продукты с неопубликовнными изменениями, то stage-версия продукта опубликована.
                            // используем один продукт для формирования двух xml
                            ps1 = ArticleFilter.LiveFilter.Filter(prods).ToArray();
                            ps2 = ArticleFilter.DefaultFilter.Filter(prods).ToArray();

                            foreach (var item in idsToRemove)
                            {
                                invisibleOrArchived.Add(item);
                            }

                            break;
                        }
                        catch (Exception ex)
                        {
                            //защита от временных сбойев sql
                            Thread.Sleep(50);
                            n++;
                            if (n >= 3)
                            {
                                errors.Add(ex);
                                foreach (var item in x)
                                {
                                    failed.Add(item);
                                }
                                return(tl);
                            }
                        }
                    }


                    var tasks =
                        ps1
                        .Section(Math.Min(bundleSize, 5))
                        .Select(z => tl.QPNotificationService.SendProductsAsync(z.ToArray(), false, userName, userId, false, false))
                        .Union
                            (ps2
                            .Section(Math.Min(bundleSize, 5))
                            .Select(z => tl.QPNotificationService.SendProductsAsync(z.ToArray(), true, userName, userId, false, false))
                            ).ToList();

                    Task.WhenAll(tasks).Wait();

                    return(tl);
                }
                catch (Exception ex)
                {
                    foreach (var item in x)
                    {
                        failed.Add(item);
                    }

                    errors.Add(ex);
                }

                HttpContextUserProvider.ForcedUserId = 0;

                return(tl);
            }, tt => { });

            var productsToRemove = missing
                                   .Union(invisibleOrArchived)
                                   .Distinct()
                                   .ToArray();

            if (productsToRemove.Length > 0)
            {
                // проверяем, какие из проблемных продуктов присутствуют на витрине
                productsToRemove = ObjectFactoryBase.Resolve <IConsumerMonitoringService>().FindExistingProducts(productsToRemove);

                if (productsToRemove.Length > 0)
                {
                    // эти продукты отсутствуют в DPC или не видны, но остались на витрине
                    // их надо удалить с витрин
                    var service = ObjectFactoryBase.Resolve <IQPNotificationService>();

                    Task.WhenAll(productsToRemove
                                 .Section(20).Select(s => service.DeleteProductsAsync(s.Select(id => new Article()
                    {
                        Id = id
                    }).ToArray(), userName, userId, false)))
                    .Wait();
                }
            }

            HttpContextUserProvider.ForcedUserId = 0;

            var products = productsToPublish.ToArray();

            return(new Result
            {
                Failed = failed.ToArray(),
                NotFound = missing.Except(productsToRemove).ToArray(),
                Removed = productsToRemove.Distinct().ToArray(),
                NeedPublishing = products,
                Errors = errors.ToArray()
            });
        }
        public override void Initialize(AnalysisContext analysisContext)
        {
            analysisContext.EnableConcurrentExecution();
            analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze);

            analysisContext.RegisterCompilationStartAction(startContext =>
            {
                var instantiatedTypes = new ConcurrentBag <INamedTypeSymbol>();
                var internalTypes     = new ConcurrentBag <INamedTypeSymbol>();

                Compilation compilation = startContext.Compilation;

                // If the assembly being built by this compilation exposes its internals to
                // any other assembly, don't report any "uninstantiated internal class" errors.
                // If we were to report an error for an internal type that is not instantiated
                // by this assembly, and then it turned out that the friend assembly did
                // instantiate the type, that would be a false positive. We've decided it's
                // better to have false negatives (which would happen if the type were *not*
                // instantiated by any friend assembly, but we didn't report the issue) than
                // to have false positives.
                INamedTypeSymbol internalsVisibleToAttributeSymbol = compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.InternalsVisibleToAttribute");
                if (AssemblyExposesInternals(compilation, internalsVisibleToAttributeSymbol))
                {
                    return;
                }

                INamedTypeSymbol systemAttributeSymbol = compilation.GetTypeByMetadataName("System.Attribute");
                INamedTypeSymbol iConfigurationSectionHandlerSymbol = compilation.GetTypeByMetadataName("System.Configuration.IConfigurationSectionHandler");
                INamedTypeSymbol configurationSectionSymbol         = compilation.GetTypeByMetadataName("System.Configuration.ConfigurationSection");
                INamedTypeSymbol safeHandleSymbol          = compilation.GetTypeByMetadataName("System.Runtime.InteropServices.SafeHandle");
                INamedTypeSymbol traceListenerSymbol       = compilation.GetTypeByMetadataName("System.Diagnostics.TraceListener");
                INamedTypeSymbol mef1ExportAttributeSymbol = compilation.GetTypeByMetadataName("System.ComponentModel.Composition.ExportAttribute");
                INamedTypeSymbol mef2ExportAttributeSymbol = compilation.GetTypeByMetadataName("System.Composition.ExportAttribute");

                startContext.RegisterOperationAction(context =>
                {
                    IObjectCreationExpression expr = (IObjectCreationExpression)context.Operation;
                    var namedType = expr.Type as INamedTypeSymbol;
                    if (namedType != null)
                    {
                        instantiatedTypes.Add(namedType);
                    }
                }, OperationKind.ObjectCreationExpression);

                startContext.RegisterSymbolAction(context =>
                {
                    INamedTypeSymbol type = (INamedTypeSymbol)context.Symbol;
                    if (type.GetResultantVisibility() != SymbolVisibility.Public &&
                        !IsOkToBeUnused(type, compilation,
                                        systemAttributeSymbol,
                                        iConfigurationSectionHandlerSymbol,
                                        configurationSectionSymbol,
                                        safeHandleSymbol,
                                        traceListenerSymbol,
                                        mef1ExportAttributeSymbol,
                                        mef2ExportAttributeSymbol))
                    {
                        internalTypes.Add(type);
                    }
                }, SymbolKind.NamedType);

                startContext.RegisterCompilationEndAction(context =>
                {
                    IEnumerable <INamedTypeSymbol> uninstantiatedInternalTypes = internalTypes
                                                                                 .Except(instantiatedTypes)
                                                                                 .Where(type => !HasInstantiatedNestedType(type, instantiatedTypes));

                    foreach (INamedTypeSymbol type in uninstantiatedInternalTypes)
                    {
                        context.ReportDiagnostic(type.CreateDiagnostic(Rule, type.FormatMemberName()));
                    }
                });
            });
        }
Ejemplo n.º 26
0
        public async Task <IEnumerable <AnalysisResult> > AnalyzeScopeAsync(IProgress <int> progress)
        {
            var           analysisResults     = new ConcurrentBag <AnalysisResult>();
            var           potentialDuplicates = new ConcurrentBag <AnalysisResult>();
            SyntaxTree    syntaxTree          = null;
            SemanticModel semanticModel       = null;
            SingleSyntaxTreeAnalysisContext analysisContext = null;

            var analyzeSyntaxTreeActions = AnalyzeSingleSyntaxTreeAndCollectResultsActions
                                           // We intentionally access the modified closure here (syntaxTree, semanticModel, analysisContext),
                                           // because we want to avoid creation of a huge number of temporary Action objects.
                                           // ReSharper disable AccessToModifiedClosure
                                           .Select(action => new Action(() => action(syntaxTree, semanticModel, analysisContext, analysisResults, potentialDuplicates)))
                                           // ReSharper restore AccessToModifiedClosure
                                           .ToArray();

            // Same here. We want to have just a single Action object created and called many times.
            // We intentionally do not want to use a local function here. Although its usage would be
            // semantically nicer and create exactly the same closure as the below Action, the fact that
            // we need to convert that local function to Action in the Task.Run() call means we would
            // end up in creating an additional Action object for every pass in the loop, and that's
            // exactly what we want to avoid.
            // ReSharper disable once ConvertToLocalFunction
            Action analyzeSyntaxTreeInParallel = () => Parallel.Invoke(analyzeSyntaxTreeActions);

            // WARNING: Keep the progress counter in sync with the logic behind the calculation of the maximum progress!
            int progressCounter = 0;

            foreach (var document in GetDocumentsToAnalyze())
            {
                analysisContext = new SingleSyntaxTreeAnalysisContext(document);

                syntaxTree = await document.GetSyntaxTreeAsync().ConfigureAwait(false);

                if (!syntaxTree.BeginsWithAutoGeneratedComment())
                {
                    semanticModel = await document.GetSemanticModelAsync();

                    // Each of the actions (analysis) will operate on the same (current) syntaxTree and semanticModel.
                    await Task.Run(analyzeSyntaxTreeInParallel);
                }

                progress.Report(++progressCounter);
            }

            // TODO-IG: Fully refactor Analysis/Scope/Analyzer/Context/Result etc.
            //          and remove this terrible temporary workaround.
            var duplicatesToRemove = FindDuplicatesToRemove();

            return(analysisResults.Except(duplicatesToRemove));

            IReadOnlyCollection <AnalysisResult> FindDuplicatesToRemove()
            {
                return(potentialDuplicates
                       // We consider the result to be a duplicate if have the same
                       // suggestion on the same node several times.
                       // The AnalysisResult does not contain node (at the moment,
                       // who knows what the upcoming refactoring will bring us ;-))
                       // so we will see if the file name and the position are the same.
                       .GroupBy(result => new { result.Suggestion, result.FilePath, result.Position })
                       .Where(group => group.Count() > 1)
                       // Just leave the first one so far and mark the rest as those to be removed.
                       // This is all a temporary workaround after all :-)
                       .SelectMany(group => group.Skip(1))
                       .ToList());
            }
        }
Ejemplo n.º 27
0
 public static void RemoveItem <T>(this ConcurrentBag <T> bag, T item)
 {
     bag = new ConcurrentBag <T>(bag.Except(new[] { item }));
 }
Ejemplo n.º 28
0
        /// <summary>
        /// Post process the types:
        ///  - If UserType has static members in more than one module, split it into multiple user types.
        ///  - Find parent type/namespace.
        /// </summary>
        /// <param name="userTypes">The list of user types.</param>
        /// <param name="symbolNamespaces">The symbol namespaces.</param>
        /// <returns>Newly generated user types.</returns>
        internal IEnumerable <UserType> ProcessTypes(IEnumerable <UserType> userTypes, Dictionary <Symbol, string> symbolNamespaces)
        {
            ConcurrentBag <UserType> newTypes = new ConcurrentBag <UserType>();

            // Split user types that have static members in more than one module
            Parallel.ForEach(Partitioner.Create(userTypes), (userType) =>
            {
                if (!userType.ExportStaticFields)
                {
                    return;
                }

                Symbol[] symbols = GlobalCache.GetSymbolStaticFieldsSymbols(userType.Symbol).ToArray();

                if (symbols.Length == 1)
                {
                    return;
                }

                bool foundSameNamespace = false;

                foreach (var symbol in symbols)
                {
                    string nameSpace = symbol.Module.Namespace;

                    if (userType.Namespace != nameSpace)
                    {
                        newTypes.Add(new UserType(symbol, null, nameSpace)
                        {
                            ExportDynamicFields = false
                        });
                    }
                    else
                    {
                        foundSameNamespace = true;
                    }
                }

                userType.ExportStaticFields = foundSameNamespace;
            });

            // Find parent type/namespace
            Dictionary <string, UserType> namespaceTypes = new Dictionary <string, UserType>();

            foreach (UserType userType in userTypes)
            {
                Symbol symbol = userType.Symbol;

                if (symbol.Tag != CodeTypeTag.Class && symbol.Tag != CodeTypeTag.Structure && symbol.Tag != CodeTypeTag.Union && symbol.Tag != CodeTypeTag.Enum)
                {
                    continue;
                }

                string        symbolName = symbol.Name;
                List <string> namespaces = symbol.Namespaces;

                if (namespaces.Count == 1)
                {
                    // Class is not defined in namespace nor in type.
                    continue;
                }

                StringBuilder currentNamespaceSB        = new StringBuilder();
                UserType      previousNamespaceUserType = null;

                for (int i = 0; i < namespaces.Count - 1; i++)
                {
                    if (i > 0)
                    {
                        currentNamespaceSB.Append("::");
                    }
                    currentNamespaceSB.Append(namespaces[i]);

                    string   currentNamespace = currentNamespaceSB.ToString();
                    UserType namespaceUserType;

                    if (!namespaceTypes.TryGetValue(currentNamespace, out namespaceUserType))
                    {
                        namespaceUserType = GlobalCache.GetUserType(currentNamespace, symbol.Module);
                    }

                    // Put type under exported template type (TODO: Remove this when template types start checking subtypes)
                    var templateType = namespaceUserType as TemplateUserType;

                    if (templateType != null)
                    {
                        namespaceUserType = templateType.TemplateType;
                    }
                    if (namespaceUserType == null)
                    {
                        namespaceUserType = new NamespaceUserType(new string[] { namespaces[i] }, previousNamespaceUserType == null ? symbolNamespaces[symbol] : null);
                        if (previousNamespaceUserType != null)
                        {
                            namespaceUserType.UpdateDeclaredInType(previousNamespaceUserType);
                        }
                        namespaceTypes.Add(currentNamespace, namespaceUserType);
                        newTypes.Add(namespaceUserType);
                    }

                    previousNamespaceUserType = namespaceUserType;
                }

                userType.UpdateDeclaredInType(previousNamespaceUserType);
            }

            // Update Class Name if it has duplicate with the namespace it is declared in
            foreach (UserType userType in newTypes.Concat(userTypes))
            {
                userType.ClassName = userType.OriginalClassName;
                if (userType.DeclaredInType != null && userType.OriginalClassName == userType.DeclaredInType.ClassName)
                {
                    userType.ClassName += "_";
                }

                TemplateUserType templateUserType = userType as TemplateUserType;

                if (templateUserType != null)
                {
                    foreach (UserType specializedUserType in templateUserType.SpecializedTypes)
                    {
                        specializedUserType.ClassName = userType.ClassName;
                    }
                }
            }

            // Remove duplicate types from exported template types (TODO: Remove this when template types start checking subtypes)
            foreach (UserType userType in userTypes)
            {
                TemplateUserType templateType = userType as TemplateUserType;

                if (templateType == null)
                {
                    continue;
                }

                HashSet <string> uniqueTypes = new HashSet <string>();

                foreach (var innerType in templateType.InnerTypes.ToArray())
                {
                    string className;

                    if (!(innerType is NamespaceUserType))
                    {
                        className = innerType.ClassName;
                    }
                    else
                    {
                        className = innerType.Namespace;
                    }
                    if (uniqueTypes.Contains(className))
                    {
                        templateType.InnerTypes.Remove(innerType);
                    }
                    else
                    {
                        uniqueTypes.Add(className);
                    }
                }
            }

            // Find all derived classes
            foreach (UserType userType in userTypes)
            {
                // We are doing this only for UDTs
                if (userType is EnumUserType || userType is GlobalsUserType || userType is NamespaceUserType)
                {
                    continue;
                }

                // For template user types, we want to remember all specializations
                TemplateUserType templateUserType = userType as TemplateUserType;

                if (templateUserType != null)
                {
                    foreach (UserType specializedUserType in templateUserType.SpecializedTypes)
                    {
                        AddDerivedClassToBaseClasses(specializedUserType);
                    }
                }
                else
                {
                    AddDerivedClassToBaseClasses(userType);
                }
            }

            // Merge namespaces when possible
            foreach (UserType userType in newTypes)
            {
                NamespaceUserType nameSpace = userType as NamespaceUserType;

                if (nameSpace == null)
                {
                    continue;
                }

                nameSpace.MergeIfPossible();
            }

            // Remove empty namespaces after merge
            List <UserType> removedUserTypes = new List <UserType>();

            foreach (UserType userType in newTypes)
            {
                NamespaceUserType nameSpace = userType as NamespaceUserType;

                if (nameSpace == null)
                {
                    continue;
                }

                if (nameSpace.InnerTypes.Count == 0)
                {
                    removedUserTypes.Add(nameSpace);
                }
            }

            return(newTypes.Except(removedUserTypes));
        }
        public void ShouldReturnEquivalentBagToNumberArray()
        {
            int[] numbers = new int[] { 1, 2, 3, 4, 3, 6, 4, 8, 17, 42, 6 };
            ConcurrentBag<int> expected = new ConcurrentBag<int>(numbers);
            ConcurrentBag<int> actual = new ConcurrentBag<int>();

            UniqueRandomNumberGenerator g = new UniqueRandomNumberGenerator(numbers);

            while (g.RemainingNumbersCount > 0)
            {
                int number = g.NewRandomNumber();
                actual.Add(number);
            }

            CollectionAssert.IsEmpty(
                actual.Except(expected), 
                "The bag of random numbers differs from the bag corresponding to the initial array.");
        }
        public void FillProduct()
        {
            if (isLoggingDebug)
            {
                Debug.WriteLine(string.Concat(debugPrefix, "MultiThreading:", isMultithreaded.ToString(), "Starting Creating ", numberOfProductToGenerate, " Random Products from data list..."));
            }
            if (isMultithreaded == false)
            {
                for (int num = 1; num < numberOfProductToGenerate + 1; num++)
                {
                    ProductCreation(num);
                }
            }
            else if (isMultithreaded)
            {
                #region multithreading
                var threadNum = new ProductNumThreadSafe();

                var enQueueTask = Task.Run(() =>
                {
                    var numTest = 0;
                    while (numTest < numberOfProductToGenerate)
                    {
                        _queueToExecute.Enqueue(() =>
                        {
                            var retValue = ProductCreation(threadNum.GetNextNumber());
                            return(retValue);
                        });

                        numTest++;
                    }
                });

                var deQueueTask = Task.Run(() =>
                {
                    while (_queueToExecute.Count > 0)
                    {
                        Func <bool> toProcess;
                        if (_executingTasks.Count < _concurrentExecution && _queueToExecute.TryDequeue(out toProcess))
                        {
                            var runTask = Task.Run(toProcess);
                            runTask.ContinueWith(x =>
                            {
                                if (x.IsFaulted)
                                {
                                    Debug.WriteLine(string.Format("ERROR Thread: {0}", x.Exception));
                                }
                            });
                            _executingTasks.Add(runTask);
                        }

                        var completedTask = _executingTasks.Where(t => t.Status == TaskStatus.RanToCompletion ||
                                                                  t.Status == TaskStatus.Faulted);
                        if (completedTask.Any())
                        {
                            _executingTasks = new ConcurrentBag <Task>(_executingTasks.Except(completedTask));
                        }
                    }
                });

                Task.WaitAll(new[] { enQueueTask, deQueueTask });

                #endregion
            }
        }
Ejemplo n.º 31
0
        /// <summary>
        /// Post process the types:
        ///  - If UserType has static members in more than one module, split it into multiple user types.
        ///  - Find parent type/namespace.
        /// </summary>
        /// <param name="userTypes">The list of user types.</param>
        /// <param name="symbolNamespaces">The symbol namespaces.</param>
        /// <returns>Newly generated user types.</returns>
        internal IEnumerable<UserType> ProcessTypes(IEnumerable<UserType> userTypes, Dictionary<Symbol, string> symbolNamespaces)
        {
            ConcurrentBag<UserType> newTypes = new ConcurrentBag<UserType>();

            // Split user types that have static members in more than one module
            Parallel.ForEach(Partitioner.Create(userTypes), (userType) =>
            {
                if (!userType.ExportStaticFields)
                    return;

                Symbol[] symbols = GlobalCache.GetSymbolStaticFieldsSymbols(userType.Symbol).ToArray();

                if (symbols.Length == 1)
                    return;

                bool foundSameNamespace = false;

                foreach (var symbol in symbols)
                {
                    string nameSpace = symbol.Module.Namespace;

                    if (userType.Namespace != nameSpace)
                        newTypes.Add(new UserType(symbol, null, nameSpace) { ExportDynamicFields = false });
                    else
                        foundSameNamespace = true;
                }

                userType.ExportStaticFields = foundSameNamespace;
            });

            // Find parent type/namespace
            Dictionary<string, UserType> namespaceTypes = new Dictionary<string, UserType>();

            foreach (UserType userType in userTypes)
            {
                Symbol symbol = userType.Symbol;

                if (symbol.Tag != SymTagEnum.SymTagUDT && symbol.Tag != SymTagEnum.SymTagEnum)
                    continue;

                string symbolName = symbol.Name;
                List<string> namespaces = symbol.Namespaces;

                if (namespaces.Count == 1)
                {
                    // Class is not defined in namespace nor in type.
                    continue;
                }

                StringBuilder currentNamespaceSB = new StringBuilder();
                UserType previousNamespaceUserType = null;

                for (int i = 0; i < namespaces.Count - 1; i++)
                {
                    if (i > 0)
                        currentNamespaceSB.Append("::");
                    currentNamespaceSB.Append(namespaces[i]);

                    string currentNamespace = currentNamespaceSB.ToString();
                    UserType namespaceUserType;

                    if (!namespaceTypes.TryGetValue(currentNamespace, out namespaceUserType))
                        namespaceUserType = GlobalCache.GetUserType(currentNamespace, symbol.Module);

                    // Put type under exported template type (TODO: Remove this when template types start checking subtypes)
                    var templateType = namespaceUserType as TemplateUserType;

                    if (templateType != null)
                        namespaceUserType = templateType.TemplateType;
                    if (namespaceUserType == null)
                    {
                        namespaceUserType = new NamespaceUserType(new string[] { namespaces[i] }, previousNamespaceUserType == null ? symbolNamespaces[symbol] : null);
                        if (previousNamespaceUserType != null)
                            namespaceUserType.UpdateDeclaredInType(previousNamespaceUserType);
                        namespaceTypes.Add(currentNamespace, namespaceUserType);
                        newTypes.Add(namespaceUserType);
                    }

                    previousNamespaceUserType = namespaceUserType;
                }

                userType.UpdateDeclaredInType(previousNamespaceUserType);
            }

            // Update Class Name if it has duplicate with the namespace it is declared in
            foreach (UserType userType in newTypes.Concat(userTypes))
            {
                userType.ClassName = userType.OriginalClassName;
                if (userType.DeclaredInType != null && userType.OriginalClassName == userType.DeclaredInType.ClassName)
                {
                    userType.ClassName += "_";
                }

                TemplateUserType templateUserType = userType as TemplateUserType;

                if (templateUserType != null)
                {
                    foreach (UserType specializedUserType in templateUserType.SpecializedTypes)
                    {
                        specializedUserType.ClassName = userType.ClassName;
                    }
                }
            }

            // Remove duplicate types from exported template types (TODO: Remove this when template types start checking subtypes)
            foreach (UserType userType in userTypes)
            {
                TemplateUserType templateType = userType as TemplateUserType;

                if (templateType == null)
                    continue;

                HashSet<string> uniqueTypes = new HashSet<string>();

                foreach (var innerType in templateType.InnerTypes.ToArray())
                {
                    string className;

                    if (!(innerType is NamespaceUserType))
                        className = innerType.ClassName;
                    else
                        className = innerType.Namespace;
                    if (uniqueTypes.Contains(className))
                        templateType.InnerTypes.Remove(innerType);
                    else
                        uniqueTypes.Add(className);
                }
            }

            // Find all derived classes
            foreach (UserType userType in userTypes)
            {
                // We are doing this only for UDTs
                if (userType is EnumUserType || userType is GlobalsUserType || userType is NamespaceUserType)
                    continue;

                // For template user types, we want to remember all specializations
                TemplateUserType templateUserType = userType as TemplateUserType;

                if (templateUserType != null)
                {
                    foreach (UserType specializedUserType in templateUserType.SpecializedTypes)
                    {
                        AddDerivedClassToBaseClasses(specializedUserType);
                    }
                }
                else
                {
                    AddDerivedClassToBaseClasses(userType);
                }
            }

            // Merge namespaces when possible
            foreach (UserType userType in newTypes)
            {
                NamespaceUserType nameSpace = userType as NamespaceUserType;

                if (nameSpace == null)
                {
                    continue;
                }

                nameSpace.MergeIfPossible();
            }

            // Remove empty namespaces after merge
            List<UserType> removedUserTypes = new List<UserType>();

            foreach (UserType userType in newTypes)
            {
                NamespaceUserType nameSpace = userType as NamespaceUserType;

                if (nameSpace == null)
                {
                    continue;
                }

                if (nameSpace.InnerTypes.Count == 0)
                {
                    removedUserTypes.Add(nameSpace);
                }
            }

            return newTypes.Except(removedUserTypes);
        }
Ejemplo n.º 32
0
 public static ConcurrentBag <T> Without <T>(this ConcurrentBag <T> bag, Func <T, bool> func)
 {
     return(new ConcurrentBag <T>(bag.Except(bag.Where(func))));
 }
Ejemplo n.º 33
0
        public void Remove(Func <object, ChoTypeConverterFormatSpec, CultureInfo, Type> fta)
        {
            if (fta == null)
            {
                return;
            }

            _fieldTypeAssessors = new ConcurrentBag <Func <object, ChoTypeConverterFormatSpec, CultureInfo, Type> >(_fieldTypeAssessors.Except(new[] { fta }));
        }