Dequeue() public method

public Dequeue ( ) : T,
return T,
コード例 #1
0
        public void AddImpressionWithFullQueue()
        {
            //Arrange
            var queue      = new BlockingQueue <KeyImpression>(1);
            var cache      = new InMemorySimpleCache <KeyImpression>(queue);
            var impression = new KeyImpression {
                feature = "test", changeNumber = 100, keyName = "date", label = "testdate", time = 10000000
            };
            var impression2 = new KeyImpression {
                feature = "test2", changeNumber = 100, keyName = "date", label = "testdate", time = 10000000
            };

            //Act
            cache.AddItems(new List <KeyImpression> {
                impression
            });
            cache.AddItems(new List <KeyImpression> {
                impression2
            });

            var element  = queue.Dequeue();
            var element2 = queue.Dequeue();

            //Assert
            Assert.IsNotNull(element);
            Assert.IsNull(element2);
        }
コード例 #2
0
        public void LogSuccessfully()
        {
            //Act
            var impressions = new List <KeyImpression>
            {
                new KeyImpression {
                    keyName = "GetTreatment", feature = "test", treatment = "on", time = 7000, changeNumber = 1, label = "test"
                }
            };

            _impressionsLog.Log(impressions);

            //Assert
            KeyImpression element = null;

            while (element == null)
            {
                element = _queue.Dequeue();
            }
            Assert.IsNotNull(element);
            Assert.AreEqual("GetTreatment", element.keyName);
            Assert.AreEqual("test", element.feature);
            Assert.AreEqual("on", element.treatment);
            Assert.AreEqual(7000, element.time);
        }
コード例 #3
0
        public void LogSuccessfullyWithNoValue()
        {
            //Act
            var eventToLog = new Event {
                key = "Key1", eventTypeId = "testEventType", trafficTypeName = "testTrafficType", timestamp = 7000
            };

            _eventLog.Log(new WrappedEvent
            {
                Event = eventToLog,
                Size  = 1024
            });

            //Assert
            WrappedEvent element = null;

            while (element == null)
            {
                element = _queue.Dequeue();
            }
            Assert.IsNotNull(element);
            Assert.AreEqual("Key1", element.Event.key);
            Assert.AreEqual("testEventType", element.Event.eventTypeId);
            Assert.AreEqual("testTrafficType", element.Event.trafficTypeName);
            Assert.AreEqual(7000, element.Event.timestamp);
            Assert.IsNull(element.Event.value);
        }
コード例 #4
0
 public Message Receive(int timeout)
 {
     if (!EnableReceiveQueue)
     {
         throw new Exception("Receive queue not enabled");
     }
     return(ReceiveQueue.Dequeue(timeout));
 }
コード例 #5
0
ファイル: Scheduler.cs プロジェクト: pcbing/brunet
        protected void Run()
        {
            while (!_finished)
            {
                //
                // Find out the next closest event in the sorted list.
                //
                BrunetTask task = null;
                bool       fire = false;
                if (_task_queue.First == null)
                {
                    task = (BrunetTask)_in_queue.Dequeue();
                }
                else
                {
                    _now_ticks = DateTime.UtcNow.Ticks;
                    LinkedListNode <BrunetTask> next_node = _task_queue.First;
                    if (next_node.Value.Instant > _now_ticks)
                    {
                        int milliseconds = (int)((next_node.Value.Instant - _now_ticks) / 10000.0);
                        if (milliseconds < 0)
                        {
                            Environment.Exit(1);
                        }
                        //Console.WriteLine("millis:{0}", milliseconds);
                        task = (BrunetTask)_in_queue.Dequeue(milliseconds,
                                                             out fire);
                    }
                    else
                    {
                        fire = true;
                    }
                }

                if (fire)
                {
                    //
                    // Time to fire the next event in the queue.
                    //

                    task = _task_queue.First.Value;
                    _task_queue.RemoveFirst();
                    //Console.WriteLine("Firing event");
                    task.Fire();
                }
                else
                {
                    //
                    // Add the new task to the sorted list.
                    //
                    if (task != null)
                    {
                        AddTask(task);
                    }
                }
            }
        }
コード例 #6
0
        public void Stop()
        {
            m_running = false;

            Thread.Sleep(1000); // let the world move

            foreach (Thread t in m_workerThreads)
            {
                Watchdog.AbortThread(t.ManagedThreadId);
            }

            // any entry in m_bycontext should have a active request on the other queues
            // so just delete contents to easy GC
            foreach (Queue <PollServiceHttpRequest> qu in m_bycontext.Values)
            {
                qu.Clear();
            }
            m_bycontext.Clear();

            try
            {
                foreach (PollServiceHttpRequest req in m_retryRequests)
                {
                    req.DoHTTPstop(m_server);
                }
            }
            catch
            {
            }

            PollServiceHttpRequest wreq;

            m_retryRequests.Clear();

            lock (m_slowRequests)
            {
                while (m_slowRequests.Count > 0)
                {
                    m_requests.Enqueue(m_slowRequests.Dequeue());
                }
            }

            while (m_requests.Count() > 0)
            {
                try
                {
                    wreq = m_requests.Dequeue(0);
                    wreq.DoHTTPstop(m_server);
                }
                catch
                {
                }
            }

            m_requests.Clear();
        }
コード例 #7
0
ファイル: BlockingQueueTests.cs プロジェクト: nataren/DReAM
 public void Dequeue_on_closed_queue_throws()
 {
     BlockingQueue<string> q = new BlockingQueue<string>();
     q.Enqueue("foo");
     Assert.IsFalse(q.IsClosed);
     q.Close();
     Assert.IsTrue(q.IsClosed);
     string x = q.Dequeue();
     Assert.AreEqual("foo", x);
     x = q.Dequeue();
 }
コード例 #8
0
 public void BlockingQueue()
 {
     for (int i = 0; i < itemCount; i++)
     {
         blockingQueue.Enqueue(i);
     }
     for (int i = 0; i < itemCount; i++)
     {
         blockingQueue.Dequeue();
     }
 }
コード例 #9
0
        public void EnqueueTest()
        {
            BlockingQueue <int> blockingQueue = new BlockingQueue <int>();

            blockingQueue.Enqueue(1);

            var d = blockingQueue.Dequeue();

            d = blockingQueue.Dequeue();

            int i = 0;
        }
コード例 #10
0
ファイル: LLEventQueue.cs プロジェクト: jhurliman/simian
        public void SendEvents(int batchWaitMS)
        {
            if (Response == null)
            {
                return;
            }

            LLEventQueueEvent eventQueueEvent = null;

            if (EventQueue.Dequeue(batchWaitMS, ref eventQueueEvent))
            {
                // An event was dequeued
                List <LLEventQueueEvent> eventsToSend = null;

                if (eventQueueEvent != null)
                {
                    eventsToSend = new List <LLEventQueueEvent>();
                    eventsToSend.Add(eventQueueEvent);

                    int start         = Util.TickCount();
                    int batchMsPassed = 0;

                    // Wait batchWaitMS milliseconds looking for more events,
                    // or until the size of the current batch equals MAX_EVENTS_PER_RESPONSE
                    while (batchMsPassed < batchWaitMS && eventsToSend.Count < MAX_EVENTS_PER_RESPONSE)
                    {
                        if (EventQueue.Dequeue(batchWaitMS - batchMsPassed, ref eventQueueEvent) && eventQueueEvent != null)
                        {
                            eventsToSend.Add(eventQueueEvent);
                        }

                        batchMsPassed = Util.TickCount() - start;
                    }
                }

                // Make sure we can actually send the events right now
                if (Response != null && !Response.Sent)
                {
                    SendResponse(eventsToSend);
                }
                else
                {
                    m_log.Info("Connection is closed, requeuing events and closing the handler thread");
                    if (eventsToSend != null)
                    {
                        for (int i = 0; i < eventsToSend.Count; i++)
                        {
                            EventQueue.Enqueue(eventsToSend[i]);
                        }
                    }
                }
            }
        }
