/*
* Copyright (c) 2009 Craig Sutherland
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using FastForward.WinCore.Configuration;
using ThoughtWorks.CruiseControl.Remote;
namespace FastForward.WinCore
{
///
/// The UI for allowing the user to add a new server.
///
public partial class AddServerForm
: Form
{
#region Private fields
private CruiseServerClientFactory factory = new CruiseServerClientFactory { UseClientCaching = false };
private string version;
private List existingServers;
#endregion
#region Constructors
///
/// Initialise a new .
///
///
public AddServerForm(List existingServers)
{
InitializeComponent();
this.existingServers = existingServers;
serverTransport.Items.Add(new ServerTransportDisplay("(Default)", ServerTransportMode.Default));
serverTransport.Items.Add(new ServerTransportDisplay("HTTP", ServerTransportMode.Http));
serverTransport.Items.Add(new ServerTransportDisplay(".NET Remoting", ServerTransportMode.Remoting));
serverTransport.Items.Add(new ServerTransportDisplay("FastForward.NET Cache (TCP)", ServerTransportMode.TcpCache));
serverTransport.SelectedIndex = 0;
}
#endregion
#region Public properties
#region ServerAddress
///
/// The address of the server.
///
public string ServerAddress
{
get
{
var address = serverAddress.Text;
if (!address.Contains("//")) address = string.Format("tcp://{0}", address);
if (address.StartsWith("tcp://", StringComparison.InvariantCultureIgnoreCase) &&
(address.LastIndexOf(":") == 3))
{
var splitPos = address.IndexOf('/', 6);
if (splitPos >= 0)
{
address = address.Substring(0, splitPos) +
":21234" +
address.Substring(splitPos);
}
else
{
address += ":21234/";
}
}
if (address.EndsWith("/")) address = address.Substring(0, address.Length - 1);
return address;
}
}
#endregion
#region TransportMode
///
/// The transportation mode to use.
///
public ServerTransportMode TransportMode
{
get { return (serverTransport.SelectedItem as ServerTransportDisplay).Mode; }
}
#endregion
#region ServerVersion
///
/// The current version of the remote server.
///
public string ServerVersion
{
get { return version; }
}
#endregion
#region TargetServer
///
/// The name of the target server (for pass-through servers).
///
public string TargetServer
{
get { return targetServer.Text; }
}
#endregion
#region Settings
///
/// The start-up settings to use.
///
public ClientStartUpSettings Settings
{
get
{
return new ClientStartUpSettings
{
BackwardsCompatable = backwardsCompatible.Checked
};
}
}
#endregion
#region Client
///
/// The client that was used to test the connection.
///
public CruiseServerClientBase Client { get; private set; }
#endregion
#endregion
#region Public events
#region OkClicked
///
/// The OK button has been clicked.
///
public event EventHandler OkClicked;
#endregion
#endregion
#region Private methods
#region ValidateAddress()
///
/// Validate the remote address.
///
///
private bool ValidateAddress()
{
var isValid = (serverAddress.Text.Length > 0);
errorProvider.SetError(serverAddress, null);
if (!isValid)
{
errorProvider.SetError(serverAddress, "Address is required");
}
else
{
if (existingServers.Contains(ServerAddress.ToLower()))
{
isValid = false;
errorProvider.SetError(serverAddress, "This address is already in use");
}
}
return isValid;
}
#endregion
#endregion
#region Event handlers
#region serverAddress_Leave()
///
/// Validate the server address.
///
///
///
private void serverAddress_Leave(object sender, EventArgs e)
{
ValidateAddress();
}
#endregion
#region okButton_Click()
///
/// The OK button was clicked.
///
///
///
private void okButton_Click(object sender, EventArgs e)
{
if (!ValidateAddress()) return;
// Initialise the client
validationResult.Text = "Validating server, please wait...";
System.Windows.Forms.Application.DoEvents();
var list = new ServerList();
Client = list.InitialiseClient(new ServerConfig
{
Address = this.ServerAddress,
TargetServer = this.TargetServer,
TransportMode = this.TransportMode,
Settings = this.Settings
});
// Attempt to connect to the server - this will validate that the settings are valid
if (Client == null)
{
validationResult.Text = "Unknown transport selected";
}
else
{
try
{
version = Client.GetServerVersion();
if (OkClicked != null) OkClicked(this, EventArgs.Empty);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception error)
{
validationResult.Text = "Unable to connect to server: " + error.Message;
}
}
}
#endregion
#region backwardsCompatible_CheckedChanged()
///
/// The checked state on the backwards compatable checkbox was changed.
///
///
///
private void backwardsCompatible_CheckedChanged(object sender, EventArgs e)
{
var showTargetServer = !backwardsCompatible.Checked;
targetServer.Enabled = showTargetServer;
targetServerLabel.Enabled = showTargetServer;
}
#endregion
#endregion
#region Private classes
#region ServerTransportDisplay
///
/// Display a transport mode.
///
private class ServerTransportDisplay
{
#region Private fields
private string text;
private ServerTransportMode mode;
#endregion
#region Constructors
///
/// Initialise a new .
///
///
///
public ServerTransportDisplay(string text, ServerTransportMode mode)
{
this.text = text;
this.mode = mode;
}
#endregion
#region Public properties
#region Mode
///
/// The transport mode.
///
public ServerTransportMode Mode { get { return mode; } }
#endregion
#endregion
#region Public methods
#region ToString()
///
/// Convert this mode to a string.
///
///
public override string ToString()
{
return text;
}
#endregion
#endregion
}
#endregion
#endregion
}
}