DCPlusPlus.Sharing Class Reference

A Class that manages shares for other clients. Holds a list of shares and functions to add/remove/search for them. More...

List of all members.

Public Member Functions

void Add (SharingEntry item)
 Add an entry to the shares list fires the added event.
void Clear ()
 Clears the shares list.
bool Contains (SharingEntry item)
void CopyTo (SharingEntry[] array, int arrayIndex)
delegate void DirectoryFinishedEventHandler (string directory)
 Prototype for the Directory Finished Event Handler.
delegate void EntriesChangedEventHandler ()
 Prototype for the Entries Changed Event Handler.
delegate void EntriesClearedEventHandler ()
 Prototype for the Entries Cleared Event Handler.
delegate void EntryAddedEventHandler (SharingEntry entry)
 Prototype for the Entry Added Event Handler.
delegate void EntryRemovedEventHandler (SharingEntry entry)
 Prototype for the Entry Removed Event Handler.
IEnumerator< SharingEntryGetEnumerator ()
string GetFileListXml ()
 Get the DC++ file list.
byte[] GetFileListXmlBZ2 ()
 Get the compressed DC++ file list.
SharingEntry GetShareByFilename (string filename)
 Tries to find a Share identified by its filename.
SharingEntry GetShareByFileRequest (string file_request)
 Tries to find a Share identified by its TTH or filename to search for a TTH put a TTH/ in front the Base32 encoded TTH.
SharingEntry GetShareByTTH (string tth)
 Tries to find a share identified by its tth.
void LoadSharesFromXml (string xml)
 Load shares from a xml string.
void LoadSharesFromXmlFile (string filename)
 Load shares from a xml file.
bool Remove (string filename)
 Remove an entry from the shares list fires the removed event if an entry was removed.
bool Remove (SharingEntry item)
 Remove an entry from the shares list fires the removed event if an entry was removed.
string SaveSharesToXml ()
 Get shares list represented as a xml string.
void SaveSharesToXmlFile (string filename)
 Save shares to a xml file.
void ShareDirectory (string directory)
 Asynchronously share a directory tree [Non blocking].
void ShareFile (string filename)
 Asynchronously share a file [Non blocking].
[Test] void TestEmptyGetFileListXml ()
 Test to see if a valid empty file list will be created.
[Test] void TestGetFileListXml ()
 Test to see a valid file list will be created.
[Test] void TestShareDirectory ()
 Test to see if sharing of a directory works as expected.
[Test] void TestShareFile ()
 Test to see if sharing of a file works as expected.
[Test] void TestShareSaveLoad ()
 Test to see if loading and saving of shares works as expected.
[Test] void TestShareSearchRemove ()
 Test to see if searching and removing of shares works as expected.
[Test] void TestSharingNotExistingDirectory ()
 Test to see if sharing of a non existing directory fails as expected (to check for crashes of unexpected uncatched exceptions).
[Test] void TestTTHs ()
 Test to see if hashing of files works as expected.
void UpdateFileLists ()
 Create the DC++ file list from our shares list.

Public Attributes

event DirectoryFinishedEventHandler DirectoryFinished
 Event handler that gets called when sharing/hashing a directory has finished.
event EntriesChangedEventHandler EntriesChanged
 Event handler that gets called when one or more entries were changed (deprecated , or possibly reused for its original purpose).
event EntriesClearedEventHandler EntriesCleared
 Event handler that gets called when all shares were removed.
event EntryAddedEventHandler EntryAdded
 Event handler that gets called when an entry was added to the shares.
event EntryRemovedEventHandler EntryRemoved
 Event handler that gets called when an entry was removed from the shares.

Protected Attributes

string cid = "D2QLOGUYDX3QA"
string generator = "vpDcPlusPlus 0.2"
List< SharingEntryitems = new List<SharingEntry>()
[XmlIgnoreAttribute] Object share_lock = new Object()
 The lock object to make the sharing class thread safe.
long total_bytes_shared = 0

Properties

string CID [get, set]
 ??CID?? i really need to look up what cid means and does ;-)
int Count [get]
 Get the number of shared files.
string Generator [get, set]
 Get/Set the file list generator name in the DC++ file list.
bool IsReadOnly [get]
List< SharingEntryItems [get, set]
 List of shares (will be an unpublic property soon,because of public locks that would be needed to use Items in a loop).
[XmlIgnoreAttribute] long TotalBytesShared [get]
 Get the total number of bytes of all files shared.

Private Member Functions

bool CleanDirectories (DirectoryContents dc)
 Cleans the directory tree from empty directories (broken somehow).
void FillDirectories (DirectoryContents root)
 Fill a directory contents tree from the shares list.
DirectoryContents FindExistingDirectory (DirectoryContents dc, string directory_name)
 Find an existing directory in the directory contents tree.
string GetDirectoryContentsString (DirectoryContents dc)
 Recursively gets all directory contents tree.
IEnumerator IEnumerable. GetEnumerator ()
void RecurseShareDirectoryAsync (string directory)
 Recursively share a directory tree.
string ShareDirectoryAsync (string directory)
 Handler to recursively share a directory tree.
void ShareDirectoryFinished (IAsyncResult result)
 Callback for the share directory Handler.
delegate string ShareDirectoryHandler (string directory)
 Private Prototype for the Share Directory Async Handler (we return the path to ensure the correct parameter will be given to the directory finished event handler [multiple sharing threads can run at the same time ,so we need a way to differentiate them]).
SharingEntry ShareFileAsync (string filename)
 Handler to share a file (will calculate its hash , so this may take a while or a lot longer ;-) ) TODO this will be renamed to ShareFile and vice versa for a more intuitive reading of the source code.
void ShareFileFinished (IAsyncResult result)
 Callback for the share file Handler.
delegate SharingEntry ShareFileHandler (string filename)
 Private Prototype for the Share File Async Handler.

Static Private Member Functions

static string FromXmlString (string org)
 Convert a xml string back to a normal string (unquoting of invalid characters).
static string ToXmlString (string org)
 Convert a string to a valid xml string (enquoting of invalid characters).

Private Attributes

string file_list = ""
 DC++ formatted file list of our shares list.
byte[] file_list_bz2
 bz2 compressed file list
bool file_list_needs_update = true
 set to TRUE if at least one file was added/removed to indicate that the file list needs to be reconstructed

Classes

class  DirectoryContents
 Helper class for the DC++ file list construction. More...
class  SharingEntry
 A Class to hold the information needed for a shared file. More...
class  SharingEnumerator
class  SharingEnumerator2


Detailed Description

A Class that manages shares for other clients. Holds a list of shares and functions to add/remove/search for them.

Definition at line 23 of file Sharing.cs.


Member Function Documentation

void DCPlusPlus.Sharing.Add ( SharingEntry  item  ) 

Add an entry to the shares list fires the added event.

Parameters:
item the entry to be added

Definition at line 397 of file Sharing.cs.

References DCPlusPlus.Sharing.EntriesChanged, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.file_list_needs_update, DCPlusPlus.Sharing.items, and DCPlusPlus.Sharing.share_lock.

Referenced by DCPlusPlus.Sharing.RecurseShareDirectoryAsync(), and DCPlusPlus.Sharing.ShareFileFinished().

00398         {
00399             lock (share_lock)
00400             {
00401                 items.Add(item);
00402             }
00403             try
00404             {
00405                 if (EntryAdded != null)
00406                     EntryAdded(item);
00407                 if (EntriesChanged != null)
00408                     EntriesChanged();
00409             }
00410             catch (Exception ex)
00411             {
00412                 Console.WriteLine("Exception occured in added event callback: " + ex.Message);
00413             }
00414             file_list_needs_update = true;
00415         }

Here is the caller graph for this function:

bool DCPlusPlus.Sharing.CleanDirectories ( DirectoryContents  dc  )  [private]

Cleans the directory tree from empty directories (broken somehow).

Parameters:
dc the starting point from which to clean
Returns:
reserved for internal purposes

Definition at line 863 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryContents.directories, and DCPlusPlus.Sharing.DirectoryContents.files.

Referenced by DCPlusPlus.Sharing.UpdateFileLists().

