Skip to content

Commit

Permalink
Added ftp support (unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnaringe committed Jul 11, 2013
1 parent 6163999 commit 450ccd9
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 48 deletions.
20 changes: 17 additions & 3 deletions FolderWatch/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@
<add name="encrypt.section" value="true" />
<add name="encrypt.passwords" value="true" />
</settings>
<sources>

<hosts>
<ftp>
<add name="MyName" hostname="my.hostname.com" password="This is a Password" />
<add name="MyFTPHost" hostname="ftp.NetBSD.org" username="ftp" password="ftp" />
</ftp>
</sources>
<other>
<add name="MyOtherHost" hostname="other.hostname.com"/>
</other>
</hosts>

<flows>
<add name="MyFlow"
sourceName="MyFTPHost"
targetName="local"
sourceFolder="/pub/NetBSD/"
targetFolder="C:\Users\GunnarIngeGjøvik\Downloads\Temp"
/>
<add name="SecondFlow" sourceName="MyOtherHost" targetName="local" sourceFolder="/something" targetFolder="C:\temp" />
</flows>
</folderwatch>

<log4net>
Expand Down
4 changes: 4 additions & 0 deletions FolderWatch/FolderWatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Impl\Flow.cs" />
<Compile Include="Impl\FtpHost.cs" />
<Compile Include="Settings\AbstractSourceElement.cs" />
<Compile Include="Settings\Other.cs" />
<Compile Include="Settings\FolderWatchSection.cs" />
<Compile Include="Settings\Ftp.cs" />
<Compile Include="Settings\SourcesConfig.cs" />
<Compile Include="Impl\FolderWatch.cs" />
<Compile Include="Service\WindowsService.cs">
Expand Down
42 changes: 42 additions & 0 deletions FolderWatch/Impl/Flow.cs
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);

}
}
}
51 changes: 42 additions & 9 deletions FolderWatch/Impl/FolderWatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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);
Expand Down
111 changes: 111 additions & 0 deletions FolderWatch/Impl/FtpHost.cs
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);
}
}
23 changes: 17 additions & 6 deletions FolderWatch/Settings/FolderWatchSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ namespace FolderWatch
{
public class FolderWatchSection : ConfigurationSection
{
[ConfigurationProperty("settings")]
private const string HostsLabel = "hosts";
private const string SettingsLabel = "settings";
private const string FlowLabel = "flows";

[ConfigurationProperty(SettingsLabel)]
public NameValueConfigurationCollection Settings
{
get { return (NameValueConfigurationCollection) this["settings"]; }
set { this["settings"] = value; }
get { return this[SettingsLabel] as NameValueConfigurationCollection; }
set { this[SettingsLabel] = value; }
}

[ConfigurationProperty("sources")]
[ConfigurationProperty(HostsLabel)]
public SourcesConfig Sources
{
get { return (SourcesConfig) this["sources"]; }
set { this["sources"] = value; }
get { return (SourcesConfig) this[HostsLabel]; }
set { this[HostsLabel] = value; }
}

[ConfigurationProperty(FlowLabel, IsDefaultCollection = true)]
public FlowElementCollection Flows
{
get { return (FlowElementCollection) this[FlowLabel]; }
set { this[FlowLabel] = value; }
}
}
}
62 changes: 62 additions & 0 deletions FolderWatch/Settings/Ftp.cs
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;
}
}
}
Loading

0 comments on commit 450ccd9

Please sign in to comment.