Example #1
0
        void OneThreadProducer(string url)
        {
            int threadName = Int32.Parse(Thread.CurrentThread.Name);

            if (!urlsForCheck.ContainsKey(url))
            {
                CrawlOneUrlProducer(url);
            }
            if (!dicitonaryOfCrawledUrls.IsEmpty)
            {
                if (threadName % 2 == 0)
                {
                    CrawlInternalUrls(dicitonaryOfCrawledUrls.ElementAt(0).Value);
                }
                CrawlExternalUrls(dicitonaryOfCrawledUrls.ElementAt(0).Value);
            }
            while (!dicitonaryOfCrawledUrls.IsEmpty)
            {
                if (cancelTicket)
                {
                    break;
                }
                if (dicitonaryOfCrawledUrls.Count <= threadName)
                {
                    continue;
                }
                var temp = dicitonaryOfCrawledUrls.ElementAtOrDefault(threadName);
                if (temp.Value != null)
                {
                    CrawlUrls(temp.Value);
                }
            }
            logger.Trace("Thread " + Thread.CurrentThread.Name + " finish");
        }
        private INetworkConnector GetNetworkConnector()
        {
            KeyValuePair <Guid, INetworkConnector> networkConnectorKeyPair;
            int networkConnectors = _networkConnectors.Count;

            switch (networkConnectors)
            {
            case 0:
                return(null);

            case 1:
                networkConnectorKeyPair = _networkConnectors.ElementAt(0);
                break;

            default:
            {
                int num = Random.Next(networkConnectors);
                networkConnectorKeyPair = _networkConnectors.ElementAt(num);
                break;
            }
            }

            if (networkConnectorKeyPair.Value.IsConnected)
            {
                return(networkConnectorKeyPair.Value);
            }
            RemoveService(networkConnectorKeyPair.Key);
            return(null);
        }
 private void FinalizeQuestionnaire()
 {
     _collab.UpdateCollabTimeout(_message.Author);
     _collab.DeactivateUser(_message.Author);
     for (int i = 0; i < _fields.Count; i++)
     {
         _fields[_fields.ElementAt(i).Key] = SanitizeMarkdown(_fields.ElementAt(i));
     }
 }
Example #4
0
        /// <inheritdoc />
        public ICounter CreateCounter(string name, string help, bool delayPublish, string[] labels)
        {
            var counters = new ICounter[_providers.Count];

            for (var index = 0; index < counters.Length; index++)
            {
                counters[index] = _providers.ElementAt(index).Value.CreateCounter(name, help, delayPublish, labels);
            }

            return(counters.Length == 1 ? counters[0] : new Counter(counters, name, help, labels));
        }
Example #5
0
 public void RollbackTransactions()
 {
     for (int i = 0; i < _PluginsSynchronizeContext.Values.Count; i++)
     {
         ITransactionScope transactionScope = _PluginsSynchronizeContext.ElementAt(i).Value as ITransactionScope;
         if (transactionScope != null)
         {
             transactionScope.RollbackTransaction();
         }
     }
 }
Example #6
0
        public override Task OnDisconnected(bool stopCalled)
        {
            string clientId = Context.ConnectionId;

            for (int i = ConnectedClients.Count - 1; i >= 0; i--)
            {
                if (ConnectedClients.ElementAt(i).Value.Equals(clientId))
                {
                    ConnectedClients.TryRemove(ConnectedClients.ElementAt(i).Key, out _);
                }
            }

            return(base.OnDisconnected(stopCalled));
        }
        private void StartStrategyExecution()
        {
            try
            {
                if (_asyncClassLogger.IsDebugEnabled)
                {
                    _asyncClassLogger.Debug("Starting strategy instance for optimization.", _type.FullName, "StratStrategyExecution");
                }

                if (_strategiesCollection.Count > 0)
                {
                    // Get the iteration to be executed;
                    var strategyExecutor = _strategiesCollection.ElementAt(0).Value;

                    // Execute strategy if its not already executing/executed
                    if (strategyExecutor.StrategyStatus.Equals(Infrastructure.Constants.StrategyStatus.None))
                    {
                        strategyExecutor.ExecuteStrategy();
                    }
                }
                // Execute each instance on a separate thread
                // Parallel.ForEach(_strategiesCollection.Values,
                //                strategyExecutor => Task.Factory.StartNew(strategyExecutor.ExecuteStrategy));
            }
            catch (Exception exception)
            {
                _asyncClassLogger.Error(exception, _type.FullName, "StratStrategyExecution");
            }
        }