コード例 #11
0
        public void Dequeue_on_closed_queue_throws()
        {
            BlockingQueue <string> q = new BlockingQueue <string>();

            q.Enqueue("foo");
            Assert.IsFalse(q.IsClosed);
            q.Close();
            Assert.IsTrue(q.IsClosed);
            string x = q.Dequeue();

            Assert.AreEqual("foo", x);
            x = q.Dequeue();
        }
コード例 #12
0
ファイル: BlockingQueue_Tests.cs プロジェクト: zz110/MQTTnet
        public void Preserve_Order()
        {
            var queue = new BlockingQueue <string>();

            queue.Enqueue("a");
            queue.Enqueue("b");
            queue.Enqueue("c");

            Assert.AreEqual(3, queue.Count);

            Assert.AreEqual("a", queue.Dequeue());
            Assert.AreEqual("b", queue.Dequeue());
            Assert.AreEqual("c", queue.Dequeue());
        }
コード例 #13
0
ファイル: BlockQueueTests.cs プロジェクト: jeason0813/iTuner
        public void Queue()
        {
            using (var queue = new BlockingQueue <string>())
            {
                using (var worker = new BackgroundWorker())
                {
                    worker.DoWork                    += new DoWorkEventHandler(DoWork);
                    worker.ProgressChanged           += new ProgressChangedEventHandler(DoProgressChanged);
                    worker.WorkerReportsProgress      = true;
                    worker.WorkerSupportsCancellation = true;
                    worker.RunWorkerAsync(queue);

                    int count = 0;
                    while (count < QueueSize)
                    {
                        string s = queue.Dequeue();
                        Console.WriteLine("dequeued " + count + "(" + s + ")");
                        count++;
                    }

                    worker.CancelAsync();
                }

                queue.Clear();

                // priority

                queue.Enqueue("one");
                queue.Enqueue("two");
                queue.Enqueue("thr");

                queue.Enqueue("one1", 1);
                queue.Enqueue("one0", 0);
                queue.Enqueue("two1", 1);
                queue.Enqueue("two0");
                queue.Enqueue("thr1", 1);
                queue.Enqueue("thr0");


                Assert.AreEqual("one", queue.Dequeue());
                Assert.AreEqual("two", queue.Dequeue());
                Assert.AreEqual("thr", queue.Dequeue());
                Assert.AreEqual("one0", queue.Dequeue());
                Assert.AreEqual("two0", queue.Dequeue());
                Assert.AreEqual("thr0", queue.Dequeue());
                Assert.AreEqual("one1", queue.Dequeue());
                Assert.AreEqual("two1", queue.Dequeue());
                Assert.AreEqual("thr1", queue.Dequeue());
            }
        }
コード例 #14
0
        // ReSharper disable once InconsistentNaming
        public void ShouldReturnMessagesInFIFOOrder()
        {
            BlockingQueue <ThreadMessage> x = new BlockingQueue <ThreadMessage>();

            ThreadMessage msg1 = new ThreadMessage(1);
            ThreadMessage msg2 = new ThreadMessage(2);
            ThreadMessage msg3 = new ThreadMessage(3);

            x.Enqueue(msg1);
            x.Enqueue(msg2);
            x.Enqueue(msg3);
            Assert.AreEqual(x.Dequeue().Cmd, msg1.Cmd);
            Assert.AreEqual(x.Dequeue().Cmd, msg2.Cmd);
            Assert.AreEqual(x.Dequeue().Cmd, msg3.Cmd);
        }