00864         {//cleans directorycontents class of empty shares
00865             //check if this dir is empty and shall be removed
00866             foreach (DirectoryContents dir in dc.directories)
00867             {
00868                 if (!CleanDirectories(dir))
00869                     dc.directories.Remove(dir); //hope this will not throw an exception
00870             }
00871             if (dc.directories.Count == 0 && dc.files.Count == 0)
00872             {
00873                 return (false);
00874             }
00875             else if (dc.directories.Count == 1 && dc.files.Count == 0)
00876             {//only one shared dir here .. exchange this , this may lead into problems of incorrect tree... investigation needed
00877                 dc = dc.directories[0];
00878             }
00879             return (true);
00880         }

Here is the caller graph for this function:

void DCPlusPlus.Sharing.Clear (  ) 

Clears the shares list.

Definition at line 419 of file Sharing.cs.

References DCPlusPlus.Sharing.EntriesChanged, DCPlusPlus.Sharing.EntriesCleared, DCPlusPlus.Sharing.file_list_needs_update, DCPlusPlus.Sharing.items, and DCPlusPlus.Sharing.share_lock.

Referenced by DCPlusPlus.Sharing.TestShareSaveLoad().

00420         {
00421             lock (share_lock)
00422             {
00423                 items.Clear();
00424             }
00425             try
00426             {
00427 
00428                 if (EntriesCleared != null)
00429                     EntriesCleared();
00430                 if (EntriesChanged != null)
00431                     EntriesChanged();
00432             }
00433             catch (Exception ex)
00434             {
00435                 Console.WriteLine("Exception occured in clear event callback: " + ex.Message);
00436             }
00437             file_list_needs_update = true;
00438         }

Here is the caller graph for this function:

bool DCPlusPlus.Sharing.Contains ( SharingEntry  item  ) 

Definition at line 927 of file Sharing.cs.

References DCPlusPlus.Sharing.items, and DCPlusPlus.Sharing.share_lock.

00928         {
00929             bool ret = false;
00930             lock (share_lock)
00931             {
00932                 ret = items.Contains(item);
00933             }
00934             return (ret);
00935         }

void DCPlusPlus.Sharing.CopyTo ( SharingEntry[]  array,
int  arrayIndex 
)

Definition at line 937 of file Sharing.cs.

References DCPlusPlus.Sharing.items, and DCPlusPlus.Sharing.share_lock.

00938         {
00939             lock (share_lock)
00940             {
00941                 foreach (SharingEntry entry in items)
00942                 {
00943                     array.SetValue(entry, arrayIndex);
00944                     arrayIndex = arrayIndex + 1;
00945                 }
00946             }
00947         }

delegate void DCPlusPlus.Sharing.DirectoryFinishedEventHandler ( string  directory  ) 

Prototype for the Directory Finished Event Handler.

Parameters:
directory The directory finished sharing

delegate void DCPlusPlus.Sharing.EntriesChangedEventHandler (  ) 

Prototype for the Entries Changed Event Handler.

delegate void DCPlusPlus.Sharing.EntriesClearedEventHandler (  ) 

Prototype for the Entries Cleared Event Handler.

delegate void DCPlusPlus.Sharing.EntryAddedEventHandler ( SharingEntry  entry  ) 

Prototype for the Entry Added Event Handler.

Parameters:
entry The entry that was added to the shares

delegate void DCPlusPlus.Sharing.EntryRemovedEventHandler ( SharingEntry  entry  ) 

Prototype for the Entry Removed Event Handler.

Parameters:
entry The entry that was removed from the shares

void DCPlusPlus.Sharing.FillDirectories ( DirectoryContents  root  )  [private]

Fill a directory contents tree from the shares list.

Parameters:
root 

Definition at line 827 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryContents.directories, DCPlusPlus.Sharing.DirectoryContents.directory_name, DCPlusPlus.Sharing.DirectoryContents.files, DCPlusPlus.Sharing.FindExistingDirectory(), DCPlusPlus.Sharing.items, and DCPlusPlus.Sharing.share_lock.

Referenced by DCPlusPlus.Sharing.UpdateFileLists().

00828         {
00829             lock (share_lock)
00830             {
00831                 foreach (SharingEntry entry in items)
00832                 {
00833                     string path = Path.GetDirectoryName(entry.Filename);
00834                     path = path.Substring(3);//strip away the drive letter --> WIN32 specific!!!! needs to be changed if other platform shall be supported
00835                     char[] seps = {'\\'};
00836                     string[] paths = path.Split(seps);
00837                     DirectoryContents actual = root;
00838                     foreach (string path_part in paths)
00839                     {
00840                         DirectoryContents existing = FindExistingDirectory(actual,path_part);
00841                         if (existing == null)
00842                         {
00843                             DirectoryContents add = new DirectoryContents();
00844                             add.directory_name = path_part;
00845                             actual.directories.Add(add);
00846                             existing = add;
00847                         }
00848                         actual = existing;
00849                     }
00850                     if(!actual.files.Contains(entry))
00851                         actual.files.Add(entry);
00852 
00853 
00854                 }
00855             }
00856         }

Here is the call graph for this function:

Here is the caller graph for this function:

DirectoryContents DCPlusPlus.Sharing.FindExistingDirectory ( DirectoryContents  dc,
string  directory_name 
) [private]

Find an existing directory in the directory contents tree.

Parameters:
dc the starting point of search
directory_name the path of the directory to search for
Returns:
the directory sub tree found or NULL

Definition at line 815 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryContents.directories.

Referenced by DCPlusPlus.Sharing.FillDirectories().

00816         {
00817             foreach (DirectoryContents dir in dc.directories)
00818             {
00819                 if (dir.directory_name == directory_name) return (dir);
00820             }
00821             return (null);
00822         }

Here is the caller graph for this function:

static string DCPlusPlus.Sharing.FromXmlString ( string  org  )  [static, private]

Convert a xml string back to a normal string (unquoting of invalid characters).

Parameters:
org xml string
Returns:
an unquoted string

Definition at line 782 of file Sharing.cs.

00783         {
00784             if (org == null) return ("");
00785             string tmp = new string(org.ToCharArray());
00786             //System.Console.WriteLine("Stripping Linefeeds on string content: '" + tmp+"'");
00787             while (tmp.IndexOf("&amp;") != -1) tmp = tmp.Replace("&amp;", "&");
00788             while (tmp.IndexOf("&lt;") != -1) tmp = tmp.Replace("&lt;", "<");
00789             while (tmp.IndexOf("&gt;") != -1) tmp = tmp.Replace("&gt;", ">");
00790             while (tmp.IndexOf("&quot;") != -1) tmp = tmp.Replace("&quot;", "\"");
00791             while (tmp.IndexOf("&apos;") != -1) tmp = tmp.Replace("&apos;", "'");
00792             for (int i = 0; i < 32; i++)
00793             {
00794                 char c = Convert.ToChar(i);
00795                 if (i != 0x09 && i != 0x0a && i != 0x0d)
00796                 {
00797                     int pos = -1;
00798                     string s = "&#" + Convert.ToString(c, 16) + ";";
00799                     while ((pos = tmp.IndexOf(s)) != -1)
00800                     {
00801                         tmp = tmp.Remove(pos, s.Length);
00802                         tmp = tmp.Insert(pos, c.ToString());
00803                     }
00804                 }
00805             }
00806             //System.Console.WriteLine("after multiple newline remove check: '" + tmp + "'");
00807             return (tmp);
00808         }

string DCPlusPlus.Sharing.GetDirectoryContentsString ( DirectoryContents  dc  )  [private]

Recursively gets all directory contents tree.

Parameters:
dc the starting point of content gathering
Returns:
the directory contents

Definition at line 886 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryContents.directories, DCPlusPlus.Sharing.DirectoryContents.directory_name, DCPlusPlus.Sharing.DirectoryContents.files, and DCPlusPlus.Sharing.ToXmlString().

Referenced by DCPlusPlus.Sharing.UpdateFileLists().

