示例#1
0
        private void Disconnected()
        {
            Receive <Connect>((cmd) =>
            {
                _connection = _clientFactory.CreateSftpClient();
                _connection.Connect();

                Become(Connected);
            });
        }
示例#2
0
        private void Disconnected()
        {
            Receive <ListDirectory>((cmd) =>
            {
                this.Stash.Stash();

                _connection = _clientFactory.CreateSftpClient();
                _connection.Connect();

                this.Stash.UnstashAll();
                StartIdlePeriod();
                Become(Connected);
            });
        }
 private void Connect()
 {
     _connection = _clientFactory.CreateSftpClient();
     _connection.Connect();
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writeMode"></param>
        /// <returns></returns>
        public override bool Put(DeliveryWriteMode writeMode)
        {
            if (SaveFileCopy)
            {
                string filename = Path.Combine(Context.BaseDirectory, string.Format("SftpDeliveryFile-{0:yyyyMMdd-HHmmss}.csv", DateTime.Now));
                _log.Info(m => m("Saving copy of SFTP upload file to '{0}'", filename));
                Source.ToFile(filename, Source.Length);
            }

            _log.Trace(m => m("Beginning SFTP communication."));
            using (Stream dataStream = new MemoryStream(Source))
            {
                _log.Trace(m => m("Getting SFTP client object..."));
                _sftp = GetClient();

                using (_sftp)
                {
                    try
                    {
                        _log.Debug(m => m("Connecting to SFTP server ({0}@{1})...", Username, Hostname));
                        _sftp.Connect();

                        _log.Trace(m => m("Does file exist at destination (and Overwrite is disabled)?"));
                        if (DeliveryWriteMode.Overwrite != writeMode && _sftp.Exists(Destination))
                        {
                            if (DeliveryWriteMode.Exception == writeMode)
                            {
                                _log.Info(m => m("Destination file exists and Overwrite flag has not been specified: '{0}'", Destination));
                                throw new SftpPermissionDeniedException("File already exists and Overwrite is not enabled.");
                            }
                            // DeliverWriteMode.Ignore
                            _log.Info(m => m("Destination file exists and flag is set to Ignore. Skipping.\n{0}", Destination));
                            return false;
                        }

                        _log.Info(m => m("Uploading file via SFTP ({0}@{1}:{2}) WriteMode: '{3}'", Username, Hostname, Destination, writeMode.ToString("F")));
                        _sftp.UploadFile(dataStream, Destination, (DeliveryWriteMode.Overwrite == writeMode));
                        // if nothing blew up we succeeded (?)
                        return true;
                    }
                    catch (SshConnectionException ex)
                    {
                        _log.Warn(m => m("Unable to establish an SFTP connection to '{0}@{1}'\n{2}", Username, Hostname, ex));
                        throw;
                    }
                    catch(SftpPermissionDeniedException ex)
                    {
                        _log.Warn(m => m("Failed to upload file to '{0}@{1}:{2}'\n{3}", Username, Hostname, Destination, ex));
                        throw;
                    }
                    catch(SshException ex)
                    {
                        _log.Warn(m => m("SSH server returned the following error '{0}'\n{1}", ex.Message, ex));
                        throw;
                    }
                    finally
                    {
                        _log.Debug(m => m("Disconnecting from SFTP server..."));
                        if (_sftp != null && _sftp.IsConnected)
                        {
                            _sftp.Disconnect();
                        }
                    }
                }
            }
            return false;
        }