Example #8
0
        public ModuleState(ConcurrentDictionary <Declaration, byte> declarations)
        {
            Declarations = declarations;
            UnresolvedMemberDeclarations = new ConcurrentDictionary <UnboundMemberDeclaration, byte>();
            TokenStream = null; UnboundMemberDeclaration
                ParseTree = null;

            if (declarations.Any() && declarations.ElementAt(0).Key.QualifiedName.QualifiedModuleName.Component != null)
            {
                State = ParserState.Pending;
            }
            else
            {
                State = ParserState.Pending;
            }

            ModuleContentHashCode = 0;
            Comments             = new List <CommentNode>();
            Annotations          = new List <IAnnotation>();
            ModuleException      = null;
            ModuleAttributes     = new Dictionary <Tuple <string, DeclarationType>, Attributes>();
            HasReferenceToModule = new ConcurrentDictionary <QualifiedModuleName, byte>();
            IsReferencedByModule = new HashSet <QualifiedModuleName>();

            IsNew = true;
        }
Example #9
0
        /// <summary>
        /// Strats executing individual strategy instances created for each iteration
        /// </summary>
        private void StartStrategyExecution()
        {
            try
            {
                if (_asyncClassLogger.IsDebugEnabled)
                {
                    _asyncClassLogger.Debug("Starting strategy instance for optimization.", _type.FullName, "StratStrategyExecution");
                }

                if (_strategiesCollection.Count > 0)
                {
                    // Get the iteration to be executed;
                    var strategyExecutor = _strategiesCollection.ElementAt(0).Value;

                    // Execute strategy if its not already executing/executed
                    if (strategyExecutor.StrategyStatus.Equals(StrategyStatus.None))
                    {
                        strategyExecutor.ExecuteStrategy();
                    }
                }
                else
                {
                    // Change Status to indicate on UI
                    _optimizationParameters.Status = OptimizationStatus.Completed;
                    EventSystem.Publish <UiElement>();
                }
                // Execute each instance on a separate thread
                // Parallel.ForEach(_strategiesCollection.Values,
                //                strategyExecutor => Task.Factory.StartNew(strategyExecutor.ExecuteStrategy));
            }
            catch (Exception exception)
            {
                _asyncClassLogger.Error(exception, _type.FullName, "StratStrategyExecution");
            }
        }
Example #10
0
File: Pipe.cs Project: icprog/Sewer
        public virtual bool Push(T element)
        {
            bool result = false;

            if (element != null)
            {
                if (Input != null && (Input != this))
                {
                    result = Input.Push(element);
                }
                else
                {
                    result = PushObject(element);
                }

                if (result)
                {
                    for (int i = 0; i < InputListeners.Count; i++)
                    {
                        InputListeners.ElementAt(i).Value.CallDelegate(this);
                    }
                }
            }
            return(result);
        }
 private void AssertNotGrouped(string[] testFiles, ConcurrentDictionary <string, List <string> > filesGroupedWithDuplicates)
 {
     _iOHelper.Verify(i => i.GetFilesInDirectory(TestDirectory), Times.Once);
     _iOHelper.Verify(i => i.ReadBytesForFile(testFiles[0]), Times.Exactly(2));
     Assert.Equal(2, filesGroupedWithDuplicates.Count);
     Assert.Single(filesGroupedWithDuplicates.First().Value);
     Assert.Single(filesGroupedWithDuplicates.ElementAt(1).Value);
 }
        private void CheckWordAndUpdateDictionary(int hssPosition)
        {
            string key       = "";
            bool   validated = false;

            try
            {
                try
                {
                    key = _cdTotalPermutations.ElementAt(hssPosition).Key.ToUpper();
                }
                catch (Exception e1)
                {
                    throw e1;
                }
                try
                {
                    validated = WordExist(key);
                }
                catch (Exception ex3)
                {
                    validated = false;
                    throw ex3;
                }

                try
                {
                    if (validated)
                    {
                        //_dTotalPermutations.Remove(key);
                        _lValidatedWords.Add(key);
                    }
                    //AddItemToDataTableWords(key);
                    else
                    {
                        _lPermutations.Add(key);
                    }
                    //AddItemToDataTablePermutation(key);
                }
                catch (Exception ex2)
                {
                    throw ex2;
                }
            }
            catch (Exception e0)
            {
                Exception newE;
                if (e0.InnerException != null)
                {
                    newE = new Exception("word: " + _WORD + " pos: " + hssPosition + " eMessage: " + e0.Message + " eIEx: " + e0.InnerException.Message + " eSource: " + e0.Source);
                }
                else
                {
                    newE = new Exception("word: " + _WORD + " pos: " + hssPosition + " eMessage: " + e0.Message + " eSource: " + e0.Source);
                }
                Logs.Log(_WORD, newE);
            }
        }