00887         {//recursively get all contents in one string
00888             string dir_string = "";
00889             if (dc.directory_name != "")
00890                 dir_string += "<Directory Name=\"" + ToXmlString(dc.directory_name) + "\">\n";
00891             foreach (DirectoryContents dir in dc.directories)
00892             {
00893                 dir_string += GetDirectoryContentsString(dir);
00894             }
00895             if (dc.directory_name != "")
00896             {
00897                 foreach (SharingEntry file in dc.files)
00898                 {
00899                     dir_string += "<File Name=\"" + ToXmlString(Path.GetFileName(file.Filename)) + "\" Size=\"" + file.Filesize + "\" TTH=\"" + file.TTH + "\"/>\n";
00900                 }
00901                 dir_string += "</Directory>\n";
00902             }
00903             return (dir_string);
00904         }

Here is the call graph for this function:

Here is the caller graph for this function:

IEnumerator IEnumerable. DCPlusPlus.Sharing.GetEnumerator (  )  [private]

Definition at line 1068 of file Sharing.cs.

References DCPlusPlus.Sharing.items.

01069         {
01070             return (new SharingEnumerator(items.ToArray()));
01071         }

IEnumerator<SharingEntry> DCPlusPlus.Sharing.GetEnumerator (  ) 

Definition at line 1022 of file Sharing.cs.

References DCPlusPlus.Sharing.items.

01023         {
01024             return (new SharingEnumerator2(items.ToArray()));
01025         }

string DCPlusPlus.Sharing.GetFileListXml (  ) 

Get the DC++ file list.

Returns:
the uncompressed file list

Definition at line 909 of file Sharing.cs.

References DCPlusPlus.Sharing.file_list, DCPlusPlus.Sharing.file_list_needs_update, and DCPlusPlus.Sharing.UpdateFileLists().

Referenced by DCPlusPlus.Sharing.TestEmptyGetFileListXml(), and DCPlusPlus.Sharing.TestGetFileListXml().

00910         {
00911             if (file_list_needs_update)
00912                 UpdateFileLists();
00913             return (file_list);
00914         }

Here is the call graph for this function:

Here is the caller graph for this function:

byte [] DCPlusPlus.Sharing.GetFileListXmlBZ2 (  ) 

Get the compressed DC++ file list.

Returns:
the compressed file list

Definition at line 919 of file Sharing.cs.

References DCPlusPlus.Sharing.file_list_bz2, DCPlusPlus.Sharing.file_list_needs_update, and DCPlusPlus.Sharing.UpdateFileLists().

Referenced by DCPlusPlus.Client.SetupPeerEventHandler().

00920         {
00921             if (file_list_needs_update)
00922                 UpdateFileLists();
00923             return(file_list_bz2);
00924         }

Here is the call graph for this function:

Here is the caller graph for this function:

SharingEntry DCPlusPlus.Sharing.GetShareByFilename ( string  filename  ) 

Tries to find a Share identified by its filename.

Parameters:
filename the filename of the share to be found
Returns:
an existing entry or NULL if none was found

Definition at line 354 of file Sharing.cs.

References DCPlusPlus.Sharing.items, and DCPlusPlus.Sharing.share_lock.

Referenced by DCPlusPlus.Sharing.GetShareByFileRequest(), and DCPlusPlus.Sharing.TestShareSearchRemove().

00355         {
00356             SharingEntry ret = null;
00357             lock (share_lock)
00358             {
00359                 foreach (SharingEntry entry in items)
00360                 {
00361                     if (entry.Filename.EndsWith(filename)) //maybe change this to a more sophisticated approach
00362                     {
00363                         //Console.WriteLine("Found entry by filename: "+filename);
00364                         ret =  entry;
00365                         break;
00366                     }
00367                 }
00368             }
00369             return (ret);
00370         }

Here is the caller graph for this function:

SharingEntry DCPlusPlus.Sharing.GetShareByFileRequest ( string  file_request  ) 

Tries to find a Share identified by its TTH or filename to search for a TTH put a TTH/ in front the Base32 encoded TTH.

Parameters:
file_request TTH or filename of share to be found
Returns:
an existing entry for the file request or NULL if none was found

Definition at line 342 of file Sharing.cs.

References DCPlusPlus.Sharing.GetShareByFilename(), and DCPlusPlus.Sharing.GetShareByTTH().

Referenced by DCPlusPlus.Client.SetupPeerEventHandler().

00343         {
00344             if(file_request.StartsWith("TTH/"))
00345                 return(GetShareByTTH(file_request.Substring(4)));
00346             return (GetShareByFilename(file_request));
00347         }

Here is the call graph for this function:

Here is the caller graph for this function:

SharingEntry DCPlusPlus.Sharing.GetShareByTTH ( string  tth  ) 

Tries to find a share identified by its tth.

Parameters:
tth the tth of the share to be found
Returns:
an existing entry or NULL if none was found

Definition at line 376 of file Sharing.cs.

References DCPlusPlus.Sharing.items, and DCPlusPlus.Sharing.share_lock.

Referenced by DCPlusPlus.Client.ConnectHub(), and DCPlusPlus.Sharing.GetShareByFileRequest().

00377         {
00378             SharingEntry ret = null;
00379             lock (share_lock)
00380             {
00381                 foreach (SharingEntry entry in items)
00382                 {
00383                     if (entry.HasTTH && entry.TTH == tth)
00384                     {
00385                         ret = entry;
00386                         break;
00387                     }
00388                 }
00389             }
00390             return (ret);
00391         }

Here is the caller graph for this function:

void DCPlusPlus.Sharing.LoadSharesFromXml ( string  xml  ) 

Load shares from a xml string.

Parameters:
xml a xml representation of a shares list

Definition at line 514 of file Sharing.cs.

References DCPlusPlus.Sharing.EntriesChanged, DCPlusPlus.Sharing.EntriesCleared, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.file_list_needs_update, DCPlusPlus.Sharing.Items, DCPlusPlus.Sharing.items, DCPlusPlus.Sharing.share_lock, and DCPlusPlus.Sharing.total_bytes_shared.

Referenced by DCPlusPlus.Sharing.LoadSharesFromXmlFile().

00515         {
00516             lock (share_lock)
00517             {
00518                 items.Clear();
00519             }
00520             if (EntriesCleared != null)
00521                 EntriesCleared();
00522             Sharing s = new Sharing();
00523             try
00524             {
00525                 XmlSerializer serializer = new XmlSerializer(typeof(Sharing));
00526                 MemoryStream ms = new MemoryStream(System.Text.Encoding.Default.GetBytes(xml));
00527                 s = (Sharing)serializer.Deserialize(ms);
00528             }
00529             catch (Exception ex)
00530             {
00531                 Console.WriteLine("Error deserializing queue: " + ex.Message);
00532             }
00533             if (s != null)
00534             {
00535                 lock (share_lock)
00536                 {
00537                     items = s.Items;
00538                 }
00539             }
00540             if (EntryAdded != null)
00541             {
00542                 lock (share_lock)
00543                 {
00544                     foreach (SharingEntry entry in items)
00545                     {
00546                         total_bytes_shared += entry.Filesize;
00547                         EntryAdded(entry);
00548                     }
00549                 }
00550             }
00551             if (EntriesChanged != null)
00552             {
00553                 EntriesChanged();
00554             }
00555             file_list_needs_update = true;
00556         }

Here is the caller graph for this function:

void DCPlusPlus.Sharing.LoadSharesFromXmlFile ( string  filename  ) 

Load shares from a xml file.

Parameters:
filename the filename of the shares xml file

Definition at line 589 of file Sharing.cs.

References DCPlusPlus.Sharing.LoadSharesFromXml().

Referenced by DCPlusPlus.Sharing.TestShareSaveLoad().

00590         {
00591             try
00592             {
00593                 if (!File.Exists(filename)) return;
00594                 LoadSharesFromXml(System.IO.File.ReadAllText(filename));
00595             }
00596             catch (Exception ex)
00597             {
00598                 Console.WriteLine("Error loading queue from: " + filename + " : " + ex.Message);
00599             }
00600         }

Here is the call graph for this function:

Here is the caller graph for this function:

