/*
* 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 FastForward.WinCore.Configuration;
using ThoughtWorks.CruiseControl.Remote;
using ThoughtWorks.CruiseControl.Remote.Monitor;
namespace FastForward.WinCore
{
///
/// Defines all the servers to be monitored.
///
public class ServerList
: IDisposable
{
#region Private fields
private List servers = new List();
private CruiseServerClientFactory factory = new CruiseServerClientFactory();
#endregion
#region Public properties
#region Servers
///
/// The monitored servers.
///
public IEnumerable Servers
{
get { return servers; }
}
#endregion
#endregion
#region Public properties
#region AddServer()
///
/// Add a new server.
///
///
public void AddServer(ServerConfig value)
{
// Initialise the client and make sure there are no duplicates
CruiseServerClientBase client = InitialiseClient(value);
if (servers.Contains(value.Server))
{
throw new Exception(
string.Format("Duplicate server ({0}-{1})- unable to add server",
value.Address,
value.TargetServer));
}
// Initialise the watcher
var watcher = new PollingServerWatcher(client);
try
{
watcher.Interval = value.PollingInterval;
value.Server = new Server(client, watcher);
}
catch
{
watcher.Dispose();
throw;
}
servers.Add(value.Server);
value.Server.Data.Set(value);
// Tell any listeners
FireServerAdded(value.Server);
}
#endregion
#region InitialiseClient()
///
/// Initialise the underlying client for a .
///
///
///
public CruiseServerClientBase InitialiseClient(ServerConfig value)
{
CruiseServerClientBase client = null;
switch (value.TransportMode)
{
case ServerTransportMode.Default:
client = factory.GenerateClient(value.Address, value.TargetServer, value.Settings);
break;
case ServerTransportMode.Http:
client = factory.GenerateHttpClient(value.Address, value.TargetServer, value.Settings);
break;
case ServerTransportMode.Remoting:
client = factory.GenerateRemotingClient(value.Address, value.TargetServer, value.Settings);
break;
case ServerTransportMode.TcpCache:
client = InitialiseCacheConnection(value);
break;
}
return client;
}
#endregion
#region RemoveServer
///
/// Remove a server.
///
///
public void RemoveServer(Server server)
{
if (servers.Contains(server))
{
FireServerRemoved(server);
servers.Remove(server);
server.Dispose();
}
}
///
/// Remove a server.
///
///
public void RemoveServer(ServerConfig value)
{
RemoveServer(value.Server);
}
#endregion
#region UpdateServer()
///
/// Update a server.
///
///
///
/// This will remove the existing server and re-add it.
///
public void UpdateServer(ServerConfig value)
{
RemoveServer(value);
AddServer(value);
}
#endregion
#region Dispose()
///
/// Clean up.
///
public void Dispose()
{
foreach (var server in servers)
{
server.Dispose();
}
}
#endregion
#region InitialiseCacheConnection()
///
/// Initialise a connection to a cache server.
///
///
///
public static CruiseServerClientBase InitialiseCacheConnection(ServerConfig server)
{
var connection = new CacheTcpConnection(server.Address);
var actualConnection = CruiseServerClientFactory.BuildUpConnection(connection, server.Settings);
var client = new CruiseServerClient(actualConnection);
client.TargetServer = server.TargetServer;
return client;
}
#endregion
#endregion
#region Public events
#region ServerAdded
///
/// A new server has been added.
///
public event EventHandler ServerAdded;
#endregion
#region ServerRemoved
///
/// An existing server has been removed.
///
public event EventHandler ServerRemoved;
#endregion
#endregion
#region Protected methods
#region FireServerAdded()
///
/// Fires the event.
///
/// The server that was added.
protected void FireServerAdded(Server server)
{
if (ServerAdded != null)
{
var args = new ServerChangedArgs(server);
ServerAdded(this, args);
}
}
#endregion
#region FireServerRemoved()
///
/// Fires the event.
///
/// The server that was added.
protected void FireServerRemoved(Server server)
{
if (ServerRemoved != null)
{
var args = new ServerChangedArgs(server);
ServerRemoved(this, args);
}
}
#endregion
#endregion
}
}