Esempio n. 1
0
        private bool AddFile(FileDescriptor file, bool force)
        {
            bool hashed = false;
            Exception hashException = null;
            int loops = 0;

            if (!force && (ContainsFile(file)))
            {
                Console.WriteLine("{0} already mounted. Ignoring.", file.FileFullName);
                return false;
            }

            //Hashing operations

            Console.WriteLine("Preparing \"{0}\"", file.FileFullName);

            file.HashGenerationCompleted += new EventHandler(delegate(object sender, EventArgs e)
            {
                hashed = true;
            });

            file.HashGenerationFailed += new EventHandler<ExceptionEventArgs>(delegate(object sender, ExceptionEventArgs e)
            {
                hashException = e.ThrownException;
            });

            file.GenerateHash();

            Console.Write("Hashing ..");

            while (!hashed && hashException == null)
            {
                if (loops++ % 10 == 0)
                {
                    Console.Write(".");
                }

                Thread.Sleep(100);
            }

            Console.WriteLine();

            if (hashException != null)
            {
                Console.WriteLine(hashException.Message);
                Console.WriteLine("Unable to hash {0}. Ignoring", file.FileName);

                return false;
            }

            if(_cachedDescriptors.Count(d => d.Value.Hash == file.Hash) > 0)
            {
                Console.WriteLine("Identically hashed file already mounted. Ignoring.");
                return false;
            }

            //Caching operations

            EnsureRequiredWatcherExists(file.DirectoryName);

            EnsureRelevantFileListExists(file.DirectoryName);

            if (!_relevantFiles[file.DirectoryName].Contains(file.FileFullName))
            {
                _relevantFiles[file.DirectoryName].Add(file.FileFullName);
            }

            //mounting

            bool fileAvailable = false;

            EventHandler<FileListModificationEventArgs> handler = delegate(object sender, FileListModificationEventArgs e)
            {
                StringBuilder builder = new StringBuilder();

                foreach (FileModificationDescriptor item in e.Modifications)
                {
                    if (item.Mode == FileModificationMode.New)
                    {
                        builder.AppendFormat("Available File: \"{0}\"{1}", item.FileFullName, Environment.NewLine);
                    }
                }

                Console.Write(builder.ToString());

                _cachedDescriptors[e.Modifications[0].FileFullName] = e.Modifications[0].ToFileDescriptor();

                fileAvailable = true;
            };

            Client.FileListUpdateReceived += new EventHandler<FileListModificationEventArgs>(handler);

            Client.AddFiles(new FileDescriptor[] { file });

            DateTime then = DateTime.Now;

            while (!fileAvailable && ((TimeSpan)(DateTime.Now - then)).TotalSeconds < 30)
            {
                Thread.Sleep(100);
            }

            if (!fileAvailable)
            {
                ConsoleColor oldColour = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;

                Console.WriteLine("File Add Request not responded after 30 seconds. Check network and consider restarting.");
                Console.WriteLine("Terminating request ...");
                Console.ForegroundColor = oldColour;

                return false;
            }

            Client.FileListUpdateReceived -= new EventHandler<FileListModificationEventArgs>(handler);

            return true;
        }
Esempio n. 2
0
        public static FileDescriptor Create(FileInfo file, bool generateHash)
        {
            FileDescriptor descriptor = new FileDescriptor();

            descriptor.FileFullName = file.FullName;

            descriptor.FileName = file.Name;

            descriptor._file = file;

            descriptor.DirectoryName = file.DirectoryName;

            if (file.Exists)
            {
                descriptor._size = file.Length;
            }

            if (generateHash)
            {
                descriptor.GenerateHash();
            }

            return descriptor;
        }