コード例 #15
0
ファイル: WorkThread.cs プロジェクト: zuojiashun/CsharpCode
 public void InternalRun()
 {
     while (_isStart)
     {
         CmdTask task = _queue.Dequeue(1);
         if (task == null)
         {
             continue;
         }
         var systemIdx = task.TaskInfo.SystemIdx;
         try
         {
             SingletonManager.Get <DurationHelp>().ProfileStart(_systemsProfiles[systemIdx]);
             var sytstem = _systems[systemIdx];
             sytstem.ExecuteUserCmd(task.Owner, task.UserCmd);
             _logger.DebugFormat("{0} Execute :{1} seq:{2} player:{3}", Name, systemIdx, task.UserCmd.Seq,
                                 task.Owner.OwnerEntityKey.EntityId);
         }
         catch (Exception e)
         {
             _logger.ErrorFormat("Execute :{0} Exception:{1}", _systems[systemIdx], e);
         }
         finally
         {
             SingletonManager.Get <DurationHelp>().ProfileEnd(_systemsProfiles[systemIdx]);
             _disparcher.TaskEnd(task);
         }
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="ShardedMongoServerProxy"/> class.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="instances">The instances.</param>
        /// <param name="connectionQueue">The state change queue.</param>
        /// <param name="connectionAttempt">The connection attempt.</param>
        /// <remarks>This constructor is used when the instances have already been instructed to connect.</remarks>
        protected MultipleInstanceMongoServerProxy(MongoServer server, IEnumerable<MongoServerInstance> instances, BlockingQueue<MongoServerInstance> connectionQueue, int connectionAttempt)
        {
            _state = MongoServerState.Connecting;
            _server = server;
            _connectedInstances = new ConnectedInstanceCollection();
            _connectionAttempt = connectionAttempt;

            _outstandingInstanceConnections = connectionQueue.Count;
            ThreadPool.QueueUserWorkItem(_ =>
            {
                while (connectionQueue.Count > 0)
                {
                    var instance = connectionQueue.Dequeue();
                    Interlocked.Decrement(ref _outstandingInstanceConnections);
                }
            });

            // It's important to have our own copy of this list because it might get modified during iteration. 
            _instances = instances.ToList();
            foreach (var instance in instances)
            {
                instance.StateChanged += InstanceStateChanged;
                ProcessInstanceStateChange(instance);
            }
        }
コード例 #17
0
ファイル: NameManager.cs プロジェクト: CasperTech/radegast
        public void RequestThread()
        {
            PendingLookups.Open();

            while (true)
            {
                List <UUID> req = null;
                if (!PendingLookups.Dequeue(Timeout.Infinite, ref req))
                {
                    break;
                }
                lookupGate.WaitOne(90 * 1000);
                client.Avatars.GetDisplayNames(req, (bool success, AgentDisplayName[] names, UUID[] badIDs) =>
                {
                    if (success)
                    {
                        ProcessDisplayNames(names);
                    }
                    else
                    {
                        Logger.Log("Failed fetching display names", Helpers.LogLevel.Warning, client);
                    }
                    lookupGate.Release(1);
                });
            }
        }
コード例 #18
0
ファイル: BlockingQueue_Tests.cs プロジェクト: zz110/MQTTnet
        public async Task Wait_For_Item()
        {
            var queue = new BlockingQueue <string>();

            string item = null;

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            Task.Run(() =>
            {
                item = queue.Dequeue();
            });
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            await Task.Delay(100);

            Assert.AreEqual(0, queue.Count);
            Assert.AreEqual(null, item);

            queue.Enqueue("x");

            await Task.Delay(100);

            Assert.AreEqual("x", item);
            Assert.AreEqual(0, queue.Count);
        }
コード例 #19
0
        public void Run()
        {
            while (m_running)
            {
                PollServiceHttpRequest req = m_request.Dequeue();
                try
                {
                    if (req.PollServiceArgs.Valid())
                    {
                        if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
                        {
                            StreamReader str;
                            try
                            {
                                str = new StreamReader(req.Request.Body);
                            }
                            catch (ArgumentException)
                            {
                                // Stream was not readable means a child agent
                                // was closed due to logout, leaving the
                                // Event Queue request orphaned.
                                continue;
                            }

                            Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id,
                                                                                   str.ReadToEnd());
                            var request = new OSHttpRequest(req.HttpContext, req.Request);
                            m_server.MessageHandler.SendGenericHTTPResponse(
                                responsedata,
                                request.MakeResponse(System.Net.HttpStatusCode.OK, "OK"),
                                request
                                );
                        }
                        else
                        {
                            if ((Environment.TickCount - req.RequestTime) > m_timeout)
                            {
                                var request = new OSHttpRequest(req.HttpContext, req.Request);
                                m_server.MessageHandler.SendGenericHTTPResponse(
                                    req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id),
                                    request.MakeResponse(System.Net.HttpStatusCode.OK, "OK"),
                                    request);
                            }
                            else
                            {
                                ReQueuePollServiceItem reQueueItem = ReQueue;
                                if (reQueueItem != null)
                                {
                                    reQueueItem(req);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    MainConsole.Instance.ErrorFormat("Exception in poll service thread: " + e);
                }
            }
        }
コード例 #20
0
 protected override void Run()
 {
     Thread.CurrentThread.Name     = Name;
     Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
     while (Running)
     {
         try
         {
             Job task = _queue.Dequeue(1);
             if (task == null)
             {
                 continue;
             }
             try
             {
                 _profile.BeginProfileOnlyEnableProfile();
                 _action(task);
                 // _profile2.BeginProfileOnlyEnableProfile();
             }
             finally
             {
                 _queue.DelRef();
                 _profile.EndProfileOnlyEnableProfile();
                 // _profile2.EndProfileOnlyEnableProfile();
             }
             _logger.Debug("end");
         }
         catch (Exception e)
         {
             _logger.ErrorFormat("Run Error:{0}", e);
         }
     }
 }
コード例 #21
0
        async void TheadLoop()
        {
            while (!Cts.IsCancellationRequested)
            {
                var queueItem = tasks.Dequeue();
                if (queueItem != null)
                {
                    try
                    {
                        var result = await queueItem.Target(queueItem.Input);

                        if (queueItem.Callback != null)
                        {
                            queueItem.Callback(queueItem.Input, result, null);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (queueItem.Callback != null)
                        {
                            queueItem.Callback(queueItem.Input, default(TResult), ex);
                        }
                    }
                }
            }
        }
コード例 #22
0
        /// <summary>
        /// 执行发送任务,发送的是localSendQueue中的数据
        /// </summary>
        public void iNetClientTaskSend()
        {
            try
            {
                while (remoteIsRunning)  //控制任务运行的信号
                {
                    //localWaitHandler.WaitOne();  //阻塞当前线程,直到WaitHandler收到信号

                    if (!localSendQueue.IsEmpty())              //遥测接收数据队列中存在待处理的数据
                    {
                        byte[] data = localSendQueue.Dequeue(); //取出遥测接收数据队列中的数据
                        if (data != null)                       //使用BlockingQueue可以不用判定,即此部可省略
                        {
                            //remoteSendMsg(data);
                            //if (!iClientTCP_frm.SendMsg(data))  //发送失败,将数据重新放入发送队列中
                            //    localSendQueue.Enqueue(data);
                        }
                    }
                    else
                    {
                        //iClientTCP_frm.SendMsg(iClientTCP_frm.testbuf3);
                    }

                    Thread.Sleep(1000);  //休息10ms
                }
            }
            catch
            {
                //iClientTCP_frm.ShowMsg("数据发送失败:" + ex.Message);
            }
        }
コード例 #23
0
 public WBHelper(bool ajax)
 {
     IsForAjaxUse = ajax;
     if (AppSetting.UserSetting.Get("不限制浏览器数量", false))
     {
         if (wbQueue.Count == 0)
         {
             container.InvokeAction(() =>
             {
                 this.WB = new ExtendedWinFormsWebBrowser();
                 container.Controls.Add(this.WB);
                 this.WB.ScriptErrorsSuppressed = true;
                 SubscribEvents();
                 if (ajax)
                 {
                     this.WB.Navigate(wbInitUrl);
                     Task.Delay(50);
                 }
             });
             return;
         }
     }
     this.WB = ajax ? ajaxWbQueue.Dequeue() : wbQueue.Dequeue();
     SubscribEvents();
 }
コード例 #24
0
        private IEnumerator <object> SendMessagesTask()
        {
            IFuture f;
            string  text = null;

            while (true)
            {
                f = _OutboundText.Dequeue();
                yield return(f);

                text = f.Result as string;

                while (true)
                {
                    try {
                        f = Output.Write(text);
                        break;
                    } catch (SocketBufferFullException) {
                    }
                    yield return(new Yield());
                }
                yield return(f);

                if (f.CheckForFailure(typeof(SocketDisconnectedException)))
                {
                    Dispose();
                    yield break;
                }
            }
        }
コード例 #25
0
        public void EnqueueBeforeDequeueTest()
        {
            var queue = new BlockingQueue<object>();
            var isEnqueued = new ManualResetEvent(false);
            var isDequeued = new ManualResetEvent(false);

            object value = null;

            ThreadPool.QueueUserWorkItem(_ =>
            {
                queue.Enqueue(new object());
                isEnqueued.Set();
            });
            ThreadPool.QueueUserWorkItem(_ =>
            {
                isEnqueued.WaitOne();
                value = queue.Dequeue();
                isDequeued.Set();
            });

            if (!isDequeued.WaitOne(10))
                Assert.Fail("Dequeue after Enqueue failed: Event hasn't been raised");

            if(value == null)
                Assert.Fail("Dequeue after Enqueue failed: Wrong value returned");
        }
コード例 #26
0
 public WBHelper(bool ajax)
 {
     IsForAjaxUse = ajax;
     if (AppSetting.UserSetting.Get("不限制浏览器数量", false))
     {
         if (wbQueue.Count == 0)
         {
             container.InvokeAction(async() =>
             {
                 this.WB = new ExtendedWinFormsWebBrowser();
                 container.Controls.Add(this.WB);
                 this.WB.ScriptErrorsSuppressed = true;
                 SubscribEvents();
                 if (ajax)
                 {
                     await SynchronousLoadDocument(wbInitUrl);
                 }
                 IsNew = true;
             });
             return;
         }
     }
     this.WB = ajax ? ajaxWbQueue.Dequeue() : wbQueue.Dequeue();
     SubscribEvents();
 }
コード例 #27
0
        public void LogSuccessfully()
        {
            //Arrange
            var queue            = new BlockingQueue <KeyImpression>(10);
            var impressionsCache = new InMemoryImpressionsCache(queue);
            var treatmentLog     = new SelfUpdatingTreatmentLog(null, 1, impressionsCache, 10);

            //Act
            var impression = new KeyImpression()
            {
                keyName = "GetTreatment", feature = "test", treatment = "on", time = 7000, changeNumber = 1, label = "test"
            };

            treatmentLog.Log(impression);

            //Assert
            KeyImpression element = null;

            while (element == null)
            {
                element = queue.Dequeue();
            }
            Assert.IsNotNull(element);
            Assert.AreEqual("GetTreatment", element.keyName);
            Assert.AreEqual("test", element.feature);
            Assert.AreEqual("on", element.treatment);
            Assert.AreEqual(7000, element.time);
        }
コード例 #28
0
 private void Consumer()
 {
     while (true)
     {
         Task task = m_Queue.Dequeue();
     }
 }
コード例 #29
0
 protected override void Run()
 {
     Thread.CurrentThread.Name     = Name;
     Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
     while (Running)
     {
         try
         {
             Job task = _queue.Dequeue(1);
             if (task == null)
             {
                 continue;
             }
             _logger.Debug("start");
             try
             {
                 SingletonManager.Get <DurationHelp>().ProfileStart(_profile);
                 _action(task);
                 SingletonManager.Get <DurationHelp>().ProfileStart(_profile2);
                 Interlocked.Decrement(ref _count);
             }
             finally
             {
                 SingletonManager.Get <DurationHelp>().ProfileEnd(_profile);
                 SingletonManager.Get <DurationHelp>().ProfileEnd(_profile2);
             }
             _logger.Debug("end");
         }
         catch (Exception e)
         {
             _logger.ErrorFormat("Run Error:{0}", e);
         }
     }
 }
コード例 #30
0
        public void LogSuccessfullyUsingBucketingKey()
        {
            //Arrange
            var queue            = new BlockingQueue <KeyImpression>(10);
            var impressionsCache = new InMemoryImpressionsCache(queue);
            var treatmentLog     = new SelfUpdatingTreatmentLog(null, 1, impressionsCache, 10);

            //Act
            Key key        = new Key(bucketingKey: "a", matchingKey: "testkey");
            var impression = new KeyImpression()
            {
                keyName = key.matchingKey, feature = "test", treatment = "on", time = 7000, changeNumber = 1, label = "test-label", bucketingKey = key.bucketingKey
            };

            treatmentLog.Log(impression);

            //Assert
            KeyImpression element = null;

            while (element == null)
            {
                element = queue.Dequeue();
            }
            Assert.IsNotNull(element);
            Assert.AreEqual("testkey", element.keyName);
            Assert.AreEqual("a", element.bucketingKey);
            Assert.AreEqual("test", element.feature);
            Assert.AreEqual("on", element.treatment);
            Assert.AreEqual(7000, element.time);
        }
コード例 #31
0
        public void TestEnqueueDequeueMany()
        {
            using (var queue = new BlockingQueue <int>(10))
            {
                const int count  = 10000;
                var       source = Enumerable.Range(0, count).ToArray();
                var       dest   = new List <int>(count);

                var writer = Task.Factory.StartNew(() =>
                {
                    foreach (var value in source)
                    {
                        queue.Enqueue(value);
                    }
                });
                var reader = Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < count; ++i)
                    {
                        dest.Add(queue.Dequeue());
                    }
                });

                writer.Wait(500).Should().BeTrue();
                reader.Wait(500).Should().BeTrue();

                dest.Should().Equal(source);
            }
        }
コード例 #32
0
        public void TestManyWritersOneReader()
        {
            using (var queue = new BlockingQueue <int>(10))
            {
                const int numWriters = 10;
                const int numValues  = 10000;

                var source  = Enumerable.Range(0, numValues).ToArray();
                var dest    = new List <int>(numWriters * numValues);
                var writers = Enumerable.Range(0, numWriters).Select(i => Task.Factory.StartNew(() =>
                {
                    foreach (var value in source)
                    {
                        queue.Enqueue(value);
                    }
                }, TaskCreationOptions.LongRunning)).ToArray();
                var reader = Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < numWriters * numValues; ++i)
                    {
                        dest.Add(queue.Dequeue());
                    }
                }, TaskCreationOptions.LongRunning);

                Task.WaitAll(writers, TimeSpan.FromSeconds(5)).Should().BeTrue();
                reader.Wait(500).Should().BeTrue();

                dest.Count.Should().Be(numWriters * numValues);

                foreach (var value in source)
                {
                    dest.Count(x => x == value).Should().Be(numWriters);
                }
            }
        }
