Example #1
0
        private void HandleSuccessfulAuth(Connection connection, ClientAuthenticationCodec.ResponseParameters response)
        {
            lock (_clientStateMutex)
            {
                CheckPartitionCount(response.PartitionCount);
                connection.ConnectedServerVersion = response.ServerHazelcastVersion;
                connection.RemoteAddress          = response.Address;
                connection.RemoteGuid             = response.MemberUuid;

                var newClusterId = response.ClusterId;

                var hasNoConnectionToCluster = _connections.IsEmpty;
                var changedCluster           = hasNoConnectionToCluster && _clusterId.HasValue && !newClusterId.Equals(_clusterId);
                if (changedCluster)
                {
                    Logger.Warning($"Switching from current cluster: {_clusterId} to new cluster: {newClusterId}");
                    _client.OnClusterRestart();
                }

                _connections.TryAdd(response.MemberUuid, connection);
                Interlocked.Increment(ref _nextConnectionId);

                if (hasNoConnectionToCluster)
                {
                    _clusterId = newClusterId;
                    if (changedCluster)
                    {
                        //TODO cluster state????
                        _clientState = ClientState.CONNECTED_TO_CLUSTER;
                        _client.ExecutionService.Submit(() => InitializeClientOnCluster(newClusterId));
                    }
                    else
                    {
                        _clientState = ClientState.INITIALIZED_ON_CLUSTER;
                        _client.LifecycleService.FireLifecycleEvent(LifecycleEvent.LifecycleState.ClientConnected);
                    }
                }
                Logger.Info(string.Format("Authenticated with server {0}:{1}, server version: {2}, local address: {3}",
                                          response.Address, response.MemberUuid, response.ServerHazelcastVersion, connection.GetLocalSocketAddress()));
                FireConnectionAddedEvent(connection);
            }

            // It could happen that this connection is already closed and
            // onConnectionClose() is called even before the synchronized block
            // above is executed. In this case, now we have a closed but registered
            // connection. We do a final check here to remove this connection
            // if needed.
            if (!connection.IsAlive)
            {
                OnConnectionClose(connection);
            }
        }