void DCPlusPlus.Sharing.RecurseShareDirectoryAsync ( string  directory  )  [private]

Recursively share a directory tree.

Parameters:
directory the path of the root directory to be shared

Definition at line 262 of file Sharing.cs.

References DCPlusPlus.Sharing.Add(), DCPlusPlus.Sharing.ShareFileAsync(), and DCPlusPlus.Sharing.total_bytes_shared.

Referenced by DCPlusPlus.Sharing.ShareDirectoryAsync().

00263         {
00264             if (!Directory.Exists(directory)) return;
00265             string[] files = Directory.GetFiles(directory);
00266             string[] dirs = Directory.GetDirectories(directory);
00267             foreach (string dir in dirs)
00268             {
00269                 if (System.IO.Directory.Exists(dir))
00270                 {
00271                     RecurseShareDirectoryAsync(dir);
00272                 }
00273             }
00274             foreach (string file in files)
00275             {
00276                 if (System.IO.File.Exists(file))
00277                 {
00278                     SharingEntry entry = ShareFileAsync(file);
00279                     if (entry != null)
00280                     {
00281                         total_bytes_shared += entry.Filesize;
00282                         Add(entry);
00283                     }
00284                 }
00285             }
00286         }

Here is the call graph for this function:

Here is the caller graph for this function:

bool DCPlusPlus.Sharing.Remove ( string  filename  ) 

Remove an entry from the shares list fires the removed event if an entry was removed.

Parameters:
filename the filename of the entry to remove
Returns:
TRUE if an entry was removed

Definition at line 476 of file Sharing.cs.

References DCPlusPlus.Sharing.EntriesChanged, DCPlusPlus.Sharing.EntryRemoved, DCPlusPlus.Sharing.file_list_needs_update, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.items, DCPlusPlus.Sharing.share_lock, and DCPlusPlus.Sharing.total_bytes_shared.

00477         {
00478             SharingEntry ret = null;
00479             lock (share_lock)
00480             {
00481                 foreach (SharingEntry entry in items)
00482                 {
00483                     if (entry.Filename == filename)
00484                     {
00485                         items.Remove(entry);
00486                         ret = entry;
00487                         break;
00488                     }
00489                 }
00490             }
00491             if (ret != null)
00492             {
00493                 total_bytes_shared -= ret.Filesize;
00494                 try
00495                 {
00496                     if (EntryRemoved != null)
00497                         EntryRemoved(ret);
00498                     if (EntriesChanged != null)
00499                         EntriesChanged();
00500                 }
00501                 catch (Exception ex)
00502                 {
00503                     Console.WriteLine("Exception occured in remove event callback: " + ex.Message);
00504                 }
00505                 file_list_needs_update = true;
00506                 return (true);
00507             }
00508             return (false);
00509         }

bool DCPlusPlus.Sharing.Remove ( SharingEntry  item  ) 

Remove an entry from the shares list fires the removed event if an entry was removed.

Parameters:
item the entry to remove
Returns:
TRUE if an entry was removed

Definition at line 446 of file Sharing.cs.

References DCPlusPlus.Sharing.EntriesChanged, DCPlusPlus.Sharing.EntryRemoved, DCPlusPlus.Sharing.file_list_needs_update, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.items, DCPlusPlus.Sharing.share_lock, and DCPlusPlus.Sharing.total_bytes_shared.

Referenced by DCPlusPlus.Sharing.TestShareSearchRemove().

00447         {
00448             total_bytes_shared -= item.Filesize;
00449             bool ret = false;
00450             lock (share_lock)
00451             {
00452                 ret = items.Remove(item);
00453             }
00454             if (ret == false) return (false); //only call event handler if an entry was actually removed
00455             try
00456             {
00457                 if (EntryRemoved != null)
00458                     EntryRemoved(item);
00459                 if (EntriesChanged != null)
00460                     EntriesChanged();
00461             }
00462             catch (Exception ex)
00463             {
00464                 Console.WriteLine("Exception occured in remove event callback: " + ex.Message);
00465             }
00466             file_list_needs_update = true;
00467             return (ret);
00468         }

Here is the caller graph for this function:

string DCPlusPlus.Sharing.SaveSharesToXml (  ) 

Get shares list represented as a xml string.

Returns:
the share list xml formatted

Definition at line 561 of file Sharing.cs.

References DCPlusPlus.Sharing.share_lock.

Referenced by DCPlusPlus.Sharing.SaveSharesToXmlFile().

00562         {
00563             //nice way but seems to not work with list<> members
00564             lock (share_lock)
00565             {
00566                 string ret = "";
00567                 try
00568                 {
00569                     XmlSerializer serializer = new XmlSerializer(typeof(Sharing));
00570                     MemoryStream ms = new MemoryStream();
00571                     serializer.Serialize(ms, this);
00572                     ms.Flush();
00573                     ret = System.Text.Encoding.Default.GetString(ms.GetBuffer(), 0, (int)ms.Length);//TODO ... 4gb crash border
00574                     //ret = ret.TrimEnd((char)0);
00575 
00576                 }
00577                 catch (Exception ex)
00578                 {
00579                     Console.WriteLine("Error serializing queue: " + ex.Message);
00580                     return (null);
00581                 }
00582                 return (ret);
00583             }
00584         }

Here is the caller graph for this function:

void DCPlusPlus.Sharing.SaveSharesToXmlFile ( string  filename  ) 

Save shares to a xml file.

Parameters:
filename the filename to write to

Definition at line 605 of file Sharing.cs.

References DCPlusPlus.Sharing.SaveSharesToXml().

Referenced by DCPlusPlus.Sharing.TestShareSaveLoad().

00606         {
00607             try
00608             {
00609                 if (File.Exists(filename + ".backup") && File.Exists(filename))
00610                     File.Delete(filename + ".backup");
00611                 if (File.Exists(filename))
00612                     File.Move(filename, filename + ".backup");
00613 
00614                 System.IO.File.WriteAllText(filename, SaveSharesToXml());
00615             }
00616             catch (Exception ex)
00617             {
00618                 Console.WriteLine("Error saving queue to: " + filename + " : " + ex.Message);
00619             }
00620         }

Here is the call graph for this function:

Here is the caller graph for this function:

void DCPlusPlus.Sharing.ShareDirectory ( string  directory  ) 

Asynchronously share a directory tree [Non blocking].

Parameters:
directory the path of the root directory to be shared

Definition at line 330 of file Sharing.cs.

References DCPlusPlus.Sharing.ShareDirectoryAsync(), DCPlusPlus.Sharing.ShareDirectoryFinished(), and DCPlusPlus.Sharing.ShareDirectoryHandler().

Referenced by DCPlusPlus.Sharing.TestEmptyGetFileListXml(), DCPlusPlus.Sharing.TestGetFileListXml(), DCPlusPlus.Sharing.TestShareDirectory(), DCPlusPlus.Sharing.TestShareSaveLoad(), DCPlusPlus.Sharing.TestShareSearchRemove(), DCPlusPlus.Sharing.TestSharingNotExistingDirectory(), and DCPlusPlus.Sharing.TestTTHs().

00331         {
00332             ShareDirectoryHandler sdh = new ShareDirectoryHandler(ShareDirectoryAsync);
00333             IAsyncResult result = sdh.BeginInvoke(directory, new AsyncCallback(ShareDirectoryFinished), sdh);
00334         }

Here is the call graph for this function:

Here is the caller graph for this function:

string DCPlusPlus.Sharing.ShareDirectoryAsync ( string  directory  )  [private]

Handler to recursively share a directory tree.

Parameters:
directory the path of the root directory to be shared
Returns:
the directories path that was given as parameter

Definition at line 253 of file Sharing.cs.

References DCPlusPlus.Sharing.RecurseShareDirectoryAsync().

Referenced by DCPlusPlus.Sharing.ShareDirectory().

00254         {//recurse into directories
00255             RecurseShareDirectoryAsync(directory);
00256             return (directory);
00257         }

Here is the call graph for this function:

Here is the caller graph for this function:

void DCPlusPlus.Sharing.ShareDirectoryFinished ( IAsyncResult  result  )  [private]

