Author: alanmc
Date: 2007-05-30 12:26:14 -0400 (Wed, 30 May 2007)
New Revision: 78233
Modified:
trunk/bitsharp/src/MonoTorrent/Client/ClientEngine.cs
trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs
trunk/bitsharp/src/SampleClient/main.cs
Log:
Refactored the ClientEngine
Modified: trunk/bitsharp/src/MonoTorrent/Client/ClientEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/ClientEngine.cs 2007-05-30
16:22:55 UTC (rev 78232)
+++ trunk/bitsharp/src/MonoTorrent/Client/ClientEngine.cs 2007-05-30
16:26:14 UTC (rev 78233)
@@ -79,19 +79,7 @@
private ConnectionManager connectionManager;
-
/// <summary>
- /// The default settings to be used by newly added torrents
- /// </summary>
- public TorrentSettings DefaultTorrentSettings
- {
- get { return this.defaultTorrentSettings; }
- set { this.defaultTorrentSettings = value; }
- }
- private TorrentSettings defaultTorrentSettings;
-
-
- /// <summary>
/// True if the engine has been started
/// </summary>
public bool IsRunning
@@ -169,17 +157,13 @@
/// </summary>
/// <param name="engineSettings">The engine settings to use</param>
/// <param name="defaultTorrentSettings">The default settings for new
torrents</param>
- public ClientEngine(EngineSettings engineSettings, TorrentSettings
defaultTorrentSettings)
+ public ClientEngine(EngineSettings engineSettings)
{
if (engineSettings == null)
throw new ArgumentNullException("engineSettings");
- if (defaultTorrentSettings == null)
- throw new ArgumentNullException("defaultTorrentSettings");
-
this.connectionManager = new ConnectionManager(this);
this.settings = engineSettings;
- this.defaultTorrentSettings = defaultTorrentSettings;
this.listener = new ConnectionListener(engineSettings.ListenPort,
new AsyncCallback(this.IncomingConnectionReceived));
this.timer = new System.Timers.Timer(TickLength);
this.timer.Elapsed += new ElapsedEventHandler(LogicTick);
@@ -194,48 +178,95 @@
#endregion
+ #region Methods
+
/// <summary>
- /// Starts all torrents in the engine if they are not already started
+ /// Returns true if a TorrentManager containing this Torrent has been
registered with this engine
/// </summary>
- internal void Start()
+ /// <param name="torrent"></param>
+ /// <returns></returns>
+ public bool Contains(Torrent torrent)
{
- if (!this.listener.IsListening)
- this.listener.Start(); // Start Listening for connections
+ if (torrent == null)
+ return false;
- if (!timer.Enabled)
- timer.Enabled = true; // Start logic ticking
+ for (int i = 0; i < this.torrents.Count; i++)
+ if (Toolbox.ByteMatch(this.torrents[i].Torrent.infoHash,
torrent.infoHash))
+ return true;
+
+ return false;
}
/// <summary>
- /// Stops the specified torrent and flushes out all peer information
+ /// Returns true if the TorrentManager has been registered with this
engine
/// </summary>
/// <param name="manager"></param>
- internal void Stop()
+ /// <returns></returns>
+ public bool Contains(TorrentManager manager)
{
- using (new ReaderLock(this.torrentsLock))
- for (int i = 0; i < this.torrents.Count; i++)
- if (this.torrents[i].State != TorrentState.Stopped)
- return;
+ if (manager == null)
+ return false;
- timer.Enabled = false; // All the torrents are
stopped, so stop ticking
-
- if (this.listener.IsListening) // Also stop listening for
incoming connections
- this.listener.Stop();
+ return Contains(manager.Torrent);
}
+ /// <summary>
+ /// Disposes the Engine as well as all TorrentManagers still
registered with the engine
+ /// </summary>
+ public void Dispose()
+ {
+ for (int i = 0; i < this.torrents.Count; i++)
+ {
+ if (torrents[i].State != TorrentState.Stopped)
+ torrents[i].Stop().WaitOne();
+ Unregister(torrents[i]);
+ }
- private bool ContainsTorrent(string p)
+ if (!this.listener.Disposed)
+ this.listener.Dispose();
+
+ this.timer.Dispose();
+ }
+
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <returns></returns>
+ private static string GeneratePeerId()
{
- for (int i = 0; i < this.torrents.Count; i++)
- if (BitConverter.ToString(this.torrents[i].Torrent.InfoHash)
== p)
- return true;
+ StringBuilder sb = new StringBuilder(20);
+ Random rand = new
Random((int)DateTime.Now.TimeOfDay.TotalMilliseconds);
- return false;
+ sb.Append(Common.VersionInfo.ClientVersion);
+ for (int i = 0; i < 12; i++)
+ sb.Append(rand.Next(0, 9));
+
+ return sb.ToString();
}
+
+ /// <summary>
+ /// Pauses all TorrentManagers which are registered to this engine
+ /// </summary>
+ public void PauseAll()
+ {
+ using (new ReaderLock(this.torrentsLock))
+ for (int i = 0; i < torrents.Count; i++)
+ if (torrents[i].State == TorrentState.Downloading ||
+ torrents[i].State == TorrentState.Seeding ||
+ torrents[i].State == TorrentState.SuperSeeding)
+ torrents[i].Pause();
+ }
+
+
+ /// <summary>
+ /// Registers the TorrentManager with this engine
+ /// </summary>
+ /// <param name="manager"></param>
public void Register(TorrentManager manager)
{
if (manager == null)
@@ -250,6 +281,69 @@
manager.Engine = this;
}
+
+ /// <summary>
+ /// Starts all stopped or paused TorrentManagers which are registered
to this engine
+ /// </summary>
+ public void StartAll()
+ {
+ using (new ReaderLock(this.torrentsLock))
+ for (int i = 0; i < torrents.Count; i++)
+ if (torrents[i].State == TorrentState.Stopped ||
torrents[i].State == TorrentState.Paused)
+ torrents[i].Start();
+ }
+
+
+ /// <summary>
+ /// Stops all TorrentManagers which are registered to this engine
+ /// </summary>
+ public void StopAll()
+ {
+ using (new ReaderLock(this.torrentsLock))
+ for (int i = 0; i < torrents.Count; i++)
+ if (torrents[i].State != TorrentState.Stopped)
+ torrents[i].Stop();
+ }
+
+
+ /// <summary>
+ /// Returns the combined download speed of all active torrents
+ /// </summary>
+ public double TotalDownloadSpeed
+ {
+ get
+ {
+ double speed = 0;
+ using (new ReaderLock(torrentsLock))
+ for (int i = 0; i < torrents.Count; i++)
+ speed += torrents[i].Monitor.DownloadSpeed;
+
+ return speed;
+ }
+ }
+
+
+ /// <summary>
+ /// Returns the combined upload speed of all active torrents
+ /// </summary>
+ public double TotalUploadSpeed
+ {
+ get
+ {
+ double speed = 0;
+ using (new ReaderLock(torrentsLock))
+ for (int i = 0; i < torrents.Count; i++)
+ speed += torrents[i].Monitor.UploadSpeed;
+
+ return speed;
+ }
+ }
+
+
+ /// <summary>
+ /// Unregisters the TorrentManager from this engine
+ /// </summary>
+ /// <param name="manager"></param>
public void Unregister(TorrentManager manager)
{
if (manager == null)
@@ -267,37 +361,24 @@
manager.Engine = null;
}
+ #endregion
- #region Misc
+ #region Private/Internal methods
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- private static string GeneratePeerId()
+
+ private void AsyncStatsUpdate(object args)
{
- StringBuilder sb = new StringBuilder(20);
- Random rand = new
Random((int)DateTime.Now.TimeOfDay.TotalMilliseconds);
-
- sb.Append(Common.VersionInfo.ClientVersion);
- for (int i = 0; i < 12; i++)
- sb.Append(rand.Next(0, 9));
-
- return sb.ToString();
+ if (StatsUpdate != null)
+ StatsUpdate(this, (StateUpdateEventArgs)args);
}
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
private void LogicTick(object sender, ElapsedEventArgs e)
{
tickCount++;
+ using(new ReaderLock(this.torrentsLock))
for (int i = 0; i < this.torrents.Count; i++)
{
switch (this.torrents[i].State)
@@ -323,58 +404,43 @@
}
- /// <summary>
- ///
- /// </summary>
- public void Dispose()
+ internal void RaiseStatsUpdate(StateUpdateEventArgs args)
{
- this.Dispose(true);
+ if (StatsUpdate != null)
+ ThreadPool.QueueUserWorkItem(new
WaitCallback(AsyncStatsUpdate), args);
}
- /// <summary>
- ///
- /// </summary>
- /// <param name="disposing"></param>
- public void Dispose(bool disposing)
+ internal void Start()
{
- // FIXME
- //WaitHandle[] handles = this.RemoveAll();
- //for (int i = 0; i < handles.Length; i++)
- // if (handles[i] != null)
- // handles[i].WaitOne();
+ if (!this.listener.IsListening)
+ this.listener.Start(); // Start Listening for connections
- //foreach (KeyValuePair<string, TorrentManager> keypair in
this.torrents)
- // keypair.Value.Dispose();
+ if (!timer.Enabled)
+ timer.Enabled = true; // Start logic ticking
+ }
- if (!this.listener.Disposed)
- this.listener.Dispose();
- this.timer.Dispose();
- }
-
- internal void RaiseStatsUpdate(StateUpdateEventArgs args)
+ internal void Stop()
{
- if (StatsUpdate != null)
- ThreadPool.QueueUserWorkItem(new
WaitCallback(AsyncStatsUpdate), args);
- }
+ using (new ReaderLock(this.torrentsLock))
+ for (int i = 0; i < this.torrents.Count; i++)
+ if (this.torrents[i].State != TorrentState.Stopped)
+ return;
- private void AsyncStatsUpdate(object args)
- {
- if (StatsUpdate != null)
- StatsUpdate(this, (StateUpdateEventArgs)args);
+ timer.Enabled = false; // All the torrents are
stopped, so stop ticking
+
+ if (this.listener.IsListening) // Also stop listening for
incoming connections
+ this.listener.Stop();
}
-
#endregion
+ #region Move to connection listener
-
-
- #region Move to connection listener
/// <summary>
///
/// </summary>
@@ -541,7 +607,6 @@
id.Peer.Connection.ProcessingQueue = false;
}
-
private void onEncryptorReady(PeerId id)
{
try
@@ -584,7 +649,6 @@
CleanupSocket(id);
}
-
/// <summary>
///
/// </summary>
@@ -612,4 +676,4 @@
#endregion
}
-}
+}
\ No newline at end of file
Modified: trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs
2007-05-30 16:22:55 UTC (rev 78232)
+++ trunk/bitsharp/src/MonoTorrent/Client/Managers/TorrentManager.cs
2007-05-30 16:26:14 UTC (rev 78233)
@@ -67,7 +67,7 @@
#endregion
- #region Private Member Variables
+ #region Member Variables
private BitField bitfield; // The bitfield representing
the pieces we've downloaded and have to download
private ClientEngine engine; // The engine that this
torrent is registered with
@@ -90,7 +90,7 @@
private TrackerManager trackerManager; // The class used to control
all access to the tracker
private int uploadingTo; // The number of peers which
we're currently uploading to
- #endregion
+ #endregion Member Variables
#region Properties
Modified: trunk/bitsharp/src/SampleClient/main.cs
===================================================================
--- trunk/bitsharp/src/SampleClient/main.cs 2007-05-30 16:22:55 UTC (rev
78232)
+++ trunk/bitsharp/src/SampleClient/main.cs 2007-05-30 16:26:14 UTC (rev
78233)
@@ -67,7 +67,7 @@
TorrentSettings torrentDefaults = new TorrentSettings(4, 50, 0, 0);
// Create an instance of the engine.
- engine = new ClientEngine(engineSettings, torrentDefaults);
+ engine = new ClientEngine(engineSettings);
// If the SavePath does not exist, we want to create it.
if (!Directory.Exists(engine.Settings.SavePath))
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches