Пример #1
0
        /// <summary>
        /// opens a recording stream
        /// set a video codec token first.
        /// </summary>
        public void OpenFile(string baseName)
        {
            string ext = Path.GetExtension(baseName);

            if (ext == null || ext.ToLower() != ".jmd")
            {
                baseName = baseName + ".jmd";
            }

            jmdfile = new JMDfile(File.Open(baseName, FileMode.Create), fpsnum, fpsden, audiosamplerate, audiochannels == 2);


            if (moviemetadata != null)
            {
                jmdfile.writemetadata(moviemetadata);
            }

            // start up thread
            // problem: since audio chunks and video frames both go through here, exactly how many zlib workers
            // gives is not known without knowing how the emulator will chunk audio packets
            // this shouldn't affect results though, just performance
            threadQ = new System.Collections.Concurrent.BlockingCollection <Object>(token.numthreads * 2);
            workerT = new System.Threading.Thread(new System.Threading.ThreadStart(threadproc));
            workerT.Start();
            GzipFrameDelegate = new GzipFrameD(GzipFrame);
        }
    static void Main(string[] args)
    {
        var c = new System.Collections.Concurrent.BlockingCollection <Tuple <bool, Action> >();
        var t = new Thread(() =>
        {
            while (true)
            {
                var item = c.Take();
                if (!item.Item1)
                {
                    break;
                }
                item.Item2();
            }
            Console.WriteLine("Exiting thread");
        });

        t.Start();

        Console.WriteLine("Press any key to queue first action");
        Console.ReadKey();
        c.Add(Tuple.Create <bool, Action>(true, () => Console.WriteLine("Executing first action")));

        Console.WriteLine("Press any key to queue second action");
        Console.ReadKey();
        c.Add(Tuple.Create <bool, Action>(true, () => Console.WriteLine("Executing second action")));
        Console.WriteLine("Press any key to stop the thread");
        Console.ReadKey();
        c.Add(Tuple.Create <bool, Action>(false, null));

        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
Пример #3
0
        /// <summary>
        /// Create new instanz of Logger
        /// </summary>
        /// <param name="appGuidId">AppGuidId</param>
        public Logger(string appGuidId)
        {
            _appGuidId = appGuidId;
            _list      = new System.Collections.Concurrent.BlockingCollection <CrashItem>
            {
                new CrashItem()
                {
                    DataType = CrashDataType.Text, Name = "OSDescription", Value = RuntimeInformation.OSDescription
                },
                new CrashItem()
                {
                    DataType = CrashDataType.Text, Name = "FrameworkDescription", Value = RuntimeInformation.FrameworkDescription
                },
                new CrashItem()
                {
                    DataType = CrashDataType.Text, Name = "OSArchitecture", Value = RuntimeInformation.OSArchitecture.ToString("g")
                },
                new CrashItem()
                {
                    DataType = CrashDataType.Text, Name = "ProcessArchitecture", Value = RuntimeInformation.ProcessArchitecture.ToString("g")
                },
            };

            if (_client == null)
            {
                _client = new HttpClient();
                _client.DefaultRequestHeaders.Accept.Clear();
                _client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                SendFromFileSystem();
            }
        }
Пример #4
0
 internal override void Initiate()
 {
     try
     {
         dataDebugItems = new System.Collections.Concurrent.BlockingCollection <LogModelDebug>(logCapacity);
         string createCommandText = string.Format("IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{0}]') AND type in (N'U')) CREATE TABLE  [dbo].[{0}](  [Id] [bigint] IDENTITY(1,1) NOT NULL,  [Time] [datetime] NOT NULL,  [IP] [varchar](50) NOT NULL, [Username] [nvarchar](50) NOT NULL, [Client] [varchar](15) NOT NULL, [URL] [varchar](127) NOT NULL,[Method] [varchar](6) NOT NULL,[DeviceName] [nvarchar](50) NOT NULL, [Data] [nvarchar](max) NOT NULL,   CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] ", logName + "_Debug");
         Task   createTable       = Task.Run(async() =>
         {
             using (SqlConnection con = new SqlConnection(logConnection))
             {
                 using (SqlCommand cmd = new SqlCommand(createCommandText, con))
                 {
                     await cmd.Connection.OpenAsync();
                     await cmd.ExecuteNonQueryAsync();
                 }
             }
         });
         createTable.Wait();
         Table.Columns.Add("Id", typeof(long));
         Table.Columns.Add("Time", typeof(DateTime));
         Table.Columns.Add("IP", typeof(string));
         Table.Columns.Add("Username", typeof(string));
         Table.Columns.Add("Client", typeof(string));
         Table.Columns.Add("URL", typeof(string));
         Table.Columns.Add("Method", typeof(string));
         Table.Columns.Add("DeviceName", typeof(string));
         Table.Columns.Add("Data", typeof(string));
         base.Initiate();
     }
     catch { }
 }
Пример #5
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="hostName"></param>
        /// <param name="userName"></param>
        /// <param name="port"></param>
        /// <param name="password"></param>
        /// <param name="maxQueueCount"></param>
        /// <param name="serializerType"></param>
        /// <param name="loger"></param>
        /// <param name="writeWorkerTaskNumber"></param>
        private RabbitMQClient(string hostName, string userName, string password, int?port, int maxQueueCount
                               , SerializerType serializerType, ILoger loger = null, short writeWorkerTaskNumber = 4)
        {
            factory          = new ConnectionFactory();
            factory.HostName = hostName;
            if (!port.HasValue || port.Value < 0)
            {
                factory.Port = 5672;
            }
            else
            {
                factory.Port = port.Value;
            }
            factory.Password = password;
            factory.UserName = userName;

            serializer = SerializerFactory.Create(serializerType);
            _queue     = new System.Collections.Concurrent.BlockingCollection <StrongBox <QueueMessage> >();

            _maxQueueCount = maxQueueCount > 0 ? maxQueueCount : Options.DefaultMaxQueueCount;


            this.loger = loger;

            var         scheduler   = new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, writeWorkerTaskNumber).ExclusiveScheduler;
            TaskFactory taskFactory = new TaskFactory(scheduler);

            for (int i = 0; i < writeWorkerTaskNumber; i++)
            {
                taskFactory.StartNew(QueueToWrite, TaskCreationOptions.LongRunning);
            }
        }
        /// <summary>
        /// Instructs the processor to access live stock data
        /// </summary>
        /// <param name="liveInterval">The interval at which to access the live data</param>
        public void SetLive(TimeSpan?liveInterval = null)
        {
            this.LiveData     = new Dictionary <string, Tuple <StockDataSetDerived <StockDataSink, StockDataSource, StockProcessingState>, DataAccessor.Subscription> >();
            this.LiveInterval = ((liveInterval != null) ? liveInterval.Value : new TimeSpan(0, 0, 1));
            this.Live         = true;

            this.LiveProcessingQueue = new System.Collections.Concurrent.BlockingCollection <ProcessingTarget>();
            this.LiveThread          = new System.Threading.Thread(ProcessLive);
            this.LiveThread.Start();
        }
