All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.client.internal.communication.NagglingManager Maven / Gradle / Ivy

package com.alachisoft.ncache.client.internal.communication;

import com.alachisoft.ncache.runtime.exceptions.ConnectionException;

import java.net.Socket;
import java.util.ArrayList;

//  Copyright (c) 2020 Alachisoft
//
//  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

class NagglingManager extends Thread {
    private Alachisoft.NCache.Common.DataStructures.Queue _msgQueue;
    private long _nagglingSize;
    private Connection _parent;
    private Socket _workingSocket;
    private Object _syncLock;
    private int _sendBufferSize = 1024 * 1024;
    private byte[] _sendBuffer;
    private long _waitTimeout = 0;

    public NagglingManager(Connection parent, Socket workingSocket, Alachisoft.NCache.Common.DataStructures.Queue msgQueue, long nagglingSize, Object syncLock) {
        _parent = parent;
        _workingSocket = workingSocket;
        _nagglingSize = nagglingSize;
        _msgQueue = msgQueue;
        _sendBuffer = new byte[_sendBufferSize];
        _syncLock = syncLock;
    }

    @Override
    public void run() {
        try {
            ArrayList msgList = new ArrayList();
            byte[] tmpBuffer;
            int totalMsgSize = 0;
            int offset = 0;
            ArrayList msgsTobeSent = new ArrayList();
            while (!_msgQueue.getClosed()) {
                try {
                    msgsTobeSent.clear();
                    synchronized (_syncLock) {
                        tmpBuffer = _sendBuffer;
                        totalMsgSize = 0;
                        offset = 0;
                        while (true) {
                            byte[] msg = (byte[]) _msgQueue.remove();

                            if (msg != null) {
                                msgsTobeSent.add(msg);
                                totalMsgSize += msg.length;

                                if (totalMsgSize > _sendBuffer.length) {
                                    tmpBuffer = new byte[totalMsgSize];
                                }
                                System.arraycopy(msg, 0, tmpBuffer, offset, msg.length);
                                offset += msg.length;
                            }
                            msg = null;

                            boolean success = false;
                            tangible.RefObject tempOut_success = new tangible.RefObject<>(success);
                            Object tempVar = _msgQueue.peek(_waitTimeout, tempOut_success);
                            msg = tempVar instanceof byte[] ? (byte[]) tempVar : null;

                            if ((msg == null || ((msg.length + totalMsgSize) > _nagglingSize))) {
                                break;
                            }

                        }
                    }
                    _parent.AssureSendNaggledData(_workingSocket, tmpBuffer, totalMsgSize, true);
                } catch (InterruptedException e) {
                    break;
                } catch (ConnectionException e) {

                }
            }
        } catch (RuntimeException e) {
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy