internal static void CheckAssertions(Database db, License license, DateTime now) { #if FALSE db.SubmitChanges(); Dictionary<int, LicenseState> cache = new Dictionary<int, LicenseState>(); List<string> errors = new List<string>(); LicenseState licenseState = GetLicenseState( db, license, now, cache, errors ); if ( licenseState == null ) throw new Exception( string.Join( ". ", errors.ToArray() ) ); if ( licenseState.Maximum.HasValue ) { double max = Math.Ceiling(licenseState.Maximum.Value * (100.0 + Settings.Default.GracePeriodPercent) / 100.0); if (licenseState.Usage > max) throw new Exception( "Maximum exceeded." ); } #endif }
internal static void CheckAssertions(Database db, License license, DateTime now) { #if FALSE db.SubmitChanges(); Dictionary <int, LicenseState> cache = new Dictionary <int, LicenseState>(); List <string> errors = new List <string>(); LicenseState licenseState = GetLicenseState(db, license, now, cache, errors); if (licenseState == null) { throw new Exception(string.Join(". ", errors.ToArray())); } if (licenseState.Maximum.HasValue) { double max = Math.Ceiling(licenseState.Maximum.Value * (100.0 + Settings.Default.GracePeriodPercent) / 100.0); if (licenseState.Usage > max) { throw new Exception("Maximum exceeded."); } } #endif }
public void ProcessRequest(HttpContext context) { this.context = context; using (Database db = new Database()) { // Parse requested product code. string productCode = context.Request.QueryString["product"]; if (string.IsNullOrEmpty(productCode)) { this.SetError(400, "Missing query string argument: product."); return; } // Parse build date. string buildDateString = context.Request.QueryString["buildDate"]; DateTime? buildDate = null; if (!string.IsNullOrEmpty(buildDateString)) { DateTime buildDateNotNull; if ( !DateTime.TryParse(buildDateString, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out buildDateNotNull)) { this.SetError(400, "Cannot parse the argument: buildDate."); return; } buildDate = buildDateNotNull; } // Parse machine name. string machine = context.Request.QueryString["machine"]; if (string.IsNullOrEmpty(machine)) { this.SetError(400, "Missing query string argument: machine."); return; } machine = machine.ToLowerInvariant(); // Parse user name. string userName = context.Request.QueryString["user"]; if (string.IsNullOrEmpty(userName)) { this.SetError(400, "Missing query string argument: user."); return; } userName = userName.ToLowerInvariant(); bool releaseMutex = false; try { while (true) { try { if (!mutex.WaitOne(TimeSpan.FromSeconds(Settings.Default.MutexTimeout))) { this.SetError(503, "Service overloaded."); return; } else { releaseMutex = true; break; } } catch (AbandonedMutexException) { try { mutex.ReleaseMutex(); } catch { } try { mutex.Close(); } catch { } mutex = CreateMutex(); } } Stopwatch stopwatch = Stopwatch.StartNew(); LicenseLease licenseLease = new LeaseService(true).GetLicenseLease(db, productCode, buildDate, machine, userName, context.User.Identity.Name, VirtualDateTime.UtcNow, errors); if (licenseLease != null) { db.SubmitChanges(); string serializedLease = licenseLease.Serialize(); context.Response.Write(serializedLease); } else { this.SetError(403, "No license with free capacity. " + string.Join(" ", this.errors.Values)); } if (stopwatch.ElapsedMilliseconds > 1000) { context.Trace.Warn(string.Format("Request served in {0}", stopwatch.Elapsed)); } } finally { if (releaseMutex) { try { mutex.ReleaseMutex(); } catch { } } } } }
public void ProcessRequest(HttpContext context) { this.context = context; using (Database db = new Database()) { // Parse requested product code. string productCode = context.Request.QueryString["product"]; if (string.IsNullOrEmpty(productCode)) { this.SetError(400, "Missing query string argument: product."); return; } // Parse version. string versionString = context.Request.QueryString["version"]; Version version; if (string.IsNullOrEmpty(versionString)) { // Versions < 5.0 do not include version in the request. version = new Version(4, 9, 9); } else { if (!Version.TryParse(versionString, out version)) { this.SetError(400, "Cannot parse the argument: version."); return; } } // Parse build date. string buildDateString = context.Request.QueryString["buildDate"]; DateTime?buildDate = null; if (!string.IsNullOrEmpty(buildDateString)) { DateTime buildDateNotNull; if ( !DateTime.TryParse(buildDateString, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out buildDateNotNull)) { this.SetError(400, "Cannot parse the argument: buildDate."); return; } buildDate = buildDateNotNull; } // Parse machine name. string machine = context.Request.QueryString["machine"]; if (string.IsNullOrEmpty(machine)) { this.SetError(400, "Missing query string argument: machine."); return; } machine = machine.ToLowerInvariant(); // Parse user name. string userName = context.Request.QueryString["user"]; if (string.IsNullOrEmpty(userName)) { this.SetError(400, "Missing query string argument: user."); return; } userName = userName.ToLowerInvariant(); bool releaseMutex = false; try { while (true) { try { if (!mutex.WaitOne(TimeSpan.FromSeconds(Settings.Default.MutexTimeout))) { this.SetError(503, "Service overloaded."); return; } else { releaseMutex = true; break; } } catch (AbandonedMutexException) { try { mutex.ReleaseMutex(); } catch { } try { mutex.Close(); } catch { } mutex = CreateMutex(); } } Stopwatch stopwatch = Stopwatch.StartNew(); LicenseLease licenseLease = new LeaseService(true).GetLicenseLease(db, productCode, version, buildDate, machine, userName, context.User.Identity.Name, VirtualDateTime.UtcNow, errors); if (licenseLease != null) { db.SubmitChanges(); string serializedLease = licenseLease.Serialize(); context.Response.Write(serializedLease); } else { this.SetError(403, "No license with free capacity. " + string.Join(" ", this.errors.Values)); } if (stopwatch.ElapsedMilliseconds > 1000) { context.Trace.Warn(string.Format("Request served in {0}", stopwatch.Elapsed)); } } finally { if (releaseMutex) { try { mutex.ReleaseMutex(); } catch { } } } } }