コード例 #1
0
        void PublishFile(ModifiedFile mf)
        {
            DateTime startTime = DateTime.Now;

            if (String.IsNullOrWhiteSpace(_sourceFolder))
            {
                log(0, "Please choose a source folder");
                return;
            }
            Match remoteMatch = rxRemote.Match(_remoteFolder);

            if (remoteMatch.Groups.Count < 4)
            {
                log(0, "Remote path does not contain a recognized protocol. ", _remoteFolder);
            }
            else
            {
                string protocol = remoteMatch.Groups[2].Value;
                string path     = remoteMatch.Groups[3].Value;
                string destPath = mf.name;
                if (protocol == "scp")
                {
                    destPath = destPath.Replace(Path.DirectorySeparatorChar, '/');
                }
                if ((path.EndsWith("/") || path.EndsWith(Path.DirectorySeparatorChar.ToString())) && (mf.name.StartsWith(Path.DirectorySeparatorChar.ToString())))
                {
                    destPath = destPath.Substring(1);
                }
                destPath = path + destPath;
                bool success = false;
                switch (protocol)
                {
                case "scp":
                    ConsoleExec ce = new ConsoleExec();
                    ce.Start(GetScpPath(), "-batch -q " + ce.QuoteArg(_sourceFolder + mf.name) + " " + destPath, _sourceFolder);
                    while ((!ce.GetCompleted()) && (DateTime.Now - startTime < timeout))
                    {
                        ce.Pump((output) => { log(0, "SCP:{0}", output); },
                                (output) => { log(0, "SCP ERROR:{0}", output); });
                        System.Threading.Thread.Sleep(100);
                        Application.DoEvents();
                    }

                    ce.Pump((output) => { log(0, "SCP:{0}", output); },
                            (output) => { log(0, "SCP ERROR:{0}", output); });

                    mf.publish = false;
                    if (!ce.GetCompleted())
                    {
                        //send this into a background worker
                        //BackgroundWorker bgw = new BackgroundWorker();
                        //bgw.DoWork += (sender,e) => {
                        bool     reported    = false;
                        TimeSpan longTimeout = TimeSpan.FromMilliseconds(timeout.TotalMilliseconds * 3);
                        while (!ce.GetCompleted())
                        {
                            if ((!reported) && (DateTime.Now - startTime > longTimeout))
                            {
                                log(0, "SCP Timed out after {0} {1}", longTimeout, destPath);
                                reported = true;
                            }
                            ce.Pump((output) => { log(0, "SCP:{0}", output); },
                                    (output) => { log(0, "SCP ERROR:{0}", output); });

                            Application.DoEvents();
                            System.Threading.Thread.Sleep(100);
                        }
                        //};
                        //bgw.RunWorkerCompleted += (sender, e) => {

                        log(ce.GetExitCode() == 0 ? -1 : 0, "scp exited with code {0}", ce.GetExitCode());
                        success = ce.GetExitCode() == 0;
                        log(0, " {1} File {0}", mf.name, success ? "Published" : "Failed to Publish");
                        log(ce.GetExitCode() == 0 ? -1 : 0, "scp exited with code {0}", ce.GetExitCode());
                        success   = ce.GetExitCode() == 0;
                        mf.status = success ? ModifiedFile.Status.Success : ModifiedFile.Status.Failure;
                        //bgw = null;
                        ce = null;
                        mf = null;
                        //};
                        //bgw.RunWorkerAsync();
                        Application.DoEvents();
                        return;
                    }
                    else
                    {
                        log(ce.GetExitCode() == 0?-1:0, "scp exited with code {0}", ce.GetExitCode());
                        success = ce.GetExitCode() == 0;

                        mf.status = success ? ModifiedFile.Status.Success : ModifiedFile.Status.Failure;
                        Application.DoEvents();
                    }
                    break;

                default:
                    log(0, "Only scp protocol is supported at this time");
                    break;
                }
            }
        }