コード例 #33
0
        public void FetchAllAndClearSuccessfully()
        {
            //Arrange
            var queue = new BlockingQueue <KeyImpression>(2);
            var cache = new InMemorySimpleCache <KeyImpression>(queue);

            var impression = new KeyImpression {
                feature = "test", changeNumber = 100, keyName = "date", label = "testdate", time = 10000000
            };
            var impression2 = new KeyImpression {
                feature = "test2", changeNumber = 100, keyName = "date", label = "testdate", time = 10000000
            };

            cache.AddItems(new List <KeyImpression> {
                impression
            });
            cache.AddItems(new List <KeyImpression> {
                impression2
            });

            //Act
            var result  = cache.FetchAllAndClear();
            var element = queue.Dequeue();

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.IsNull(element);
        }
コード例 #34
0
        public void CreateAndUseBlockingQueue()
        {
            BlockingQueue<int> queue = new BlockingQueue<int>(1);

            Thread thread = new Thread(new ThreadStart(delegate() { queue.Enqueue(1); }));
            thread.Start();

            int element = queue.Dequeue();
            Assert.AreEqual(1, element);
        }
コード例 #35
0
        public void CreateAndUseBlockingQueueTenTimes()
        {
            BlockingQueue<int> queue = new BlockingQueue<int>(5);

            Thread thread = new Thread(new ThreadStart(delegate() { for (int k=1; k<=10; k++) queue.Enqueue(k); }));
            thread.Start();

            for (int j = 1; j <= 10; j++)
            {
                int element = queue.Dequeue();
                Assert.AreEqual(element, j);
            }
        }
コード例 #36
0
ファイル: RpcDNSClient.cs プロジェクト: kyungyonglee/ipop
 /// <summary>
 /// This mehod allows the client to add a buddy to the network which 
 /// is used for DNS, ip translation
 /// </summary>
 /// <param name="name">A string DNS name used to register the address</param>
 /// <param name="address">A string brunet address</param>
 /// <returns>A string IP address for the added name</returns>
 public string AddBuddy(string name, string address)
 {
     BlockingQueue q = new BlockingQueue();
     _brpc.Rpc.Invoke(_brpc.IPHandler.CreateUnicastSender(_remEP), q, "RpcIpopNode.RegisterMapping", name, address);
     try {
       RpcResult res = (RpcResult)q.Dequeue();
       Console.WriteLine(_remEP + ":" + ((UnicastSender)res.ResultSender).EndPoint);
       Console.WriteLine(res.Result);
       return (string)res.Result;
     }
     catch (InvalidOperationException e) {
       Console.WriteLine(e.Message);
     }
     catch (Exception e) {
       Console.WriteLine(e.Message);
     }
     return null;
 }
コード例 #37
0
        public void Test()
        {
            var q = new BlockingQueue<int>(4);

            // Producer
            new Thread(() =>
            {
                for (var x = 0;; x++)
                {
                    if (!q.Enqueue(x))
                        break;
                    Trace.WriteLine(x.ToString("0000") + " >");
                }
                Trace.WriteLine("Producer quitting");
            }).Start();

            // Consumers
            for (var i = 0; i < 2; i++)
            {
                new Thread(() =>
                {
                    for (;;)
                    {
                        Thread.Sleep(100);
                        int x;
                        if (!q.Dequeue(out x))
                            break;
                        Trace.WriteLine("     < " + x.ToString("0000"));
                    }
                    Trace.WriteLine("Consumer quitting");
                }).Start();
            }

            Thread.Sleep(2000);

            Trace.WriteLine("Quitting");

            q.Quit();
        }
コード例 #38
0
ファイル: BlockQueueTests.cs プロジェクト: pengyancai/cs-util
        public void Queue()
        {
            BlockingQueue<string> queue = new BlockingQueue<string>();

            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(DoWork);
            worker.ProgressChanged += new ProgressChangedEventHandler(DoProgressChanged);
            worker.WorkerReportsProgress = true;
            worker.RunWorkerAsync(queue);

            int count = 0;
            while (count < 5)
            {
                string s = queue.Dequeue();
                Console.WriteLine("dequeued " + count + "(" + s + ")");
                count++;
            }

            queue.Clear();
            queue.Dispose();
            queue = null;
        }
