Esempio n. 1
0
        public bool ModifyLockState(int lockId, int userId, LockState state)
        {
            using (var smartLock = new SmartLockEntities())
            {
                LockInfo lockInfo = smartLock.LockInfoes.FirstOrDefault(l => l.LockId == lockId);
                if (lockInfo == null)
                {
                    // lock not found.
                    throw new LockNotFoundException("Lock not found.");
                }

                LockAccess lockAccess =
                    smartLock.LockAccesses.FirstOrDefault(l => l.UserId == userId && l.LockId == lockId);

                // TODO: Expand check to limit access based on time
                if (lockAccess == null)
                {
                    // user does not have access to the lock. Throw exception.
                    throw new UnauthorizedUserException("User unauthorized.");
                }

                // modify lock state.
                lockInfo.State = String.Concat(state.ToString(), "ed");

                int changes = smartLock.SaveChanges();
                return(changes == 1 ? true : false);
            }
        }
Esempio n. 2
0
        private async Task UpdateNukiDeviceState(INukiDeviceStateMessage nukiStateCmd)
        {
            var action = new Action(() =>
            {
                if (nukiStateCmd != null)
                {
                    LockState       = nukiStateCmd.LockState;
                    NukiState       = nukiStateCmd.NukiState;
                    CriticalBattery = nukiStateCmd.CriticalBattery;
                }
                else
                {
                    LockState = NukiLockState.Undefined;
                }
                LockRingState = LockState.ToString();
            });

            if (Dispatcher != null)
            {
                await Dispatcher.DispatchAsync(action);
            }
            else
            {
                action();
            }
        }
Esempio n. 3
0
        private void Release(int token, bool demandMatch = true)
        {
            void ThrowInvalidLockHolder() => throw new InvalidOperationException("Attempt to release a MutexSlim lock token that was not held");

            // release the token (we can check for wrongness without needing the lock, note)
            Log($"attempting to release token {LockState.ToString(token)}");
            if (Interlocked.CompareExchange(ref _token, LockState.ChangeState(token, LockState.Pending), token) != token)
            {
                Log($"token {LockState.ToString(token)} is invalid");
                if (demandMatch)
                {
                    ThrowInvalidLockHolder();
                }
                return;
            }


            if (_mayHavePendingItems)
            {
                ActivateNextQueueItem();
            }
            else
            {
                Log($"no pending items to activate");
            }
        }
Esempio n. 4
0
        public Task SendLockChangedMessageAsync(Lock @lock, LockState oldState, LockState newState, string userId)
        {
            var message = new LockStateChangedMessage
            {
                LockId           = @lock.Id,
                EventCreatedDate = DateTime.UtcNow,
                UserId           = userId,
                EventId          = Guid.NewGuid(),
                PreviousState    = oldState.ToString(),
                CurrentState     = newState.ToString()
            };

            return(_bus.Publish <LockStateChanged>(message));
        }
Esempio n. 5
0
        private void Release(int token, bool demandMatch = true)
        {
            // validate the token
            if (Volatile.Read(ref _token) != token)
            {
                Log($"token {LockState.ToString(token)} is invalid");
                if (demandMatch)
                {
                    Throw.InvalidLockHolder();
                }
                return;
            }

            ActivateNextQueueItemWithValidatedToken(token);
        }
Esempio n. 6
0
        private void Release(int token, bool demandMatch = true)
        {
            // release the token (we can check for wrongness without needing the lock, note)
            Log($"attempting to release token {LockState.ToString(token)}");
            if (Interlocked.CompareExchange(ref _token, LockState.ChangeState(token, LockState.Pending), token) != token)
            {
                Log($"token {LockState.ToString(token)} is invalid");
                if (demandMatch)
                {
                    Throw.InvalidLockHolder();
                }
                return;
            }

            ActivateNextQueueItem();
        }
Esempio n. 7
0
        /// <summary>
        /// Unlock datafile based on state and position
        /// </summary>
        public void Unlock(LockState state, int position)
        {
#if HAVE_LOCK
            // only shared mode lock datafile
            if (_options.FileMode != FileMode.Shared || state == LockState.Unlocked)
            {
                return;
            }

            var length = state == LockState.Read ? LOCK_READ_LENGTH : LOCK_WRITE_LENGTH;

            _log.Write(Logger.LOCK, "unlocking file in {0} mode (position: {1})", state.ToString().ToLower(), position);

            _stream.TryUnlock(position, length);
#endif
        }
Esempio n. 8
0
        /// <summary>
        /// Implement datafile lock. Return lock position
        /// </summary>
        public int Lock(LockState state, TimeSpan timeout)
        {
#if HAVE_LOCK
            // only shared mode lock datafile
            if (_options.FileMode != FileMode.Shared)
            {
                return(0);
            }

            var position = state == LockState.Read ? _lockReadRand.Next(LOCK_INITIAL_POSITION, LOCK_INITIAL_POSITION + LOCK_WRITE_LENGTH) : LOCK_INITIAL_POSITION;
            var length   = state == LockState.Read ? 1 : LOCK_WRITE_LENGTH;

            _log.Write(Logger.LOCK, "locking file in {0} mode (position: {1})", state.ToString().ToLower(), position);

            _stream.TryLock(position, length, timeout);

            return(position);
#else
            return(0);
#endif
        }
Esempio n. 9
0
        public bool ModifyLockState(int lockId, int userId, LockState state)
        {
            LockInfo lockInfo;

            if (!this.LockData.TryGetValue(lockId, out lockInfo))
            {
                throw new LockNotFoundException("Not found.");
            }

            LockAccess lockAccess =
                this.LockAccessData.SingleOrDefault(l => l.UserId == userId && l.LockId == lockId);

            if (lockAccess == null)
            {
                throw new UnauthorizedUserException("Unauthorized");
            }

            lockInfo.State = String.Concat(state.ToString(), "ed");

            return(true);
        }
Esempio n. 10
0
        /// <summary>
        /// Unlock datafile based on state
        /// </summary>
        public void Unlock(LockState state)
        {
#if NET35
            // only shared mode lock datafile
            if (_options.FileMode != FileMode.Shared)
            {
                return;
            }

            var position =
                state == LockState.Shared ? _lockSharedPosition :
                state == LockState.Reserved ? LOCK_RESERVED_POSITION :
                state == LockState.Exclusive ? LOCK_POSITION : 0;

            var length = state == LockState.Exclusive ? LOCK_SHARED_LENGTH : 1;

            _log.Write(Logger.LOCK, "unlocking file in {0} mode (position: {1}, length: {2})", state.ToString().ToLower(), position, length);

            _stream.TryUnlock(position, length);
#endif
        }
 public override string ToString() => LockState.ToString(_token);