Beispiel #1
0
        public Lease ProlongLease(Lease oldLease, string authenticatedUserName, DateTime time)
        {
            Lease newLease = new Lease
            {
                License           = oldLease.License,
                AuthenticatedUser = authenticatedUserName,
                OverwritesLease   = oldLease,
                StartTime         = oldLease.StartTime,
                EndTime           = time.AddDays(Settings.Default.NewLeaseDays),
                Machine           = oldLease.Machine,
                UserName          = oldLease.UserName,
                Grace             = oldLease.Grace
            };

            if (!this.FixLease(newLease, time))
            {
                return(null);
            }

            this.Leases.InsertOnSubmit(newLease);

            LeaseService.CheckAssertions(this, newLease.License, time);

            return(newLease);
        }
Beispiel #2
0
        public Lease CreateLease(License license, string user, string machine, string authenticatedUserName, DateTime time, bool grace)
        {
            Lease lease = new Lease
            {
                License           = license,
                AuthenticatedUser = authenticatedUserName,
                EndTime           = time.AddDays(Settings.Default.NewLeaseDays),
                Machine           = machine,
                StartTime         = time,
                UserName          = user,
                Grace             = grace
            };

            if (!this.FixLease(lease, time))
            {
                return(null);
            }


            this.Leases.InsertOnSubmit((lease));

            LeaseService.CheckAssertions(this, license, time);


            return(lease);
        }
        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
                        {

                        }
                    }
                }
            }
        }
Beispiel #4
0
        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
                        {
                        }
                    }
                }
            }
        }