Пример #7
0
        protected internal void Start()
        {
            this.MessageQueue = this.QueueSize == null
                ? new System.Collections.Concurrent.BlockingCollection <Generic.LogMessage <System.Object> >(
                new System.Collections.Concurrent.ConcurrentQueue <Generic.LogMessage <System.Object> >())
                : new System.Collections.Concurrent.BlockingCollection <Generic.LogMessage <System.Object> >(
                new System.Collections.Concurrent.ConcurrentQueue <Generic.LogMessage <System.Object> >(), this.QueueSize.Value);

            this.CancellationTokenSource = new System.Threading.CancellationTokenSource();
            this.OutputTask = System.Threading.Tasks.Task.Factory.StartNew(state => this.ProcessLogQueue(), null,
                                                                           System.Threading.Tasks.TaskCreationOptions.LongRunning);
        }
Пример #8
0
        /// <summary>opens an avi file for recording, with <paramref name="nameProvider"/> being used to name files</summary>
        /// <exception cref="InvalidOperationException">no video codec token set</exception>
        public void OpenFile(IEnumerator <string> nameProvider)
        {
            _nameProvider = nameProvider;
            if (_currVideoCodecToken == null)
            {
                throw new InvalidOperationException("Tried to start recording an AVI with no video codec token set");
            }

            threadQ = new System.Collections.Concurrent.BlockingCollection <object>(30);
            workerT = new System.Threading.Thread(new System.Threading.ThreadStart(threadproc));
            workerT.Start();
        }
Пример #9
0
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            if (_entityQueue != null)
            {
                _entityQueue.Dispose();
            }
            _entityQueue = null;

            if (_mainToken == null)
            {
                return;
            }
            _mainToken.Cancel();
            _mainToken.Dispose();
            _mainToken = null;
        }
Пример #10
0
        private void StartWorkers(List <GlobalConfig.SignatureSource> repos, int workerCount, CancellationToken cancelToken)
        {
            _repos = new System.Collections.Concurrent.BlockingCollection <GlobalConfig.SignatureSource>();
            repos.ForEach(x => _repos.Add(x));
            _repos.CompleteAdding();
            Running            = true;
            _token             = cancelToken;
            InitialTaskCount   = repos.Count;
            CompletedTaskCount = 0;

            _activeWorkers = workerCount;
            for (var i = 0; i < workerCount; i++)
            {
                Task.Factory.StartNew(StartWork);
            }
        }
Пример #11
0
        /// <summary>
        /// 重置
        /// </summary>
        public void Reset()
        {
            lock (this._threadLock)
            {
                this.ClearThread();

                lock (this._blockingCollectionLock)
                {
                    this._blockingCollection.Dispose();
                    this._blockingCollection = new System.Collections.Concurrent.BlockingCollection <T>();
                }

                //this._blockingCollection.Clear();
                this._allowAddThread = true;
                this.CreateProcessThread();
            }
        }
Пример #12
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="poolPolicy">A <seealso cref="PoolPolicy{T}"/> instance containing configuration information for the pool.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="poolPolicy"/> argument is null.</exception>
        /// <exception cref="System.ArgumentException">Thrown if the <see cref="PoolPolicy{T}.Factory"/> property of the <paramref name="poolPolicy"/> argument is null.</exception>
        public Pool(PoolPolicy <T> poolPolicy) : base(poolPolicy)
        {
            _Pool = new System.Collections.Concurrent.ConcurrentBag <T>();

            if (PoolPolicy.InitializationPolicy == PooledItemInitialization.AsyncReturn)
            {
                _ItemsToInitialise = new System.Collections.Concurrent.BlockingCollection <T>();
#if SUPPORTS_THREADS
                _ReinitialiseThread              = new System.Threading.Thread(this.BackgroundReinitialise);
                _ReinitialiseThread.Name         = this.GetType().FullName + " Background Reinitialise";
                _ReinitialiseThread.IsBackground = true;
                _ReinitialiseThread.Start();
#else
                System.Threading.Tasks.Task.Factory.StartNew(this.BackgroundReinitialise, System.Threading.Tasks.TaskCreationOptions.LongRunning);
#endif
            }
        }