コード例 #39
0
ファイル: Program.cs プロジェクト: pazjacket/malys-_-NDexer
        public static IEnumerator<object> ScanFiles()
        {
            var time_start = DateTime.UtcNow.Ticks;

            var completion = new Future<object>();
            var batchQueue = new BlockingQueue<IEnumerable<string>>();
            var changedFiles = new List<string>();
            var deletedFiles = new List<string>();

            for (int i = 0; i < System.Environment.ProcessorCount; i++)
                Scheduler.Start(
                    CommitBatches(batchQueue, completion),
                    TaskExecutionPolicy.RunAsBackgroundTask
                );

            using (new ActiveWorker("Scanning folders for changes")) {
                var changeSet = new BlockingQueue<TagDatabase.Change>();

                var changeGenerator = Scheduler.Start(
                     Database.UpdateFileListAndGetChangeSet(changeSet),
                     TaskExecutionPolicy.RunAsBackgroundTask
                );
                changeGenerator.RegisterOnComplete((f) => changeSet.Enqueue(new TagDatabase.Change()));

                int numChanges = 0;
                int numDeletes = 0;

                while (!changeGenerator.Completed || (changeSet.Count > 0)) {
                    var f = changeSet.Dequeue();
                    yield return f;
                    var change = f.Result;

                    if (change.Filename == null)
                        continue;

                    if (change.Deleted) {
                        deletedFiles.Add(change.Filename);
                        numDeletes += 1;
                    } else {
                        yield return Database.GetSourceFileID(change.Filename);
                        changedFiles.Add(change.Filename);
                        numChanges += 1;
                    }

                    if (deletedFiles.Count >= BatchSize) {
                        var transaction = Database.Connection.CreateTransaction();
                        yield return transaction;

                        foreach (string filename in deletedFiles)
                            yield return Database.DeleteSourceFile(filename);

                        deletedFiles.Clear();
                        yield return transaction.Commit();
                    }

                    if (changedFiles.Count >= BatchSize) {
                        string[] batch = changedFiles.ToArray();
                        changedFiles.Clear();

                        batchQueue.Enqueue(batch);
                    }
                }

                if (deletedFiles.Count > 0) {
                    var transaction = Database.Connection.CreateTransaction();
                    yield return transaction;

                    foreach (string filename in deletedFiles)
                        yield return Database.DeleteSourceFile(filename);

                    deletedFiles.Clear();
                    yield return transaction.Commit();
                }

                if (changedFiles.Count > 0) {
                    string[] batch = changedFiles.ToArray();
                    batchQueue.Enqueue(batch);
                }

                completion.Complete();

                while (batchQueue.Count < 0)
                    batchQueue.Enqueue(null);

                var time_end = DateTime.UtcNow.Ticks;
                var elapsed = TimeSpan.FromTicks(time_end - time_start).TotalSeconds;

                System.Diagnostics.Debug.WriteLine(String.Format("Disk scan complete after {2:00000.00} seconds. {0} change(s), {1} delete(s).", numChanges, numDeletes, elapsed));
            }
        }
コード例 #40
0
        /**
         * Simple program to find out if any Grid Appliances are running on the
         * local system!
         */
        public static void Main(String[] args)
        {
            ISender sender = null;
              BrunetRpc brpc = new BrunetRpc();
              if(args.Length == 0) {
            // Step one:  Gather all non-VMware IP Addresses for the block list
            ArrayList local_ips = new ArrayList();
            IPAddresses ipaddrs = IPAddresses.GetIPAddresses();
            foreach(Hashtable ht in ipaddrs.AllInterfaces) {
              if(ht["inet addr"] == null) {
            continue;
              }
              string ifname = (string) ht["interface"];
              if(ifname.StartsWith("vmnet") || ifname.StartsWith("VMware")) {
            local_ips.Add(IPAddress.Parse((String) ht["inet addr"]));
              }
            }
            IPAddress[] lips = (IPAddress[]) local_ips.ToArray(typeof(IPAddress));
            sender = brpc.IPHandler.CreateMulticastSender(lips);
              }
              else {
            sender = brpc.IPHandler.CreateMulticastSender();
              }

              // Step two: Setup BrunetRpc using Multicast to find nodes
              BlockingQueue q = new BlockingQueue();
              brpc.Rpc.Invoke(sender, q, "Information.Info");
              Hashtable retrieved = new Hashtable();
              while(true) {
            RpcResult res = null;
            try {
              // Step three: Get result and print it, need something better for autmoated service
              bool timedout = false;
              res = (RpcResult) q.Dequeue(2000, out timedout);
              if(timedout) {
            break;
              }
              Hashtable ht = (Hashtable) res.Result;
              Hashtable neighbors = (Hashtable) ht["neighbors"];
              string self = (string) neighbors["self"];
              if(retrieved.Contains(self)) {
            continue;
              }
              retrieved[self] = true;

              ArrayList vips = ht["VirtualIPs"] as ArrayList;

              string vips_list = "";
              if(vips != null) {
            foreach(string ip in vips) {
              vips_list += ip + ", ";
            }
            vips_list = vips_list.Substring(0, vips_list.Length - 2);
              }

              ArrayList ips = (ArrayList) ht["localips"];
              string iplist = "";
              foreach(string ip in ips) {
            iplist += ip + ", ";
              }
              iplist = iplist.Substring(0, iplist.Length - 2);

              Console.WriteLine(vips_list + ": " + iplist);
            }
            //This occurs when all attempts are done, unless you close the queue first
            catch(InvalidOperationException) {
              break;
            }
            catch(Exception e) {}
              }
              //BrunetRpc has a timer thread, it needs to explicitly be closed
              brpc.Close();
        }
コード例 #41
0
ファイル: RpcDNSClient.cs プロジェクト: kyungyonglee/ipop
 /// <summary>
 /// This method uses the sys:link.GetNeighbors rpc call to get the 
 /// brunet address of the RpcIpopNode on the localhost
 /// </summary>
 /// <returns>A string representation of the brunet address</returns>
 public string GetAddress()
 {
     BlockingQueue q = new BlockingQueue();
     _brpc.Rpc.Invoke(_brpc.IPHandler.CreateUnicastSender(_remEP), q, "sys:link.GetNeighbors");
     try {
       RpcResult res = (RpcResult)q.Dequeue();
       Hashtable hash = (Hashtable)res.Result;
       Console.WriteLine(_remEP + ":" + ((UnicastSender)res.ResultSender).EndPoint);
       foreach (DictionaryEntry de in hash) {
     Console.WriteLine("{0} = {1}", de.Key, de.Value);
       }
       return (string)hash["self"];
     }
     catch (InvalidOperationException e) {
       Console.WriteLine(e.Message);
     }
     catch (Exception e) {
       Console.WriteLine(e.Message);
     }
     return null;
 }
