public static void init(string cgibinptrUrl) { m_mapsPath = Project.GetMapsTempPath(); LibSys.StatusBar.WriteLine("Actual location for mapping cache in " + m_mapsPath); try { #if DEBUG LibSys.StatusBar.Trace("IP: reaching cgibinptr URL='" + cgibinptrUrl + "'"); #endif /* * this code has long uncontrolled timeout, and has been replaced with the WebDownload-based code below HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cgibinptrUrl); if(Project.suspendKeepAlive) { req.KeepAlive = false; } WebResponse res = req.GetResponse(); Stream responseStream = res.GetResponseStream(); StreamReader reader = new StreamReader (responseStream); */ WebDownload webDL = new WebDownload(Project.webTimeoutMs); // default 7 seconds to time out // Create the state object. DownloadInfo info = new DownloadInfo(); info.baseName = ""; info.strUrl = cgibinptrUrl; info.addMonitoredMethod = null; byte[] downloadedData = null; int tries = 1; int maxTries = 1; while (tries <= maxTries && downloadedData == null && !info.is404) { downloadedData = webDL.Download(info, ProgressCallback); // will timeout #if DEBUG if(downloadedData != null) { LibSys.StatusBar.Trace("IP: try " + tries + " TileCache:Download() - " + cgibinptrUrl + " delivered " + downloadedData.Length + " bytes" ); } else { LibSys.StatusBar.Trace("IP: try " + tries + " TileCache:Download() - " + cgibinptrUrl + " delivered null bytes" ); } #endif tries++; } if(downloadedData == null || downloadedData.Length == 0) { LibSys.StatusBar.Error("failed to reach QuakeMap.com"); return; // Project.serverAvailable will be set to false, and no web attempts will take place. } LibSys.StatusBar.Trace("OK: reached QuakeMap.com"); string responseString = Project.ByteArrayToStr(downloadedData); StringReader reader = new StringReader (responseString); string upgrVersion = ""; bool ignoreUpgrade = false; int state = 0; string line; while((line=reader.ReadLine()) != null) { try { switch(state) { case 0: if(line.StartsWith("MAPSERVER=")) { MappingServer ms = new MappingServer(line.Substring("MAPSERVER=".Length)); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - ms='" + ms + "'"); #endif m_mappingServers.Add(ms); } else if(line.StartsWith("ZIPSERVER=")) { ZipcodeServer zs = new ZipcodeServer(line.Substring("ZIPSERVER=".Length)); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - zs='" + zs + "'"); #endif m_zipcodeServer = zs; } else if(line.StartsWith("TILERABOUT=")) { Project.ABOUT_URL = line.Substring("TILERABOUT=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - about='" + Project.ABOUT_URL + "'"); #endif } else if(line.StartsWith("TILERORDER=")) { Project.ORDER_URL = line.Substring("TILERORDER=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - order='" + Project.ORDER_URL + "'"); #endif } else if(line.StartsWith("TILERDLOAD=")) { Project.DLOAD_URL = line.Substring("TILERDLOAD=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - download='" + Project.DLOAD_URL + "'"); #endif } else if(line.StartsWith("TILERUPDATE=")) { Project.UPDATE_URL = line.Substring("TILERUPDATE=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - update='" + Project.UPDATE_URL + "'"); #endif } else if(line.StartsWith("TILERPRIVACY=")) { Project.PRIVACY_URL = line.Substring("TILERPRIVACY=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - privacy='" + Project.PRIVACY_URL + "'"); #endif } else if(line.StartsWith("TILERPDA=")) { Project.PDA_URL = line.Substring("TILERPDA=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - pda='" + Project.PDA_URL + "'"); #endif } else if(line.StartsWith("TILERHELP=")) { Project.HELP_FILE_URL = line.Substring("TILERHELP=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - help='" + Project.HELP_FILE_URL + "'"); #endif } else if(line.StartsWith("TILERHDATE=")) { string sDate = line.Substring("TILERHDATE=".Length); Project.HELP_FILE_DATE = Convert.ToDateTime(sDate); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - helpFileDate='" + Project.HELP_FILE_DATE + "'"); #endif } else if(line.StartsWith("TILERMISC=")) { Project.MISC_FOLDER_URL = line.Substring("TILERMISC=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - misc='" + Project.MISC_FOLDER_URL + "'"); #endif } else if(line.StartsWith("TILERSAMPLES=")) { Project.SAMPLES_FOLDER_URL = line.Substring("TILERSAMPLES=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - samples='" + Project.SAMPLES_FOLDER_URL + "'"); #endif } else if(line.StartsWith("MESSAGE=")) { state = 1; Project.serverMessage = ""; } else if(line.StartsWith("UPGR")) { upgrVersion = line.Substring(4); if(Project.PROGRAM_VERSION_RELEASEDATE.Equals(upgrVersion)) { ignoreUpgrade = true; } else { Project.upgradeMessage = "\n"; } state = 2; } break; case 1: if(line.StartsWith("ENDMESSAGE")) { state = 0; } else { Project.serverMessage += (line + "\n"); } break; case 2: if(line.StartsWith("ENDUPGR")) { state = 0; } else { if(!ignoreUpgrade) { Project.upgradeMessage += (line + "\n"); } } break; } } catch {} } if(!Project.serverMessageLast.Equals(Project.serverMessage) && Project.upgradeMessage.Length == 0) { string message = Project.serverMessage; // + Project.upgradeMessage; upgrade message shows up in MainForm LibSys.StatusBar.Trace(message); if(greetingForm == null) { Project.MessageBox(null, message); } else { System.Windows.Forms.MessageBox.Show (greetingForm, message, Project.PROGRAM_NAME_HUMAN, System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } Project.serverMessageLast = Project.serverMessage; } } catch (Exception e) { LibSys.StatusBar.Error("exception: " + e.Message); } }
//public void Download(object state, DateTime requestEnqueueTime ) // good for LibSys.ThreadPool public void Download(object state) // good for LibSys.ThreadPool2 { //LibSys.StatusBar.Trace("IP: " + threadCount + " DownloadThread:Download() - " + DownloadUrl); if (CompleteCallback != null && DownloadUrl != "") { threadCount++; WebDownload webDL = new WebDownload(0); // no timeout, except for natural TCP/IP stack one. // Create the state object. DownloadInfo info = new DownloadInfo(); info.baseName = _baseName; info.strUrl = _downloadUrl; info.addMonitoredMethod = addMonitoredMethod; // may be null // Make sure progress monitor is created: if (info.addMonitoredMethod != null) { info.monitored = new Monitored(); info.monitored.Comment = info.strUrl; info.addMonitoredMethod(info.monitored); } byte[] downloadedData = null; if (Project.serverAvailable) { int tries = 1; while (true) { //try //{ downloadedData = webDL.Download(info, ProgressCallback); //} //catch (Exception e) //{ // LibSys.StatusBar.Error("try " + tries + " DownloadThread:Download() - " + DownloadUrl + " exception " + e.Message ); //} #if DEBUG if (downloadedData != null) { LibSys.StatusBar.Trace("IP: try " + tries + " DownloadThread:Download() - " + DownloadUrl + " delivered " + downloadedData.Length + " bytes"); } else { LibSys.StatusBar.Trace("IP: try " + tries + " DownloadThread:Download() - " + DownloadUrl + " delivered null bytes"); } #endif tries++; if (tries <= maxTries && downloadedData == null && !info.is404) { // if there was a server error (Web Exception, error 500), give it some time to get in shape before retrying: Thread.Sleep(3000); } else { break; } } } threadCount--; CompleteCallback(_tile, info, _fileName, downloadedData); } }
// good for LibSys.ThreadPool2 //public void Download(object state, DateTime requestEnqueueTime ) // good for LibSys.ThreadPool public void Download(object state ) { //LibSys.StatusBar.Trace("IP: " + threadCount + " DownloadThread:Download() - " + DownloadUrl); if ( CompleteCallback != null && DownloadUrl != "" ) { threadCount++; WebDownload webDL = new WebDownload(0); // no timeout, except for natural TCP/IP stack one. // Create the state object. DownloadInfo info = new DownloadInfo(); info.baseName = _baseName; info.strUrl = _downloadUrl; info.addMonitoredMethod = addMonitoredMethod; // may be null // Make sure progress monitor is created: if(info.addMonitoredMethod != null) { info.monitored = new Monitored(); info.monitored.Comment = info.strUrl; info.addMonitoredMethod(info.monitored); } byte[] downloadedData = null; if(Project.serverAvailable) { int tries = 1; while (true) { //try //{ downloadedData = webDL.Download(info, ProgressCallback); //} //catch (Exception e) //{ // LibSys.StatusBar.Error("try " + tries + " DownloadThread:Download() - " + DownloadUrl + " exception " + e.Message ); //} #if DEBUG if(downloadedData != null) { LibSys.StatusBar.Trace("IP: try " + tries + " DownloadThread:Download() - " + DownloadUrl + " delivered " + downloadedData.Length + " bytes" ); } else { LibSys.StatusBar.Trace("IP: try " + tries + " DownloadThread:Download() - " + DownloadUrl + " delivered null bytes" ); } #endif tries++; if(tries <= maxTries && downloadedData == null && !info.is404) { // if there was a server error (Web Exception, error 500), give it some time to get in shape before retrying: Thread.Sleep(3000); } else { break; } } } threadCount--; CompleteCallback( _tile, info, _fileName, downloadedData ); } }
public static void init(string cgibinptrUrl) { try { #if DEBUG LibSys.StatusBar.Trace("IP: reaching cgibinptr URL='" + cgibinptrUrl + "'"); #endif WebDownload webDL = new WebDownload(Project.webTimeoutMs); // default 7 seconds to time out // Create the state object. DownloadInfo info = new DownloadInfo(); info.baseName = ""; info.strUrl = cgibinptrUrl; info.addMonitoredMethod = null; byte[] downloadedData = null; int tries = 1; int maxTries = 1; while (tries <= maxTries && downloadedData == null && !info.is404) { downloadedData = webDL.Download(info, ProgressCallback); // will timeout #if DEBUG if(downloadedData != null) { LibSys.StatusBar.Trace("IP: try " + tries + " WebsiteInfo:Download() - " + cgibinptrUrl + " delivered " + downloadedData.Length + " bytes" ); } else { LibSys.StatusBar.Trace("IP: try " + tries + " WebsiteInfo:Download() - " + cgibinptrUrl + " delivered null bytes" ); } #endif tries++; } if(downloadedData == null || downloadedData.Length == 0) { LibSys.StatusBar.Error("failed to reach QuakeMap.com"); return; // Project.serverAvailable will be set to false, and no web attempts will take place. } LibSys.StatusBar.Trace("OK: reached QuakeMap.com"); string responseString = Project.ByteArrayToStr(downloadedData); StringReader reader = new StringReader (responseString); string upgrVersion = ""; bool ignoreUpgrade = false; int state = 0; string line; while((line=reader.ReadLine()) != null) { try { switch(state) { case 0: if(line.StartsWith("GBWABOUT=")) { Project.ABOUT_URL = line.Substring("GBWABOUT=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - about='" + Project.ABOUT_URL + "'"); #endif HasReachedServer = true; } else if(line.StartsWith("GBWDLOAD=")) { Project.DLOAD_URL = line.Substring("GBWDLOAD=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - download='" + Project.DLOAD_URL + "'"); #endif } else if(line.StartsWith("GBWUPDATE=")) { Project.UPDATE_URL = line.Substring("GBWUPDATE=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - update='" + Project.UPDATE_URL + "'"); #endif } else if(line.StartsWith("GBWHELP=")) { Project.HELP_FILE_URL = line.Substring("GBWHELP=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - help='" + Project.HELP_FILE_URL + "'"); #endif } else if(line.StartsWith("GBWHDATE=")) { string sDate = line.Substring("GBWHDATE=".Length); Project.HELP_FILE_DATE = Convert.ToDateTime(sDate); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - helpFileDate='" + Project.HELP_FILE_DATE + "'"); #endif } else if(line.StartsWith("GBWGPSBABELHOME=")) { Project.GPSBABEL_URL = line.Substring("GBWGPSBABELHOME=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - GPSBabel Home='" + Project.GPSBABEL_URL + "'"); #endif } else if(line.StartsWith("GBWMISC=")) { Project.MISC_FOLDER_URL = line.Substring("GBWMISC=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - misc='" + Project.MISC_FOLDER_URL + "'"); #endif } else if(line.StartsWith("MESSAGE=")) { state = 1; Project.serverMessage = ""; } else if(line.StartsWith("UPGR")) { upgrVersion = line.Substring(4); if(Project.PROGRAM_VERSION_RELEASEDATE.Equals(upgrVersion)) { ignoreUpgrade = true; } else { Project.upgradeMessage = "\n"; } state = 2; } break; case 1: if(line.StartsWith("ENDMESSAGE")) { state = 0; } else { Project.serverMessage += (line + "\n"); } break; case 2: if(line.StartsWith("ENDUPGR")) { state = 0; } else { if(!ignoreUpgrade) { Project.upgradeMessage += (line + "\n"); } } break; } } catch {} } if(!Project.serverMessageLast.Equals(Project.serverMessage) && Project.upgradeMessage.Length == 0) { string message = Project.serverMessage; // + Project.upgradeMessage; upgrade message shows up in MainForm LibSys.StatusBar.Trace(message); Project.MessageBox(null, message); Project.serverMessageLast = Project.serverMessage; } } catch (Exception e) { LibSys.StatusBar.Error("exception: " + e.Message); } }