Пример #13
0
        private void ThreadProcessCommands()
        {
            CommandQueue = new System.Collections.Concurrent.BlockingCollection <CCommandStucture>();

            CCommandStucture comStruct = new CCommandStucture();

            comStruct.CommandName = "Dummy";   //to remove first iteration delay
            CommandQueue.Add(comStruct);

            foreach (CCommandStucture cs in CommandQueue.GetConsumingEnumerable())
            {
                if (bEnableCommandQueue)
                {
                    Log("Process command struct");
                    ProcessCommandStructure(cs);
                }
            }
        }
Пример #14
0
        private List <Customer> BuildCustomerList(List <CommerceEntity> organizations)
        {
            var customers = new System.Collections.Concurrent.BlockingCollection <Customer>();
            Dictionary <string, Dsr>    dsrDict  = RetrieveDsrDictionary();
            Dictionary <string, string> termDict = RetrieveTermsCodeDict();

            System.Threading.Tasks.Parallel.ForEach(organizations, e => {
                Organization org = new KeithLink.Svc.Core.Models.Generated.Organization(e);

                if (org.OrganizationType == ORGANIZATION_TYPE_CUSTOMER)
                {
                    Customer myCustomer = org.ToCustomer();

                    string termKey = GetTermKey(org.BranchNumber, org.TermCode);

                    if (termDict.ContainsKey(termKey))
                    {
                        myCustomer.TermDescription = termDict[termKey];
                    }

                    string dsrKey = GetDsrKey(myCustomer.CustomerBranch, myCustomer.DsrNumber);
                    Dsr myDsr     = null;

                    if (dsrDict.ContainsKey(dsrKey))
                    {
                        myDsr = dsrDict[dsrKey];
                    }
                    else
                    {
                        dsrKey = GetDsrKey(myCustomer.CustomerBranch, DEFAULT_DSR_NUMBER);
                        if (dsrDict.ContainsKey(dsrKey))
                        {
                            myDsr = dsrDict[dsrKey];
                        }
                    }

                    myCustomer.Dsr = myDsr;

                    customers.Add(myCustomer);
                }
            });

            return(customers.ToList());
        }
Пример #15
0
        public MinecraftServer(object DataContext)
        {
            ViewModel = DataContext as ViewModel;

            _config = new Config();
            _config.init();

            _stateManager = new StateManager();
            _stateManager.Init();
            _clients = new ArrayList();

            this._tcpListener = new TcpListener(IPAddress.Any, _config.port);
            Start();

            Packets = new System.Collections.Concurrent.BlockingCollection <APacket>();
            new PacketWriterWorker(Packets, 1u);

            //DefaultWorld = new World();
            DefaultWorld.Updated = true;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="hostName"></param>
        /// <param name="userName"></param>
        /// <param name="port"></param>
        /// <param name="password"></param>
        /// <param name="maxQueueCount"></param>
        /// <param name="serializerType"></param>
        /// <param name="loger"></param>
        /// <param name="writeWorkerTaskNumber"></param>
        private RabbitMQClient(string hostName, string userName, string password, int?port, int maxQueueCount
                               , SerializerType serializerType, ILoger loger = null, short writeWorkerTaskNumber = 4)
        {
            factory          = new ConnectionFactory();
            factory.HostName = hostName;
            if (!port.HasValue || port.Value < 0)
            {
                factory.Port = 5672;
            }
            else
            {
                factory.Port = port.Value;
            }
            factory.Password = password;
            factory.UserName = userName;

            serializer = SerializerFactory.Create(serializerType);
            _queue     = new System.Collections.Concurrent.BlockingCollection <StrongBox <QueueMessage> >();

            //_queue = new System.Collections.Queue();
            _maxQueueCount = maxQueueCount > 0 ? maxQueueCount : Options.DefaultMaxQueueCount;

            //isQueueToWrite = false;
            //resetEvent = new AutoResetEvent(false);

            this.loger = loger;
            //this.waitMillisecondsTimeout = 10000;

            //queueWorkThread = new Thread(QueueToWrite);
            //queueWorkThread.IsBackground = true;
            //queueWorkThread.Start();

            var         scheduler   = new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, writeWorkerTaskNumber).ExclusiveScheduler;
            TaskFactory taskFactory = new TaskFactory(scheduler);

            for (int i = 0; i < writeWorkerTaskNumber; i++)
            {
                taskFactory.StartNew(QueueToWrite, TaskCreationOptions.LongRunning);
                //Task.Factory.StartNew(QueueToWrite);
            }
        }
Пример #17
0
        public Server(object DataContext)
        {
            ViewModel = DataContext as ViewModel;

            _server = new Thread(StartListening);

            _config = new Config();
            _config.init();

            _world         = new World(_config.maxPlayers);
            _world.Updated = true;

            _stateManager = new StateManager();
            _stateManager.Init();

            Packets = new System.Collections.Concurrent.BlockingCollection <APacket>();
            new PacketWriterWorker(Packets, 1u);

            _server.Start();
            ViewModel.Log("Server is now listening on " + _config.port + ".");
        }
Пример #18
0
        static void ProcessFile(string input, string output)
        {
            var inputsLine     = new System.Collections.Concurrent.BlockingCollection <string>();
            var processedLines = new System.Collections.Concurrent.BlockingCollection <string>();

            // stage #1
            var readLines = Task.Factory.StartNew(() =>
            {
                try
                {
                    foreach (var line in File.ReadLines(input))
                    {
                        inputsLine.Add(line);
                    }
                }
                finally { inputsLine.CompleteAdding(); }
            });

            // stage #2
            var processLines = Task.Factory.StartNew(() =>
            {
                try
                {
                    foreach (var line in inputsLine.GetConsumingEnumerable()
                             .Select(line => Regex.Replace(line, @"\s+", ", ")))
                    {
                        processedLines.Add(line);
                    }
                }
                finally { processedLines.CompleteAdding(); }
            });

            // stage #3
            var writeLines = Task.Factory.StartNew(() =>
            {
                File.WriteAllLines(output, processedLines.GetConsumingEnumerable());
            });

            Task.WaitAll(readLines, processLines, writeLines);
        }