コード例 #42
0
ファイル: SystemTest.cs プロジェクト: xujyan/brunet
    public static void Main(string []args) {
      int starting_network_size = 10;
      brunet_namespace += rand.Next();

      int carg = 0;
      while(carg < args.Length) {
        String[] parts = args[carg++].Split('=');
        try {
          switch(parts[0]) {
            case "--n":
              starting_network_size = Int32.Parse(parts[1]);
              break;
            case "--td":
              time_interval = Int32.Parse(parts[1]);
              break;
            case "--ari":
              add_remove_interval = Int32.Parse(parts[1]);
              break;
            case "--ard":
              add_remove_delta = Int32.Parse(parts[1]);
              break;
            case "--dp":
              dht_enabled = true;
              dht_put_interval = Int32.Parse(parts[1]);
              break;
            case "--dg":
              dht_enabled = true;
              dht_get_interval = Int32.Parse(parts[1]);
              break;
            case "--et":
              edge_type = parts[1];
              if(edge_type != "udp" && edge_type != "tcp" && edge_type != "function") {
                throw new Exception();
              }
              break;
            case "--discovery":
              discovery = true;
              break;
            case "--help":
              PrintHelp();
              break;
            default:
              PrintHelp();
              break;
          }
        }
        catch {
          PrintHelp();
        }
      }

      Console.WriteLine("Initializing...");

      for(int i = 0; i < starting_network_size; i++) {
        Console.WriteLine("Setting up node: " + i);
        add_node(false);
      }

      Console.WriteLine("Done setting up...\n");
      Thread system_thread = null;
      if(time_interval > 0) {
        system_thread = new Thread(system);
        system_thread.IsBackground = true;
        system_thread.Start();
      }

      string command = String.Empty;
      Console.WriteLine("Type HELP for a list of commands.\n");
      while (command != "Q") {
        Console.Write("#: ");
        // Commands can have parameters separated by spaces
        string[] parts = Console.ReadLine().Split(' ');
        command = parts[0];

        if(command.Equals("C")) {
          check_ring();
        }
        else if(command.Equals("P")) {
          PrintConnections();
        }
        else if(command.Equals("M")) {
          Console.WriteLine("Memory Usage: " + GC.GetTotalMemory(true));
        }
        else if(command.Equals("G")) {
          if(dht_enabled) {
            NodeMapping nm = (NodeMapping) nodes.GetByIndex(rand.Next(0, nodes.Count));
            Dht dht = nm.Dht;
            if(!dht.Activated)
              continue;
            BlockingQueue returns = new BlockingQueue();
            dht.AsGet("tester", returns);
            int count = 0;
            try {
              while(true) {
                returns.Dequeue();
                count++;
              }
            }
            catch {}
            Console.WriteLine("Count: " + count);
          }
        }
        else if(command.Equals("CR")) {
          Crawl();
        }
        else if(command.Equals("A")) {
          add_node(true);
        }
        else if(command.Equals("R")) {
          remove_node(true);
        }
        else if(command.Equals("ST")) {
          int count = 1024;
          if(parts.Length > 1) {
            count = Int32.Parse(parts[1]);
          }
          SenderSpeedTest(count);
        }
        else if(command.Equals("CM")) {
          StructuredNode n = (StructuredNode) ((NodeMapping) nodes.GetByIndex(nodes.Count - 1)).Node;
          foreach(Address addr in nodes.Keys) {
            n.ManagedCO.AddAddress(addr);
          }
        }
        else if(command.Equals("HELP")) {
          Console.WriteLine("Commands: \n");
          Console.WriteLine("A - add a node");
          Console.WriteLine("R - remove a node");
          Console.WriteLine("C - check the ring using ConnectionTables");
          Console.WriteLine("P - Print connections for each node to the screen");
          Console.WriteLine("M - Current memory usage according to the garbage collector");
          Console.WriteLine("G - Retrieve total dht entries");
          Console.WriteLine("CR - Perform a crawl of the network using RPC");
          Console.WriteLine("ST - Speed test, parameter - integer - times to end data");
          Console.WriteLine("CM - ManagedCO test");
          Console.WriteLine("Q - Quit");
        }
        Console.WriteLine();
      }

      if(system_thread != null) {
        system_thread.Abort();
        system_thread.Join();
      }

      foreach(DictionaryEntry de in nodes) {
        Node node = ((NodeMapping) de.Value).Node;
        node.Disconnect();
      }
    }
コード例 #43
0
		public void TestBlockingQueue()
		{
			Initialize();

			BlockingQueue<int> queue = new BlockingQueue<int>();

			ItemDelegate<int> addToQueue = delegate(int numberToAdd)
			{
				for (int i = 0; i < numberToAdd; ++i)
				{
					queue.Enqueue(1);
				}
				
				this.IncrementEnqueued(numberToAdd);
				this.IncrementExpectedDequeued(numberToAdd);
			};

			addToQueue(100000);

			ThreadStart start = delegate()
			{
				while (true)
				{
					int next;
					bool queueEmpty = !queue.Dequeue(out next);

					if (queueEmpty)
					{
						if (_stopThreads)
							break;
					}
					else
					{
						this.IncrementDequeued(1);
					}
					
					Thread.Sleep(0);
				}
			};

			List<Thread> threads = new List<Thread>();
			for (int i = 0; i < _threadCount; ++i)
			{
				Thread thread = new Thread(start);
				thread.Start();
				threads.Add(thread);
			}

			//continually add to the queue a bit.
			int numberTimesAdded = 0;
			for (int i = 0; i < _threadCount; ++i)
			{
				addToQueue(100000);
				Thread.Sleep(5);
			}

			//'pulse' the queue by letting it go empty, then adding more.
			numberTimesAdded = 0;
			while (true)
			{
				if (queue.Count == 0)
				{
					if (++numberTimesAdded <= _threadCount)
					{
						addToQueue(100000);
					}
					else
					{
						//the real test of exiting the queue is when it's empty, not when it's non-empty.
						queue.ContinueBlocking = false;
						break;
					}
				}

				Thread.Sleep(5);
			}
			
			_stopThreads = true;

			foreach (Thread thread in threads)
				thread.Join();

			threads.Clear();

			Assert.AreEqual(_expectedDequeued, _dequeued, "expectedValue != numberDequeued");
		}
コード例 #44
0
        protected IEnumerator<object> SendTask()
        {
            var sleep = new Sleep(SendInterval);

            Dictionary<string, object> prefs = new Dictionary<string, object>();
            yield return Preferences.GetAll().Bind(() => prefs);

            List<string> allItems = new List<string>();

            var oldQueue = Queue;
            Queue = new BlockingQueue<string>();
            if (oldQueue != null)
                Queue.EnqueueMultiple(oldQueue.DequeueAll());

            while (true) {
                var nextItem = Queue.Dequeue();

                using (nextItem)
                    yield return nextItem;

                yield return sleep;

                allItems.Clear();
                allItems.Add(nextItem.Result);
                allItems.AddRange(Queue.DequeueAll());

                yield return new Start(
                    Send(prefs, allItems.ToArray()), TaskExecutionPolicy.RunAsBackgroundTask
                );
            }
        }
コード例 #45
0
ファイル: SystemTest.cs プロジェクト: xujyan/brunet
    private static void Crawl() {
      int count = 0, consistency = 0;
      NodeMapping nm = (NodeMapping) nodes.GetByIndex(0);
      Node lnode = nm.Node;
      Address rem_addr = lnode.Address, prev = null, first_left = null;
      bool failed = false;
      try {
        do {
          Console.WriteLine("Current address: " + rem_addr);
          ISender sender = new AHGreedySender(lnode, rem_addr);
          BlockingQueue q = new BlockingQueue();
          lnode.Rpc.Invoke(sender, q, "sys:link.GetNeighbors");
          RpcResult res = (RpcResult) q.Dequeue();
          Hashtable ht = (Hashtable) res.Result;

          Address tmp = AddressParser.Parse((String) ht["left"]);
          Address next = AddressParser.Parse((String) ht["right"]);
          if(prev != null && tmp.Equals(prev)) {
            consistency++;
          }
          else {
            first_left = tmp;
          }
          if(next == lnode.Address && first_left == rem_addr) {
            consistency++;
          }
          prev = rem_addr;
          rem_addr = next;
          q.Close();
          count++;
        } while((rem_addr != lnode.Address) && (count < nodes.Count));
      }
      catch(Exception e) {
        failed = true;
        Console.WriteLine("Crawl failed due to exception...");
        Console.WriteLine(e);
      }
      if(!failed) {
        if(count != nodes.Count) {
          Console.WriteLine("Crawl failed due to missing nodes!");
          Console.WriteLine("Expected nodes: {0}, found: {1}.", nodes.Count, count);
        }
        else if(consistency != count) {
          Console.WriteLine("Crawl failed due to bad consistency!");
          Console.WriteLine("Expected consistency: {0}, actual: {1}.", count, consistency);
        }
        else {
          Console.WriteLine("Crawl succeeded!");
        }
      }
    }
