public void Start(string hostAddress, DEAElement config, IDeaClient deaClientInstance) { Logger.Info("Starting directory server on interface '{0}:{1}'.", hostAddress, config.DirectoryServer.V2Port); if (config == null) { throw new ArgumentNullException("config"); } this.deaClient = deaClientInstance; this.streamingTimeout = config.DirectoryServer.StreamingTimeoutMS; this.listener.Start(); this.listener.Prefixes.Add(string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}/", hostAddress, config.DirectoryServer.V2Port)); ThreadStart listenerThreadStart = new ThreadStart( () => { while (this.listener.IsListening) { try { Logger.Debug("Directory server waiting for a request ..."); HttpListenerContext request = this.listener.GetContext(); Logger.Debug("Directory server got a request, '{0}'", request.Request.Url); ThreadPool.QueueUserWorkItem(ServeHttp, request); } catch (Exception ex) { Logger.Error(Strings.HttpListenerError, ex.ToString()); } } }); Thread listenerThread = new Thread(listenerThreadStart); listenerThread.IsBackground = true; listenerThread.Name = "DEA Directory Server Thread"; listenerThread.Start(); }
public void TestServerDirectoryFileTail() { string pathQuery = Guid.NewGuid().ToString(); string tempDir = Path.Combine(Path.GetTempPath(), pathQuery); Directory.CreateDirectory(tempDir); string loremIpsum = "Lorem ipsum dolor sit amet"; string filePath = Path.Combine(tempDir, "file.txt"); File.WriteAllText(filePath, loremIpsum); MockDeaClient.SetResponse(tempDir); MockDeaClient client = new MockDeaClient(); CloudFoundry.WinDEA.DirectoryServer.DirectoryServer server = new CloudFoundry.WinDEA.DirectoryServer.DirectoryServer(); DEAElement config = DirectoryConfiguration.ReadConfig(); config.DirectoryServer.StreamingTimeoutMS = 5000; server.Start("127.0.0.1", config, client); Random rnd = new Random(); string returnBytes = string.Empty; string sentBytes = string.Empty; int readCount = 0; string appendChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Thread newThread = new Thread(() => { byte[] randomBytes = new byte[rnd.Next(100)]; rnd.NextBytes(randomBytes); File.AppendAllText(filePath, ASCIIEncoding.ASCII.GetString(randomBytes)); HttpWebResponse response = null; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://127.0.0.1:{0}/{1}?tail", DirectoryConfiguration.ReadConfig().DirectoryServer.V2Port, "file.txt")); response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); byte[] responseBytes = new byte[200]; int read; do { read = responseStream.Read(responseBytes, 0, responseBytes.Length); returnBytes += ASCIIEncoding.ASCII.GetString(responseBytes, 0, read); readCount++; }while (read != 0); }); Thread writerThread = new Thread(() => { for (int i = 0; i < 10; i++) { Thread.Sleep(50); string toWrite = string.Empty; for (int j = 0; j < rnd.Next(100); j++) { toWrite += appendChars[rnd.Next(appendChars.Length)]; } File.AppendAllText(filePath, toWrite); sentBytes += toWrite; } }); newThread.Start(); Thread.Sleep(1000); writerThread.Start(); writerThread.Join(); newThread.Join(); server.Stop(); Assert.AreEqual(sentBytes, returnBytes); Assert.IsTrue(readCount > 1); }