<commit_before>/* 
 * Copyright (c) 2008-2010, Hazel Ltd. 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.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

public class OutRunnable extends IORunnable {
    final PacketWriter writer;
    final BlockingQueue<Call> queue = new LinkedBlockingQueue<Call>();

    private Connection connection = null;

    private volatile boolean reconnection;

    private Collection<Call> reconnectionCalls;

    public OutRunnable(final HazelcastClient client, final Map<Long, Call> calls, final PacketWriter writer) {
        super(client, calls);
        this.writer = writer;
        this.reconnection = false;
    }

    protected void customRun() throws InterruptedException {
        Call call = queue.poll(100, TimeUnit.MILLISECONDS);
        try {
            if (call == null) {
                if (reconnectionCalls != null) {
                    checkOnReconnect(call);
                }
                return;
            }
            if (call != RECONNECT_CALL) {
                callMap.put(call.getId(), call);
            }
            Connection oldConnection = connection;
            connection = client.getConnectionManager().getConnection();
            boolean wrote = false;
            if (restoredConnection(oldConnection, connection)) {
                resubscribe(call, oldConnection);
            } else if (connection != null) {
                if (call != RECONNECT_CALL) {
                    logger.log(Level.FINEST, "Sending: " + call);
                    wrote = true;
                    writer.write(connection, call.getRequest());
                }
            } else {
                clusterIsDown(oldConnection);
            }
            if (connection != null && wrote) {
                writer.flush(connection);
            }
            if (reconnectionCalls != null) {
                checkOnReconnect(call);
            }
        } catch (Throwable io) {
            logger.log(Level.WARNING, "OutRunnable got exception:" + io.getMessage(), io);
            if (call != null) {
                enQueue(call);
            }
            clusterIsDown(connection);
        }
    }

    private void checkOnReconnect(Call call) {
        final Collection oldCalls = reconnectionCalls;
        try {
            Object response = reconnectionCalls.contains(call) ?
                    call.getResponse(100L, TimeUnit.MILLISECONDS) :
                    null;
            if (response != null) {
                reconnectionCalls.remove(call);
            } else {
                for (final Iterator<Call> it = reconnectionCalls.iterator(); it.hasNext();) {
                    final Call c = it.next();
                    response = !c.hasResponse() ? c.getResponse(100L, TimeUnit.MILLISECONDS) : Boolean.TRUE;
                    if (response != null) {
                        it.remove();
                    }
                }
            }
        } catch (Throwable e) {
            // nothing to do
        }
        if (reconnectionCalls.isEmpty()) {
            reconnectionCalls = null;
        }<commit_msg>Fix for NPE<commit_after>/* 
 * Copyright (c) 2008-2010, Hazel Ltd. 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.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

public class OutRunnable extends IORunnable {
    final PacketWriter writer;
    final BlockingQueue<Call> queue = new LinkedBlockingQueue<Call>();

    private Connection connection = null;

    private volatile boolean reconnection;

    private Collection<Call> reconnectionCalls;

    public OutRunnable(final HazelcastClient client, final Map<Long, Call> calls, final PacketWriter writer) {
        super(client, calls);
        this.writer = writer;
        this.reconnection = false;
    }

    protected void customRun() throws InterruptedException {
        Call call = queue.poll(100, TimeUnit.MILLISECONDS);
        try {
            if (call == null) {
                if (reconnectionCalls != null) {
                    checkOnReconnect(call);
                }
                return;
            }
            if (call != RECONNECT_CALL) {
                callMap.put(call.getId(), call);
            }
            Connection oldConnection = connection;
            connection = client.getConnectionManager().getConnection();
            boolean wrote = false;
            if (restoredConnection(oldConnection, connection)) {
                resubscribe(call, oldConnection);
            } else if (connection != null) {
                if (call != RECONNECT_CALL) {
                    logger.log(Level.FINEST, "Sending: " + call);
                    wrote = true;
                    writer.write(connection, call.getRequest());
                }
            } else {
                clusterIsDown(oldConnection);
            }
            if (connection != null && wrote) {
                writer.flush(connection);
            }
            if (reconnectionCalls != null) {
                checkOnReconnect(call);
            }
        } catch (Throwable io) {
            logger.log(Level.WARNING, "OutRunnable got exception:" + io.getMessage(), io);
            if (call != null) {
                enQueue(call);
            }
            clusterIsDown(connection);
        }
    }

    private void checkOnReconnect(Call call) {
        final Collection oldCalls = reconnectionCalls;
        try {
            Object response = reconnectionCalls.contains(call) ?
                    call.getResponse(100L, TimeUnit.MILLISECONDS) :
                    null;
            if (response != null) {
                reconnectionCalls.remove(call);
            } else {
                for (final Iterator<Call> it = reconnectionCalls.iterator(); it.hasNext();) {
                    final Call c = it.next();
                    response = !c.hasResponse() ? c.getResponse(100L, TimeUnit.MILLISECONDS) : Boolean.TRUE;
                    if (response != null) {
                        it.remove();
                    }
                }
            }
        } catch (Throwable e) {
            // nothing to do
        }
        if (reconnectionCalls != null && reconnectionCalls.isEmpty()) {
            reconnectionCalls = null;
        }
{'fraction_non_alphanumeric': 0.09314162107138313, 'fraction_numerical': 0.005344191372948212, 'mean_word_length': 1.8886438809261301, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '829770', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1957, 'n_words': 548}