-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6163999
commit 450ccd9
Showing
11 changed files
with
380 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using log4net; | ||
|
||
namespace FolderWatch.Impl | ||
{ | ||
class Flow | ||
{ | ||
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
|
||
private readonly string _name; | ||
private readonly Host _sourceHost; | ||
private readonly Host _targetHost; | ||
private string _sourceFolder; | ||
private string _targetFolder; | ||
|
||
public Flow(Dictionary<string, Host> hosts, FlowElement flowSetting) | ||
{ | ||
_name = flowSetting.Name; | ||
_sourceHost = hosts[flowSetting.SourceName]; | ||
_targetHost = hosts[flowSetting.TargetName]; | ||
_sourceFolder = flowSetting.SourceFolder; | ||
_targetFolder = flowSetting.TargetFolder; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return string.Format("Name: {0} Source: {1} Target: {2}", _name, _sourceHost.Name, _targetHost.Name); | ||
} | ||
|
||
public void Run() | ||
{ | ||
int numberOfFiles; | ||
_sourceHost.Download(_sourceFolder, _targetFolder, out numberOfFiles); | ||
Log.InfoFormat("Number of files: {0}", numberOfFiles); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FolderWatch.Impl; | ||
using FolderWatch.Settings; | ||
using Starksoft.Net.Ftp; | ||
using log4net; | ||
|
@@ -47,7 +48,33 @@ public FolderWatch(Configuration config) | |
StringEncryption.EncryptPassword(config, folderWatchSection); | ||
} | ||
|
||
timer = new Timer(Callback, timer, Timeout.Infinite, Timeout.Infinite); | ||
|
||
// Get list of all hosts | ||
var hosts = new Dictionary<String, Host> { {"local", new LocalHost()} }; | ||
|
||
foreach (FtpElement ftp in folderWatchSection.Sources.Ftps) | ||
{ | ||
hosts[ftp.Name] = new FtpHost(ftp); | ||
} | ||
|
||
foreach (OtherElement e in folderWatchSection.Sources.Others) | ||
{ | ||
hosts[e.Name] = new OtherHost(e); | ||
} | ||
|
||
foreach (string host in hosts.Keys) | ||
{ | ||
Log.DebugFormat("Host name: {0}", host); | ||
} | ||
|
||
var flows = new List<Flow>(); | ||
foreach (FlowElement flowSetting in folderWatchSection.Flows) | ||
{ | ||
Flow flow = new Flow(hosts, flowSetting); | ||
flows.Add(flow); | ||
} | ||
|
||
timer = new Timer(Callback, flows, Timeout.Infinite, Timeout.Infinite); | ||
} | ||
|
||
public void Start() | ||
|
@@ -73,16 +100,22 @@ public void Stop() | |
} | ||
} | ||
|
||
private void Callback(object state) | ||
private void Callback(object flowsObj) | ||
{ | ||
var flows = flowsObj as List<Flow>; | ||
if (flows == null) | ||
{ | ||
Log.Error("Settings not set"); | ||
return; | ||
} | ||
|
||
Log.Info("Callback invoked"); | ||
//FtpClient ftp = new FtpClient | ||
// { | ||
// Host = @"ftp.mozilla.org/pub/mozilla.org/", | ||
//Port = 22, | ||
// }; | ||
//ftp.Open("anonymous", "[email protected]"); | ||
//Log.WarnFormat("Num files: {0}", ftp.GetDirList().Count); | ||
|
||
foreach (Flow flow in flows) | ||
{ | ||
Log.InfoFormat("Flow: {0}", flow); | ||
flow.Run(); | ||
} | ||
|
||
Thread.Sleep(4000); | ||
timer.Change(1000, Timeout.Infinite); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Security; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FolderWatch.Settings; | ||
using Starksoft.Net.Ftp; | ||
using log4net; | ||
|
||
namespace FolderWatch.Impl | ||
{ | ||
class LocalHost : Host | ||
{ | ||
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
private readonly string _name = "local"; | ||
|
||
public LocalHost() | ||
{ | ||
} | ||
|
||
public override string Name | ||
{ | ||
get { return _name; } | ||
} | ||
|
||
public override void Download(string sourceFolder, string targetFolder, out int numberOfFiles) | ||
{ | ||
numberOfFiles = 0; | ||
Log.InfoFormat("Download called for LocalHost (Name: {0})", _name); | ||
} | ||
} | ||
|
||
class FtpHost : Host | ||
{ | ||
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
private readonly string _name; | ||
private string _hostname; | ||
private SecureString _password; | ||
private int _port; | ||
private string _username; | ||
|
||
public FtpHost(FtpElement hostSettings) | ||
{ | ||
_name = hostSettings.Name; | ||
_hostname = hostSettings.Hostname; | ||
_username = hostSettings.UserName; | ||
_password = hostSettings.IsEncrypted ? StringEncryption.DecryptString(hostSettings.Password) : StringEncryption.ToSecureString(hostSettings.Password); | ||
_port = 21; | ||
} | ||
|
||
public override string Name | ||
{ | ||
get { return _name; } | ||
} | ||
|
||
public override void Download(string sourceFolder, string targetFolder, out int numberOfFiles) | ||
{ | ||
Log.DebugFormat("Download called for FtpHost (Name: {0})", _name); | ||
using (var c = new FtpClient(_hostname, _port, FtpSecurityProtocol.None)) | ||
{ | ||
c.AlwaysAcceptServerCertificate = true; | ||
c.Open(_username, StringEncryption.ToInsecureString(_password)); | ||
c.ChangeDirectory(sourceFolder); | ||
|
||
var files = c.GetDirList().Where(ftpItem => ftpItem.ItemType == FtpItemType.File).ToList(); | ||
Log.DebugFormat("Current directory: {0}", c.CurrentDirectory); | ||
foreach (var file in files) | ||
{ | ||
Log.DebugFormat("[File] Size: {1} Name: {0}", file.Name, file.Size); | ||
} | ||
|
||
foreach (var file in files) | ||
{ | ||
c.GetFile(file.FullPath, targetFolder + @"\" + file.Name, FileAction.Create); | ||
} | ||
|
||
numberOfFiles = files.Count; | ||
} | ||
} | ||
} | ||
|
||
class OtherHost : Host | ||
{ | ||
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
private readonly string _name; | ||
|
||
public OtherHost(OtherElement hostSettings) | ||
{ | ||
_name = hostSettings.Name; | ||
} | ||
|
||
public override string Name { | ||
get { return _name; } | ||
} | ||
|
||
public override void Download(string sourceFolder, string targetFolder, out int numberOfFiles) | ||
{ | ||
numberOfFiles = 0; | ||
Log.InfoFormat("Download called for OtherHost (Name: {0})", _name); | ||
} | ||
} | ||
|
||
abstract class Host | ||
{ | ||
abstract public string Name { get; } | ||
|
||
abstract public void Download(string sourceFolder, string targetFolder, out int numberOfFiles); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Configuration; | ||
|
||
namespace FolderWatch | ||
{ | ||
public class FtpElement : AbstractSourceConfig | ||
{ | ||
private const string IsEncryptedLabel = "isEncrypted"; | ||
private const string PasswordLabel = "password"; | ||
private const string UserNameLabel = "username"; | ||
|
||
[ConfigurationProperty("hostname", IsRequired = true)] | ||
//[RegexStringValidator(@"https?\://\S+")] | ||
public string Hostname | ||
{ | ||
get { return (string)this["hostname"]; } | ||
set { this["hostname"] = value; } | ||
} | ||
|
||
[ConfigurationProperty(UserNameLabel, IsRequired = true)] | ||
public string UserName | ||
{ | ||
get { return (string)this[UserNameLabel]; } | ||
set { this[UserNameLabel] = value; } | ||
} | ||
|
||
[ConfigurationProperty(PasswordLabel, IsRequired = true)] | ||
public string Password | ||
{ | ||
get { return (string)this[PasswordLabel]; } | ||
set { this[PasswordLabel] = value; } | ||
} | ||
|
||
|
||
[ConfigurationProperty(IsEncryptedLabel, IsRequired = false, DefaultValue = false)] | ||
public bool IsEncrypted | ||
{ | ||
get { return (bool)this[IsEncryptedLabel]; } | ||
set { this[IsEncryptedLabel] = value; } | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return String.Format("{0} Hostname: {1}", base.ToString(), Hostname); | ||
} | ||
} | ||
|
||
|
||
[ConfigurationCollection(typeof(FtpElement))] | ||
public class FtpElementCollection : ConfigurationElementCollection | ||
{ | ||
protected override ConfigurationElement CreateNewElement() | ||
{ | ||
return new FtpElement(); | ||
} | ||
|
||
protected override object GetElementKey(ConfigurationElement element) | ||
{ | ||
return ((FtpElement)element).Name; | ||
} | ||
} | ||
} |
Oops, something went wrong.