コード例 #2
0
ファイル: SyncSettings.cs プロジェクト: dbremner/KeepSync
        void PublishFile(ModifiedFile mf)
        {
            DateTime startTime = DateTime.Now;
            if (String.IsNullOrWhiteSpace(_sourceFolder)) {
                log(0, "Please choose a source folder");
                return;
            }
            Match remoteMatch = rxRemote.Match(_remoteFolder);
            if (remoteMatch.Groups.Count < 4) {
                log(0, "Remote path does not contain a recognized protocol. ", _remoteFolder);
            } else {
                string protocol = remoteMatch.Groups[2].Value;
                string path = remoteMatch.Groups[3].Value;
                string destPath = mf.name;
                if (protocol == "scp") {
                    destPath = destPath.Replace(Path.DirectorySeparatorChar, '/');
                }
                if ((path.EndsWith("/") || path.EndsWith(Path.DirectorySeparatorChar.ToString())) && (mf.name.StartsWith(Path.DirectorySeparatorChar.ToString()))) {
                    destPath = destPath.Substring(1);
                }
                destPath = path + destPath;
                bool success = false;
                switch (protocol) {
                    case "scp":
                        ConsoleExec ce = new ConsoleExec();
                        ce.Start(GetScpPath(), "-batch -q " + ce.QuoteArg(_sourceFolder + mf.name) + " " + destPath, _sourceFolder);
                        while ((!ce.GetCompleted()) && (DateTime.Now-startTime < timeout))  {
                            ce.Pump((output)=>{log(0,"SCP:{0}",output);},
                                    (output)=>{log(0,"SCP ERROR:{0}",output);});
                            System.Threading.Thread.Sleep(100);
                            Application.DoEvents();
                        }

                        ce.Pump((output)=>{log(0,"SCP:{0}",output);},
                                (output)=>{log(0,"SCP ERROR:{0}",output);});

                        mf.publish = false;
                        if (!ce.GetCompleted()) {
                            //send this into a background worker
                            //BackgroundWorker bgw = new BackgroundWorker();
                            //bgw.DoWork += (sender,e) => {
                                bool reported = false;
                                TimeSpan longTimeout = TimeSpan.FromMilliseconds(timeout.TotalMilliseconds* 3);
                                while (!ce.GetCompleted()) {
                                    if ((!reported) && (DateTime.Now - startTime > longTimeout)) {
                                        log(0, "SCP Timed out after {0} {1}", longTimeout,destPath);
                                        reported = true;
                                    }
                                    ce.Pump((output) => { log(0, "SCP:{0}", output); },
                                            (output) => { log(0, "SCP ERROR:{0}", output); });

                                    Application.DoEvents();
                                    System.Threading.Thread.Sleep(100);
                                }
                            //};
                            //bgw.RunWorkerCompleted += (sender, e) => {

                                log(ce.GetExitCode() == 0 ? -1 : 0, "scp exited with code {0}", ce.GetExitCode());
                                success = ce.GetExitCode() == 0;
                                log(0, " {1} File {0}", mf.name, success ? "Published" : "Failed to Publish");
                                log(ce.GetExitCode() == 0 ? -1 : 0, "scp exited with code {0}", ce.GetExitCode());
                                success = ce.GetExitCode() == 0;
                                mf.status = success ? ModifiedFile.Status.Success : ModifiedFile.Status.Failure;
                                //bgw = null;
                                ce = null;
                                mf = null;
                            //};
                            //bgw.RunWorkerAsync();
                                Application.DoEvents();
                            return;
                        } else {
                            log(ce.GetExitCode()==0?-1:0, "scp exited with code {0}", ce.GetExitCode());
                            success = ce.GetExitCode() == 0;

                            mf.status = success ? ModifiedFile.Status.Success : ModifiedFile.Status.Failure;
                            Application.DoEvents();
                        }
                    break;
                    default:
                        log(0, "Only scp protocol is supported at this time");
                        break;
                }
            }
        }