public void Start(object buffer)
        {
            ByteBufferOnArray byteBuffer = (ByteBufferOnArray)buffer;

            while (byteBuffer.HasAlmostOneChunk())
            {
                processedChunksCount = 0;
                dict.Clear();
                processingThreads.Clear();
                for (int i = 0; i < threadCount; i++)
                {
                    if (byteBuffer.HasAlmostOneChunk())
                    {
                        try
                        {
                            byte[] chunk  = byteBuffer.GetChunk();
                            long   number = currentChunk;

                            Thread thread = new Thread(() => ProcessChunk(chunk, number));
                            processingThreads.Add(thread);
                            thread.Start();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
                        }

                        processedChunksCount++;
                        currentChunk++;
                    }
                }
                foreach (Thread thread in processingThreads)
                {
                    thread.Join();
                }

                int id = 0;
                foreach (byte[] chunk in GetOrderedChunks())
                {
                    ConsoleOutputter.Write(currentChunk - processedChunksCount + id, chunk);
                    id++;
                }
            }
            if (byteBuffer.NotProcessedBytesCount() > 0)
            {
                ConsoleOutputter.Write(currentChunk, hasher.ComputeHash(byteBuffer.GetLastChunk()));
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                String fileName  = args[0];
                int    chunkSize = Int32.Parse(args[1]);

                if (chunkSize <= 0)
                {
                    throw new InvalidValueException("Invalid chunk size value, must be natural number");
                }

                ByteBufferOnArray byteBuffer1 = new ByteBufferOnArray(chunkSize);
                ByteBufferOnArray byteBuffer2 = new ByteBufferOnArray(chunkSize);
                ByteBufferOnArray byteBuffer  = byteBuffer1;
                bool firstBuffer = true;

                Processor processor = new Processor(Environment.ProcessorCount, new SHA256Hasher());
                Thread    processingBufferThread = new Thread(processor.Start);

                using (FileStream file = File.OpenRead(fileName))
                {
                    FileByteInputer inputer = new FileByteInputer(file);
                    while (inputer.HasNext())
                    {
                        if (byteBuffer.HasSpaceForChunk(inputer.GetChunkSize()))
                        {
                            byteBuffer.InsertNext(inputer.Next());
                        }
                        else if (byteBuffer.BytesForAlliquotSize() != 0)
                        {
                            byteBuffer.InsertByte(inputer.NextByte());
                        }
                        else
                        {
                            if (processingBufferThread.IsAlive)
                            {
                                processingBufferThread.Join();
                            }
                            processingBufferThread = new Thread(processor.Start);
                            processingBufferThread.Start(byteBuffer);

                            byteBuffer = byteBuffer2;
                            if (!firstBuffer)
                            {
                                byteBuffer = byteBuffer1;
                            }
                            firstBuffer = !firstBuffer;

                            byteBuffer.Clear();
                        }
                    }
                    if (processingBufferThread.IsAlive)
                    {
                        processingBufferThread.Join();
                    }
                    processor.Start(byteBuffer);
                }
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("Not enought arguments: Need path to file and processed chunk size");
            }
            catch (FormatException)
            {
                Console.WriteLine("Second parameter must be an integer");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File not found");
            }
            catch (InvalidValueException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }
        }