Callback for the share directory Handler.

Parameters:
result Async Result/State

Definition at line 306 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryFinished, DCPlusPlus.Sharing.file_list_needs_update, and DCPlusPlus.Sharing.ShareDirectoryHandler().

Referenced by DCPlusPlus.Sharing.ShareDirectory().

00307         {
00308             ShareDirectoryHandler sdh = (ShareDirectoryHandler)result.AsyncState;
00309             string directory = sdh.EndInvoke(result);
00310             if (DirectoryFinished != null)
00311                 DirectoryFinished(directory);
00312             file_list_needs_update = true;
00313         }

Here is the call graph for this function:

Here is the caller graph for this function:

delegate string DCPlusPlus.Sharing.ShareDirectoryHandler ( string  directory  )  [private]

Private Prototype for the Share Directory Async Handler (we return the path to ensure the correct parameter will be given to the directory finished event handler [multiple sharing threads can run at the same time ,so we need a way to differentiate them]).

Parameters:
directory the path of the directory to be shared
Returns:
the directories path that was given as parameter

Referenced by DCPlusPlus.Sharing.ShareDirectory(), and DCPlusPlus.Sharing.ShareDirectoryFinished().

Here is the caller graph for this function:

void DCPlusPlus.Sharing.ShareFile ( string  filename  ) 

Asynchronously share a file [Non blocking].

Parameters:
filename the filename of the file to be shared

Definition at line 320 of file Sharing.cs.

References DCPlusPlus.Sharing.ShareFileAsync(), DCPlusPlus.Sharing.ShareFileFinished(), and DCPlusPlus.Sharing.ShareFileHandler().

Referenced by DCPlusPlus.Sharing.TestShareFile().

00321         {
00322             ShareFileHandler sfh = new ShareFileHandler(ShareFileAsync);
00323             IAsyncResult result = sfh.BeginInvoke(filename, new AsyncCallback(ShareFileFinished), sfh);
00324         }

Here is the call graph for this function:

Here is the caller graph for this function:

SharingEntry DCPlusPlus.Sharing.ShareFileAsync ( string  filename  )  [private]

Handler to share a file (will calculate its hash , so this may take a while or a lot longer ;-) ) TODO this will be renamed to ShareFile and vice versa for a more intuitive reading of the source code.

Parameters:
filename the filename of the file to be shared
Returns:
an entry of the file that was shared

Definition at line 212 of file Sharing.cs.

References DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, and DCPlusPlus.Sharing.SharingEntry.TTH.

Referenced by DCPlusPlus.Sharing.RecurseShareDirectoryAsync(), and DCPlusPlus.Sharing.ShareFile().

00213         {//share file
00214             if (!System.IO.File.Exists(filename)) return (null);
00215             SharingEntry entry = new SharingEntry();
00216             entry.Filename = filename;
00217             try
00218             {
00219                 System.IO.FileInfo fi = new FileInfo(filename);
00220             entry.Filesize = fi.Length;
00221             //now try to hash the file also
00222             /*
00223             Tiger192 tiger = new Tiger192();
00224             byte[] file_contents = System.IO.File.ReadAllBytes(filename);
00225             tiger.ComputeHash(file_contents, 0, file_contents.Length-1);
00226             Console.WriteLine("hash:" + Base32.GetString(tiger.Hash));
00227             entry.TTH = Base32.GetString(tiger.Hash);
00228             */
00229             ThexThreaded TTH = new ThexThreaded();
00230             entry.TTH = Base32.ToBase32String(TTH.GetTTH_Value(filename));
00231 
00232             }
00233             catch (Exception ex)
00234             {
00235                 Console.WriteLine("exception during hashing:" + ex.Message);
00236             }
00237             return (entry);
00238         }

Here is the caller graph for this function:

void DCPlusPlus.Sharing.ShareFileFinished ( IAsyncResult  result  )  [private]

Callback for the share file Handler.

Parameters:
result Async Result/State

Definition at line 291 of file Sharing.cs.

References DCPlusPlus.Sharing.Add(), DCPlusPlus.Sharing.file_list_needs_update, DCPlusPlus.Sharing.ShareFileHandler(), and DCPlusPlus.Sharing.total_bytes_shared.

Referenced by DCPlusPlus.Sharing.ShareFile().

00292         {
00293             ShareFileHandler sfh = (ShareFileHandler)result.AsyncState;
00294             SharingEntry entry = sfh.EndInvoke(result);
00295             if (entry != null)
00296             {
00297                 total_bytes_shared += entry.Filesize;
00298                 Add(entry);
00299             }
00300             file_list_needs_update = true;
00301         }

Here is the call graph for this function:

Here is the caller graph for this function:

delegate SharingEntry DCPlusPlus.Sharing.ShareFileHandler ( string  filename  )  [private]

Private Prototype for the Share File Async Handler.

Parameters:
filename the filename of the file to be shared
Returns:
an entry of the file that was shared

Referenced by DCPlusPlus.Sharing.ShareFile(), and DCPlusPlus.Sharing.ShareFileFinished().

Here is the caller graph for this function:

[Test] void DCPlusPlus.Sharing.TestEmptyGetFileListXml (  ) 

Test to see if a valid empty file list will be created.

Definition at line 1357 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryFinished, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.file_list, DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.GetFileListXml(), DCPlusPlus.Sharing.ShareDirectory(), and DCPlusPlus.Sharing.SharingEntry.TTH.

01358         {
01359             Console.WriteLine("Test to see if a correct empty filelist was created.");
01360             bool wait = true;
01361             Sharing s = new Sharing();
01362             s.EntryAdded = delegate(SharingEntry entry)
01363             {
01364                 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01365             };
01366             s.DirectoryFinished = delegate(string directory)
01367             {
01368                 wait = false;
01369             };
01370             s.ShareDirectory("..\\..\\..\\TestFileListEmpty"); 
01371 
01372             Console.WriteLine("Waiting for data");
01373             DateTime start = DateTime.Now;
01374             while (wait)
01375             {
01376                 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01377                 {
01378                     Console.WriteLine("");
01379                     Console.WriteLine("Operation took too long");
01380                     wait = false;
01381                     Assert.Fail("Operation took too long");
01382                 }
01383                 Console.Write(".");
01384                 Thread.Sleep(250);
01385             }
01386             string file_list = s.GetFileListXml();
01387             Console.WriteLine("\nfilelist:\n" + file_list);
01388 
01389             //now check if items are correct
01390             Assert.IsTrue(file_list == "", "Empty FileList expected.");
01391             Console.WriteLine("Empty GetFilesList Creation Test successful.");
01392         }

Here is the call graph for this function:

[Test] void DCPlusPlus.Sharing.TestGetFileListXml (  ) 

Test to see a valid file list will be created.

Definition at line 1397 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryFinished, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.file_list, DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.GetFileListXml(), DCPlusPlus.Sharing.ShareDirectory(), and DCPlusPlus.Sharing.SharingEntry.TTH.

01398         {
01399             Console.WriteLine("Test to see if a correct filelist was created.");
01400             bool wait = true;
01401             Sharing s = new Sharing();
01402             s.EntryAdded = delegate(SharingEntry entry)
01403             {
01404                 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01405             };
01406             s.DirectoryFinished = delegate(string directory)
01407             {
01408                 wait = false;
01409             };
01410             s.ShareDirectory("..\\..\\..\\TestFileList");
01411 
01412             Console.WriteLine("Waiting for data");
01413             DateTime start = DateTime.Now;
01414             while (wait)
01415             {
01416                 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01417                 {
01418                     Console.WriteLine("");
01419                     Console.WriteLine("Operation took too long");
01420                     wait = false;
01421                     Assert.Fail("Operation took too long");
01422                 }
01423                 Console.Write(".");
01424                 Thread.Sleep(250);
01425             }
01426             string file_list = s.GetFileListXml();
01427             Console.WriteLine("\nfilelist:\n" + file_list);
01428 
01429             //now check if items are correct
01430             Assert.IsTrue(file_list == "", "Empty FileList expected.");
01431             Console.WriteLine("GetFilesList Creation Test successful.");
01432         }

