/*
* Copyright (C) 2004-2005 Jonathan Bindel
* Copyright (C) 2006-2007 Eskil Bylund
*
* 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;
using System.Collections.Generic;
using DCSharp.Backend.Connections;
using DCSharp.Backend.Objects;
namespace DCSharp.Backend.Managers{
public sealed class HubManager : IEnumerable<HubConnection>
{
public event EventHandler<ConnectionEventArgs> HubAdded;
public event EventHandler<ConnectionEventArgs> HubRemoved;
public event EventHandler<ConnectionEventArgs> HubStateChanged;
private List<HubConnection> hubs;
public HubManager()
{
hubs = new List<HubConnection>();
}
#region Properties
public int Count
{
get { return hubs.Count; }
}
public HubConnection[] Hubs
{
get
{
lock (hubs)
{
return hubs.ToArray();
}
}
}
public object SyncRoot
{
get { return hubs; }
}
#endregion
#region Methods
public void Add(HubConnection hub)
{
if (hub == null)
{
throw new ArgumentNullException("hub");
}
lock (hubs)
{
if (!hubs.Contains(hub))
{
hub.StateChanged += OnHubStateChanged;
hubs.Add(hub);
OnHubAdded(hub);
}
}
}
public void Remove(HubConnection hub)
{
if (hub == null)
{
throw new ArgumentNullException("hub");
}
lock (hubs)
{
if (hubs.Contains(hub))
{
hub.StateChanged -= OnHubStateChanged;
hubs.Remove(hub);
OnHubRemoved(hub);
}
}
}
public bool Contains(HubConnection hub)
{
return hubs.Contains(hub);
}
public HubConnection Find(Predicate<HubConnection> match)
{
if (match == null)
{
throw new ArgumentNullException("match");
}
lock (hubs)
{
return hubs.Find(match);
}
}
public void ForEach(Action<HubConnection> action)
{
if (action == null)
{
throw new ArgumentNullException("action");
}
lock (hubs)
{
hubs.ForEach(action);
}
}
public IEnumerator<HubConnection> GetEnumerator()
{
return hubs.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public HubConnection GetHub(string hostname)
{
return Find(delegate(HubConnection hub)
{
return hub.Hostname == hostname || hub.Address == hostname;
});
}
public HubConnection GetHubWithUser(User user)
{
return Find(delegate(HubConnection hub)
{
return hub.Users[user] != null;
});
}
protected void OnHubAdded(HubConnection hub)
{
if (HubAdded != null)
{
HubAdded(this, new ConnectionEventArgs(hub));
}
}
protected void OnHubRemoved(HubConnection hub)
{
if (HubRemoved != null)
{
HubRemoved(this, new ConnectionEventArgs(hub));
}
}
protected void OnHubStateChanged(object obj, EventArgs args)
{
if (HubStateChanged != null)
{
HubStateChanged(this, new ConnectionEventArgs((Connection)obj));
}
}
#endregion
}
}
|