diff --git a/File.cs b/File.cs index 46f7611..6dceafc 100644 --- a/File.cs +++ b/File.cs @@ -54,6 +54,8 @@ namespace Dizzy type = new FileType (path); this.user = user; + this.path = path; + Console.WriteLine("Path set to: {0}", this.path); string[] pieces = path.Split ('/'); name = pieces[pieces.Length - 1]; diff --git a/FileTreeView.cs b/FileTreeView.cs index 74d47fa..4ee55e2 100644 --- a/FileTreeView.cs +++ b/FileTreeView.cs @@ -74,7 +74,7 @@ namespace Dizzy { TreeIter iter = new TreeIter (); fileListStore.GetIter(out iter, path); - File f = new File((string) fileListStore.GetValue(iter, 0), + File f = new File((string) fileListStore.GetValue(iter, 2), (string) fileListStore.GetValue(iter, 1)); return f; } diff --git a/MainWindow.cs b/MainWindow.cs index 72938a8..c487111 100644 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -38,6 +38,7 @@ public partial class MainWindow : Gtk.Window protected virtual void OnRowActivated (object o, Gtk.RowActivatedArgs args) { + Console.WriteLine("row_activated called"); File f = this.files.GetFileFromPath (args.Path); // this.tasks.AddTask (f.name, 24); this.protocol.Download(f, ref tasks); diff --git a/Protocol.cs b/Protocol.cs index 60043c0..60a6c5d 100644 --- a/Protocol.cs +++ b/Protocol.cs @@ -21,7 +21,15 @@ namespace Dizzy // I dati del nostro povero utente. string user, password; + // I transfer attivi + ArrayList transfers = new ArrayList (); + + public delegate void Del (string src, string dest, + int transferredBytes, int totalBytes, string message); + + ArrayList fileList = new ArrayList (); + ArrayList threads = new ArrayList (); Thread updater; SFTPConnection sftpUpdater; @@ -85,19 +93,45 @@ namespace Dizzy public void Disconnect () { + foreach(FileTransfer t in this.transfers) + { + t.Stop (); + } + foreach(Thread t in this.threads) + { + if(t.IsAlive) + t.Abort (); + } + if (this.updater.IsAlive) { this.updater.Abort (); } this.sftpUpdater.Disconnect (); } - + public void Download(File f, ref TaskTreeView tasks) { - SFTPConnection sftp = new SFTPConnection(this.user, this.password); - sftp.Connect (); + ArrayList args = new ArrayList (); + args.Add (f); + args.Add (tasks); + Thread t = new Thread (new ParameterizedThreadStart(_Download)); + t.Start (args); + this.threads.Add (t); + } + + protected void _Download(object args) + { + // Questi cast sono piuttosto brutali ma posso passare + // solo un oggetto a Download e quindi è il meglio che + // mi riesce di fare. + File f = (File) ((ArrayList) args)[0]; + TaskTreeView tasks = (TaskTreeView) ((ArrayList) args)[1]; + Console.WriteLine (" => Download starting"); + FileTransfer transfer = new FileTransfer (f, ref tasks, this.user, this.password); + this.transfers.Add (transfer); } } @@ -106,9 +140,15 @@ namespace Dizzy { public Sftp sftp; + public delegate void SFTPEvent (string src, string dest, int transferredBytes, + int totalBytes, string message); + + public event SFTPEvent TransferStarted; + public event SFTPEvent TransferProgress; + public event SFTPEvent TransferStopped; - public delegate void Delegate (string src, string dest, - int transferredBytes, int totalBytes, string message); + public delegate void Del (string src, string dest, + int transferredBytes, int totalBytes, string message); public SFTPConnection (string user, string password) { @@ -117,20 +157,30 @@ namespace Dizzy this.sftp = new Sftp ("poisson.phc.unipi.it", user); this.sftp.Password = password; + this.sftp.OnTransferStart += new FileTransferEvent(this.TransferStartedHandler); + this.sftp.OnTransferProgress += new FileTransferEvent(this.TransferProgressHandler); + this.sftp.OnTransferEnd += new FileTransferEvent(this.TransferStoppedHandler); + } - public void SetTransferHandlers(Delegate OnTransferStart, - Delegate OnTransferProgress, - Delegate OnTransferEnd) + private void TransferStartedHandler(string src, string dest, int transferredBytes, + int totalBytes, string message) { - - this.sftp.OnTransferStart += new FileTransferEvent (OnTransferStart); - this.sftp.OnTransferProgress += new FileTransferEvent (OnTransferProgress); - this.sftp.OnTransferEnd += new FileTransferEvent (OnTransferEnd); - + Console.WriteLine (" => TransferStarted"); + TransferStarted(src, dest, transferredBytes, totalBytes, message); } + private void TransferProgressHandler(string src, string dest, int transferredBytes, + int totalBytes, string message) + { + TransferProgress(src, dest, transferredBytes, totalBytes, message); + } + private void TransferStoppedHandler(string src, string dest, int transferredBytes, + int totalBytes, string message) + { + TransferStopped(src, dest, transferredBytes, totalBytes, message); + } public void Connect() { @@ -178,16 +228,88 @@ namespace Dizzy user = folder; if(user.Contains("/")) user = user.Substring(0, user.IndexOf("/")); - f = new File(folder + "/" + entry, user ); + f = new File("/nobackup/" + folder + "/" + entry, user ); matches.Add(f); } } return matches; } + public void Download(File f) + { + Console.WriteLine (" => Getting {0}", f.name); + try + { + Console.WriteLine (f.path); + this.sftp.Get (f.path, "/home/leonardo/" + f.name); + } + catch(Exception e) + { + Console.WriteLine("Error in Get... "); + Console.WriteLine(e.Message); + } + } + public void Disconnect () { sftp.Close (); } } + + public class FileTransfer + { + + TaskTreeView tasks; + File file; + Gtk.TreeIter iter; + + SFTPConnection sftp; + + public delegate void Handler(string src, string dest, int transferredBytes, + int totalBytes, string message); + + public FileTransfer (File f, ref TaskTreeView tasks, string user, string password) + { + // Connessione al server + this.sftp = new SFTPConnection (user, password); + this.sftp.Connect (); + + this.tasks = tasks; + this.file = f; + + sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted); + sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress); + sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped); + + Console.WriteLine(" => Starting sftp command"); + sftp.Download (this.file); + } + + public void Stop () + { + this.sftp.Disconnect (); + } + + public void OnTransferStarted (string src, string dest, int transferredBytes, + int totalBytes, string message) + { + Console.WriteLine(" => TransferStarted received"); + this.iter = this.tasks.AddTask(file.name, 0); + } + + public void OnTransferProgress(string src, string dest, int transferredBytes, + int totalBytes, string message) + { + if(message != "") + Console.WriteLine(message); + this.tasks.SetProgress(this.iter, 100 * transferredBytes / totalBytes); + } + + public void OnTransferStopped (string src, string dest, int transferredBytes, + int totalBytes, string message) + { + this.tasks.DeleteRow (this.iter); + this.Stop (); + } + } } diff --git a/TaskTreeView.cs b/TaskTreeView.cs index 7894fc6..a79ce13 100644 --- a/TaskTreeView.cs +++ b/TaskTreeView.cs @@ -48,21 +48,31 @@ namespace Dizzy progressColumn.AddAttribute (progressRenderer, "value", 1); } + // Ritorna un TreeIter all'elemento aggiunto public TreeIter AddTask (string name, int progress) { - TreeIter iter = tasklist.AppendValues (name, progress); - return iter; + lock (this) + { + TreeIter iter = tasklist.AppendValues (name, progress); + return iter; + } } public void SetProgress (TreeIter iter, int progress) { - tasklist.SetValue(iter, 1, progress); + lock (this) + { + tasklist.SetValue (iter, 1, progress); + } } public void DeleteRow(TreeIter iter) { - tasklist.Remove(ref iter); + lock (this) + { + tasklist.Remove(ref iter); + } } } }