Here is the call graph for this function:

[Test] void DCPlusPlus.Sharing.TestShareDirectory (  ) 

Test to see if sharing of a directory works as expected.

Definition at line 1114 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryFinished, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.items, DCPlusPlus.Sharing.ShareDirectory(), and DCPlusPlus.Sharing.SharingEntry.TTH.

01115         {
01116             Console.WriteLine("Test to share a directory.");
01117             bool wait = true;
01118             Sharing s = new Sharing();
01119             s.EntryAdded = delegate(SharingEntry entry)
01120             {
01121                 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01122             };
01123             s.DirectoryFinished = delegate(string directory)
01124             {
01125                 wait = false;
01126             };
01127 
01128             s.ShareDirectory("..\\..\\..\\TestDateien");
01129 
01130             Console.WriteLine("Waiting for data");
01131             DateTime start = DateTime.Now;
01132             while (wait)
01133             {
01134                 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01135                 {
01136                     Console.WriteLine("");
01137                     Console.WriteLine("Operation took too long");
01138                     wait = false;
01139                     Assert.Fail("Operation took too long");
01140                 }
01141                 Console.Write(".");
01142                 Thread.Sleep(250);
01143             }
01144 
01145             //now check if items are correct
01146             Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01147             Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01148             Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01149             Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01150             Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01151             Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01152             Console.WriteLine("Share Directory Test successful.");
01153         }

Here is the call graph for this function:

[Test] void DCPlusPlus.Sharing.TestShareFile (  ) 

Test to see if sharing of a file works as expected.

Definition at line 1080 of file Sharing.cs.

References DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.ShareFile(), and DCPlusPlus.Sharing.SharingEntry.TTH.

01081         {
01082             Console.WriteLine("Test to share a file.");
01083             bool wait = true;
01084             Sharing s = new Sharing();
01085             s.EntryAdded = delegate(SharingEntry entry)
01086             {
01087                 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01088                 Assert.IsTrue(entry.Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01089                 Assert.IsTrue(entry.Filesize == 6053888, "Filesize not correct(test.mp3).");
01090                 wait = false;
01091             };
01092             s.ShareFile("..\\..\\..\\TestDateien\\test.mp3");
01093 
01094             Console.WriteLine("Waiting for data");
01095             DateTime start = DateTime.Now;
01096             while (wait)
01097             {
01098                 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01099                 {
01100                     Console.WriteLine("");
01101                     Console.WriteLine("Operation took too long");
01102                     wait = false;
01103                     Assert.Fail("Operation took too long");
01104                 }
01105                 Console.Write(".");
01106                 Thread.Sleep(250);
01107             }
01108             Console.WriteLine("Share File Test successful.");
01109         }

Here is the call graph for this function:

[Test] void DCPlusPlus.Sharing.TestShareSaveLoad (  ) 

Test to see if loading and saving of shares works as expected.

Definition at line 1158 of file Sharing.cs.

References DCPlusPlus.Sharing.Clear(), DCPlusPlus.Sharing.DirectoryFinished, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.items, DCPlusPlus.Sharing.LoadSharesFromXmlFile(), DCPlusPlus.Sharing.SaveSharesToXmlFile(), DCPlusPlus.Sharing.ShareDirectory(), and DCPlusPlus.Sharing.SharingEntry.TTH.

01159         {
01160             Console.WriteLine("Test to save and load shares.");
01161             bool wait = true;
01162             Sharing s = new Sharing();
01163             s.EntryAdded = delegate(SharingEntry entry)
01164             {
01165                 //Console.WriteLine("");
01166                 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01167             };
01168             s.DirectoryFinished = delegate(string directory)
01169             {
01170                 wait = false;
01171             };
01172             s.ShareDirectory("..\\..\\..\\TestDateien");
01173 
01174             Console.WriteLine("Waiting for data");
01175             DateTime start = DateTime.Now;
01176             while (wait)
01177             {
01178                 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01179                 {
01180                     Console.WriteLine("");
01181                     Console.WriteLine("Operation took too long");
01182                     wait = false;
01183                     Assert.Fail("Operation took too long");
01184                 }
01185                 Console.Write(".");
01186                 Thread.Sleep(250);
01187             }
01188             Console.WriteLine("");
01189             Console.WriteLine("Sharing of files completed.");
01190             //now check if items are correct
01191             Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01192             Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01193             Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01194             Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01195             Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01196             Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01197             s.SaveSharesToXmlFile("..\\..\\..\\TestDateien\\test_shares.xml");
01198             Console.WriteLine("Saved Shares.");
01199             s.Clear();
01200             Console.WriteLine("Cleared Shares");
01201             Assert.IsTrue(s.items.Count == 0, "Clearing Shares failed.");
01202             s.LoadSharesFromXmlFile("..\\..\\..\\TestDateien\\test_shares.xml");
01203             Console.WriteLine("Shares loaded.");
01204             Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01205             Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01206             Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01207             Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01208             Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01209             Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01210             Console.WriteLine("Save and Load Shares Test successful.");
01211         }

Here is the call graph for this function:

[Test] void DCPlusPlus.Sharing.TestShareSearchRemove (  ) 

Test to see if searching and removing of shares works as expected.

Definition at line 1216 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryFinished, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.GetShareByFilename(), DCPlusPlus.Sharing.items, DCPlusPlus.Sharing.Remove(), DCPlusPlus.Sharing.ShareDirectory(), and DCPlusPlus.Sharing.SharingEntry.TTH.

01217         {
01218             Console.WriteLine("Test to search and remove a share.");
01219             bool wait = true;
01220             Sharing s = new Sharing();
01221             s.EntryAdded = delegate(SharingEntry entry)
01222             {
01223                 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01224             };
01225             s.DirectoryFinished = delegate(string directory)
01226             {
01227                 wait = false;
01228             };
01229             s.ShareDirectory("..\\..\\..\\TestDateien");
01230 
01231             Console.WriteLine("Waiting for data");
01232             DateTime start = DateTime.Now;
01233             while (wait)
01234             {
01235                 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01236                 {
01237                     Console.WriteLine("");
01238                     Console.WriteLine("Operation took too long");
01239                     wait = false;
01240                     Assert.Fail("Operation took too long");
01241                 }
01242                 Console.Write(".");
01243                 Thread.Sleep(250);
01244             }
01245             Console.WriteLine("Sharing of files completed.");
01246             //now check if items are correct
01247             Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01248             Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01249             Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test2.mp3).");
01250             Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01251             Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01252             Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01253             int num = s.items.Count;
01254             SharingEntry found = s.GetShareByFilename("..\\..\\..\\TestDateien\\test.mp3");
01255             Assert.IsTrue(found != null, "Searching Share failed.");
01256             s.Remove(found);
01257             Console.WriteLine("Removed Share");
01258             Assert.IsTrue(s.items.Count == num - 1, "Removing Share failed(test.mp3).");
01259             found = s.GetShareByFilename("..\\..\\..\\TestDateien\\test.mp3");
01260             Assert.IsTrue(found == null, "Removing Share failed(test.mp3).");
01261             s.Remove("..\\..\\..\\TestDateien\\test2.mp3");
01262             Console.WriteLine("Removed another Share");
01263             Assert.IsTrue(s.items.Count == num - 2, "Removing Share failed(test2.mp3).");
01264             found = s.GetShareByFilename("..\\..\\..\\TestDateien\\test2.mp3");
01265             Assert.IsTrue(found == null, "Removing Share failed(test2.mp3).");
01266             Console.WriteLine("Search and Remove Test successful.");
01267         }

Here is the call graph for this function:

[Test] void DCPlusPlus.Sharing.TestSharingNotExistingDirectory (  ) 

Test to see if sharing of a non existing directory fails as expected (to check for crashes of unexpected uncatched exceptions).

Definition at line 1319 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryFinished, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.items, DCPlusPlus.Sharing.ShareDirectory(), and DCPlusPlus.Sharing.SharingEntry.TTH.

