public LicenseLease GetLicenseLease(Database db, string productCode, Version version, DateTime?buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary <int, string> errors) { Settings settings = Settings.Default; // Get suitable active licenses. License[] licenses = (from license in db.Licenses where license.ProductCode == productCode && license.Priority >= 0 orderby license.Priority select license).ToArray(); // Check if the machine belongs to build server user. if (IsBuildServer(machine)) { License buildServerLicense = GetBuildServerLicense(licenses); if (buildServerLicense != null) { DateTime endTime = now.AddDays(Settings.Default.NewLeaseDays); LicenseLease buildServerLicenseLease = new LicenseLease(buildServerLicense.LicenseKey, now, endTime, endTime.AddDays(-settings.MinLeaseDays)); // Lease for build server should not be stored - build server users shouldn't steal developer licenses. return(buildServerLicenseLease); } // try to acquire lease in normal way } Lease lease = GetLease(db, productCode, version, buildDate, machine, userName, authenticatedUserName, now, errors, licenses); if (lease == null) { return(null); } LicenseLease licenseLease = new LicenseLease(lease.License.LicenseKey, lease.StartTime, lease.EndTime, lease.EndTime.AddDays(-settings.MinLeaseDays)); return(licenseLease); }
public void Main() { Guid guid = Guid.NewGuid(); MessageSink messageSink = new MessageSink(); RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(string.Format("SOFTWARE\\SharpCrafters\\PostSharp\\LicenseClient\\{0}", guid)); using ( registryKey ) { LicenseLease lease = null; while (true) { DateTime day = VirtualDateTime.UtcNow.ToLocalTime(); // We don't start before 9. DateTime startTime = day.Date.AddHours(8 + (random.NextDouble() - 0.5) * 4); VirtualDateTime.WakeOn(startTime); double probWorksToday; switch (day.DayOfWeek) { case DayOfWeek.Sunday: probWorksToday = user.WorksOnWeekend; break; case DayOfWeek.Saturday: probWorksToday = user.WorksOnWeekend; break; default: probWorksToday = 1; break; } if (random.NextDouble() < probWorksToday) { DateTime endTime = startTime.AddHours(9 + random.NextDouble()); // Which machine will he use this day? int machineIndex = (int)Math.Floor(random.NextDouble() * user.Machines.Count); string machine = user.Machines[machineIndex]; // We are running PostSharp every 15 minute. for (DateTime time = startTime; time < endTime; time = time.AddMinutes(30 * random.NextDouble())) { //Console.WriteLine("{2} {0} waiting until {1}", VirtualDateTime.UtcNow.ToLocalTime(), time, user); VirtualDateTime.WakeOn(time); if (lease == null || lease.RenewTime < time) { Interlocked.Increment(ref waitingUsers); Console.WriteLine("{2} {0} on {3}: Acquiring lease; waiting users = {1}", user.AuthenticatedName, waitingUsers, VirtualDateTime.UtcNow.ToLocalTime(), machine); string url = Program.Url.TrimEnd('/') + string.Format("/Lease.ashx?user={0}&machine={1}&product={2}", user.AuthenticatedName, machine, LicensedProduct.PostSharp30); Stopwatch stopwatch = Stopwatch.StartNew(); lease = LicenseServerClient.TryGetLease(url, registryKey, VirtualDateTime.UtcNow.ToLocalTime(), messageSink); if (lease == null) { Console.WriteLine("Could not get a valid lease: lease is null"); } else if (lease.EndTime < time) { Console.WriteLine("Could not get a valid lease: lease end time is in the past"); } else if (lease.RenewTime < time) { Console.WriteLine("Got a lease with past renewal time: {0}, it is now {1}", lease.RenewTime, time); Program.FixVirtualTime(); } Interlocked.Decrement(ref waitingUsers); Console.WriteLine("{3} {0}: response received in {1}, waiting users = '{2}'", user, stopwatch.Elapsed, waitingUsers, VirtualDateTime.UtcNow.ToLocalTime()); } } } } } }
public LicenseLease GetLicenseLease(Database db, string productCode, DateTime? buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary<int, string> errors) { Settings settings = Settings.Default; // Get suitable active licenses. License[] licenses = (from license in db.Licenses where license.ProductCode == productCode && license.Priority >= 0 orderby license.Priority select license).ToArray(); // Check if the machine belongs to build server user. if ( IsBuildServer( machine ) ) { License buildServerLicense = GetBuildServerLicense( licenses ); if ( buildServerLicense != null ) { DateTime endTime = now.AddDays( Settings.Default.NewLeaseDays ); LicenseLease buildServerLicenseLease = new LicenseLease( buildServerLicense.LicenseKey, now, endTime, endTime.AddDays( -settings.MinLeaseDays ) ); // Lease for build server should not be stored - build server users shouldn't steal developer licenses. return buildServerLicenseLease; } // try to acquire lease in normal way } Lease lease = GetLease( db, productCode, buildDate, machine, userName, authenticatedUserName, now, errors, licenses ); if ( lease == null ) return null; LicenseLease licenseLease = new LicenseLease(lease.License.LicenseKey, lease.StartTime, lease.EndTime, lease.EndTime.AddDays(-settings.MinLeaseDays)); return licenseLease; }