Пример #19
0
        public List <Account> GetAccounts()
        {
            var queryOrg = new CommerceServer.Foundation.CommerceQuery <KeithLink.Svc.Core.Models.Generated.Organization>("Organization");

            queryOrg.SearchCriteria.WhereClause = "u_organization_type = '1'"; // org type of account

            CommerceQueryOperationResponse res = (Svc.Impl.Helpers.FoundationService.ExecuteRequest(queryOrg.ToRequest())).OperationResponses[0] as CommerceQueryOperationResponse;

            var accounts = new System.Collections.Concurrent.BlockingCollection <Account>();

            System.Threading.Tasks.Parallel.ForEach(res.CommerceEntities, e =>
            {
                KeithLink.Svc.Core.Models.Generated.Organization org = new KeithLink.Svc.Core.Models.Generated.Organization(e);
                accounts.Add(new Account()
                {
                    Id   = Guid.Parse(org.Id),
                    Name = org.Name,
                });
            });

            return(accounts.ToList());
        }
Пример #20
0
        public static void Main(string[] argv)
        {
            var genesis  = Network.BatzMain.GetGenesis();
            var queue    = new System.Collections.Concurrent.ConcurrentQueue <uint>();
            var blocking = new System.Collections.Concurrent.BlockingCollection <uint>(queue, 100);


            int cpu = 6;

            for (int i = 0; i <= cpu; i++)
            {
                Task.Factory.StartNew(() => {
                    foreach (var nonce in blocking.GetConsumingEnumerable())
                    {
                        var block          = genesis.Clone();
                        block.Header.Nonce = nonce;
                        var hash           = block.GetHash();
                        if (hash < minhash)
                        {
                            lock (sync)
                            {
                                if (hash < minhash)
                                {
                                    Console.WriteLine("Better hash found nonce : {0}, hash : {1}", nonce, hash);
                                    minhash  = hash;
                                    minnonce = nonce;
                                }
                            }
                        }
                    }
                });
            }

            for (uint nonce = 0; nonce <= uint.MaxValue; nonce++)
            {
                blocking.Add(nonce);
            }
        }
Пример #21
0
        public PacketWriterWorker(System.Collections.Concurrent.BlockingCollection <APacket> packets, uint numberOfInstance)
        {
            Packets  = packets;
            Instance = this;
            MyMutex  = new Mutex();

            for (uint i = 0u; i < numberOfInstance; ++i)
            {
                Task.Factory.StartNew(() =>
                {
                    uint myId = i;

                    foreach (var packet in packets.GetConsumingEnumerable())
                    {
                        NetworkStream wrapper = new NetworkStream(packet.Destination.Socket);
                        lock (packet.Destination.Socket)
                        {
                            System.Diagnostics.Debug.WriteLine("SendingTo " + packet.Destination.Socket.RemoteEndPoint + " " + packet.Name);
                            packet.Write(wrapper, packet.Destination);
                        }
                    }
                }, TaskCreationOptions.LongRunning);
            }
        }
Пример #22
0
 public MsgSender(System.Net.Sockets.TcpClient client)
 {
   connection_lost = false;
   serverside_client = client;
   stream = serverside_client.GetStream();
   inbound = new System.Collections.Concurrent.BlockingCollection<string>();
   outbound = new System.Collections.Concurrent.BlockingCollection<string>();
   read_task = Task.Run(() => read());
   send_task = Task.Run(() => send());
 }
Пример #23
0
		/// <summary>
		/// opens an avi file for recording with the supplied enumerator used to name files.
		/// set a video codec token first.
		/// </summary>
		/// <param name="nameProvider"></param>
		public void OpenFile(IEnumerator<string> nameProvider)
		{
			this.nameProvider = nameProvider;
			if (currVideoCodecToken == null)
				throw new InvalidOperationException("Tried to start recording an AVI with no video codec token set");

			threadQ = new System.Collections.Concurrent.BlockingCollection<Object>(30);
			workerT = new System.Threading.Thread(new System.Threading.ThreadStart(threadproc));
			workerT.Start();
		}