コード例 #46
0
        private void Discover(TimeSpan timeout)
        {
            var connectionQueue = new BlockingQueue<MongoServerInstance>();

            for (int i = 0; i < _instances.Count; i++)
            {
                var local = _instances[i];
                connectionQueue.EnqueuWorkItem(() =>
                {
                    try
                    {
                        local.Connect();
                    }
                    catch
                    {
                        // instance is keeping it's last ConnectionException
                    }
                    return local;
                });
            }

            MongoServerInstance instance = null;
            var timeoutAt = DateTime.UtcNow;
            while ((instance = connectionQueue.Dequeue(timeout)) != null)
            {
                if (instance.ConnectException == null)
                {
                    CreateActualProxy(instance, connectionQueue);
                    return;
                }

                timeout = DateTime.UtcNow - timeoutAt;
            }

            throw new MongoConnectionException(string.Format("Unable to connect in the specified timeframe of '{0}'.", timeout));
        }
コード例 #47
0
ファイル: DhtTest.cs プロジェクト: johnynek/brunet
 public void SerialAsyncGet(object data) {
   Hashtable ht = (Hashtable) data;
   byte[] key = (byte[]) ht["key"];
   byte[][] expected_results = (byte[][]) ht["results"];
   int op = (int) ht["op"];
   try {
     BlockingQueue queue = new BlockingQueue();
     default_dht.AsyncGet(key, queue);
     bool found = false;
     int found_count = 0;
     while(true) {
       Hashtable dgr = null;
       try {
         dgr = (Hashtable) queue.Dequeue();
       }
       catch(Exception){
           break;
       }
       for(int j = 0; j < expected_results.Length; j++) {
         if(ArrayComparer((byte[]) dgr["value"], expected_results[j])) {
           found = true;
           break;
         }
       }
       if(found) {
         found_count++;
         found =  false;
       }
     }
     if(found_count != expected_results.Length) {
       lock(_lock) {
         Console.WriteLine("Failed get... attempted to get " + 
             expected_results.Length + " found " + found_count +
             " operation: " + op);
       }
     }
   }
   catch(Exception e) {
     Console.WriteLine("Failure at operation: " + op);
     Console.WriteLine(e);
     throw e;
   }
 }