01320         {
01321             Console.WriteLine("Test to see if no files were shared.");
01322             bool wait = true;
01323             Sharing s = new Sharing();
01324             s.EntryAdded = delegate(SharingEntry entry)
01325             {
01326                 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01327             };
01328             s.DirectoryFinished = delegate(string directory)
01329             {
01330                 wait = false;
01331             };
01332             s.ShareDirectory("..\\..\\..\\NotExistingDirectory");
01333 
01334             Console.WriteLine("Waiting for data");
01335             DateTime start = DateTime.Now;
01336             while (wait)
01337             {
01338                 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01339                 {
01340                     Console.WriteLine("");
01341                     Console.WriteLine("Operation took too long");
01342                     wait = false;
01343                     Assert.Fail("Operation took too long");
01344                 }
01345                 Console.Write(".");
01346                 Thread.Sleep(250);
01347             }
01348 
01349             //now check if items are correct
01350             Assert.IsTrue(s.items.Count == 0, "Test failed : More than none files were shared.");
01351             Console.WriteLine("Sharing Empty Dir Test successful.");
01352         }

Here is the call graph for this function:

[Test] void DCPlusPlus.Sharing.TestTTHs (  ) 

Test to see if hashing of files works as expected.

Definition at line 1272 of file Sharing.cs.

References DCPlusPlus.Sharing.DirectoryFinished, DCPlusPlus.Sharing.EntryAdded, DCPlusPlus.Sharing.SharingEntry.Filename, DCPlusPlus.Sharing.SharingEntry.Filesize, DCPlusPlus.Sharing.items, DCPlusPlus.Sharing.ShareDirectory(), and DCPlusPlus.Sharing.SharingEntry.TTH.

01273         {
01274             Console.WriteLine("Test to see if correct TTHs were created.");
01275             bool wait = true;
01276             Sharing s = new Sharing();
01277             s.EntryAdded = delegate(SharingEntry entry)
01278             {
01279                 Console.WriteLine("File Added: " + entry.Filename + ", filesize: " + entry.Filesize + ",tth: " + entry.TTH);
01280             };
01281             s.DirectoryFinished = delegate(string directory)
01282             {
01283                 wait = false;
01284             };
01285             s.ShareDirectory("..\\..\\..\\TestDateien");
01286 
01287             Console.WriteLine("Waiting for data");
01288             DateTime start = DateTime.Now;
01289             while (wait)
01290             {
01291                 if (DateTime.Now - start > new TimeSpan(0, 0, 30))
01292                 {
01293                     Console.WriteLine("");
01294                     Console.WriteLine("Operation took too long");
01295                     wait = false;
01296                     Assert.Fail("Operation took too long");
01297                 }
01298                 Console.Write(".");
01299                 Thread.Sleep(250);
01300             }
01301 
01302             //now check if items are correct
01303             Assert.IsTrue(s.items[0].Filename == "..\\..\\..\\TestDateien\\2sd.avi", "Filename not correct(2sd.avi).");
01304             Assert.IsTrue(s.items[0].TTH == "QNGNAPOTVVZRPGSQPOH5X4RWITB3OI27KWXGCEI", "TTH not correct(2sd.avi)(\"" + s.items[0].TTH + "\"!=\"QNGNAPOTVVZRPGSQPOH5X4RWITB3OI27KWXGCEI\").");
01305             Assert.IsTrue(s.items[0].Filesize == 28495872, "Filesize not correct(2sd.avi).");
01306             Assert.IsTrue(s.items[1].Filename == "..\\..\\..\\TestDateien\\test.mp3", "Filename not correct(test.mp3).");
01307             Assert.IsTrue(s.items[1].TTH == "LODVHCUGIS5G534HRWG4LIPXT5TPIO4SS6D2KKI", "TTH not correct(test.mp3)(\"" + s.items[1].TTH + "\"!=\"LODVHCUGIS5G534HRWG4LIPXT5TPIO4SS6D2KKI\").");
01308             Assert.IsTrue(s.items[1].Filesize == 6053888, "Filesize not correct(test.mp3).");
01309             Assert.IsTrue(s.items[2].Filename == "..\\..\\..\\TestDateien\\test2.mp3", "Filename not correct(test2.mp3).");
01310             Assert.IsTrue(s.items[2].TTH == "6CFXRPW5GWT5NQGAU3DYZOCQBAYM63WST5J3HAY", "TTH not correct(test2.mp3)(\"" + s.items[2].TTH + "\"!=\"6CFXRPW5GWT5NQGAU3DYZOCQBAYM63WST5J3HAY\").");
01311             Assert.IsTrue(s.items[2].Filesize == 10539254, "Filesize not correct(test2.mp3).");
01312             Console.WriteLine("TTHs Creation Test successful.");
01313         }

Here is the call graph for this function:

static string DCPlusPlus.Sharing.ToXmlString ( string  org  )  [static, private]

Convert a string to a valid xml string (enquoting of invalid characters).

Parameters:
org some string to be converted to a xml string
Returns:
a quoted xml string

Definition at line 728 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.GetDirectoryContentsString().

00729         {
00730             if (String.IsNullOrEmpty(org)) return ("");
00731 
00732             string tmp = new string(org.ToCharArray());
00733             //System.Console.WriteLine("ToXml on string content: '" + tmp+"'");
00734             int p = 0;
00735             //while ((p = tmp.IndexOf("&", p)) != -1) { tmp = tmp.Replace("&", "&amp;"); if(p<tmp.Length)p++; }
00736             List<int> amps = new List<int>();
00737             while ((p = tmp.IndexOf("&", p)) != -1)
00738             {
00739                 //System.Console.WriteLine("ToXml add amp: '" + p + "'");
00740                 amps.Add(p);
00741                 if (p < tmp.Length - 1) p++;
00742                 else break;
00743 
00744 
00745             }
00746             //System.Console.WriteLine("ToXml amp count: '" + amps.Count + "'");
00747             for (int i = 0; i < amps.Count; i++)
00748             {
00749                 //System.Console.WriteLine("ToXml amps["+i+"]: '" + amps[i] + "' length of tmp:"+tmp.Length);
00750                 tmp = tmp.Remove(amps[i] + (i * 4), 1);
00751                 tmp = tmp.Insert(amps[i] + (i * 4), "&amp;");
00752                 //System.Console.WriteLine("ToXml tmp after amp conversion: '" + tmp + "'");
00753             }
00754 
00755 
00756             while (tmp.IndexOf("<") != -1) tmp = tmp.Replace("<", "&lt;");
00757             while (tmp.IndexOf(">") != -1) tmp = tmp.Replace(">", "&gt;");
00758             while (tmp.IndexOf("\"") != -1) tmp = tmp.Replace("\"", "&quot;");
00759             while (tmp.IndexOf("'") != -1) tmp = tmp.Replace("'", "&apos;");
00760             for (int i = 0; i < 32; i++)
00761             {
00762                 char c = Convert.ToChar(i);
00763                 if (i != 0x09 && i != 0x0a && i != 0x0d)
00764                 {
00765                     int pos = -1;
00766                     while ((pos = tmp.IndexOf(c)) != -1)
00767                     {
00768                         tmp = tmp.Remove(pos, 1);
00769                         tmp = tmp.Insert(pos, "&#" + Convert.ToString(c, 16) + ";");
00770                     }
00771                 }
00772             }
00773             //System.Console.WriteLine("after multiple newline remove check: '" + tmp + "'");
00774             return (tmp);
00775         }

Here is the caller graph for this function:

void DCPlusPlus.Sharing.UpdateFileLists (  ) 

Create the DC++ file list from our shares list.

Definition at line 638 of file Sharing.cs.

References DCPlusPlus.Sharing.cid, DCPlusPlus.Sharing.CleanDirectories(), DCPlusPlus.Sharing.file_list, DCPlusPlus.Sharing.file_list_bz2, DCPlusPlus.Sharing.FillDirectories(), DCPlusPlus.Sharing.generator, and DCPlusPlus.Sharing.GetDirectoryContentsString().

Referenced by DCPlusPlus.Sharing.GetFileListXml(), and DCPlusPlus.Sharing.GetFileListXmlBZ2().

