Logo Search packages:      
Sourcecode: bareftp version File versions  Download package

Connection.cs

// Connection.cs
//
//  Copyright (C) 2008 Christian Eide
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.Collections.Generic;
using bareFTP.Protocol;
using System.Threading;

namespace bareFTP.Connection
{     
      public class Connection
      {
            private List<IProtocol> connections;
            private ProtocolType protocol_type;
            private ConnectionProperties conn_props;
            private IDialogHost dialog_host;
            private bareFTP.Preferences.Config config;
            
            public Connection()
            {
                  connections = new List<IProtocol>();
            }
            
            private IProtocol GetConnection()
            {
                  if(connections.Count == 0)
                  {
                        connections.Add(CreateConnection());
                        return connections[0];
                  }
                  else
                  {
                        foreach(IProtocol conn in connections)
                        {
                              if(!conn.TransferInProgress)
                                    return conn;
                        }
                        
                        IProtocol c = CreateConnection();
                        connections.Add(c);
                        c.Open(conn_props.Hostname, conn_props.Port, conn_props.User, conn_props.Password);
                        c.SetCurrentDirectory(connections[0].CurrentDirectory);
                        return c;
                  }
            }
            
            private IProtocol GetTransferConnection()
            {
                  foreach(IProtocol conn in connections)
                  {
                              if(conn.TransferInProgress)
                                    return conn;
                  }
                  return GetConnection();
            }

            private IProtocol CreateConnection()
            {
                  IProtocol conn = null;
                  switch(protocol_type)
                  {
                        case bareFTP.Protocol.ProtocolType.FTP:
                              conn = new bareFTP.Protocol.Ftp.FTPConnection(conn_props);
                              break;
                        case bareFTP.Protocol.ProtocolType.FTPS:
                            conn_props.FTPS = true;
                              conn = new bareFTP.Protocol.Ftp.FTPConnection(conn_props);
                        break;
                        case bareFTP.Protocol.ProtocolType.SFTP:
                              conn = new bareFTP.Protocol.Sftp.SftpConnection(conn_props);
                        break;
                        default:
                              Console.WriteLine("Oooops..");
                              break;
                  }
                  
                  conn.Init(dialog_host, Configuration);
                  conn.LogTextEmitted += OnLogTextEmitted;
                  conn.LostConnection += OnConnectionLost;
                  
                  return conn;
            }
            
            public void CleanUp()
            {
                  for(int x=connections.Count-1; x >= 1; x--)
                  {
                        if(!connections[x].TransferInProgress)
                        {
                              connections[x].Close();
                              connections.RemoveAt(x);
                        }
                  }
            }
            
            public event EventHandler LogTextEmitted;
            public virtual void OnLogTextEmitted(object sender, EventArgs e)
            {
                  LogTextEmitted(this, e);
            }
      
            public virtual void OnConnectionLost(object dead_conn, EventArgs e)
            {
                  /*
                  IProtocol dead = (IProtocol)dead_conn;
                  
                  CreateConnection();
                  
                  int pos = connections.FindIndex(dead);
                  CleanUp();
                  Open();
                  */
                  //ConnectionLost(cwd, null);
            }
            
            public ProtocolType Protocol
            {
                  get { return protocol_type; }
                  set { protocol_type = value; }
            }

            
            public ConnectionProperties ConnProperties {
                  get { return conn_props; }
                  set { conn_props = value; }
            }
            
            public IDialogHost DialogHost {
                  get { return dialog_host; }
                  set { dialog_host = value; }
            }
            
            public bareFTP.Preferences.Config Configuration {
                  get { return config; }
                  set { config = value; }
            }

            
            
            
            // IProtocol implementation
            public void Open()
            {
                  GetConnection().Open(conn_props.Hostname, conn_props.Port, conn_props.User, conn_props.Password);
            }
            
            public void Open(string remoteHost, int remotePort, string user, string password)
            {
                  GetConnection().Open(remoteHost, remotePort, user, password);
            }
            
            public void Close()
            {
                  GetConnection().Close();
            }
            
            public bool Connected 
            { 
                  get
                  {
                        try{
                              foreach(IProtocol conn in connections)
                              {
                                    if(conn.Connected)
                                          return true;
                              }
                              
                              return false;
                        }
                        catch { return false; }
                  }
            }
            
            public string SendCommand(string command)
            {
                  return GetConnection().SendCommand(command);
            }
            
            public List<string> Dir()
            {
                  return GetConnection().Dir();
            }
            
            public List<RemoteFile> XDir()
            {
                  return GetConnection().XDir();
            }
            
            public void SendFile(XferFile file)
            {
                  GetConnection().SendFile(file);
            }
            
            public void GetFile(XferFile file)
            {
                  GetConnection().GetFile(file);
            }
            
            public void DeleteFile(string remoteFileName)
            {
                  GetConnection().DeleteFile(remoteFileName);
            }
            
            public void MoveFile(string remoteFileName, string toRemotePath)
            {
                  GetConnection().MoveFile(remoteFileName, toRemotePath);
            }
            
            public void RenameFile(string fromRemoteFileName, string toRemoteFileName)
            {
                  GetConnection().RenameFile(fromRemoteFileName, toRemoteFileName);
            }
            
            public void SetCurrentDirectory(string remotePath)
            {
                  GetConnection().SetCurrentDirectory(remotePath);
            }
            
            public bool IsDir(string remotePath)
            {
                  return GetConnection().IsDir(GetConnection().CurrentDirectory, remotePath);
            }
            
            public string GetCurrentDirectory()
            {
                  return GetConnection().GetCurrentDirectory();
            }
            
            public void MakeDir(string directoryName)
            {
                  GetConnection().MakeDir(directoryName);
            }
            
            public void RemoveDir(string directoryName)
            {
                  GetConnection().RemoveDir(directoryName);
            }
            
            public void Chmod(string mode, string filename)
            {
                  GetConnection().Chmod(mode, filename);
            }
            
            public void Abort()
            {
                  GetTransferConnection().Abort();
            }

      }
}

Generated by  Doxygen 1.6.0   Back to index