public void CanExtractDriverInformaiton() { const string InfoString = "{\"remoteId\":\"socket://10.121.136.231:14272\",\"startTime\":\"2014 08 28 10:50:32\",\"services\":[{\"serviceName\":\"NameServer\",\"serviceInfo\":\"10.121.136.231:16663\"}]}"; AvroDriverInfo info = AvroJsonSerializer <AvroDriverInfo> .FromString(InfoString); Assert.IsTrue(info.remoteId.Equals("socket://10.121.136.231:14272")); Assert.IsTrue(info.startTime.Equals("2014 08 28 10:50:32")); Assert.IsTrue(new DriverInformation(info.remoteId, info.startTime, info.services).NameServerId.Equals("10.121.136.231:16663")); }
public static DriverInformation GetDriverInformationFromHttp(Uri queryUri) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(queryUri); request.AllowAutoRedirect = false; request.KeepAlive = false; request.ContentType = "text/html"; string driverInfomation; AvroDriverInfo info = null; try { using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse()) { Stream stream = webResponse.GetResponseStream(); if (stream == null) { return(null); } using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8)) { driverInfomation = streamReader.ReadToEnd(); LOGGER.Log(Level.Verbose, "Http response line: " + driverInfomation); info = AvroJsonSerializer <AvroDriverInfo> .FromString(driverInfomation); } } } catch (WebException) { LOGGER.Log(Level.Warning, string.Format(CultureInfo.InvariantCulture, "In RECOVERY mode, cannot connect to [{0}] for driver information, will try again later.", queryUri)); return(null); } catch (Exception e) { Org.Apache.Reef.Utilities.Diagnostics.Exceptions.CaughtAndThrow(e, Level.Error, string.Format(CultureInfo.InvariantCulture, "Cannot read content from {0}.", queryUri), LOGGER); } if (info != null) { LOGGER.Log( Level.Verbose, string.Format(CultureInfo.InvariantCulture, "Driver information extracted with remote identier [{0}], start time [{1}], and servics [{2}]", info.remoteId, info.startTime, info.services)); return(new DriverInformation(info.remoteId, info.startTime, info.services)); } return(null); }
public static DriverInformation GetDriverInformationFromHttp(Uri queryUri) { while (queryUri != null) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(queryUri); request.AllowAutoRedirect = true; request.KeepAlive = false; request.ContentType = "text/html"; queryUri = null; try { using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse()) { var refresh = webResponse.Headers.AllKeys.FirstOrDefault(k => k.Equals("refresh", StringComparison.OrdinalIgnoreCase)); if (refresh != null) { var refreshContent = webResponse.Headers.GetValues(refresh); foreach (var refreshParam in refreshContent.SelectMany(content => content.Split(';').Select(c => c.Trim()))) { var refreshKeyValue = refreshParam.Split('=').Select(kv => kv.Trim()).ToArray(); if (refreshKeyValue.Length == 2 && refreshKeyValue[0].Equals("url", StringComparison.OrdinalIgnoreCase)) { queryUri = new Uri(refreshKeyValue[1]); break; } } } // We have received a redirect URI, look there instead. if (queryUri != null) { LOGGER.Log(Level.Verbose, "Received redirect URI:[{0}], redirecting...", queryUri); continue; } Stream stream = webResponse.GetResponseStream(); if (stream == null || !stream.CanRead) { return(null); } using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8)) { var driverInformation = streamReader.ReadToEnd(); LOGGER.Log(Level.Verbose, "Http response line: {0}", driverInformation); AvroDriverInfo info = null; try { info = AvroJsonSerializer <AvroDriverInfo> .FromString(driverInformation); } catch (Exception e) { Utilities.Diagnostics.Exceptions.CaughtAndThrow( e, Level.Error, string.Format(CultureInfo.InvariantCulture, "Cannot read content: {0}.", driverInformation), LOGGER); } if (info == null) { LOGGER.Log(Level.Info, "Cannot read content: {0}.", driverInformation); return(null); } return(new DriverInformation(info.remoteId, info.startTime, info.services)); } } } catch (WebException) { LOGGER.Log(Level.Warning, "In RECOVERY mode, cannot connect to [{0}] for driver information, will try again later.", queryUri); return(null); } } return(null); }