Example #1
0
        /// <summary>
        /// Gets the <see cref="Role"/> object corresponding to a Role ID.
        /// </summary>
        /// <param name="RoleId">Role ID.</param>
        /// <returns>Role object.</returns>
        public static async Task <Role> GetRole(string RoleId)
        {
            await synchObj.BeginWrite();

            try
            {
                if (roles.TryGetValue(RoleId, out Role Role))
                {
                    return(Role);
                }

                Role = await Database.FindFirstDeleteRest <Role>(new FilterFieldEqualTo("Id", RoleId));

                if (Role is null)
                {
                    Role = new Role()
                    {
                        Id          = RoleId,
                        Description = string.Empty,
                        Privileges  = new PrivilegePattern[0]
                    };

                    await Database.Insert(Role);
                }

                roles[RoleId] = Role;

                return(Role);
            }
            finally
            {
                await synchObj.EndWrite();
            }
        }
Example #2
0
        public async Task Test_06_WriteWrite()
        {
            using (MultiReadSingleWriteObject obj = new MultiReadSingleWriteObject())
            {
                TaskCompletionSource <bool> Done1 = new TaskCompletionSource <bool>();
                TaskCompletionSource <bool> Done2 = new TaskCompletionSource <bool>();

                Thread T1 = new Thread(async() =>
                {
                    try
                    {
                        Assert.IsFalse(obj.IsWriting);
                        await obj.BeginWrite();
                        Assert.IsTrue(obj.IsWriting);
                        Assert.AreEqual(0, obj.NrReaders);
                        Thread.Sleep(2000);
                        Assert.IsTrue(obj.IsWriting);
                        Assert.AreEqual(0, obj.NrReaders);
                        await obj.EndWrite();
                        Assert.IsFalse(obj.IsWriting);
                        Done1.SetResult(true);
                    }
                    catch (Exception ex)
                    {
                        Done1.SetException(ex);
                    }
                });

                Thread T2 = new Thread(async() =>
                {
                    try
                    {
                        Thread.Sleep(1000);
                        Assert.IsTrue(obj.IsWriting);
                        Assert.AreEqual(0, obj.NrReaders);
                        await obj.BeginWrite();
                        Assert.IsTrue(obj.IsWriting);
                        Assert.AreEqual(0, obj.NrReaders);
                        await obj.EndWrite();
                        Assert.IsFalse(obj.IsWriting);
                        Done2.SetResult(true);
                    }
                    catch (Exception ex)
                    {
                        Done2.SetException(ex);
                    }
                });

                T1.Start();
                T2.Start();

                Assert.IsTrue(await Done1.Task);
                Assert.IsTrue(await Done2.Task);
            }
        }
Example #3
0
        /// <summary>
        /// Gets the <see cref="Privilege"/> object corresponding to a full Privilege ID.
        /// </summary>
        /// <param name="PrivilegeId">Full Privilege ID, consisting of the concatenation of the full parent privelege ID,
        /// a period character and the local id of the privilege.</param>
        /// <returns>Privilege object.</returns>
        public static async Task <Privilege> GetPrivilege(string PrivilegeId)
        {
            await synchObj.BeginWrite();

            try
            {
                return(await GetPrivilegeLocked(PrivilegeId));
            }
            finally
            {
                await synchObj.EndWrite();
            }
        }
Example #4
0
        /// <summary>
        /// Gets the <see cref="User"/> object corresponding to a User Name.
        /// </summary>
        /// <param name="UserName">User Name.</param>
        /// <param name="CreateIfNew">If user should be created, if it does not exist.</param>
        /// <returns>User object, or null if not found and not created.</returns>
        public static async Task <User> GetUser(string UserName, bool CreateIfNew)
        {
            User User;
            bool Load;

            await synchObj.BeginWrite();

            try
            {
                if (users.TryGetValue(UserName, out User))
                {
                    return(User);
                }

                User = await Database.FindFirstDeleteRest <User>(new FilterFieldEqualTo("UserName", UserName));

                if (User is null)
                {
                    if (!CreateIfNew)
                    {
                        return(null);
                    }

                    User = new User()
                    {
                        UserName     = UserName,
                        PasswordHash = string.Empty,
                        RoleIds      = new string[0]
                    };

                    await Database.Insert(User);

                    Load = false;
                }
                else
                {
                    Load = true;
                }

                users[UserName] = User;
            }
            finally
            {
                await synchObj.EndWrite();
            }

            if (Load)
            {
                await User.LoadRoles();
            }

            return(User);
        }