Пример #24
0
        static void Main()
        {
            // create the state machine model
            var model = new StateMachine <Player>("model");

            // create the vertices within the model
            var initial     = model.CreatePseudoState("initial", PseudoStateKind.Initial);
            var operational = model.CreateState("operational");
            var choice      = model.CreatePseudoState("choice", PseudoStateKind.Choice);
            var flipped     = model.CreateState("flipped");
            var final       = model.CreateFinalState("final");

            var history = operational.CreatePseudoState("history", PseudoStateKind.DeepHistory);
            var stopped = operational.CreateState("stopped");
            var active  = operational.CreateState("active").Entry(i => i.EngageHead()).Exit(i => i.DisengageHead());

            var running = active.CreateState("running").Entry(i => i.StartMotor()).Exit(i => i.StopMotor());
            var paused  = active.CreateState("paused");

            // create the transitions between vertices of the model
            initial.To(operational).Effect(i => i.DisengageHead()).Effect(i => i.StopMotor());
            history.To(stopped);
            stopped.To(running).When <string>(command => command == "play");
            active.To(stopped).When <string>(command => command == "stop");
            running.To(paused).When <string>(command => command == "pause");
            running.To().When <string>(command => command == "tick").Effect((Player instance) => instance.Count++);
            paused.To(running).When <string>(command => command == "play");
            operational.To(final).When <string>(command => command == "off");
            operational.To(choice).When <string>(command => command == "rand");
            choice.To(operational).Effect(() => Console.WriteLine("- transition A back to operational"));
            choice.To(operational).Effect(() => Console.WriteLine("- transition B back to operational"));
            operational.To(flipped).When <string>(command => command == "flip");
            flipped.To(operational).When <string>(command => command == "flip");

            // validate the model for correctness
            model.Validate();

            // create a blocking collection make events from multiple sources thread-safe
            var queue = new System.Collections.Concurrent.BlockingCollection <Object>();

            // create an instance of the player - enqueue a tick message for the machine while its playing
            var player = new Player(() => queue.Add("tick"));

            // initialises the players initial state (enters the region for the first time, causing transition from the initial PseudoState)
            model.Initialise(player);

            // create a task to capture commands from the console in another thread
            System.Threading.Tasks.Task.Run(() => {
                string command = "";

                while (command.Trim().ToLower() != "exit")
                {
                    queue.Add(command = Console.ReadLine());
                }

                queue.CompleteAdding();
            });

            // write the initial command prompt
            Console.Write("{0:0000}> ", player.Count);

            // process messages from the queue
            foreach (var message in queue.GetConsumingEnumerable())
            {
                // process the message
                model.Evaluate(player, message);

                // manage the command prompt
                var left = Math.Max(Console.CursorLeft, 6);
                var top  = Console.CursorTop;
                Console.SetCursorPosition(0, top);
                Console.Write("{0:0000}>", player.Count);
                Console.SetCursorPosition(left, top);
            }
        }
Пример #25
0
        protected override void CollectChunks(System.Collections.Concurrent.BlockingCollection <IReadOnlyList <IObjectResolver> > chunkedSource)
        {
            using (var file = File.OpenRead(_filePath))
                using (var stringReader = new StreamReader(file))
                    using (var xmlReader = XmlReader.Create(stringReader, new XmlReaderSettings()
                    {
                        IgnoreWhitespace = true,
                        IgnoreComments = true
                    }))
                    {
                        xmlReader.MoveToContent();

                        var chunk = new List <IObjectResolver>(1000);

                        var nameToIndexMap         = new Dictionary <string, int>();
                        var indexToObjectAccessMap = new Dictionary <int, Func <dynamic, object> >();

                        var elements = new Stack <DynamicElement>();

                        do
                        {
                            switch (xmlReader.NodeType)
                            {
                            case XmlNodeType.Element:
                                var element = new DynamicElement
                                {
                                    { "element", xmlReader.LocalName },
                                    { "parent", elements.Count > 0 ? elements.Peek() : null },
                                    { "value", xmlReader.HasValue ? xmlReader.Value : null }
                                };

                                element.Add(xmlReader.Name, element);

                                elements.Push(element);

                                if (xmlReader.HasAttributes)
                                {
                                    while (xmlReader.MoveToNextAttribute())
                                    {
                                        element.Add(xmlReader.Name, xmlReader.Value);
                                    }
                                }

                                xmlReader.MoveToElement();
                                break;

                            case XmlNodeType.Text:
                                elements.Peek().Add("text", xmlReader.Value);
                                break;

                            case XmlNodeType.EndElement:
                                chunk.Add(new DictionaryResolver(elements.Pop()));
                                break;
                            }

                            if (chunk.Count >= 1000)
                            {
                                chunkedSource.Add(chunk);
                                chunk = new List <IObjectResolver>(1000);
                            }
                        }while (xmlReader.Read());

                        if (chunk.Count > 0)
                        {
                            chunkedSource.Add(chunk);
                        }
                    }
        }
