Esempio n. 1
0
 private void HandleStateChanged(Event.KeeperState state)
 {
     foreach (var listener in this._stateListeners.Keys)
     {
         Task.Run(async() => { await listener.StateChangedHandler(state); });
     }
 }
 private void SetCurrentState(Event.KeeperState state)
 {
     lock (this)
     {
         _currentState = state;
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 设置zk当前状态
 /// </summary>
 /// <param name="keeperState"></param>
 private void SetCurrentState(Event.KeeperState keeperState)
 {
     lock (_zkEventLock)
     {
         _currentState = keeperState;
     }
 }
Esempio n. 4
0
 private void FireStateChangedEvent(Event.KeeperState state)
 {
     foreach (var listener in _stateListener)
     {
         Task.Factory.StartNew(() => listener.HandleStateChanged(state));
     }
 }
Esempio n. 5
0
        private bool checkState(Event.KeeperState state, bool wasConnected)
        {
            bool isConnected = wasConnected;
            bool checkNewConnectionString = true;

            switch (state)
            {
            case Event.KeeperState.Disconnected:
            {
                isConnected = false;
                break;
            }

            case Event.KeeperState.SyncConnected:
            case Event.KeeperState.ConnectedReadOnly:
            {
                isConnected = true;
                break;
            }

            case Event.KeeperState.AuthFailed:
            {
                isConnected = false;
                log.Error("Authentication failed");
                break;
            }

            case Event.KeeperState.Expired:
            {
                isConnected = false;
                checkNewConnectionString = false;
                handleExpiredSession();
                break;
            }

            default:
            {
                // NOP
                break;
            }
            }

            if (checkNewConnectionString && zooKeeper.hasNewConnectionString())
            {
                handleNewConnectionString();
            }

            return(isConnected);
        }
        /// <summary>
        /// 等待zk连接到具体的某一个状态。
        /// </summary>
        /// <param name="states">希望达到的状态。</param>
        /// <param name="timeout">最长等待时间。</param>
        /// <returns>如果成功则返回true,否则返回false。</returns>
        public bool WaitForKeeperState(Event.KeeperState states, TimeSpan timeout)
        {
            var stillWaiting = true;

            while (_currentState != states)
            {
                if (!stillWaiting)
                {
                    return(false);
                }

                stillWaiting = _stateChangedCondition.WaitOne(timeout);
            }
            return(true);
        }
Esempio n. 7
0
        public override Task process(WatchedEvent @event)
        {
            Event.KeeperState state = @event.getState();
            switch (state)
            {
            case Event.KeeperState.AuthFailed:
            // case Event.KeeperState.Disconnected:
            case Event.KeeperState.Expired:
                //case Event.KeeperState.SyncConnected:
                var task = new Task(() => { Thread.Sleep(1000); this.CallBack(null); });
                task.Start();
                return(task);
            }

            return(Task.FromResult(1));
        }
Esempio n. 8
0
        /// <summary>
        /// 等待Zookeeper变为某种状态
        /// </summary>
        /// <param name="keeperState"></param>
        /// <param name="waitTimeout"></param>
        /// <returns></returns>
        public bool WaitForKeeperState(Event.KeeperState keeperState, TimeSpan waitTimeout)
        {
            var stillWaiting = true;

            while (_currentState != keeperState)
            {
                if (!stillWaiting)
                {
                    return(false);
                }

                Logger.Info($"waitForKeeperState: {keeperState}, currentState: {_currentState}");

                stillWaiting = _stateChangEvent.WaitOne(waitTimeout);

                if (_currentState == Event.KeeperState.AuthFailed)
                {
                    throw new KeeperException.AuthFailedException();
                }
            }
            return(true);
        }
Esempio n. 9
0
 public override async Task process(WatchedEvent @event)
 {
     this.keeperState = @event.getState();
     await Task.Yield();
 }