Example #5
0
        /// <summary>
        /// Sets a string-valued setting.
        /// </summary>
        /// <param name="Key">Key name.</param>
        /// <param name="Value">New value.</param>
        /// <returns>If the setting was saved (true). If the setting existed, and had the same value, false is returned.</returns>
        public static async Task <bool> SetAsync(string Key, string Value)
        {
            await synchObject.BeginWrite();

            try
            {
                StringSetting Setting = await Database.FindFirstDeleteRest <StringSetting>(new FilterFieldEqualTo("Key", Key));

                if (Setting is null)
                {
                    Setting = new StringSetting(Key, Value);
                    await Database.Insert(Setting);

                    return(true);
                }
                else
                {
                    if (Setting.Value != Value)
                    {
                        Setting.Value = Value;
                        await Database.Update(Setting);

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            finally
            {
                await synchObject.EndWrite();
            }
        }
Example #6
0
        public async Task Test_02_Write()
        {
            using (MultiReadSingleWriteObject obj = new MultiReadSingleWriteObject())
            {
                Assert.IsFalse(obj.IsWriting);
                await obj.BeginWrite();

                Assert.IsTrue(obj.IsWriting);
                await obj.EndWrite();

                Assert.IsFalse(obj.IsWriting);
            }
        }
Example #7
0
        public async Task Test_02_Write()
        {
            using (MultiReadSingleWriteObject obj = new MultiReadSingleWriteObject())
            {
                Assert.AreEqual(0, obj.NrWriters);
                await obj.BeginWrite();

                Assert.AreEqual(1, obj.NrWriters);
                await obj.EndWrite();

                Assert.AreEqual(0, obj.NrWriters);
            }
        }
Example #8
0
        public async Task Test_08_RandomLoad()
        {
            using (MultiReadSingleWriteObject obj = new MultiReadSingleWriteObject())
            {
                LinkedList <TaskCompletionSource <bool> > Tasks = new LinkedList <TaskCompletionSource <bool> >();
                Random rnd = new Random();
                int    i;
                int    NrReads  = 0;
                int    NrWrites = 0;

                for (i = 0; i < 100; i++)
                {
                    TaskCompletionSource <bool> Result = new TaskCompletionSource <bool>();
                    Thread T = new Thread(async(P) =>
                    {
                        try
                        {
                            DateTime Start = DateTime.Now;
                            int j;

                            while ((DateTime.Now - Start).TotalSeconds < 30)
                            {
                                lock (rnd)
                                {
                                    j = rnd.Next(1, 200);
                                }

                                if (j > 100)
                                {
                                    await obj.BeginWrite();
                                    Assert.AreEqual(0, obj.NrReaders);
                                    Assert.IsTrue(obj.IsWriting);
                                    NrWrites++;
                                    Thread.Sleep(10);
                                    Assert.AreEqual(0, obj.NrReaders);
                                    Assert.IsTrue(obj.IsWriting);
                                    await obj.EndWrite();
                                    Thread.Sleep(j - 100);
                                }
                                else
                                {
                                    await obj.BeginRead();
                                    Assert.IsFalse(obj.IsWriting);
                                    Assert.IsTrue(obj.NrReaders > 0);
                                    NrReads++;
                                    Thread.Sleep(10);
                                    Assert.IsFalse(obj.IsWriting);
                                    Assert.IsTrue(obj.NrReaders > 0);
                                    await obj.EndRead();
                                    Thread.Sleep(j);
                                }
                            }

                            ((TaskCompletionSource <bool>)P).SetResult(true);
                        }
                        catch (Exception ex)
                        {
                            ((TaskCompletionSource <bool>)P).SetException(ex);
                        }
                    });

                    Tasks.AddLast(Result);
                    T.Start(Result);
                }

                foreach (TaskCompletionSource <bool> Task in Tasks)
                {
                    Assert.IsTrue(await Task.Task);
                }

                Console.Out.WriteLine("Nr reads: " + NrReads.ToString());
                Console.Out.WriteLine("Nr writes: " + NrWrites.ToString());
            }
        }