private void downloadFile(string version = "") { try { if (!string.IsNullOrEmpty(version)) { if (this.serverVersion() == version) { udger.WriteDebug("Download skipped, existing data file is current"); long time = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; this.selectQuery("UPDATE _info_ SET lastupdate=" + time.ToString() + " WHERE key=1"); return; } } using (WebClient myWebClient = new WebClient()) { udger.WriteDebug("Downloading the data file"); if (sqlite != null) { sqlite.Close(); sqlite.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); this.connected = false; } myWebClient.DownloadFile(new Uri(baseUrl + access_key + @"/udgerdb.dat"), DataSourcePath); myWebClient.Dispose(); if (this.GetMD5HashFromFile(DataSourcePath) != this.serverHash()) { File.Delete(DataSourcePath); udger.WriteDebug("Data file hash mismatch."); } else { udger.WriteDebug("File downloaded"); } } if (File.Exists(DataSourcePath)) { sqlite = new SQLiteConnection(@"Data Source=" + DataSourcePath); this.connected = true; long time = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; this.selectQuery("UPDATE _info_ SET lastupdate=" + time.ToString() + " WHERE key=1"); } } catch (Exception e) { throw e; } }
public void connect(UdgerParser _udger) { udger = _udger; try { if (!Directory.Exists(data_dir)) { throw new Exception("Data dir not found"); } if (!this.Connected) { udger.WriteDebug("Open DB file: " + DataSourcePath); if (!string.IsNullOrEmpty(this.access_key)) { if (File.Exists(DataSourcePath)) { sqlite = new SQLiteConnection(@"Data Source=" + DataSourcePath); this.connected = true; if (sqlite != null) { DataTable _info_ = this.selectQuery("SELECT lastupdate,version FROM _info_ where key=1"); if (_info_.Rows.Count > 0) { DataRow row = _info_.Rows[0]; long time = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; long lastUpdate = Convert.ToInt32(row["lastupdate"].ToString()); if (lastUpdate + this.updateInteval < time) { udger.WriteDebug("Data is maybe outdated, check new data from server"); this.downloadFile(row["version"].ToString()); } else { udger.WriteDebug("Data is current and will be used"); } } else { udger.WriteDebug("Data is corrupted, download data"); this.downloadFile(); } } } else { udger.WriteDebug("Data dir is empty, download data"); this.downloadFile(); } } if (File.Exists(DataSourcePath)) { sqlite = new SQLiteConnection(@"Data Source=" + DataSourcePath); this.connected = true; } } } catch (Exception e) { throw e; } }
public void connect(UdgerParser _udger) { udger = _udger; try { if (!Directory.Exists(data_dir)) throw new Exception("Data dir not found"); if (!this.Connected) { udger.WriteDebug("Open DB file: " + DataSourcePath); if (!string.IsNullOrEmpty(this.access_key)) { if (File.Exists(DataSourcePath)) { sqlite = new SQLiteConnection(@"Data Source=" + DataSourcePath); this.connected = true; if (sqlite != null) { DataTable _info_ = this.selectQuery("SELECT lastupdate,version FROM _info_ where key=1"); if (_info_.Rows.Count > 0) { DataRow row = _info_.Rows[0]; long time = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; long lastUpdate = Convert.ToInt32(row["lastupdate"].ToString()); if (lastUpdate + this.updateInteval < time) { udger.WriteDebug("Data is maybe outdated, check new data from server"); this.downloadFile(row["version"].ToString()); } else { udger.WriteDebug("Data is current and will be used"); } } else { udger.WriteDebug("Data is corrupted, download data"); this.downloadFile(); } } } else { udger.WriteDebug("Data dir is empty, download data"); this.downloadFile(); } } if (File.Exists(DataSourcePath)) { sqlite = new SQLiteConnection(@"Data Source=" + DataSourcePath); this.connected = true; } } } catch (Exception e) { throw e; } }