Example #13
0
        private async Task HeartbeatTask()
        {
            var random = new Random();

            while (_cancellationTokenSource.IsCancellationRequested == false)
            {
                await Task.Delay(_options.Heartbeat, _cancellationTokenSource.Token);

                if (_activeView.Count == 0 && _passiveView.Length == 0)
                {
                    continue;
                }
                Pipe pipe;
                var  next = random.Next(0, _activeView.Count + _passiveView.Length);
                if (next < _activeView.Count)
                {
                    var kvp = _activeView.ElementAt(next);
                    pipe = kvp.Value;
                    _log.Debug("Sending heartbeat to active {0}", kvp.Key);
                }
                else
                {
                    var connectionInfo = _passiveView.ElementAt(next);
                    _log.Debug("Sending heartbeat to passive {0}", connectionInfo);
                    pipe = new OutgoingPipe(connectionInfo, _cancellationTokenSource.Token, _options, _tcpListenerPiper);
                    _tempConnections.TryAdd(pipe, connectionInfo);
                }

                var priority = NeighborPriority.Low;
                if (_activeView.Count == _options.MaxActiveView)
                {
                    priority = NeighborPriority.Passive;
                }
                else if (_activeView.Count == 0)
                {
                    priority = NeighborPriority.High;
                }
                pipe.Write(new NeighborMessage
                {
                    Priority   = priority,
                    SenderInfo = _senderInfo
                });
            }
        }
Example #14
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            string connectionId = this.Context.ConnectionId;

            for (int x = 0; SignalRGridSessions.Count > x; x++)
            {
                KeyValuePair <string, SignalRGridSession> session = SignalRGridSessions.ElementAt(x);
                session.Value.GridConnections.Remove(connectionId);
            }
        }
Example #15
0
        public IEnumerable <IRemoteNode> GetRandomSetOfTrackedPeers(int upperLimit)
        {
            List <IRemoteNode> randomList = new List <IRemoteNode>();

            for (int i = 0; i < upperLimit; i++)
            {
                randomList.Add(_trackedPeers.ElementAt(_random.Next(0, _trackedPeers.Count)).Value);
            }

            return(randomList.Distinct());
        }
Example #16
0
        /// <summary>
        /// Prints the key values for all keys accessed in this transaction
        /// </summary>
        public void printKeyValues()
        {
            if (mKeyValues.Count == 0)
            {
                System.Console.Write("Empty ");
            }
            else
            {
                for (int i = 0; i < mKeyValues.Count; i++)
                {
                    System.Console.Write(mKeyValues.ElementAt(i).Key + ": ");

                    for (int j = 0; j < mKeyValues.ElementAt(i).Value.Count; i++)
                    {
                        System.Console.Write(mKeyValues.ElementAt(i).Value.ElementAt(j).Value + " ");
                    }
                    System.Console.WriteLine();
                }
            }
        }
