<commit_before>/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.hazelcast.test.mocknetwork;

import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.core.Member;
import com.hazelcast.instance.Node;
import com.hazelcast.instance.NodeState;
import com.hazelcast.internal.cluster.impl.ClusterServiceImpl;
import com.hazelcast.logging.ILogger;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.Connection;
import com.hazelcast.nio.ConnectionListener;
import com.hazelcast.nio.ConnectionManager;
import com.hazelcast.nio.IOService;
import com.hazelcast.nio.Packet;
import com.hazelcast.util.executor.StripedRunnable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MockConnectionManager implements ConnectionManager {

    private static final int RETRY_NUMBER = 5;
    private static final int DELAY_FACTOR = 100;

    private final ConcurrentMap<Address, Connection> mapConnections = new ConcurrentHashMap<Address, Connection>(10);
    private final TestNodeRegistry registry;
    private final Node node;

    private final Set<ConnectionListener> connectionListeners = new CopyOnWriteArraySet<ConnectionListener>();
    private final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
    private final IOService ioService;
    private final ILogger logger;

    private volatile boolean live;

    public MockConnectionManager(IOService ioService, Node node, TestNodeRegistry registry) {
        this.ioService = ioService;
        this.registry = registry;
        this.node = node;
        this.logger = ioService.getLogger(MockConnectionManager.class.getName());
        // add a listener for member shutdown so that the client side connection also receives notification of connection closes
        node.getNodeEngine().getHazelcastInstance().getLifecycleService().addLifecycleListener(new LifecycleListener() {
            @Override
            public void stateChanged(LifecycleEvent event) {
                if (LifecycleEvent.LifecycleState.SHUTTING_DOWN == event.getState()) {
                    for (Connection connection : mapConnections.values()) {
                        if (connection.isClient()) {
                            destroyConnection(connection);
                        }
                    }
                }
            }
        });
    }

    @Override
    public Connection getConnection(Address address) {
        return mapConnections.get(address);
    }

    @Override
    public Connection getOrConnect(Address address) {
        Connection conn = mapConnections.get(address);
        if (live && (conn == null || !conn.isAlive())) {
            Node otherNode = registry.getNode(address);
            if (otherNode != null && otherNode.getState() != NodeState.SHUT_DOWN) {
                MockConnection thisConnection = new MockConnection(address, node.getThisAddress(), node.getNodeEngine());
                MockConnection mockConn = new MockConnection(node.getThisAddress(), address, otherNode.getNodeEngine());
                mockConn.localConnection = thisConnection;
                thisConnection.localConnection = mockConn;
                mapConnections.put(address, mockConn);
                logger.info("Created connection to endpoint: " + address + ", connection: " + mockConn);
                return mockConn;
            }
        }
        return conn;
    }

    @Override
    public Connection getOrConnect(Address address, boolean silent) {
        return getOrConnect(address);
    }
<commit_msg>Removing a code piece that is not needed. #9351<commit_after>/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.hazelcast.test.mocknetwork;

import com.hazelcast.core.Member;
import com.hazelcast.instance.Node;
import com.hazelcast.instance.NodeState;
import com.hazelcast.internal.cluster.impl.ClusterServiceImpl;
import com.hazelcast.logging.ILogger;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.Connection;
import com.hazelcast.nio.ConnectionListener;
import com.hazelcast.nio.ConnectionManager;
import com.hazelcast.nio.IOService;
import com.hazelcast.nio.Packet;
import com.hazelcast.util.executor.StripedRunnable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MockConnectionManager implements ConnectionManager {

    private static final int RETRY_NUMBER = 5;
    private static final int DELAY_FACTOR = 100;

    private final ConcurrentMap<Address, Connection> mapConnections = new ConcurrentHashMap<Address, Connection>(10);
    private final TestNodeRegistry registry;
    private final Node node;

    private final Set<ConnectionListener> connectionListeners = new CopyOnWriteArraySet<ConnectionListener>();
    private final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(4);
    private final IOService ioService;
    private final ILogger logger;

    private volatile boolean live;

    public MockConnectionManager(IOService ioService, Node node, TestNodeRegistry registry) {
        this.ioService = ioService;
        this.registry = registry;
        this.node = node;
        this.logger = ioService.getLogger(MockConnectionManager.class.getName());
    }

    @Override
    public Connection getConnection(Address address) {
        return mapConnections.get(address);
    }

    @Override
    public Connection getOrConnect(Address address) {
        Connection conn = mapConnections.get(address);
        if (live && (conn == null || !conn.isAlive())) {
            Node otherNode = registry.getNode(address);
            if (otherNode != null && otherNode.getState() != NodeState.SHUT_DOWN) {
                MockConnection thisConnection = new MockConnection(address, node.getThisAddress(), node.getNodeEngine());
                MockConnection mockConn = new MockConnection(node.getThisAddress(), address, otherNode.getNodeEngine());
                mockConn.localConnection = thisConnection;
                thisConnection.localConnection = mockConn;
                mapConnections.put(address, mockConn);
                logger.info("Created connection to endpoint: " + address + ", connection: " + mockConn);
                return mockConn;
            }
        }
        return conn;
    }

    @Override
    public Connection getOrConnect(Address address, boolean silent) {
        return getOrConnect(address);
    }

{'fraction_non_alphanumeric': 0.08364879716693124, 'fraction_numerical': 0.00512883135914031, 'mean_word_length': 3.502473886750962, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6038023', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2102, 'n_words': 581}