public FileModificationDescriptor(FileDescriptor descriptor, FileModificationMode mode) { Exists = descriptor.Exists; FileFullName = descriptor.FileFullName; Hash = descriptor.Hash; Mode = mode; OriginId = descriptor.OriginId; }
public FileWrapper(FileDescriptor file) { Id = Guid.NewGuid().ToString(); File = file; File.HashGenerationCompleted += new EventHandler(File_HashGenerationCompleted); File.HashGenerationStarting += new EventHandler(File_HashGenerationStarting); }
public AvailableFile(ClientInstance client, FileDescriptor descriptor) : base(descriptor) { _state = FileState.Available; _client = client; _percent = 0d; _client.FileRequestResponseReceived += new EventHandler<ObviousCode.Interlace.BitTunnelLibrary.Events.FileRequestResponseEventArgs>(_client_FileRequestResponseReceived); _client.FileTransferProgressed += new EventHandler<ObviousCode.Interlace.BitTunnelLibrary.Events.FileTransferEventArgs>(_client_FileTransferProgressed); _client.FileTransferCompleted += new EventHandler<ObviousCode.Interlace.BitTunnelLibrary.Events.FileTransferCompletedEventArgs>(_client_FileTransferCompleted); _client.FileTransferFailed += new EventHandler<ObviousCode.Interlace.BitTunnelLibrary.Events.FileDescriptorEventArgs>(_client_FileTransferFailed); }
private void AddFileToList(FileDescriptor file) { Invoke((MethodInvoker)delegate { if (_helper.GetListViewItem(file.FileFullName, ListViewHelper.ComparisonKey.FileFullName) != null) return; _helper.AddFileToList(CreateSharedFile(file), file.FileName, file.FileFullName, string.Format("{0} kb", Math.Round((double)(file.Size / 1024), 2)), "" ); }); }
internal static bool TryCreate(FileDescriptor file, out FileModificationDescriptor modification) { try { modification = new FileModificationDescriptor(file, FileModificationMode.New); return true; } catch (Exception e) { modification = null; return false; } }
private void AddFileToFileList(FileDescriptor file) { Invoke((MethodInvoker)delegate { if (_helper.GetListViewItem(file.Hash) != null) return; AvailableFile availableFile = CreateAvailableFile(file); _helper.AddFileToList( availableFile, file.FileName, GetSizeString(file.Size), availableFile.State, file.Hash ); }); }
public void SendReadyToReceiveFile(FileDescriptor file, string negotiatorId, long chunkIndex) { InternalSendFileRequestResponse(file, FileRequestMode.ReadyToReceive, negotiatorId, chunkIndex); }
public void SendFileAvailabilityRequest(FileDescriptor file, long chunkIndex) { InternalSendFileRequestResponse(file, FileRequestMode.Request, null, chunkIndex); }
public void SendFileRequestResponse(FileDescriptor file, FileRequestMode response) { InternalSendFileRequestResponse(file, response, null, 0); }
private bool FilesIsEqualToRequestedFile(FileDescriptor fileDescriptor, FileInfo requestedFile) { try { byte[] requestedFileStream; byte[] originalFileStream; using (FileStream stream = requestedFile.OpenRead()) { requestedFileStream = new byte[stream.Length]; stream.Read(requestedFileStream, 0, (int)stream.Length); } using (FileStream stream = new FileStream(fileDescriptor.FileFullName, FileMode.Open, FileAccess.Read)) { originalFileStream = new byte[stream.Length]; stream.Read(originalFileStream, 0, (int)stream.Length); } if (requestedFileStream.Length != originalFileStream.Length) return false; for (int i = 0; i < requestedFileStream.Length; i++) { if (requestedFileStream[i] != originalFileStream[i]) return false; } return true; } catch (Exception ex) { throw ex; } }
//Negotiator Id only required (currently) in ReadyToReceive, so Protocol -> Factory -> Service knows where to send received chunks to private void InternalSendFileRequestResponse(FileDescriptor file, FileRequestMode response, string negotiatorId, long chunkIndex) { FileRequestHeader header = new FileRequestHeader(); header.Response = response; header.Id = negotiatorId; header.ChunkIndex = chunkIndex; using (FileRequestMessage message = new FileRequestMessage(header)) { AddValueToMessage(message, file); SendMessage(message); } }
public FileRequestResponseEventArgs(FileDescriptor file, FileRequestMode response) { File = file; Response = response; }
public int CountOf(FileDescriptor file) { if (string.IsNullOrEmpty(file.Hash)) { throw new InvalidOperationException("File must contain a valid hash to compare for Count"); } return CountOf(file.Hash); }
internal void RequestFile(FileDescriptor fileDescriptor, long chunkIndex, Action<FileDescriptor, FileRequestMode> sendResponseCallback, Action<FileDescriptor, FileChunkMessage> sendChunkCallback) { _service.RequestFile(fileDescriptor, chunkIndex, sendResponseCallback, sendChunkCallback); }
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; }
private void RenameFile(FileDescriptor file) { if (InvokeRequired) { Invoke((MethodInvoker) delegate { RenameFile(file); } ); } else { ListViewItem item = _helper.GetListViewItem(file.Hash); item.Text = file.FileName; item.Tag = CreateAvailableFile(file); } }
public void RequestFile(FileDescriptor file) { ClientFactory.SendInitialFileRequest(file); }
private AvailableFile CreateAvailableFile(FileDescriptor file) { AvailableFile availableFile = new AvailableFile(_client, file); availableFile.PropertyChanged += new PropertyChangedEventHandler(availableFile_PropertyChanged); return availableFile; }
public void RequestFile(FileDescriptor file) { if (_localFiles.Contains(file)) { if (FileTransferCompleted != null) { FileTransferCompleted(this, new FileTransferCompletedEventArgs(file.Hash, _localFiles[file.Hash].FileFullName)); } } else { (Connection.Service as BitTunnelClientService).RequestFile(file); } }
public FileDescriptorEventArgs(FileDescriptor descriptor) { File = descriptor; }
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; }
private SharedFile CreateSharedFile(FileDescriptor fileDescriptor) { SharedFile shared = new SharedFile(fileDescriptor); shared.PropertyChanged += new PropertyChangedEventHandler(shared_PropertyChanged); return shared; }
/// <summary> /// Create FileDescriptor without Origin Id or File full path name /// </summary> internal static FileDescriptor CreateGenericReference(FileDescriptor descriptor) { FileDescriptor newInstance = Serialiser.Restore<FileDescriptor>( Serialiser.Tunnel(descriptor) ); newInstance.OriginId = null; newInstance.FileName = null; return newInstance; }
public SharedFile(FileDescriptor file) : base(file) { }
private void SendFileChunk(FileDescriptor description, FileChunkMessage chunk) { AddDataToMessage(chunk, chunk.Chunk); SendMessage(chunk); }
internal void RequestFile(FileDescriptor file, long chunkIndex, Action<FileDescriptor, FileRequestMode> sendResponseCallback, Action<FileDescriptor, FileChunkMessage> sendChunkCallback) { FileRequestEventArgs e = new FileRequestEventArgs(file); //This is giving a chance for the Server application to globally block a file - it has nothing to do with the client's desire to server the file if (FileRequested != null) { FileRequested(this, e); } if (!e.Allow) { sendResponseCallback(file, FileRequestMode.NotAvailable); return; } if (ServerFactory.Protocols.Count(p => p.Files.Contains(file)) == 0) { sendResponseCallback(file, FileRequestMode.NotAvailable); return; } //Negotiate File Transfer _activeNegotiators.Add( new FileServerNegotiator(ServerFactory, file, sendResponseCallback, sendChunkCallback)); _activeNegotiators[_activeNegotiators.Count - 1].TimedOut += new EventHandler(FileServiceNegotiator_TimedOut); _activeNegotiators[_activeNegotiators.Count - 1].Settings = _settings; _activeNegotiators[_activeNegotiators.Count - 1].Negotiate(chunkIndex); }
public bool AddFile(FileDescriptor file) { return AddFile(file, false); }
public FileRequestEventArgs(FileDescriptor descriptor) : base(descriptor) { //By default, allow. If this is a security risk, then switch here Allow = true; }
private bool ContainsFile(FileDescriptor file) { if (_relevantFiles.ContainsKey(file.DirectoryName)) { return ( _relevantFiles[file.DirectoryName].Contains(AllFilesTag) || _relevantFiles[file.DirectoryName].Contains(file.FileFullName)); } return false; }
private void PrepareForPendingFile(FileDescriptor fileDescriptor) { }