Ejemplo n.º 1
0
 public static void CreateCompressionTasks(int numOfTasks, ThreadPool tp, StreamManager sm)
 {
     for (int i = 0; i < numOfTasks; i++)
     {
         Block block = new Block(StreamManager.BlockSize);
         Task  task  = new Task(block, sm);
         tp.AddTaskToQueue(task);
     }
 }
Ejemplo n.º 2
0
 public static void CreateDecompressionTasks(ThreadPool tp, StreamManager sm)
 {
     sm.ReadBlockSizes();
     while (sm.ReadBlockSizesStack.Count != 0)
     {
         int   blockSize = sm.ReadBlockSizesStack.Pop();
         Block block     = new Block(blockSize);
         Task  task      = new Task(block, sm);
         tp.AddTaskToQueue(task);
     }
 }
Ejemplo n.º 3
0
        private int RunProgram()
        {
            using (var reader = new FileStream(ApplicationSettings.InFile, FileMode.Open, FileAccess.Read))
            {
                using (var writer = new FileStream(ApplicationSettings.OutFile, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    //Create input/output manager
                    StreamManager sm         = new StreamManager(reader, writer);
                    int           numOfTasks = 0;

                    //Get appropriate number of tasks that we'll nedd to run
                    if (ApplicationSettings.Compress)
                    {
                        numOfTasks = sm.GetNumberOfBlocksForCompression();
                    }
                    else if (ApplicationSettings.Decompress)
                    {
                        numOfTasks = sm.GetNumberOfBlocksForDecompression();
                    }

                    //Start the pool
                    ThreadPool tp = new ThreadPool(numOfTasks);
                    tp.Start();

                    //Create appropriate tasks and add them to the queue so that threads can pick tasks up
                    if (ApplicationSettings.Compress)
                    {
                        Compressor.CreateCompressionTasks(numOfTasks, tp, sm);
                    }
                    else if (ApplicationSettings.Decompress)
                    {
                        Decompressor.CreateDecompressionTasks(tp, sm);
                    }

                    //Play some animation while proccessing is happening
                    while (!tp.Finished && tp.Started)
                    {
                        Thread.Sleep(500);
                        Console.Write("\rProcessing \\ ");
                        Thread.Sleep(500);
                        Console.Write("\rProcessing | ");
                        Thread.Sleep(500);
                        Console.Write("\rProcessing / ");
                        Thread.Sleep(500);
                        Console.Write("\rProcessing | ");
                    }
                    Console.WriteLine();

                    //Add block sizes and mount of block to the end of the file
                    if (ApplicationSettings.Compress)
                    {
                        sm.WriteBlockSizes();
                    }

                    //Stop the pool
                    tp.Stop();

                    //Assert the results
                    if (tp.ExceptionHappened)
                    {
                        return(1);
                    }
                    else
                    {
                        return(0);
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public Task(Block block, StreamManager streamManager)
 {
     this.block         = block;
     this.streamManager = streamManager;
 }