Example #17
0
        public static string GetRandomProxyAddress()
        {
            var itemsToRemove = Proxies.Where(x => x.Value > 2);

            foreach (var item in itemsToRemove)
            {
                Proxies.TryRemove(item.Key, out int value);
            }
            int r = rnd.Next(Proxies.Count);

            return(Proxies.ElementAt(r).Key);
        }
        private static async Task CloseAllSocketsAsync()
        {
            // We can't dispose the sockets until the processing loops are terminated,
            // but terminating the loops will abort the sockets, preventing graceful closing.
            var disposeQueue = new List <WebSocket>(Clients.Count);

            while (Clients.Count > 0)
            {
                var client = Clients.ElementAt(0).Value;
                Console.WriteLine($"Closing Socket {client.SocketId}");

                Console.WriteLine("... ending broadcast loop");
                client.BroadcastLoopTokenSource.Cancel();

                if (client.Socket.State != WebSocketState.Open)
                {
                    Console.WriteLine($"... socket not open, state = {client.Socket.State}");
                }
                else
                {
                    var timeout = new CancellationTokenSource(Program.CLOSE_SOCKET_TIMEOUT_MS);
                    try
                    {
                        Console.WriteLine("... starting close handshake");
                        await client.Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", timeout.Token);
                    }
                    catch (OperationCanceledException ex)
                    {
                        Program.ReportException(ex);
                        // normal upon task/token cancellation, disregard
                    }
                }

                if (Clients.TryRemove(client.SocketId, out _))
                {
                    // only safe to Dispose once, so only add it if this loop can't process it again
                    disposeQueue.Add(client.Socket);
                }

                Console.WriteLine("... done");
            }

            // now that they're all closed, terminate the blocking ReceiveAsync calls in the SocketProcessingLoop threads
            SocketLoopTokenSource.Cancel();

            // dispose all resources
            foreach (var socket in disposeQueue)
            {
                socket.Dispose();
            }
        }
        private static async Task CloseAllSocketsAsync()
        {
            //Não podemos descartar os soquetes até que os ciclos de processamento sejam finalizados,
                        //mas encerrar os loops abortará os soquetes, impedindo o fechamento gracioso.
            var disposeQueue = new List <WebSocket>(Clients.Count);

            while (Clients.Count > 0)
            {
                var client = Clients.ElementAt(0).Value;
                Console.WriteLine($"Socket fechado{client.SocketId}");

                Console.WriteLine("... final do loop do servidor");
                client.LoopTokenSource.Cancel();

                if (client.Socket.State != WebSocketState.Open)
                {
                    Console.WriteLine($"... socket não aberto, Estado = {client.Socket.State}");
                }
                else
                {
                    var timeout = new CancellationTokenSource(Program.CLOSE_SOCKET_TIMEOUT_MS);
                    try
                    {
                        Console.WriteLine("... starting close handshake");
                        await client.Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Fechado", timeout.Token);
                    }
                    catch (OperationCanceledException ex)
                    {
                        Program.ReportException(ex);
                        // normal mediante cancelamento de tarefa / token, desconsidera
                    }
                }

                if (Clients.TryRemove(client.SocketId, out _))
                {
                    // só é seguro Dispor uma vez, portanto, adicione-o apenas se esse loop não puder processá-lo novamente
                    disposeQueue.Add(client.Socket);
                }

                Console.WriteLine("... done");
            }

            // agora que todos estão fechados, encerre as chamadas ReceiveAsync de bloqueio nos threads SocketProcessingLoop
            SocketLoopTokenSource.Cancel();

            // dispor de todos os recursos
            foreach (var socket in disposeQueue)
            {
                socket.Dispose();
            }
        }
        private IWorker GetPollWorker()
        {
            int curIndex = GetNextWorkerIndex();

            if (curIndex < 0)
            {
                Logger.Log.Info(true, "GetPollWorker:为空");
                return(null);
            }
            else
            {
                return(_workList.ElementAt(curIndex).Value);
            }
        }