00639         {
00640             string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n";
00641             xml += "<FileListing Version=\"1\" CID=\"" + cid + "\" Base=\"/\" Generator=\"" + generator + "\">\n";
00642             //hierarchical tree that lists all directories that have
00643             // 1. a shared file in it
00644             // 2. at least two directories in it that have shared files in its tree
00645             //use DirectoryContents class to hold data structure
00646             //combine directoryContents to xml string
00647             //return xml string
00648             DirectoryContents root = new DirectoryContents();
00649             FillDirectories(root);
00650             bool empty_shares = CleanDirectories(root);
00651             xml += GetDirectoryContentsString(root);
00652             xml += "</FileListing>\n";
00653             /*if (empty_shares)
00654                 file_list = "";
00655             else */
00656             file_list = xml;
00657             try
00658             {
00659                 MemoryStream input = new MemoryStream(System.Text.Encoding.Default.GetBytes(file_list));
00660                 MemoryStream output = new MemoryStream();
00661                 ICSharpCode.SharpZipLib.BZip2.BZip2.Compress(input, output, 1024);
00662                 input.Flush();
00663                 byte[] out_data = output.GetBuffer();
00664                 file_list_bz2 = out_data;
00665             }
00666             catch (Exception ex)
00667             {
00668                 Console.WriteLine("Error compressing file list: " + ex.Message);
00669                 file_list_bz2 = null;
00670             }
00671         }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

string DCPlusPlus.Sharing.cid = "D2QLOGUYDX3QA" [protected]

Definition at line 672 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.UpdateFileLists().

event DirectoryFinishedEventHandler DCPlusPlus.Sharing.DirectoryFinished

Event handler that gets called when sharing/hashing a directory has finished.

Definition at line 159 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.ShareDirectoryFinished(), DCPlusPlus.Sharing.TestEmptyGetFileListXml(), DCPlusPlus.Sharing.TestGetFileListXml(), DCPlusPlus.Sharing.TestShareDirectory(), DCPlusPlus.Sharing.TestShareSaveLoad(), DCPlusPlus.Sharing.TestShareSearchRemove(), DCPlusPlus.Sharing.TestSharingNotExistingDirectory(), and DCPlusPlus.Sharing.TestTTHs().

event EntriesChangedEventHandler DCPlusPlus.Sharing.EntriesChanged

Event handler that gets called when one or more entries were changed (deprecated , or possibly reused for its original purpose).

Definition at line 189 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.Add(), DCPlusPlus.Sharing.Clear(), DCPlusPlus.Sharing.LoadSharesFromXml(), and DCPlusPlus.Sharing.Remove().

event EntriesClearedEventHandler DCPlusPlus.Sharing.EntriesCleared

Event handler that gets called when all shares were removed.

Definition at line 198 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.Clear(), and DCPlusPlus.Sharing.LoadSharesFromXml().

event EntryAddedEventHandler DCPlusPlus.Sharing.EntryAdded

Event handler that gets called when an entry was added to the shares.

Definition at line 169 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.Add(), DCPlusPlus.Sharing.LoadSharesFromXml(), DCPlusPlus.Sharing.TestEmptyGetFileListXml(), DCPlusPlus.Sharing.TestGetFileListXml(), DCPlusPlus.Sharing.TestShareDirectory(), DCPlusPlus.Sharing.TestShareFile(), DCPlusPlus.Sharing.TestShareSaveLoad(), DCPlusPlus.Sharing.TestShareSearchRemove(), DCPlusPlus.Sharing.TestSharingNotExistingDirectory(), and DCPlusPlus.Sharing.TestTTHs().

event EntryRemovedEventHandler DCPlusPlus.Sharing.EntryRemoved

Event handler that gets called when an entry was removed from the shares.

Definition at line 179 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.Remove().

string DCPlusPlus.Sharing.file_list = "" [private]

DC++ formatted file list of our shares list.

Definition at line 630 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.GetFileListXml(), DCPlusPlus.Sharing.TestEmptyGetFileListXml(), DCPlusPlus.Sharing.TestGetFileListXml(), and DCPlusPlus.Sharing.UpdateFileLists().

byte [] DCPlusPlus.Sharing.file_list_bz2 [private]

bz2 compressed file list

Definition at line 634 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.GetFileListXmlBZ2(), and DCPlusPlus.Sharing.UpdateFileLists().

bool DCPlusPlus.Sharing.file_list_needs_update = true [private]

set to TRUE if at least one file was added/removed to indicate that the file list needs to be reconstructed

Definition at line 626 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.Add(), DCPlusPlus.Sharing.Clear(), DCPlusPlus.Sharing.GetFileListXml(), DCPlusPlus.Sharing.GetFileListXmlBZ2(), DCPlusPlus.Sharing.LoadSharesFromXml(), DCPlusPlus.Sharing.Remove(), DCPlusPlus.Sharing.ShareDirectoryFinished(), and DCPlusPlus.Sharing.ShareFileFinished().

string DCPlusPlus.Sharing.generator = "vpDcPlusPlus 0.2" [protected]

Definition at line 689 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.UpdateFileLists().

List<SharingEntry> DCPlusPlus.Sharing.items = new List<SharingEntry>() [protected]

Definition at line 90 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.Add(), DCPlusPlus.Sharing.Clear(), DCPlusPlus.Sharing.Contains(), DCPlusPlus.Sharing.CopyTo(), DCPlusPlus.Sharing.FillDirectories(), DCPlusPlus.Sharing.GetEnumerator(), DCPlusPlus.Sharing.GetShareByFilename(), DCPlusPlus.Sharing.GetShareByTTH(), DCPlusPlus.Sharing.LoadSharesFromXml(), DCPlusPlus.Sharing.Remove(), DCPlusPlus.Sharing.TestShareDirectory(), DCPlusPlus.Sharing.TestShareSaveLoad(), DCPlusPlus.Sharing.TestShareSearchRemove(), DCPlusPlus.Sharing.TestSharingNotExistingDirectory(), and DCPlusPlus.Sharing.TestTTHs().

[XmlIgnoreAttribute] Object DCPlusPlus.Sharing.share_lock = new Object() [protected]

The lock object to make the sharing class thread safe.

Definition at line 136 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.Add(), DCPlusPlus.Sharing.Clear(), DCPlusPlus.Sharing.Contains(), DCPlusPlus.Sharing.CopyTo(), DCPlusPlus.Sharing.FillDirectories(), DCPlusPlus.Sharing.GetShareByFilename(), DCPlusPlus.Sharing.GetShareByTTH(), DCPlusPlus.Sharing.LoadSharesFromXml(), DCPlusPlus.Sharing.Remove(), and DCPlusPlus.Sharing.SaveSharesToXml().

long DCPlusPlus.Sharing.total_bytes_shared = 0 [protected]

Definition at line 107 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.LoadSharesFromXml(), DCPlusPlus.Sharing.RecurseShareDirectoryAsync(), DCPlusPlus.Sharing.Remove(), and DCPlusPlus.Sharing.ShareFileFinished().


Property Documentation

string DCPlusPlus.Sharing.CID [get, set]

??CID?? i really need to look up what cid means and does ;-)

Definition at line 679 of file Sharing.cs.

int DCPlusPlus.Sharing.Count [get]

Get the number of shared files.

Definition at line 125 of file Sharing.cs.

string DCPlusPlus.Sharing.Generator [get, set]

Get/Set the file list generator name in the DC++ file list.

Definition at line 694 of file Sharing.cs.

bool DCPlusPlus.Sharing.IsReadOnly [get]

Definition at line 951 of file Sharing.cs.

List<SharingEntry> DCPlusPlus.Sharing.Items [get, set]

List of shares (will be an unpublic property soon,because of public locks that would be needed to use Items in a loop).

Definition at line 97 of file Sharing.cs.

Referenced by DCPlusPlus.Sharing.LoadSharesFromXml().

[XmlIgnoreAttribute] long DCPlusPlus.Sharing.TotalBytesShared [get]

Get the total number of bytes of all files shared.

Definition at line 114 of file Sharing.cs.


The documentation for this class was generated from the following file:
Generated on Wed Mar 7 19:09:46 2007 for DCPlusPlus by  doxygen 1.5.1-p1