コード例 #48
0
        IEnumerator<object> SearchInFiles(SearchQuery search, BlockingQueue<string> filenames, IFuture completionFuture)
        {
            var searchedFiles = new List<string>();
            var buffer = new List<SearchResult>();
            var sb = new StringBuilder();

            int numFiles = 0;

            using (Finally.Do(() => {
                SetSearchResults(buffer);
                lblStatus.Text = String.Format("{0} result(s) found.", buffer.Count);
                pbProgress.Style = ProgressBarStyle.Continuous;
                pbProgress.Value = 0;
            }))
            while (filenames.Count > 0 || !completionFuture.Completed) {
                var f = filenames.Dequeue();
                yield return f;

                var filename = f.Result as string;

                if (filename == null)
                    continue;
                if (searchedFiles.Contains(filename))
                    continue;

                if (PendingSearchQuery != null)
                    break;

                searchedFiles.Add(filename);

                int lineNumber = 0;
                var lineBuffer = new LineEntry[3];

                var insertResult = (Action)(() => {
                    var item = new SearchResult();
                    item.Filename = filename;
                    item.LineNumber = lineBuffer[1].LineNumber;

                    sb.Remove(0, sb.Length);
                    for (int i = 0; i < 3; i++) {
                        if (lineBuffer[i].Text != null) {
                            var line = lineBuffer[i].Text;
                            if (line.Length > 512)
                                line = line.Substring(0, 512);
                            sb.Append(line);
                        }

                        if (i < 2)
                            sb.Append("\r\n");
                    }
                    item.Context = sb.ToString();

                    buffer.Add(item);

                    if ((buffer.Count % 250 == 0) || ((buffer.Count < 50) && (buffer.Count % 5 == 1)))
                        SetSearchResults(buffer);
                });

                var stepSearch = (Action)(() => {
                    string currentLine = lineBuffer[1].Text;

                    if ((currentLine != null) && search.Regex.IsMatch(currentLine))
                        insertResult();
                });

                var insertLine = (Action<LineEntry>)((line) => {
                    lineBuffer[0] = lineBuffer[1];
                    lineBuffer[1] = lineBuffer[2];
                    lineBuffer[2] = line;

                    stepSearch();
                });

                numFiles += 1;
                if (numFiles % 50 == 0) {
                    lblStatus.Text = String.Format("Scanning '{0}'...", filename);
                    if (completionFuture.Completed) {
                        int totalNumFiles = numFiles + filenames.Count;
                        int progress = (numFiles * 1000 / totalNumFiles);

                        if (pbProgress.Value != progress)
                            pbProgress.Value = progress;
                        if (pbProgress.Style != ProgressBarStyle.Continuous)
                            pbProgress.Style = ProgressBarStyle.Continuous;
                    }
                }

                FileDataAdapter adapter = null;
                try {
                    adapter = new FileDataAdapter(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                } catch {
                    if (adapter != null)
                        adapter.Dispose();
                    continue;
                }
                using (adapter) {
                    var fEncoding = Future.RunInThread(
                        () => DetectEncoding(adapter.BaseStream)
                    );
                    yield return fEncoding;

                    Future<string> thisLine = null, nextLine = null;

                    using (var reader = new AsyncTextReader(adapter, fEncoding.Result, SearchBufferSize))
                    while (true) {
                        thisLine = nextLine;

                        if (thisLine != null)
                            yield return thisLine;

                        nextLine = reader.ReadLine();

                        if (thisLine == null)
                            continue;

                        lineNumber += 1;
                        string line = thisLine.Result;
                        insertLine(new LineEntry { Text = line, LineNumber = lineNumber });

                        if (line == null)
                            break;
                        if (PendingSearchQuery != null)
                            break;

                        if (lineNumber % 10000 == 5000) {
                            var newStatus = String.Format("Scanning '{0}'... (line {1})", filename, lineNumber);
                            if (lblStatus.Text != newStatus)
                                lblStatus.Text = newStatus;
                        }
                    }
                }
            }
        }
コード例 #49
0
ファイル: RpcDNSClient.cs プロジェクト: kyungyonglee/ipop
 /// <summary>
 /// This method looks for the RpcIpopNode running on the localhost and
 /// sets an Endpoint for future unicast communication
 /// </summary>
 public void Init()
 {
     BlockingQueue q = new BlockingQueue();
     _brpc.Rpc.Invoke(_brpc.IPHandler.CreateMulticastSender(loopback), q, "RpcIpopNode.CheckInstance");
     while (true) {
       try {
     RpcResult res = (RpcResult)q.Dequeue();
     _remEP = ((UnicastSender)res.ResultSender).EndPoint;
     if ((bool)res.Result) {
       break;
     }
       }
       catch (InvalidOperationException e) {
     Console.WriteLine(e.Message);
     break;
       }
       catch (Exception e) {
     Console.WriteLine(e.Message);
     continue;
       }
     }
 }
コード例 #50
0
ファイル: Dht.cs プロジェクト: xujyan/brunet
 /**
 <summary>Synchronous get.</summary>
 <param name="key">The index to look up.</param>
 <returns>An array of DhtGetResult type containing all the results returned.
 </returns>
 */
 public DhtGetResult[] Get(MemBlock key) {
   BlockingQueue returns = new BlockingQueue();
   AsGet(key, returns);
   ArrayList allValues = new ArrayList();
   while(true) {
     // Still a chance for Dequeue to execute on an empty closed queue 
     // so we'll do this instead.
     try {
       DhtGetResult dgr = (DhtGetResult) returns.Dequeue();
       allValues.Add(dgr);
     }
     catch (Exception) {
       break;
     }
   }
   return (DhtGetResult []) allValues.ToArray(typeof(DhtGetResult));
 }
コード例 #51
0
ファイル: XmlRpcManager.cs プロジェクト: pstjuste/brunet
 private object[] Proxy(ISender sender,int maxResultsToWait, string method, object[] args) {
   BlockingQueue q = new BlockingQueue(maxResultsToWait);
   args = AdrXmlRpcConverter.XmlRpc2AdrParams(args);
   _rpc.Invoke(sender, q, method, args);
   ArrayList allValues = new ArrayList();
   int counter = 0;
   ISender rsSender = null;
   try {
     do {
       rsSender = null; //Reset it before the following:
       RpcResult rpcRs = (RpcResult)q.Dequeue();
       rsSender = rpcRs.ResultSender;  //get it before exception thrown
       object val = rpcRs.Result;
       Debug.WriteLine(string.Format("Original Result: {0}", val));
       object xmlrpc_val = AdrXmlRpcConverter.Adr2XmlRpc(val); //conversion in here
       counter++;
       allValues.Add(xmlrpc_val);
     } while (maxResultsToWait < 0 ? true : (counter < maxResultsToWait));
   } catch (Exception e) {
     Debug.WriteLine(e);
     string s = string.Empty;
     if (e is AdrException) {
       if (rsSender != null) {
         s = AdrXmlRpcConverter.Adr2XmlRpc(rsSender) as string;
       }
     }
     if (e is InvalidOperationException) {
       /*
        * this is what we expect at the end of Dequeuing, so just return what we've gotten so far
        * it could be an empty array
        */
       return allValues.ToArray();
     }
     Exception new_e = AdrXmlRpcConverter.Adr2XmlRpc(e) as Exception;
     throw new Exception(new_e.Message + 
       (s.Equals(string.Empty) ? string.Empty : string.Format("thrown by: {0}", s)));
   } finally {
     if (!q.Closed) {
       q.Close();
     }
   }
   return allValues.ToArray();
 }
コード例 #52
0
ファイル: Dht.cs プロジェクト: kyungyonglee/BrunetTutorial
 public Hashtable[] Get(MemBlock key) {
   BlockingQueue returns = new BlockingQueue();
   AsyncGet(key, returns);
   ArrayList values = new ArrayList();
   while(true) {
     // Still a chance for Dequeue to execute on an empty closed queue 
     // so we'll do this instead.
     try {
       Hashtable hash = (Hashtable) returns.Dequeue();
       values.Add(hash);
     }
     catch (Exception) {
       break;
     }
   }
   return (Hashtable[]) values.ToArray(typeof(Hashtable));
 }
コード例 #53
0
ファイル: Program.cs プロジェクト: pazjacket/malys-_-NDexer
        public static IEnumerator<object> CommitBatches(BlockingQueue<IEnumerable<string>> batches, IFuture completion)
        {
            while (batches.Count > 0 || !completion.Completed) {
                var f = batches.Dequeue();
                yield return f;

                var batch = f.Result as IEnumerable<string>;
                if (batch != null)
                    yield return UpdateIndex(batch);
            }
        }
コード例 #54
0
//    static Hashtable taken_ports = new Hashtable();
//    static ArrayList RemoteTA = new ArrayList();

    public static void Main(string []args) {
      if (args.Length < 6) {
        Console.WriteLine("Input format %1 %2 %3 %4 %5 %6");
        Console.WriteLine("\t%1 = [network size]");
        Console.WriteLine("\t%2 = [base time]");
        Console.WriteLine("\t%3 = [add/remove interval]");
        Console.WriteLine("\t%4 = [add/remove delta]");
        Console.WriteLine("\t%5 = [dht put interval]");
        Console.WriteLine("\t%6 = [dht get interval]");
        Console.WriteLine("Specifying 3, 4, 5, 6 disables the event.");
        Environment.Exit(0);
      }

      int starting_network_size = Int32.Parse(args[0]);
      max_range = starting_network_size;

      base_time = Int32.Parse(args[1]);
      add_remove_interval = Int32.Parse(args[2]);
      add_remove_delta = Int32.Parse(args[3]);
      dht_put_interval = Int32.Parse(args[4]);
      dht_get_interval = Int32.Parse(args[5]);
      Console.WriteLine("Initializing...");

      for(int i = 0; i < starting_network_size; i++) {
        Console.WriteLine("Setting up node: " + i);
        add_node();
      }
      Console.WriteLine("Done setting up...\n");

      Thread system_thread = new Thread(system);
      system_thread.IsBackground = true;
      system_thread.Start();

      string command = String.Empty;
      while (command != "Q") {
        Console.WriteLine("Enter command (M/C/P/G/Q)");
        command = Console.ReadLine();
        if(command.Equals("C")) {
          check_ring();
        }
        else if(command.Equals("P")) {
          PrintConnections();
        }
        else if(command.Equals("M")) {
          Console.WriteLine("Memory Usage: " + GC.GetTotalMemory(true));
        }
        else if(command.Equals("G")) {
          Node node = (Node) nodes.GetByIndex(rand.Next(0, network_size));
          Dht dht = (Dht) dhts[node];
          if(!dht.Activated)
            continue;
          BlockingQueue returns = new BlockingQueue();
          dht.AsGet("tester", returns);
          int count = 0;
          try {
            while(true) {
              returns.Dequeue();
              count++;
            }
          }
          catch {}
          Console.WriteLine("Count: " + count);
        }
        Console.WriteLine();
      }

      system_thread.Abort();

      int lcount = 0;
      foreach(DictionaryEntry de in nodes) {
        Console.WriteLine(lcount++);
        Node node = (Node)de.Value;
        node.Disconnect();
      }
    }
コード例 #55
0
ファイル: Dht.cs プロジェクト: kyungyonglee/BrunetTutorial
 /// <summary>This is the sychronous version of the generic Put used by both
 /// the Put and Create methods.  The use of the unique variable
 /// differentiates the two.  Returns true if successful or an exception if
 /// there are network errors in adding the entry, creates also fail if a
 /// previous entry exists.</summary>
 /// <param name="key">The index to store the value at.</param>
 /// <param name="value">The value to store.</param>
 /// <param name="ttl">The dht lease time for the key:value pair.</param>
 /// <param name="unique">True to do a create, false otherwise.</param>
 /// <returns>True if success, exception on fail</returns>
 public bool Put(MemBlock key, MemBlock value, int ttl, bool unique) {
   BlockingQueue returns = new BlockingQueue();
   AsyncPut(key, value, ttl, returns, unique);
   object result = returns.Dequeue();
   try {
     return (bool) result;
   }
   catch {
     throw (Exception) result;
   }
 }
コード例 #56
0
ファイル: BlockingQueueTests.cs プロジェクト: nataren/DReAM
 public void Single_threaded_queue_dequeue()
 {
     int n = 10000;
     List<string> guids = new List<string>();
     BlockingQueue<string> q = new BlockingQueue<string>();
     for(int i = 0; i < n; i++) {
         string guid = Guid.NewGuid().ToString();
         q.Enqueue(guid);
         guids.Add(guid);
     }
     Assert.AreEqual(n, q.Count);
     for(int i = 0; i < n; i++) {
         string guid = q.Dequeue();
         Assert.AreEqual(guids[i], guid);
     }
 }