Пример #26
0
        private async void UpdateHomeItems()
        {
            #region Subscriptions

            Log.Info("Updating the videos on the home page");

            PlaylistDataType YTItemsListTemp = new PlaylistDataType()
            {
                Title = "Today"
            };
            PlaylistDataType YTItemsListTempYesterday = new PlaylistDataType()
            {
                Title = "Yesterday"
            };
            PlaylistDataType YTItemsListTempTwoDays = new PlaylistDataType()
            {
                Title = "Two Days Ago"
            };
            PlaylistDataType YTItemsListTempThreeDays = new PlaylistDataType()
            {
                Title = "Three Days Ago"
            };
            PlaylistDataType YTItemsListTempFourDays = new PlaylistDataType()
            {
                Title = "Four Days Ago"
            };
            PlaylistDataType YTItemsListTempFiveDays = new PlaylistDataType()
            {
                Title = "Five Days Ago"
            };

            System.Collections.Concurrent.BlockingCollection <Google.Apis.YouTube.v3.Data.SearchResult> searchResponseList = new System.Collections.Concurrent.BlockingCollection <Google.Apis.YouTube.v3.Data.SearchResult>();

            var service = await YoutubeMethodsStatic.GetServiceAsync();

            await Task.Run(() =>
            {
                Parallel.ForEach(Constants.MainPageRef.subscriptionsList, subscription =>
                {
                    try
                    {
                        var tempService        = service.Search.List("snippet");
                        tempService.ChannelId  = subscription.Id;
                        tempService.Order      = SearchResource.ListRequest.OrderEnum.Date;
                        tempService.MaxResults = 8;
                        var response           = tempService.Execute();
                        foreach (var video in response.Items)
                        {
                            searchResponseList.Add(video);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("A subscription's videos failed to load.");
                        subscription.Thumbnail = null;
                        Log.Error(JsonConvert.SerializeObject(subscription));
                        Log.Error(ex.Message);
                    }
                });
            });

            var orderedSearchResponseList = searchResponseList.OrderByDescending(x => x.Snippet.PublishedAt).ToList();

            Log.Info("Ordering videos by date and placing them in the correct list");
            foreach (var video in orderedSearchResponseList)
            {
                var methods = new YoutubeMethods();
                if (video != null && video.Id.Kind == "youtube#video" && video.Id.VideoId != null && video.Snippet.LiveBroadcastContent != "live")
                {
                    try
                    {
                        DateTime now       = DateTime.Now;
                        var      ytubeItem = methods.VideoToYoutubeItem(video);
                        if (ytubeItem.Failed != true)
                        {
                            if (video.Snippet.PublishedAt > now.AddHours(-24))
                            {
                                YTItemsListTemp.Items.Add(ytubeItem);
                            }
                            else if (video.Snippet.PublishedAt > now.AddHours(-48))
                            {
                                YTItemsListTempYesterday.Items.Add(ytubeItem);
                            }
                            else if (video.Snippet.PublishedAt > now.AddHours(-72))
                            {
                                YTItemsListTempTwoDays.Items.Add(ytubeItem);
                            }
                            else if (video.Snippet.PublishedAt > now.AddHours(-96))
                            {
                                YTItemsListTempThreeDays.Items.Add(ytubeItem);
                            }
                            else if (video.Snippet.PublishedAt > now.AddHours(-120))
                            {
                                YTItemsListTempFourDays.Items.Add(ytubeItem);
                            }
                            else if (video.Snippet.PublishedAt > now.AddHours(-144) && video.Snippet.PublishedAt <= now)
                            {
                                YTItemsListTempFiveDays.Items.Add(ytubeItem);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(String.Format("A video failed to load into the home page. Json: {0}", JsonConvert.SerializeObject(video)));
                        Log.Error(ex.Message);
                    }
                }
            }

            YTItems.Add(YTItemsListTemp);
            YTItems.Add(YTItemsListTempYesterday);
            YTItems.Add(YTItemsListTempTwoDays);
            YTItems.Add(YTItemsListTempThreeDays);
            YTItems.Add(YTItemsListTempFourDays);
            YTItems.Add(YTItemsListTempFiveDays);
            #endregion

            LoadingRing.IsActive = false;

            Parallel.ForEach(YTItems, playlist =>
            {
                var methodsLocal = new YoutubeMethods();
                methodsLocal.FillInViews(playlist.Items, service);
            });
        }
Пример #27
0
        public PcapStreamReader(System.IO.Stream pcapStream, int packetQueueSize, StreamReadCompletedCallback streamReadCompletedCallback, bool startBackgroundWorkers, long streamMaxLength, int readTimeoutMilliseconds)
        {
            this.pcapStream              = pcapStream;
            this.streamLength            = streamMaxLength;
            this.readBytesEstimate       = 0;
            this.readTimeoutMilliseconds = readTimeoutMilliseconds;

            this.packetQueueMaxSize          = packetQueueSize;
            this.streamReadCompletedCallback = streamReadCompletedCallback;


            //TODO: Figure out if it is a libpcap or pcapNG stream...
            this.pcapParser = PcapParserFactory.CreatePcapParser(this);// new PcapParser(pcapStream, this.AbortReadingPcapStream);

            this.packetQueueBC = new System.Collections.Concurrent.BlockingCollection <PcapFrame>(this.packetQueueMaxSize);
            //this.packetQueue = new System.Collections.Concurrent.ConcurrentQueue<PcapFrame>();
            //this.packetQueueHasRoomEvent = new System.Threading.AutoResetEvent(true);
            this.enqueuedByteCount = 0;
            this.dequeuedByteCount = 0;

            this.backgroundStreamReaderCanceller = new System.Threading.CancellationTokenSource();
            System.Threading.CancellationToken cancellationToken = backgroundStreamReaderCanceller.Token;
            this.readAction = new Action(() => {
                DateTime firstFrameTimestamp = DateTime.MinValue;
                DateTime lastFrameTimestamp  = DateTime.MinValue;
                int framesCount = 0;
                try {
                    //int sleepMilliSecs = 20;

                    while (!cancellationToken.IsCancellationRequested && !this.EndOfStream())
                    {
                        PcapFrame packet = this.pcapParser.ReadPcapPacketBlocking();
                        //PcapFrame packet = await this.pcapParser.ReadPcapPacketAsync(cancellationToken);
                        if (firstFrameTimestamp == DateTime.MinValue)
                        {
                            firstFrameTimestamp = packet.Timestamp;
                        }
                        lastFrameTimestamp = packet.Timestamp;
                        framesCount++;
                        this.enqueuedByteCount += packet.Data.Length;

                        while (!this.packetQueueBC.TryAdd(packet, 1000, cancellationToken))
                        {
                            if (cancellationToken.IsCancellationRequested || this.EndOfStream())
                            {
                                break;
                            }
                        }
                        //this.packetQueue.Enqueue(packet);
                    }
                }
                catch (System.IO.EndOfStreamException) {
                    //Do nothing, just stop reading
                    this.pcapStream = null;
                }
                catch (System.IO.IOException) {
                    //probably a socket timout
                    if (!(this.pcapStream is System.IO.FileStream) && this.pcapStream != null)
                    {
                        if (this.pcapStream.CanWrite)
                        {
                            this.pcapStream.Flush();
                        }
                        this.pcapStream.Dispose();
                    }
                    //this.pcapStream = null;
                }
                catch (OperationCanceledException) {
                    if (!(this.pcapStream is System.IO.FileStream) && this.pcapStream != null)
                    {
                        if (this.pcapStream.CanWrite)
                        {
                            this.pcapStream.Flush();
                        }
                        this.pcapStream.Dispose();
                    }
                }

#if !DEBUG
                catch (Exception ex) {
                    this.pcapStream = null;
                    //this.backgroundStreamReaderCanceller.Cancel();
                    //e.Cancel = true;
                    //e.Result = ex.Message;
                    this.AbortFileRead();
                }
#endif
                //do a callback with this.filename as well as first and last timestamp
                if (this.streamReadCompletedCallback != null && firstFrameTimestamp != DateTime.MinValue && lastFrameTimestamp != DateTime.MinValue)
                {
                    this.streamReadCompletedCallback(framesCount, firstFrameTimestamp, lastFrameTimestamp);
                }
            });

            if (startBackgroundWorkers)
            {
                this.StartBackgroundWorkers();
            }
        }
Пример #28
0
		static void Main () {
			// create the state machine model
			var model = new StateMachine<Player>("model");

			// create the vertices within the model
			var initial = model.CreatePseudoState("initial", PseudoStateKind.Initial);
			var operational = model.CreateState("operational");
			var choice = model.CreatePseudoState("choice", PseudoStateKind.Choice);
			var flipped = model.CreateState("flipped");
			var final = model.CreateFinalState("final");

			var history = operational.CreatePseudoState("history", PseudoStateKind.DeepHistory);
			var stopped = operational.CreateState("stopped");
			var active = operational.CreateState("active").Entry(i => i.EngageHead()).Exit(i => i.DisengageHead());

			var running = active.CreateState("running").Entry(i => i.StartMotor()).Exit(i => i.StopMotor());
			var paused = active.CreateState("paused");

			// create the transitions between vertices of the model
			initial.To(operational).Effect(i => i.DisengageHead()).Effect(i => i.StopMotor());
			history.To(stopped);
			stopped.To(running).When<string>(command => command == "play");
			active.To(stopped).When<string>(command => command == "stop");
			running.To(paused).When<string>(command => command == "pause");
			running.To().When<string>(command => command == "tick").Effect((Player instance) => instance.Count++);
			paused.To(running).When<string>(command => command == "play");
			operational.To(final).When<string>(command => command == "off");
			operational.To(choice).When<string>(command => command == "rand");
			choice.To(operational).Effect(() => Console.WriteLine("- transition A back to operational"));
			choice.To(operational).Effect(() => Console.WriteLine("- transition B back to operational"));
			operational.To(flipped).When<string>(command => command == "flip");
			flipped.To(operational).When<string>(command => command == "flip");

			// validate the model for correctness
			model.Validate();

			// create a blocking collection make events from multiple sources thread-safe
			var queue = new System.Collections.Concurrent.BlockingCollection<Object>();

			// create an instance of the player - enqueue a tick message for the machine while its playing
			var player = new Player(() => queue.Add("tick"));

			// initialises the players initial state (enters the region for the first time, causing transition from the initial PseudoState)
			model.Initialise(player);

			// create a task to capture commands from the console in another thread
			System.Threading.Tasks.Task.Run(() => {
				string command = "";

				while (command.Trim().ToLower() != "exit") {
					queue.Add(command = Console.ReadLine());
				}

				queue.CompleteAdding();
			});

			// write the initial command prompt
			Console.Write("{0:0000}> ", player.Count);

			// process messages from the queue
			foreach (var message in queue.GetConsumingEnumerable()) {
				// process the message
				model.Evaluate(player, message);

				// manage the command prompt
				var left = Math.Max(Console.CursorLeft, 6);
				var top = Console.CursorTop;
				Console.SetCursorPosition(0, top);
				Console.Write("{0:0000}>", player.Count);
				Console.SetCursorPosition(left, top);
			}
		}
Пример #29
0
 public MyQueue()
 {
     this.col = new System.Collections.Concurrent.BlockingCollection <DateTime>(5);
 }
Пример #30
0
      public void Stop()
      {
        inbound?.CompleteAdding();
        outbound?.CompleteAdding();

        read_task?.Wait(1000);
        send_task?.Wait(1000);
        mex_task?.Wait(1000);
        app_read_task?.Wait(1000);

        reader?.Close();
        reader?.Dispose();
        stream?.Dispose();

        //TcpClient.Dispose
        //https://msdn.microsoft.com/en-us/library/dn823304(v=vs.110).aspx
        serverside_client?.Dispose();

        read_task = send_task = mex_task = app_read_task = null;

        inbound?.Dispose();
        outbound?.Dispose();
        inbound = null;
        outbound = null;

        reader = null;
        writer = null;
        stream = null;
        serverside_client = null;
      }
Пример #31
0
    public void Stop()
    {
      inbound?.CompleteAdding();
      outbound?.CompleteAdding();

      read_task?.Wait(1000);
      send_task?.Wait(1000);
      mex_task?.Wait(1000);
      app_read_task?.Wait(1000);

      reader?.Close();
      reader?.Dispose();
      stream?.Dispose();

      //TcpClient.Dispose
      //https://msdn.microsoft.com/en-us/library/dn823304(v=vs.110).aspx
      client?.Dispose();

      read_task = send_task = mex_task = app_read_task = null;

      inbound?.Dispose();
      outbound?.Dispose();
      inbound = null;
      outbound = null;

      reader = null;
      writer = null;
      stream = null;
      client = null;

      Console.WriteLine($"Received msg count: {msgs.Count}\n{msgs.Aggregate(new StringBuilder(), (w, n) => w.AppendFormat("->{0}\n", n))}");
    }
Пример #32
0
    public MsgReceiver3(string host, int port)
    {
      connection_lost = false;
      this.host = host;
      this.port = port;
      client = new System.Net.Sockets.TcpClient();
      inbound = new System.Collections.Concurrent.BlockingCollection<string>();
      outbound = new System.Collections.Concurrent.BlockingCollection<string>();

      msgs = new List<string>();
    }
Пример #33
0
 internal override void Initiate()
 {
     dataExceptionItems = new System.Collections.Concurrent.BlockingCollection <LogModelException>(logCapacity);
     base.Initiate();
 }
Пример #34
0
 public MsgSender(System.Net.Sockets.TcpClient client)
 {
   connection_lost = false;
   serverside_client = client;
   stream = serverside_client.GetStream();
   reader = new System.IO.StreamReader(stream, Encoding.UTF8); //System.IO.BufferedStream? https://msdn.microsoft.com/en-us/library/system.io.bufferedstream(v=vs.110).aspx
   writer = new System.IO.StreamWriter(stream, Encoding.UTF8);
   writer.AutoFlush = true;
   inbound = new System.Collections.Concurrent.BlockingCollection<string>();
   outbound = new System.Collections.Concurrent.BlockingCollection<string>();
   read_task = Task.Run(() => read());
   send_task = Task.Run(() => send());
 }
Пример #35
0
		/// <summary>
		/// opens a recording stream
		/// set a video codec token first.
		/// </summary>
		public void OpenFile(string baseName)
		{
			string ext = Path.GetExtension(baseName);
			if (ext == null || ext.ToLower() != ".jmd")
				baseName = baseName + ".jmd";

			jmdfile = new JMDfile(File.Open(baseName, FileMode.Create), fpsnum, fpsden, audiosamplerate, audiochannels == 2);


			if (moviemetadata != null)
				jmdfile.writemetadata(moviemetadata);

			// start up thread
			// problem: since audio chunks and video frames both go through here, exactly how many zlib workers
			// gives is not known without knowing how the emulator will chunk audio packets
			// this shouldn't affect results though, just performance
			threadQ = new System.Collections.Concurrent.BlockingCollection<Object>(token.numthreads * 2);
			workerT = new System.Threading.Thread(new System.Threading.ThreadStart(threadproc));
			workerT.Start();
			GzipFrameDelegate = new GzipFrameD(GzipFrame);
		}
Пример #36
0
 public void StartCrawling()
 {
     var journalsISSN = new List<string>();
     using (var stream = new FileStream("data//scopus-journals.csv", FileMode.Open))
     using (var reader = new StreamReader(stream))
     {
         var regex = new Regex("\\d{8}");
         var line = string.Empty;
         while ((line = reader.ReadLine()) != null)
         {
             var match = regex.Match(line);
             if (match.Success)
             {
                 journalsISSN.Add(match.Groups[0].Value);
                 //match = match.NextMatch();
             }
         }
     }
     var startYear = 1990;
     var finishYear = 2016;
     var workersCount = 10;
     var tasks = new Task[workersCount];
     System.Collections.Concurrent.BlockingCollection<string> processedArticles = new System.Collections.Concurrent.BlockingCollection<string>();
     System.Collections.Concurrent.BlockingCollection<string> brokenArticles = new System.Collections.Concurrent.BlockingCollection<string>();
     using (ScopusDbContext dbContext = new ScopusDbContext())
     {
         foreach (var article in dbContext.Articles.AsNoTracking())
         {
             processedArticles.Add(article.ScopusID);
         }
     }
     //for (int i = 0; i < workersCount; i++)
     {
         //tasks[i] = Task.Factory.StartNew(() =>
         //{
         var counter = 0;
         var processedEntries = new System.Collections.Concurrent.BlockingCollection<Tuple<string, int>>();
         while (journalsISSN.Count > 0)
         {
             var issn = journalsISSN.First();
             journalsISSN.RemoveAt(0);
             for (int year = finishYear; year >= startYear; year--)
             {
                 var toStart = false;
                 using (ScopusDbContext dbContext = new ScopusDbContext())
                 {
                     if (!dbContext.EntriesDone.Any(e => e.Issn == issn && e.Year == year))
                         toStart = true;
                 }
                 if (toStart)
                 {
                     var startParameters = new Tuple<string, int>(issn, year);
                     tasks[counter] = Task.Factory.StartNew((parameters) =>
                     {
                         var p = (Tuple<string, int>)parameters;
                         var worker = new Worker(processedArticles, brokenArticles);
                         var result = worker.Start(p.Item2, p.Item1);
                         processedEntries.Add(p);
                         return result;
                     }, startParameters);
                     counter++;
                 }
                 if(counter == workersCount)
                 {
                     Task.WaitAll(tasks);
                     for (int i = 0; i < tasks.Length; i++)
                     {
                         var task = (Task<List<Article>>)tasks[i];
                         if(task.Result != null)
                             UploadArticles(task.Result);
                         using (ScopusDbContext dbContext = new ScopusDbContext())
                         {
                             var entry = new EntriesDone()
                             {
                                 Issn = processedEntries.ElementAt(i).Item1,
                                 Year = processedEntries.ElementAt(i).Item2
                             };
                             dbContext.EntriesDone.Add(entry);
                             dbContext.SaveChanges();
                             Console.WriteLine("---" + processedEntries.ElementAt(i).Item1 + " - " +
                                 processedEntries.ElementAt(i).Item2.ToString() + "---");
                         }
                     }
                     counter = 0;
                 }
             }
         }
         //});
     }
 }