Example #21
0
        private void _ApplyAllowDeny()
        {
            _ClearCell();
            lock (m_Channels)
            {
                for (int i = m_Channels.Count - 1; i >= 0; i--)
                {
                    string[] items = m_Channels.ElementAt(i).Key.Split('.');
                    if (Allow.Count > 0)
                    {
                        if (!Allow.ContainsKey(items[0]) || (Allow[items[0]].Count > 0 && !Allow[items[0]].Contains(items[1])))
                        {
                            ChannelInfo ci;
                            m_Channels.TryRemove(m_Channels.ElementAt(i).Key, out ci);
                            ci.Dispose();
                            continue;
                        }
                    }

                    if (Deny.ContainsKey(items[0]))
                    {
                        if (Deny[items[0]].Contains(items[1]))
                        {
                            ChannelInfo ci;
                            m_Channels.TryRemove(m_Channels.ElementAt(i).Key, out ci); ci.Dispose();
                        }
                    }
                }
            }
            _HeaderCell();
            foreach (var channel in m_Channels.Keys)
            {
                _ContentCell(channel);
            }
            grid1.AutoSizeCells();
            _Sort();
        }
Example #22
0
        public static void Purge()
        {
            lock (lockThis)
            {
                WriteLog("Purge...");
                for (int i = ServeursDns.Count - 1; i >= 0; i--)
                {
                    var item = ServeursDns.ElementAt(i);
                    ServeursDns[item.Key].OrderBy(server => server.Fails);
                    if (ServeursDns[item.Key].Sum(server => server.Queries) < 1)
                    {
                        continue;
                    }

                    List <Serveur> list = ServeursDns[item.Key];

                    for (int j = list.Count - 1; j >= 0; j--)
                    {
                        // 20% de fails et au moins 100 queries.
                        if (list[j].Queries > 100 && list[j].Fails * 100 / list[j].Queries > 20)
                        {
                            var count = list.Count();
                            if (count > 1)
                            {
                                WriteLog("Le serveur " + list[j].Ns + " , " + list[j].Ip + " est purgé..");
                                list.RemoveAt(j);
                            }
                            else
                            {
                                // il ne reste qu'un seul serveur, même s'il est pourri on fait avec..
                                WriteLog("Le serveur " + list[j].Ns + " , " + list[j].Ip + " seul candidat restant.");
                            }
                        }
                    }
                }
            }
        }
Example #23
0
        public static IDictionary <TKey, TResult> MapReduce <TKey, TValue, TResult>(this DataTable table,
                                                                                    Func <MapReduceData, KeyValueClass <TKey, TValue> > map, Func <TKey, IList <TValue>, TResult> reduce)
        {
            object locker = new object();
            ConcurrentDictionary <TKey, TResult> result = new ConcurrentDictionary <TKey, TResult>();
            //保存map出来的结果
            ConcurrentDictionary <TKey, IList <TValue> > mapDic = new ConcurrentDictionary <TKey, IList <TValue> >();
            var parallelOptions = new ParallelOptions();

            parallelOptions.MaxDegreeOfParallelism = Environment.ProcessorCount;
            //并行map
            Parallel.For(0, table.Rows.Count, parallelOptions, t =>
            {
                MapReduceData data = new MapReduceData
                {
                    Row   = table.Rows[t],
                    Index = t,
                    Table = table,
                };
                var pair = map(data);
                if (pair != null && pair.Valid)
                {
                    //锁住防止并发操作list造成数据缺失
                    lock (locker)
                    {
                        //将匹配出来的结果加入结果集放入字典
                        IList <TValue> list = null;
                        if (mapDic.ContainsKey(pair.Key))
                        {
                            list = mapDic[pair.Key];
                        }
                        else
                        {
                            list             = new List <TValue>();
                            mapDic[pair.Key] = list;
                        }
                        list.Add(pair.Value);
                    }
                }
            });

            //并行reduce
            Parallel.For(0, mapDic.Keys.Count, parallelOptions, t =>
            {
                KeyValuePair <TKey, IList <TValue> > pair = mapDic.ElementAt(t);
                result[pair.Key] = reduce(pair.Key, pair.Value);
            });
            return(result);
        }
