2 using System
.Collections
.Generic
;
3 using System
.Diagnostics
;
6 using System
.Runtime
.Serialization
;
7 using System
.Runtime
.Serialization
.Formatters
.Binary
;
8 using System
.ServiceModel
.Channels
;
9 using System
.Threading
;
11 using Indexer
.ShareDiscoveryReference
;
17 internal class Program
19 private static int _sleepTimeout
= /*1 minute*/ 60000;
20 private static int _numSharesIndexed
= 0;
21 private static int _nDenial
= 0;
23 private static void Main( string[] args
)
25 ShareDiscoveryServiceClient client
= new ShareDiscoveryServiceClient();
29 Stopwatch startWatch
= new Stopwatch();
32 string errorMessage
= "Got no shares to index.";
33 if ( _numSharesIndexed
== 0 )
35 errorMessage
+= " Share discovery service is probably initializing now.";
40 share
= (new StreamReader(client
.GetShareToIndex())).ReadToEnd();
42 catch ( TimeoutException
)
44 errorMessage
= "Share discovery service timed out.";
47 if ( String
.IsNullOrEmpty( share
) )
50 double timeout
= _sleepTimeout
* Math
.Sqrt( _nDenial
);
51 Console
.WriteLine( "[{0}] Error: {1} Sleeping for {2:0.##} seconds.", DateTime
.Now
, errorMessage
,
53 Thread
.Sleep( (int) timeout
);
58 Console
.WriteLine( "[{0}] About to index share {1}.", DateTime
.Now
, share
);
62 Stopwatch indexWatch
= new Stopwatch();
65 TreeNode shareNode
= IndexFolder( share
);
68 if ( shareNode
!= null )
70 if ( startWatch
.IsRunning
)
73 Console
.WriteLine( "[{0}] Share discovery service initialized in {1}", DateTime
.Now
,
77 string[] parts
= share
.Split( new[] { '\\' }
, StringSplitOptions
.RemoveEmptyEntries
);
79 FileInfo shareInfo
= new FileInfo( share
);
80 shareNode
.CreationTime
= shareInfo
.CreationTime
;
81 shareNode
.AccessTime
= shareInfo
.LastAccessTime
;
82 shareNode
.ModificationTime
= shareInfo
.LastWriteTime
;
84 TreeNode compNode
= new TreeNode();
85 compNode
.Name
= "//" + parts
[ 0 ];
86 compNode
.Children
= new[] { shareNode }
;
88 // Count indexed nodes.
90 Queue
<TreeNode
> queue
= new Queue
<TreeNode
>();
91 queue
.Enqueue( compNode
);
93 while ( queue
.Count
> 0 )
95 TreeNode next
= queue
.Dequeue();
97 if ( next
.Children
!= null )
99 foreach ( TreeNode child
in next
.Children
)
101 queue
.Enqueue( child
);
109 Console
.WriteLine( "[{0}] Indexed {1}-th share {2} in {3}. {4} nodes in graph.",
112 share
, indexWatch
.Elapsed
, numNodes
);
114 Stream stream
= new MemoryStream();
115 IFormatter fmt
= new BinaryFormatter();
116 fmt
.Serialize( stream
, compNode
);
117 stream
.Seek( 0, SeekOrigin
.Begin
);
118 client
.SaveIndex( stream
);
121 Console
.WriteLine( "[{0}] Saved share {1}.", DateTime
.Now
, share
);
126 Console
.WriteLine( "[{0}] Wasted {1} on indexing {2}.", DateTime
.Now
, indexWatch
.Elapsed
, share
);
129 catch ( IOException ex
)
131 Console
.Error
.WriteLine( "[{0}] Error: failed to save {1}: {2}", DateTime
.Now
, share
, ex
.Message
);
136 public static TreeNode
IndexFolder( string directoryName
)
138 FileInfo dirinfo
= null;
143 dirinfo
= new FileInfo( directoryName
);
147 Name
= Path
.GetFileName( directoryName
),
148 CreationTime
= dirinfo
.CreationTime
,
149 AccessTime
= dirinfo
.LastAccessTime
,
150 ModificationTime
= dirinfo
.LastWriteTime
153 List
<TreeNode
> listing
= new List
<TreeNode
>();
157 string[] subdirs
= Directory
.GetDirectories( directoryName
);
158 string[] subfiles
= Directory
.GetFiles( directoryName
);
160 if ( subdirs
.Length
== 0 && subfiles
.Length
== 0 )
168 from directory
in subdirs
169 let child
= IndexFolder( directory
)
170 where child
!= null && child
.Name
!= ".svn"
173 from fileName
in subfiles
174 let info
= new FileInfo( fileName
)
180 CreationTime
= info
.CreationTime
,
181 AccessTime
= info
.LastAccessTime
,
182 ModificationTime
= info
.LastWriteTime
187 catch ( UnauthorizedAccessException ex
)
189 Console
.Error
.WriteLine( "Access denied to {0}", directoryName
);
191 catch ( IOException ex
)
193 Console
.Error
.WriteLine( "Cannot read {0}: {1}", directoryName
, ex
.Message
);
196 catch ( IOException ex
)
198 Console
.Error
.WriteLine( "Path {0} not found.", directoryName
);