Example #24
0
        public void FailoverFileSaveTest()
        {
            FailoverReactor.DISK_FILE_WRITER_DUETIME = 0;
            FailoverReactor.DIR_NOT_FOUND_DUETIME    = 0;
            string path     = AppDomain.CurrentDomain.BaseDirectory;
            var    failover = new FailoverReactor(_hostReactor, path);

            Thread.Sleep(100);

            var infos = DiskCache.GetServiceInfos(Path.Combine(path, FailoverReactor.FAILOVER_PATH));

            Assert.False(failover.IsFailoverSwitch());
            Assert.NotNull(infos);
            Assert.Equal(2, infos.Count);

            ServiceInfo infoOne = infos.First().Value;
            ServiceInfo infoTwo;

            if (infos.First().Key == _serviceMap.First().Key)
            {
                infoTwo = _serviceMap.First().Value;
            }
            else
            {
                infoTwo = _serviceMap.ElementAt(1).Value;
            }

            Assert.Equal(infoOne.AllIPs, infoTwo.AllIPs);
            Assert.Equal(infoOne.CacheMillis, infoTwo.CacheMillis);
            Assert.Equal(infoOne.CheckSum, infoTwo.CheckSum);
            Assert.Equal(infoOne.Clusters, infoTwo.Clusters);
            Assert.Equal(infoOne.GroupName, infoTwo.GroupName);
            Assert.Equal(infoOne.Hosts.Count, infoTwo.Hosts.Count);
            Assert.Equal(infoOne.Hosts[0].ClusterName, infoTwo.Hosts[0].ClusterName);
            Assert.Equal(infoOne.Hosts[0].Enable, infoTwo.Hosts[0].Enable);
            Assert.Equal(infoOne.Hosts[0].Ephemeral, infoTwo.Hosts[0].Ephemeral);
            Assert.Equal(infoOne.Hosts[0].Healthy, infoTwo.Hosts[0].Healthy);
            Assert.Equal(infoOne.Hosts[0].InstanceId, infoTwo.Hosts[0].InstanceId);
            Assert.Equal(infoOne.Hosts[0].Ip, infoTwo.Hosts[0].Ip);
            Assert.Equal(infoOne.Hosts[0].Metadata.Count, infoTwo.Hosts[0].Metadata.Count);
            Assert.Equal(infoOne.Hosts[0].Port, infoTwo.Hosts[0].Port);
            Assert.Equal(infoOne.Hosts[0].ServiceName, infoTwo.Hosts[0].ServiceName);
            Assert.Equal(infoOne.Hosts[0].Weight, infoTwo.Hosts[0].Weight);
            Assert.Equal(infoOne.LastRefTime, infoTwo.LastRefTime);
            Assert.Equal(infoOne.Name, infoTwo.Name);

            Directory.Delete(Path.Combine(path, FailoverReactor.FAILOVER_PATH), true);
        }
Example #25
0
        private async Task CloseAllSocketsAsync()
        {
            var disposeQueue = new List <System.Net.WebSockets.WebSocket>(Clients.Count);

            while (Clients.Count > 0)
            {
                var client = Clients.ElementAt(0).Value;
                _logger.LogInformation($"Closing Socket {client.SocketId}");

                _logger.LogInformation("... ending broadcast loop");
                client.TaskCompletion.SetCanceled();

                if (client.Socket.State != WebSocketState.Open)
                {
                    _logger.LogInformation($"... socket not open, state = {client.Socket.State}");
                }
                else
                {
                    var timeout = new CancellationTokenSource(2500);
                    try
                    {
                        _logger.LogInformation("... starting close handshake");
                        await client.Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", timeout.Token);
                    }
                    catch (OperationCanceledException ex)
                    {
                        //Program.ReportException(ex);
                        // normal upon task/token cancellation, disregard
                    }
                }

                if (Clients.TryRemove(client.SocketId, out _))
                {
                    // only safe to Dispose once, so only add it if this loop can't process it again
                    disposeQueue.Add(client.Socket);
                }
            }

            // now that they're all closed, terminate the blocking ReceiveAsync calls in the SocketProcessingLoop threads
            SocketLoopTokenSource.Cancel();

            // dispose all resources
            foreach (var socket in disposeQueue)
            {
                socket.Dispose();
            }
        }
Example #26
0
        /// <summary>
        /// this method sends its own flow to all of its destinations
        /// according to the current flow that is reaching to destinations
        /// </summary>
        private void _SendMyOwnFlow()
        {
            int   destID = 0;
            float Currdemand;

            this._DestNum = this.MyDestinationsAndDemands.Count;
            try
            {
                for (int j = 0; j < this._DestNum; j++)
                {
                    destID = MyDestinationsAndDemands.ElementAt(j).Key;

                    Currdemand = MyDestinationsAndCurrentDemands.ElementAt(j).Value;
                    AdHocFlow myFlow = new AdHocFlow();
                    myFlow.FlowCameFrom  = this.ID;
                    myFlow.OriginID      = this.ID;
                    myFlow.DestinationID = destID;
                    myFlow.OriginalFlow  = Currdemand;
                    myFlow.CurrFlow      = Currdemand;
                    myFlow.FlowCameFrom  = this.ID;
                    string key = this.ID + ":" + destID;
                    if (Targets[ForwardingTable[destID]])
                    {
                        if (NodeList.Nodes[ForwardingTable[destID]].InFlow[this.ID].ContainsKey(key))
                        {
                            NodeList.Nodes[ForwardingTable[destID]].InFlow[this.ID][key] = myFlow;
                        }
                        else
                        {
                            NodeList.Nodes[ForwardingTable[destID]].InFlow[this.ID].GetOrAdd(key, myFlow);
                        }
                    }
                    if (TargetsAndMyFlowSent[ForwardingTable[destID]].ContainsKey(destID))
                    {
                        TargetsAndMyFlowSent[ForwardingTable[destID]][destID] = Currdemand;
                    }
                    else
                    {
                        TargetsAndMyFlowSent[ForwardingTable[destID]].GetOrAdd(destID, Currdemand);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Node: " + this.ID + "was trying to send its own  flow to: " + ForwardingTable[destID] + "\n" + ex.ToString());
            }
        }
Example #27
0
        public void AddJob(JobInfo jobInfo)
        {
            if (JobExists(jobInfo))
            {
                return;
            }

            _jobResults.TryAdd(jobInfo.JobID, new BuildResult(jobInfo));
            _jobs.Enqueue(jobInfo);

            if (_jobResults.Count > MAX_ACTIVE_JOB_RESULTS)
            {
                _jobResults.TryRemove(_jobResults.ElementAt(_jobResults.Count - 1).Key, out BuildResult removedJob);
            }

            Broadcast(new JobsAnalysisCommand(_jobResults.Values));
        }
Example #28
0
        public static IEnumerable <KeyValuePair <TKey, TValue> > RandomValues <TKey, TValue>(this ConcurrentDictionary <TKey, TValue> dict, int take)
        {
            int size = dict.Count;

            if (size > take)
            {
                Random rand = new Random();
                List <KeyValuePair <TKey, TValue> > values = new List <KeyValuePair <TKey, TValue> >();
                for (int i = 0; i < take; i++)
                {
                    values.Add(dict.ElementAt(rand.Next(size)));
                    //values.Add(aa);
                }
                return(values);
            }
            return(dict.ToList());
        }
    public SignalStatisticsItem DequeueItem()
    {
        if (_DicSignalStatisticsItems.Count == 0)
        {
            return(null);
        }

        if (_dicIndex >= _DicSignalStatisticsItems.Count)
        {
            _dicIndex = 0;
        }

        SignalStatisticsItem re = _DicSignalStatisticsItems.ElementAt(_dicIndex).Value;

        _dicIndex++;

        return(re);
    }
Example #30
0
        private static async Task CloseAllSocketsAsync()
        {
            var disposeQueue = new List <WebSocket>(Clients.Count);

            while (Clients.Count > 0)
            {
                var client = Clients.ElementAt(0).Value;
                Console.WriteLine($"Closing Socket {client.SocketId}");

                Console.WriteLine("... ending broadcast loop");

                if (client.Socket.State != WebSocketState.Open)
                {
                    Console.WriteLine($"... socket not open, state = {client.Socket.State}");
                }
                else
                {
                    var timeout = new CancellationTokenSource(2500);
                    try
                    {
                        Console.WriteLine("... starting close handshake");
                        await client.Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", timeout.Token);
                    }
                    catch (OperationCanceledException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }

                if (Clients.TryRemove(client.SocketId, out _))
                {
                    disposeQueue.Add(client.Socket);
                }

                Console.WriteLine("... done");
            }

            SocketLoopTokenSource.Cancel();

            foreach (var socket in disposeQueue)
            {
                socket.Dispose();
            }
        }
Example #31
0
      public void Parallel_PutGetDelete_Random()
      {
        const int PUTTER_CNT  = 2, PUTTER_OP_CNT  = 2 * 10000;
        const int GETTER_CNT  = 6, GETTER_OP_CNT  = 2 * 30000;
        const int DELETER_CNT = 2, DELETER_OP_CNT = 2 * 10000;

        var data = new ConcurrentDictionary<PilePointer, string>();

        var getAccessViolations = new ConcurrentDictionary<int, int>();
        var deleteAccessViolations = new ConcurrentDictionary<int, int>();

        using (var pile = new DefaultPile())
        {
          pile.Start();

          var ipile = pile as IPile;

          // putter tasks
          var putters = new Task[PUTTER_CNT];
          for (int it = 0; it < PUTTER_CNT; it++)
          {
            var task = new Task(() => {

              for (int i = 0; i < PUTTER_OP_CNT; i++)
              {
                var str = NFX.Parsing.NaturalTextGenerator.Generate();
                var pp = ipile.Put(str);
                data.TryAdd(pp, str);
              }

            });

            putters[it] = task;
          }

          // getter tasks
          var getters = new Task[GETTER_CNT];
          for (int it = 0; it < GETTER_CNT; it++)
          {
            var task = new Task(() => {

              for (int i = 0; i < GETTER_OP_CNT; i++)
              {
                if (data.Count == 0) {
                  System.Threading.Thread.Yield();
                  continue;
                }
                var idx = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, data.Count-1);
                var kvp = data.ElementAt(idx);
                try
                {
                  
                  var str = ipile.Get(kvp.Key);
                  Assert.AreEqual(str, kvp.Value);
                }
                catch (PileAccessViolationException)
                {
                  getAccessViolations.AddOrUpdate(System.Threading.Thread.CurrentThread.ManagedThreadId, 1, (mid, val) => val + 1);
                }
              }
            });
            getters[it] = task;
          }

          // deleter tasks
          var deleters = new Task[DELETER_CNT];
          for (int it = 0; it < DELETER_CNT; it++)
          {
            var task = new Task(() => {

              for (int i = 0; i < DELETER_OP_CNT; i++)
              {
                if (data.Count == 0) {
                  System.Threading.Thread.Yield();
                  continue;
                }
                var idx = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, data.Count-1);
                var kvp = data.ElementAt(idx);
                try
                {
                  ipile.Delete(kvp.Key);
                }
                catch (PileAccessViolationException)
                {
                  deleteAccessViolations.AddOrUpdate(System.Threading.Thread.CurrentThread.ManagedThreadId, 1, (mid, val) => val + 1);
                }
              }
            });
            deleters[it] = task;
          }


          foreach (var task in putters) task.Start();
          foreach (var task in getters) task.Start();
          foreach (var task in deleters) task.Start();


          Task.WaitAll(putters.Concat(getters).Concat(deleters).ToArray());

          foreach (var kvp in getAccessViolations)
            Console.WriteLine("Get thread '{0}' {1:n0} times accessed deleted pointer", kvp.Key, kvp.Value);

          foreach (var kvp in deleteAccessViolations)
            Console.WriteLine("Del thread '{0}' {1:n0} times accessed deleted pointer", kvp.Key, kvp.Value);
        }
      }